blob: 44e5582577c5d7e03fd99dce33a355ee70ed06a2 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * The SSL 3.0 specification was drafted by Netscape in 1996,
24 * and became an IETF standard in 1999.
25 *
26 * http://wp.netscape.com/eng/ssl3/
27 * http://www.ietf.org/rfc/rfc2246.txt
28 * http://www.ietf.org/rfc/rfc4346.txt
29 */
30
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#else
34#include POLARSSL_CONFIG_FILE
35#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker40e46942009-01-03 21:51:57 +000037#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker0be444a2013-08-27 21:55:01 +020039#include "polarssl/debug.h"
40#include "polarssl/ssl.h"
41
Rich Evans00ab4702015-02-06 13:43:58 +000042#include <string.h>
43
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020044#if defined(POLARSSL_X509_CRT_PARSE_C) && \
45 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
46#include "polarssl/oid.h"
47#endif
48
Paul Bakker7dc4c442014-02-01 22:50:26 +010049#if defined(POLARSSL_PLATFORM_C)
50#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020051#else
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <stdlib.h>
Paul Bakker6e339b52013-07-03 13:37:05 +020053#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker6edcd412013-10-29 15:22:54 +010057#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
58 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000059#define strcasecmp _stricmp
60#endif
61
Paul Bakker34617722014-06-13 17:20:13 +020062/* Implementation that should never be optimized out by the compiler */
63static void polarssl_zeroize( void *v, size_t n ) {
64 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
65}
66
Paul Bakker05decb22013-08-15 13:33:48 +020067#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020068/*
69 * Convert max_fragment_length codes to length.
70 * RFC 6066 says:
71 * enum{
72 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
73 * } MaxFragmentLength;
74 * and we add 0 -> extension unused
75 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020076static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020077{
78 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
79 512, /* SSL_MAX_FRAG_LEN_512 */
80 1024, /* SSL_MAX_FRAG_LEN_1024 */
81 2048, /* SSL_MAX_FRAG_LEN_2048 */
82 4096, /* SSL_MAX_FRAG_LEN_4096 */
83};
Paul Bakker05decb22013-08-15 13:33:48 +020084#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020085
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020086static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
87{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020088 ssl_session_free( dst );
89 memcpy( dst, src, sizeof( ssl_session ) );
90
Paul Bakker7c6b2c32013-09-16 13:49:26 +020091#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020092 if( src->peer_cert != NULL )
93 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020094 int ret;
95
Mansour Moufidc531b4a2015-02-15 17:35:38 -050096 dst->peer_cert = polarssl_malloc( sizeof(x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020097 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020098 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
99
Paul Bakkerb6b09562013-09-18 14:17:41 +0200100 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200101
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200102 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
103 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200104 {
105 polarssl_free( dst->peer_cert );
106 dst->peer_cert = NULL;
107 return( ret );
108 }
109 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200110#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200111
Paul Bakkera503a632013-08-14 13:48:06 +0200112#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200113 if( src->ticket != NULL )
114 {
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500115 dst->ticket = polarssl_malloc( src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200116 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200117 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
118
119 memcpy( dst->ticket, src->ticket, src->ticket_len );
120 }
Paul Bakkera503a632013-08-14 13:48:06 +0200121#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200122
123 return( 0 );
124}
125
Paul Bakker05ef8352012-05-08 09:17:57 +0000126#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200127int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200128 const unsigned char *key_enc, const unsigned char *key_dec,
129 size_t keylen,
130 const unsigned char *iv_enc, const unsigned char *iv_dec,
131 size_t ivlen,
132 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200133 size_t maclen ) = NULL;
134int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
135int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
136int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
137int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
138int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200139#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000140
Paul Bakker5121ce52009-01-03 21:22:43 +0000141/*
142 * Key material generation
143 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200144#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200145static int ssl3_prf( const unsigned char *secret, size_t slen,
146 const char *label,
147 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000148 unsigned char *dstbuf, size_t dlen )
149{
150 size_t i;
151 md5_context md5;
152 sha1_context sha1;
153 unsigned char padding[16];
154 unsigned char sha1sum[20];
155 ((void)label);
156
Paul Bakker5b4af392014-06-26 12:09:34 +0200157 md5_init( &md5 );
158 sha1_init( &sha1 );
159
Paul Bakker5f70b252012-09-13 14:23:06 +0000160 /*
161 * SSLv3:
162 * block =
163 * MD5( secret + SHA1( 'A' + secret + random ) ) +
164 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
165 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
166 * ...
167 */
168 for( i = 0; i < dlen / 16; i++ )
169 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200170 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000171
172 sha1_starts( &sha1 );
173 sha1_update( &sha1, padding, 1 + i );
174 sha1_update( &sha1, secret, slen );
175 sha1_update( &sha1, random, rlen );
176 sha1_finish( &sha1, sha1sum );
177
178 md5_starts( &md5 );
179 md5_update( &md5, secret, slen );
180 md5_update( &md5, sha1sum, 20 );
181 md5_finish( &md5, dstbuf + i * 16 );
182 }
183
Paul Bakker5b4af392014-06-26 12:09:34 +0200184 md5_free( &md5 );
185 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000186
Paul Bakker34617722014-06-13 17:20:13 +0200187 polarssl_zeroize( padding, sizeof( padding ) );
188 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000189
190 return( 0 );
191}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200192#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000193
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200194#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200195static int tls1_prf( const unsigned char *secret, size_t slen,
196 const char *label,
197 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000198 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000199{
Paul Bakker23986e52011-04-24 08:57:21 +0000200 size_t nb, hs;
201 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200202 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 unsigned char tmp[128];
204 unsigned char h_i[20];
205
206 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000207 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000208
209 hs = ( slen + 1 ) / 2;
210 S1 = secret;
211 S2 = secret + slen - hs;
212
213 nb = strlen( label );
214 memcpy( tmp + 20, label, nb );
215 memcpy( tmp + 20 + nb, random, rlen );
216 nb += rlen;
217
218 /*
219 * First compute P_md5(secret,label+random)[0..dlen]
220 */
221 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
222
223 for( i = 0; i < dlen; i += 16 )
224 {
225 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
226 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
227
228 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
229
230 for( j = 0; j < k; j++ )
231 dstbuf[i + j] = h_i[j];
232 }
233
234 /*
235 * XOR out with P_sha1(secret,label+random)[0..dlen]
236 */
237 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
238
239 for( i = 0; i < dlen; i += 20 )
240 {
241 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
242 sha1_hmac( S2, hs, tmp, 20, tmp );
243
244 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
245
246 for( j = 0; j < k; j++ )
247 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
248 }
249
Paul Bakker34617722014-06-13 17:20:13 +0200250 polarssl_zeroize( tmp, sizeof( tmp ) );
251 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000252
253 return( 0 );
254}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200255#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000256
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200257#if defined(POLARSSL_SSL_PROTO_TLS1_2)
258#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200259static int tls_prf_sha256( const unsigned char *secret, size_t slen,
260 const char *label,
261 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000262 unsigned char *dstbuf, size_t dlen )
263{
264 size_t nb;
265 size_t i, j, k;
266 unsigned char tmp[128];
267 unsigned char h_i[32];
268
269 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
270 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
271
272 nb = strlen( label );
273 memcpy( tmp + 32, label, nb );
274 memcpy( tmp + 32 + nb, random, rlen );
275 nb += rlen;
276
277 /*
278 * Compute P_<hash>(secret, label + random)[0..dlen]
279 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200280 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000281
282 for( i = 0; i < dlen; i += 32 )
283 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200284 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
285 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000286
287 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
288
289 for( j = 0; j < k; j++ )
290 dstbuf[i + j] = h_i[j];
291 }
292
Paul Bakker34617722014-06-13 17:20:13 +0200293 polarssl_zeroize( tmp, sizeof( tmp ) );
294 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000295
296 return( 0 );
297}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200298#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000299
Paul Bakker9e36f042013-06-30 14:34:05 +0200300#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200301static int tls_prf_sha384( const unsigned char *secret, size_t slen,
302 const char *label,
303 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000304 unsigned char *dstbuf, size_t dlen )
305{
306 size_t nb;
307 size_t i, j, k;
308 unsigned char tmp[128];
309 unsigned char h_i[48];
310
311 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
312 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
313
314 nb = strlen( label );
315 memcpy( tmp + 48, label, nb );
316 memcpy( tmp + 48 + nb, random, rlen );
317 nb += rlen;
318
319 /*
320 * Compute P_<hash>(secret, label + random)[0..dlen]
321 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200322 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000323
324 for( i = 0; i < dlen; i += 48 )
325 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200326 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
327 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000328
329 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
330
331 for( j = 0; j < k; j++ )
332 dstbuf[i + j] = h_i[j];
333 }
334
Paul Bakker34617722014-06-13 17:20:13 +0200335 polarssl_zeroize( tmp, sizeof( tmp ) );
336 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000337
338 return( 0 );
339}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200340#endif /* POLARSSL_SHA512_C */
341#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000342
Paul Bakker66d5d072014-06-17 16:39:18 +0200343static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200344
345#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
346 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200347static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200348#endif
Paul Bakker380da532012-04-18 16:10:25 +0000349
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200350#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200351static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
352static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200353#endif
354
355#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200356static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
357static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200358#endif
359
360#if defined(POLARSSL_SSL_PROTO_TLS1_2)
361#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200362static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
363static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
364static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200365#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100366
Paul Bakker9e36f042013-06-30 14:34:05 +0200367#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200368static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
369static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
370static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100371#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200372#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000373
Paul Bakker5121ce52009-01-03 21:22:43 +0000374int ssl_derive_keys( ssl_context *ssl )
375{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200376 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000378 unsigned char keyblk[256];
379 unsigned char *key1;
380 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100381 unsigned char *mac_enc;
382 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200383 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100384 const cipher_info_t *cipher_info;
385 const md_info_t *md_info;
386
Paul Bakker48916f92012-09-16 19:57:18 +0000387 ssl_session *session = ssl->session_negotiate;
388 ssl_transform *transform = ssl->transform_negotiate;
389 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000390
391 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
392
Paul Bakker68884e32013-01-07 18:20:04 +0100393 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
394 if( cipher_info == NULL )
395 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200396 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100397 transform->ciphersuite_info->cipher ) );
398 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
399 }
400
401 md_info = md_info_from_type( transform->ciphersuite_info->mac );
402 if( md_info == NULL )
403 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200404 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100405 transform->ciphersuite_info->mac ) );
406 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
407 }
408
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000410 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000411 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200412#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000413 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000414 {
Paul Bakker48916f92012-09-16 19:57:18 +0000415 handshake->tls_prf = ssl3_prf;
416 handshake->calc_verify = ssl_calc_verify_ssl;
417 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000418 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200419 else
420#endif
421#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
422 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000423 {
Paul Bakker48916f92012-09-16 19:57:18 +0000424 handshake->tls_prf = tls1_prf;
425 handshake->calc_verify = ssl_calc_verify_tls;
426 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000427 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200428 else
429#endif
430#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200431#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200432 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
433 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000434 {
Paul Bakker48916f92012-09-16 19:57:18 +0000435 handshake->tls_prf = tls_prf_sha384;
436 handshake->calc_verify = ssl_calc_verify_tls_sha384;
437 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000438 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000439 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200440#endif
441#if defined(POLARSSL_SHA256_C)
442 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000443 {
Paul Bakker48916f92012-09-16 19:57:18 +0000444 handshake->tls_prf = tls_prf_sha256;
445 handshake->calc_verify = ssl_calc_verify_tls_sha256;
446 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000447 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200448 else
449#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200450#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200451 {
452 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200453 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200454 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000455
456 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000457 * SSLv3:
458 * master =
459 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
460 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
461 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200462 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200463 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000464 * master = PRF( premaster, "master secret", randbytes )[0..47]
465 */
Paul Bakker0a597072012-09-25 21:55:46 +0000466 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000467 {
Paul Bakker48916f92012-09-16 19:57:18 +0000468 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
469 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200471#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
472 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200473 {
474 unsigned char session_hash[48];
475 size_t hash_len;
476
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200477 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200478
479 ssl->handshake->calc_verify( ssl, session_hash );
480
481#if defined(POLARSSL_SSL_PROTO_TLS1_2)
482 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
483 {
484#if defined(POLARSSL_SHA512_C)
485 if( ssl->transform_negotiate->ciphersuite_info->mac ==
486 POLARSSL_MD_SHA384 )
487 {
488 hash_len = 48;
489 }
490 else
491#endif
492 hash_len = 32;
493 }
494 else
495#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
496 hash_len = 36;
497
498 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
499
500 handshake->tls_prf( handshake->premaster, handshake->pmslen,
501 "extended master secret",
502 session_hash, hash_len, session->master, 48 );
503
504 }
505 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200506#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000507 handshake->tls_prf( handshake->premaster, handshake->pmslen,
508 "master secret",
509 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200511
Paul Bakker34617722014-06-13 17:20:13 +0200512 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 }
514 else
515 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
516
517 /*
518 * Swap the client and server random values.
519 */
Paul Bakker48916f92012-09-16 19:57:18 +0000520 memcpy( tmp, handshake->randbytes, 64 );
521 memcpy( handshake->randbytes, tmp + 32, 32 );
522 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200523 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525 /*
526 * SSLv3:
527 * key block =
528 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
529 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
530 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
531 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
532 * ...
533 *
534 * TLSv1:
535 * key block = PRF( master, "key expansion", randbytes )
536 */
Paul Bakker48916f92012-09-16 19:57:18 +0000537 handshake->tls_prf( session->master, 48, "key expansion",
538 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
Paul Bakker48916f92012-09-16 19:57:18 +0000540 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
541 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
542 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
543 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
545
Paul Bakker34617722014-06-13 17:20:13 +0200546 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
548 /*
549 * Determine the appropriate key, IV and MAC length.
550 */
Paul Bakker68884e32013-01-07 18:20:04 +0100551
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200552 transform->keylen = cipher_info->key_length / 8;
553
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200554 if( cipher_info->mode == POLARSSL_MODE_GCM ||
555 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200557 transform->maclen = 0;
558
Paul Bakker68884e32013-01-07 18:20:04 +0100559 transform->ivlen = 12;
560 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200561
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200562 /* Minimum length is expicit IV + tag */
563 transform->minlen = transform->ivlen - transform->fixed_ivlen
564 + ( transform->ciphersuite_info->flags &
565 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100566 }
567 else
568 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200569 /* Initialize HMAC contexts */
570 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
571 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100572 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200573 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
574 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100575 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200577 /* Get MAC length */
578 transform->maclen = md_get_size( md_info );
579
580#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
581 /*
582 * If HMAC is to be truncated, we shall keep the leftmost bytes,
583 * (rfc 6066 page 13 or rfc 2104 section 4),
584 * so we only need to adjust the length here.
585 */
586 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
587 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
588#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
589
590 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100591 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000592
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200593 /* Minimum length */
594 if( cipher_info->mode == POLARSSL_MODE_STREAM )
595 transform->minlen = transform->maclen;
596 else
Paul Bakker68884e32013-01-07 18:20:04 +0100597 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200598 /*
599 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100600 * 1. if EtM is in use: one block plus MAC
601 * otherwise: * first multiple of blocklen greater than maclen
602 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200603 */
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100604#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
605 if( session->encrypt_then_mac == SSL_ETM_ENABLED )
606 {
607 transform->minlen = transform->maclen
608 + cipher_info->block_size;
609 }
610 else
611#endif
612 {
613 transform->minlen = transform->maclen
614 + cipher_info->block_size
615 - transform->maclen % cipher_info->block_size;
616 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200617
618#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
619 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
620 ssl->minor_ver == SSL_MINOR_VERSION_1 )
621 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100622 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200623#endif
624#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
625 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
626 ssl->minor_ver == SSL_MINOR_VERSION_3 )
627 {
628 transform->minlen += transform->ivlen;
629 }
630 else
631#endif
632 {
633 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
634 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
635 }
Paul Bakker68884e32013-01-07 18:20:04 +0100636 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000637 }
638
639 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000640 transform->keylen, transform->minlen, transform->ivlen,
641 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000642
643 /*
644 * Finally setup the cipher contexts, IVs and MAC secrets.
645 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100646#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000647 if( ssl->endpoint == SSL_IS_CLIENT )
648 {
Paul Bakker48916f92012-09-16 19:57:18 +0000649 key1 = keyblk + transform->maclen * 2;
650 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000651
Paul Bakker68884e32013-01-07 18:20:04 +0100652 mac_enc = keyblk;
653 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000654
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000655 /*
656 * This is not used in TLS v1.1.
657 */
Paul Bakker48916f92012-09-16 19:57:18 +0000658 iv_copy_len = ( transform->fixed_ivlen ) ?
659 transform->fixed_ivlen : transform->ivlen;
660 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
661 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000662 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 }
664 else
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100665#endif /* POLARSSL_SSL_CLI_C */
666#if defined(POLARSSL_SSL_SRV_C)
667 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 {
Paul Bakker48916f92012-09-16 19:57:18 +0000669 key1 = keyblk + transform->maclen * 2 + transform->keylen;
670 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
Paul Bakker68884e32013-01-07 18:20:04 +0100672 mac_enc = keyblk + transform->maclen;
673 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000674
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000675 /*
676 * This is not used in TLS v1.1.
677 */
Paul Bakker48916f92012-09-16 19:57:18 +0000678 iv_copy_len = ( transform->fixed_ivlen ) ?
679 transform->fixed_ivlen : transform->ivlen;
680 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
681 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000682 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100684 else
685#endif /* POLARSSL_SSL_SRV_C */
686 {
687 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
688 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
689 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000690
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200691#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100692 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
693 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100694 if( transform->maclen > sizeof transform->mac_enc )
695 {
696 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200697 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100698 }
699
Paul Bakker68884e32013-01-07 18:20:04 +0100700 memcpy( transform->mac_enc, mac_enc, transform->maclen );
701 memcpy( transform->mac_dec, mac_dec, transform->maclen );
702 }
703 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200704#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200705#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
706 defined(POLARSSL_SSL_PROTO_TLS1_2)
707 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100708 {
709 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
710 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
711 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200712 else
713#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200714 {
715 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200716 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200717 }
Paul Bakker68884e32013-01-07 18:20:04 +0100718
Paul Bakker05ef8352012-05-08 09:17:57 +0000719#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200720 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000721 {
722 int ret = 0;
723
724 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
725
Paul Bakker07eb38b2012-12-19 14:42:06 +0100726 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
727 transform->iv_enc, transform->iv_dec,
728 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100729 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100730 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000731 {
732 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200733 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000734 }
735 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200736#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000737
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200738 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
739 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000740 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200741 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
742 return( ret );
743 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200744
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200745 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
746 cipher_info ) ) != 0 )
747 {
748 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
749 return( ret );
750 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200751
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200752 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
753 cipher_info->key_length,
754 POLARSSL_ENCRYPT ) ) != 0 )
755 {
756 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
757 return( ret );
758 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200759
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200760 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
761 cipher_info->key_length,
762 POLARSSL_DECRYPT ) ) != 0 )
763 {
764 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
765 return( ret );
766 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200767
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200768#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200769 if( cipher_info->mode == POLARSSL_MODE_CBC )
770 {
771 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
772 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200773 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200774 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
775 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200776 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200777
778 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
779 POLARSSL_PADDING_NONE ) ) != 0 )
780 {
781 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
782 return( ret );
783 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000784 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200785#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000786
Paul Bakker34617722014-06-13 17:20:13 +0200787 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000788
Paul Bakker2770fbd2012-07-03 13:30:23 +0000789#if defined(POLARSSL_ZLIB_SUPPORT)
790 // Initialize compression
791 //
Paul Bakker48916f92012-09-16 19:57:18 +0000792 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000793 {
Paul Bakker16770332013-10-11 09:59:44 +0200794 if( ssl->compress_buf == NULL )
795 {
796 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
797 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
798 if( ssl->compress_buf == NULL )
799 {
800 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
801 SSL_BUFFER_LEN ) );
802 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
803 }
804 }
805
Paul Bakker2770fbd2012-07-03 13:30:23 +0000806 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
807
Paul Bakker48916f92012-09-16 19:57:18 +0000808 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
809 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000810
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200811 if( deflateInit( &transform->ctx_deflate,
812 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000813 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000814 {
815 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
816 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
817 }
818 }
819#endif /* POLARSSL_ZLIB_SUPPORT */
820
Paul Bakker5121ce52009-01-03 21:22:43 +0000821 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
822
823 return( 0 );
824}
825
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200826#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000827void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000828{
829 md5_context md5;
830 sha1_context sha1;
831 unsigned char pad_1[48];
832 unsigned char pad_2[48];
833
Paul Bakker380da532012-04-18 16:10:25 +0000834 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000835
Paul Bakker48916f92012-09-16 19:57:18 +0000836 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
837 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000838
Paul Bakker380da532012-04-18 16:10:25 +0000839 memset( pad_1, 0x36, 48 );
840 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000841
Paul Bakker48916f92012-09-16 19:57:18 +0000842 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000843 md5_update( &md5, pad_1, 48 );
844 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000845
Paul Bakker380da532012-04-18 16:10:25 +0000846 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000847 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000848 md5_update( &md5, pad_2, 48 );
849 md5_update( &md5, hash, 16 );
850 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000851
Paul Bakker48916f92012-09-16 19:57:18 +0000852 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000853 sha1_update( &sha1, pad_1, 40 );
854 sha1_finish( &sha1, hash + 16 );
855
856 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000857 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000858 sha1_update( &sha1, pad_2, 40 );
859 sha1_update( &sha1, hash + 16, 20 );
860 sha1_finish( &sha1, hash + 16 );
861
862 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
863 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
864
Paul Bakker5b4af392014-06-26 12:09:34 +0200865 md5_free( &md5 );
866 sha1_free( &sha1 );
867
Paul Bakker380da532012-04-18 16:10:25 +0000868 return;
869}
Paul Bakker9af723c2014-05-01 13:03:14 +0200870#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000871
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200872#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000873void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
874{
875 md5_context md5;
876 sha1_context sha1;
877
878 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
879
Paul Bakker48916f92012-09-16 19:57:18 +0000880 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
881 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000882
Paul Bakker48916f92012-09-16 19:57:18 +0000883 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000884 sha1_finish( &sha1, hash + 16 );
885
886 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
887 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
888
Paul Bakker5b4af392014-06-26 12:09:34 +0200889 md5_free( &md5 );
890 sha1_free( &sha1 );
891
Paul Bakker380da532012-04-18 16:10:25 +0000892 return;
893}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200894#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000895
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200896#if defined(POLARSSL_SSL_PROTO_TLS1_2)
897#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000898void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
899{
Paul Bakker9e36f042013-06-30 14:34:05 +0200900 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000901
902 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
903
Paul Bakker9e36f042013-06-30 14:34:05 +0200904 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
905 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000906
907 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
908 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
909
Paul Bakker5b4af392014-06-26 12:09:34 +0200910 sha256_free( &sha256 );
911
Paul Bakker380da532012-04-18 16:10:25 +0000912 return;
913}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200914#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000915
Paul Bakker9e36f042013-06-30 14:34:05 +0200916#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000917void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
918{
Paul Bakker9e36f042013-06-30 14:34:05 +0200919 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000920
921 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
922
Paul Bakker9e36f042013-06-30 14:34:05 +0200923 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
924 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000925
Paul Bakkerca4ab492012-04-18 14:23:57 +0000926 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000927 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
928
Paul Bakker5b4af392014-06-26 12:09:34 +0200929 sha512_free( &sha512 );
930
Paul Bakker5121ce52009-01-03 21:22:43 +0000931 return;
932}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200933#endif /* POLARSSL_SHA512_C */
934#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000935
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200936#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200937int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
938{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200939 unsigned char *p = ssl->handshake->premaster;
940 unsigned char *end = p + sizeof( ssl->handshake->premaster );
941
942 /*
943 * PMS = struct {
944 * opaque other_secret<0..2^16-1>;
945 * opaque psk<0..2^16-1>;
946 * };
947 * with "other_secret" depending on the particular key exchange
948 */
949#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
950 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
951 {
952 if( end - p < 2 + (int) ssl->psk_len )
953 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
954
955 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
956 *(p++) = (unsigned char)( ssl->psk_len );
957 p += ssl->psk_len;
958 }
959 else
960#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200961#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
962 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
963 {
964 /*
965 * other_secret already set by the ClientKeyExchange message,
966 * and is 48 bytes long
967 */
968 *p++ = 0;
969 *p++ = 48;
970 p += 48;
971 }
972 else
973#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200974#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
975 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
976 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200977 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200978 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200979
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200980 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200981 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200982 p + 2, &len,
983 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200984 {
985 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
986 return( ret );
987 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200988 *(p++) = (unsigned char)( len >> 8 );
989 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200990 p += len;
991
992 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
993 }
994 else
995#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
996#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
997 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
998 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200999 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001000 size_t zlen;
1001
1002 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001003 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001004 ssl->f_rng, ssl->p_rng ) ) != 0 )
1005 {
1006 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1007 return( ret );
1008 }
1009
1010 *(p++) = (unsigned char)( zlen >> 8 );
1011 *(p++) = (unsigned char)( zlen );
1012 p += zlen;
1013
1014 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1015 }
1016 else
1017#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1018 {
1019 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001020 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001021 }
1022
1023 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001024 if( end - p < 2 + (int) ssl->psk_len )
1025 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1026
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001027 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1028 *(p++) = (unsigned char)( ssl->psk_len );
1029 memcpy( p, ssl->psk, ssl->psk_len );
1030 p += ssl->psk_len;
1031
1032 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1033
1034 return( 0 );
1035}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001036#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001037
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001038#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001039/*
1040 * SSLv3.0 MAC functions
1041 */
Paul Bakker68884e32013-01-07 18:20:04 +01001042static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1043 unsigned char *buf, size_t len,
1044 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001045{
1046 unsigned char header[11];
1047 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001048 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001049 int md_size = md_get_size( md_ctx->md_info );
1050 int md_type = md_get_type( md_ctx->md_info );
1051
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001052 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001053 if( md_type == POLARSSL_MD_MD5 )
1054 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001055 else
Paul Bakker68884e32013-01-07 18:20:04 +01001056 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001057
1058 memcpy( header, ctr, 8 );
1059 header[ 8] = (unsigned char) type;
1060 header[ 9] = (unsigned char)( len >> 8 );
1061 header[10] = (unsigned char)( len );
1062
Paul Bakker68884e32013-01-07 18:20:04 +01001063 memset( padding, 0x36, padlen );
1064 md_starts( md_ctx );
1065 md_update( md_ctx, secret, md_size );
1066 md_update( md_ctx, padding, padlen );
1067 md_update( md_ctx, header, 11 );
1068 md_update( md_ctx, buf, len );
1069 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001070
Paul Bakker68884e32013-01-07 18:20:04 +01001071 memset( padding, 0x5C, padlen );
1072 md_starts( md_ctx );
1073 md_update( md_ctx, secret, md_size );
1074 md_update( md_ctx, padding, padlen );
1075 md_update( md_ctx, buf + len, md_size );
1076 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001077}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001078#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001079
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001080#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1081 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1082 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1083#define POLARSSL_SOME_MODES_USE_MAC
1084#endif
1085
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001086/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001087 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001088 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001089static int ssl_encrypt_buf( ssl_context *ssl )
1090{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001091 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001092 cipher_mode_t mode;
1093 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001094
1095 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1096
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001097 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1098 {
1099 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1100 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1101 }
1102
1103 mode = cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
1104
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001105 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1106 ssl->out_msg, ssl->out_msglen );
1107
Paul Bakker5121ce52009-01-03 21:22:43 +00001108 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001109 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001110 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001111#if defined(POLARSSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001112 if( mode == POLARSSL_MODE_STREAM ||
1113 ( mode == POLARSSL_MODE_CBC
1114#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1115 && ssl->session_out->encrypt_then_mac == SSL_ETM_DISABLED
1116#endif
1117 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001118 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001119#if defined(POLARSSL_SSL_PROTO_SSL3)
1120 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1121 {
1122 ssl_mac( &ssl->transform_out->md_ctx_enc,
1123 ssl->transform_out->mac_enc,
1124 ssl->out_msg, ssl->out_msglen,
1125 ssl->out_ctr, ssl->out_msgtype );
1126 }
1127 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001128#endif
1129#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001130 defined(POLARSSL_SSL_PROTO_TLS1_2)
1131 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1132 {
1133 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1134 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1135 ssl->out_msg, ssl->out_msglen );
1136 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1137 ssl->out_msg + ssl->out_msglen );
1138 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1139 }
1140 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001141#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001142 {
1143 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001144 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001145 }
1146
1147 SSL_DEBUG_BUF( 4, "computed mac",
1148 ssl->out_msg + ssl->out_msglen,
1149 ssl->transform_out->maclen );
1150
1151 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001152 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001153 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001154#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001155
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001156 /*
1157 * Encrypt
1158 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001159#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001160 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001161 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001162 int ret;
1163 size_t olen = 0;
1164
Paul Bakker5121ce52009-01-03 21:22:43 +00001165 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1166 "including %d bytes of padding",
1167 ssl->out_msglen, 0 ) );
1168
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001169 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001170 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001171 ssl->transform_out->ivlen,
1172 ssl->out_msg, ssl->out_msglen,
1173 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001174 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001175 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001176 return( ret );
1177 }
1178
1179 if( ssl->out_msglen != olen )
1180 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001181 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001182 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001183 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 }
Paul Bakker68884e32013-01-07 18:20:04 +01001185 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001186#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001187#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1188 if( mode == POLARSSL_MODE_GCM ||
1189 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001190 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001191 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001192 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001193 unsigned char *enc_msg;
1194 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001195 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1196 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001197
Paul Bakkerca4ab492012-04-18 14:23:57 +00001198 memcpy( add_data, ssl->out_ctr, 8 );
1199 add_data[8] = ssl->out_msgtype;
1200 add_data[9] = ssl->major_ver;
1201 add_data[10] = ssl->minor_ver;
1202 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1203 add_data[12] = ssl->out_msglen & 0xFF;
1204
1205 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1206 add_data, 13 );
1207
Paul Bakker68884e32013-01-07 18:20:04 +01001208 /*
1209 * Generate IV
1210 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001211#if defined(POLARSSL_SSL_AEAD_RANDOM_IV)
Paul Bakker68884e32013-01-07 18:20:04 +01001212 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001213 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1214 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001215 if( ret != 0 )
1216 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001217
Paul Bakker68884e32013-01-07 18:20:04 +01001218 memcpy( ssl->out_iv,
1219 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1220 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001221#else
1222 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1223 {
1224 /* Reminder if we ever add an AEAD mode with a different size */
1225 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1226 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1227 }
1228
1229 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1230 ssl->out_ctr, 8 );
1231 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1232#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00001233
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001234 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001235 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001236
Paul Bakker68884e32013-01-07 18:20:04 +01001237 /*
1238 * Fix pointer positions and message length with added IV
1239 */
1240 enc_msg = ssl->out_msg;
1241 enc_msglen = ssl->out_msglen;
1242 ssl->out_msglen += ssl->transform_out->ivlen -
1243 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001244
Paul Bakker68884e32013-01-07 18:20:04 +01001245 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1246 "including %d bytes of padding",
1247 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001248
Paul Bakker68884e32013-01-07 18:20:04 +01001249 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001250 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001251 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001252 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1253 ssl->transform_out->iv_enc,
1254 ssl->transform_out->ivlen,
1255 add_data, 13,
1256 enc_msg, enc_msglen,
1257 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001258 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001259 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001260 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001261 return( ret );
1262 }
1263
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001264 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001265 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001266 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001267 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001268 }
1269
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001270 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001271 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001272
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001273 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001274 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001275 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001276#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001277#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1278 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001279 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001280 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001281 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001282 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001283 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001284
Paul Bakker48916f92012-09-16 19:57:18 +00001285 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1286 ssl->transform_out->ivlen;
1287 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001288 padlen = 0;
1289
1290 for( i = 0; i <= padlen; i++ )
1291 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1292
1293 ssl->out_msglen += padlen + 1;
1294
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001295 enc_msglen = ssl->out_msglen;
1296 enc_msg = ssl->out_msg;
1297
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001298#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001299 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001300 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1301 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001302 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001303 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001304 {
1305 /*
1306 * Generate IV
1307 */
Manuel Pégourié-Gonnarda67fd792015-08-27 12:02:40 +02001308 ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001309 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001310 if( ret != 0 )
1311 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001312
Paul Bakker92be97b2013-01-02 17:30:03 +01001313 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001314 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001315
1316 /*
1317 * Fix pointer positions and message length with added IV
1318 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001319 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001320 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001321 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001322 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001323#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001324
Paul Bakker5121ce52009-01-03 21:22:43 +00001325 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001326 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001327 ssl->out_msglen, ssl->transform_out->ivlen,
1328 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001329
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001330 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001331 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001332 ssl->transform_out->ivlen,
1333 enc_msg, enc_msglen,
1334 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001335 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001336 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001337 return( ret );
1338 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001339
Paul Bakkercca5b812013-08-31 17:40:26 +02001340 if( enc_msglen != olen )
1341 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001342 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001343 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001344 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001345
1346#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001347 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1348 {
1349 /*
1350 * Save IV in SSL3 and TLS1
1351 */
1352 memcpy( ssl->transform_out->iv_enc,
1353 ssl->transform_out->cipher_ctx_enc.iv,
1354 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001355 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001356#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001357
1358#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001359 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001360 {
1361 /*
1362 * MAC(MAC_write_key, seq_num +
1363 * TLSCipherText.type +
1364 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001365 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001366 * IV + // except for TLS 1.0
1367 * ENC(content + padding + padding_length));
1368 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001369 unsigned char pseudo_hdr[13];
1370
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001371 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1372
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001373 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1374 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001375 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1376 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1377
1378 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001379
1380 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1381 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1382 ssl->out_iv, ssl->out_msglen );
1383 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1384 ssl->out_iv + ssl->out_msglen );
1385 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1386
1387 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001388 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001389 }
1390#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001391 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001392 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001393#endif /* POLARSSL_CIPHER_MODE_CBC &&
1394 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001395 {
1396 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001397 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001398 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001399
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001400 /* Make extra sure authentication was performed, exactly once */
1401 if( auth_done != 1 )
1402 {
1403 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1404 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1405 }
1406
Paul Bakkerca4ab492012-04-18 14:23:57 +00001407 for( i = 8; i > 0; i-- )
1408 if( ++ssl->out_ctr[i - 1] != 0 )
1409 break;
1410
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001411 /* The loops goes to its end iff the counter is wrapping */
1412 if( i == 0 )
1413 {
1414 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1415 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1416 }
1417
Paul Bakker5121ce52009-01-03 21:22:43 +00001418 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1419
1420 return( 0 );
1421}
1422
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001423#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001424
Paul Bakker5121ce52009-01-03 21:22:43 +00001425static int ssl_decrypt_buf( ssl_context *ssl )
1426{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001427 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001428 cipher_mode_t mode;
1429 int auth_done = 0;
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001430#if defined(POLARSSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001431 size_t padlen = 0, correct = 1;
1432#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001433
1434 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1435
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001436 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1437 {
1438 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1439 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1440 }
1441
1442 mode = cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
1443
Paul Bakker48916f92012-09-16 19:57:18 +00001444 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001445 {
1446 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001447 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001448 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001449 }
1450
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001451#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001452 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001453 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001454 int ret;
1455 size_t olen = 0;
1456
Paul Bakker68884e32013-01-07 18:20:04 +01001457 padlen = 0;
1458
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001459 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001460 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001461 ssl->transform_in->ivlen,
1462 ssl->in_msg, ssl->in_msglen,
1463 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001464 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001465 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001466 return( ret );
1467 }
1468
1469 if( ssl->in_msglen != olen )
1470 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001471 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001472 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001473 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001474 }
Paul Bakker68884e32013-01-07 18:20:04 +01001475 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001476#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001477#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1478 if( mode == POLARSSL_MODE_GCM ||
1479 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001480 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001481 int ret;
1482 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001483 unsigned char *dec_msg;
1484 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001485 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001486 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1487 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001488 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1489 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001490
Manuel Pégourié-Gonnard06d75192015-02-11 14:54:11 +00001491 if( ssl->in_msglen < (size_t) explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001492 {
1493 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1494 "+ taglen (%d)", ssl->in_msglen,
1495 explicit_iv_len, taglen ) );
1496 return( POLARSSL_ERR_SSL_INVALID_MAC );
1497 }
1498 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1499
Paul Bakker68884e32013-01-07 18:20:04 +01001500 dec_msg = ssl->in_msg;
1501 dec_msg_result = ssl->in_msg;
1502 ssl->in_msglen = dec_msglen;
1503
1504 memcpy( add_data, ssl->in_ctr, 8 );
1505 add_data[8] = ssl->in_msgtype;
1506 add_data[9] = ssl->major_ver;
1507 add_data[10] = ssl->minor_ver;
1508 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1509 add_data[12] = ssl->in_msglen & 0xFF;
1510
1511 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1512 add_data, 13 );
1513
1514 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1515 ssl->in_iv,
1516 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1517
1518 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1519 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001520 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001521
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001522 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001523 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001524 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001525 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1526 ssl->transform_in->iv_dec,
1527 ssl->transform_in->ivlen,
1528 add_data, 13,
1529 dec_msg, dec_msglen,
1530 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001531 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001532 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001533 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1534
1535 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1536 return( POLARSSL_ERR_SSL_INVALID_MAC );
1537
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001538 return( ret );
1539 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001540 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001541
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001542 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001543 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001544 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001545 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001546 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001547 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001548 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001549#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001550#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1551 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001552 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001553 {
Paul Bakker45829992013-01-03 14:52:21 +01001554 /*
1555 * Decrypt and check the padding
1556 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001557 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001558 unsigned char *dec_msg;
1559 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001560 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001561 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001562 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001563
Paul Bakker5121ce52009-01-03 21:22:43 +00001564 /*
Paul Bakker45829992013-01-03 14:52:21 +01001565 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001566 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001567#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001568 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1569 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001570#endif
Paul Bakker45829992013-01-03 14:52:21 +01001571
1572 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1573 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1574 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001575 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1576 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1577 ssl->transform_in->ivlen,
1578 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001579 return( POLARSSL_ERR_SSL_INVALID_MAC );
1580 }
1581
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001582 dec_msglen = ssl->in_msglen;
1583 dec_msg = ssl->in_msg;
1584 dec_msg_result = ssl->in_msg;
1585
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001586 /*
1587 * Authenticate before decrypt if enabled
1588 */
1589#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001590 if( ssl->session_in->encrypt_then_mac == SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001591 {
1592 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001593 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001594
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001595 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1596
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001597 dec_msglen -= ssl->transform_in->maclen;
1598 ssl->in_msglen -= ssl->transform_in->maclen;
1599
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001600 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1601 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1602 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1603 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1604
1605 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1606
1607 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001608 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1609 ssl->in_iv, ssl->in_msglen );
1610 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1611 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1612
1613 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1614 ssl->transform_in->maclen );
1615 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1616 ssl->transform_in->maclen );
1617
1618 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1619 ssl->transform_in->maclen ) != 0 )
1620 {
1621 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1622
1623 return( POLARSSL_ERR_SSL_INVALID_MAC );
1624 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001625 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001626 }
1627#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1628
1629 /*
1630 * Check length sanity
1631 */
1632 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1633 {
1634 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1635 ssl->in_msglen, ssl->transform_in->ivlen ) );
1636 return( POLARSSL_ERR_SSL_INVALID_MAC );
1637 }
1638
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001639#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001640 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001641 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001642 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001643 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001644 {
Paul Bakker48916f92012-09-16 19:57:18 +00001645 dec_msglen -= ssl->transform_in->ivlen;
1646 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001647
Paul Bakker48916f92012-09-16 19:57:18 +00001648 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001649 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001650 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001651#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001652
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001653 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001654 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001655 ssl->transform_in->ivlen,
1656 dec_msg, dec_msglen,
1657 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001658 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001659 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001660 return( ret );
1661 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001662
Paul Bakkercca5b812013-08-31 17:40:26 +02001663 if( dec_msglen != olen )
1664 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001665 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001666 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001667 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001668
1669#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001670 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1671 {
1672 /*
1673 * Save IV in SSL3 and TLS1
1674 */
1675 memcpy( ssl->transform_in->iv_dec,
1676 ssl->transform_in->cipher_ctx_dec.iv,
1677 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001678 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001679#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001680
1681 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001682
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001683 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001684 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001685 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001686#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001687 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1688 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001689#endif
Paul Bakker45829992013-01-03 14:52:21 +01001690 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001691 correct = 0;
1692 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001693
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001694#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001695 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1696 {
Paul Bakker48916f92012-09-16 19:57:18 +00001697 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001698 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001699#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001700 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1701 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001702 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001703#endif
Paul Bakker45829992013-01-03 14:52:21 +01001704 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001705 }
1706 }
1707 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001708#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001709#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1710 defined(POLARSSL_SSL_PROTO_TLS1_2)
1711 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001712 {
1713 /*
Paul Bakker45829992013-01-03 14:52:21 +01001714 * TLSv1+: always check the padding up to the first failure
1715 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001716 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001717 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001718 size_t padding_idx = ssl->in_msglen - padlen - 1;
1719
Paul Bakker956c9e02013-12-19 14:42:28 +01001720 /*
1721 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001722 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001723 *
Paul Bakker61885c72014-04-25 12:59:03 +02001724 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1725 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001726 *
1727 * In both cases we reset padding_idx to a safe value (0) to
1728 * prevent out-of-buffer reads.
1729 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001730 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001731 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1732 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001733
1734 padding_idx *= correct;
1735
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001736 for( i = 1; i <= 256; i++ )
1737 {
1738 real_count &= ( i <= padlen );
1739 pad_count += real_count *
1740 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1741 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001742
1743 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001744
Paul Bakkerd66f0702013-01-31 16:57:45 +01001745#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001746 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001747 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001748#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001749 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001750 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001751 else
1752#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1753 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001754 {
1755 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001756 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001757 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001758
1759 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001760 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001761 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001762#endif /* POLARSSL_CIPHER_MODE_CBC &&
1763 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001764 {
1765 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001766 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001767 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001768
1769 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1770 ssl->in_msg, ssl->in_msglen );
1771
1772 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001773 * Authenticate if not done yet.
1774 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001775 */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001776#if defined(POLARSSL_SOME_MODES_USE_MAC)
1777 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001778 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001779 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1780
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001781 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001782
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001783 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1784 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001785
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001786 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001787
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001788#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001789 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1790 {
1791 ssl_mac( &ssl->transform_in->md_ctx_dec,
1792 ssl->transform_in->mac_dec,
1793 ssl->in_msg, ssl->in_msglen,
1794 ssl->in_ctr, ssl->in_msgtype );
1795 }
1796 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001797#endif /* POLARSSL_SSL_PROTO_SSL3 */
1798#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001799 defined(POLARSSL_SSL_PROTO_TLS1_2)
1800 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1801 {
1802 /*
1803 * Process MAC and always update for padlen afterwards to make
1804 * total time independent of padlen
1805 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001806 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001807 *
1808 * Known timing attacks:
1809 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1810 *
1811 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1812 * correctly. (We round down instead of up, so -56 is the correct
1813 * value for our calculations instead of -55)
1814 */
1815 size_t j, extra_run = 0;
1816 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1817 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001818
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001819 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001820
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001821 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1822 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1823 ssl->in_msglen );
1824 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1825 ssl->in_msg + ssl->in_msglen );
Manuel Pégourié-Gonnard7d1e95c2015-04-29 01:35:48 +02001826 /* Call md_process at least once due to cache attacks */
1827 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001828 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001829
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001830 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1831 }
1832 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001833#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001834 POLARSSL_SSL_PROTO_TLS1_2 */
1835 {
1836 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001837 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001838 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001839
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001840 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1841 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1842 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001843
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001844 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001845 ssl->transform_in->maclen ) != 0 )
1846 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001847#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001848 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001849#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001850 correct = 0;
1851 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001852 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001853
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001854 /*
1855 * Finally check the correct flag
1856 */
1857 if( correct == 0 )
1858 return( POLARSSL_ERR_SSL_INVALID_MAC );
1859 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001860#endif /* POLARSSL_SOME_MODES_USE_MAC */
1861
1862 /* Make extra sure authentication was performed, exactly once */
1863 if( auth_done != 1 )
1864 {
1865 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1866 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1867 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001868
1869 if( ssl->in_msglen == 0 )
1870 {
1871 ssl->nb_zero++;
1872
1873 /*
1874 * Three or more empty messages may be a DoS attack
1875 * (excessive CPU consumption).
1876 */
1877 if( ssl->nb_zero > 3 )
1878 {
1879 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1880 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001881 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001882 }
1883 }
1884 else
1885 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001886
Paul Bakker23986e52011-04-24 08:57:21 +00001887 for( i = 8; i > 0; i-- )
1888 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001889 break;
1890
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001891 /* The loops goes to its end iff the counter is wrapping */
1892 if( i == 0 )
1893 {
1894 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1895 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1896 }
1897
Paul Bakker5121ce52009-01-03 21:22:43 +00001898 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1899
1900 return( 0 );
1901}
1902
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001903#undef MAC_NONE
1904#undef MAC_PLAINTEXT
1905#undef MAC_CIPHERTEXT
1906
Paul Bakker2770fbd2012-07-03 13:30:23 +00001907#if defined(POLARSSL_ZLIB_SUPPORT)
1908/*
1909 * Compression/decompression functions
1910 */
1911static int ssl_compress_buf( ssl_context *ssl )
1912{
1913 int ret;
1914 unsigned char *msg_post = ssl->out_msg;
1915 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001916 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001917
1918 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1919
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001920 if( len_pre == 0 )
1921 return( 0 );
1922
Paul Bakker2770fbd2012-07-03 13:30:23 +00001923 memcpy( msg_pre, ssl->out_msg, len_pre );
1924
1925 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1926 ssl->out_msglen ) );
1927
1928 SSL_DEBUG_BUF( 4, "before compression: output payload",
1929 ssl->out_msg, ssl->out_msglen );
1930
Paul Bakker48916f92012-09-16 19:57:18 +00001931 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1932 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1933 ssl->transform_out->ctx_deflate.next_out = msg_post;
1934 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001935
Paul Bakker48916f92012-09-16 19:57:18 +00001936 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001937 if( ret != Z_OK )
1938 {
1939 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1940 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1941 }
1942
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001943 ssl->out_msglen = SSL_BUFFER_LEN -
1944 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001945
Paul Bakker2770fbd2012-07-03 13:30:23 +00001946 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1947 ssl->out_msglen ) );
1948
1949 SSL_DEBUG_BUF( 4, "after compression: output payload",
1950 ssl->out_msg, ssl->out_msglen );
1951
1952 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1953
1954 return( 0 );
1955}
1956
1957static int ssl_decompress_buf( ssl_context *ssl )
1958{
1959 int ret;
1960 unsigned char *msg_post = ssl->in_msg;
1961 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001962 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001963
1964 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1965
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001966 if( len_pre == 0 )
1967 return( 0 );
1968
Paul Bakker2770fbd2012-07-03 13:30:23 +00001969 memcpy( msg_pre, ssl->in_msg, len_pre );
1970
1971 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1972 ssl->in_msglen ) );
1973
1974 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1975 ssl->in_msg, ssl->in_msglen );
1976
Paul Bakker48916f92012-09-16 19:57:18 +00001977 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1978 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1979 ssl->transform_in->ctx_inflate.next_out = msg_post;
1980 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001981
Paul Bakker48916f92012-09-16 19:57:18 +00001982 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001983 if( ret != Z_OK )
1984 {
1985 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1986 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1987 }
1988
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001989 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1990 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001991
Paul Bakker2770fbd2012-07-03 13:30:23 +00001992 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1993 ssl->in_msglen ) );
1994
1995 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1996 ssl->in_msg, ssl->in_msglen );
1997
1998 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1999
2000 return( 0 );
2001}
2002#endif /* POLARSSL_ZLIB_SUPPORT */
2003
Paul Bakker5121ce52009-01-03 21:22:43 +00002004/*
2005 * Fill the input message buffer
2006 */
Paul Bakker23986e52011-04-24 08:57:21 +00002007int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002008{
Paul Bakker23986e52011-04-24 08:57:21 +00002009 int ret;
2010 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002011
2012 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
2013
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002014 if( nb_want > SSL_BUFFER_LEN - 8 )
2015 {
2016 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2017 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2018 }
2019
Paul Bakker5121ce52009-01-03 21:22:43 +00002020 while( ssl->in_left < nb_want )
2021 {
2022 len = nb_want - ssl->in_left;
2023 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
2024
2025 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2026 ssl->in_left, nb_want ) );
2027 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2028
Paul Bakker831a7552011-05-18 13:32:51 +00002029 if( ret == 0 )
2030 return( POLARSSL_ERR_SSL_CONN_EOF );
2031
Paul Bakker5121ce52009-01-03 21:22:43 +00002032 if( ret < 0 )
2033 return( ret );
2034
2035 ssl->in_left += ret;
2036 }
2037
2038 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2039
2040 return( 0 );
2041}
2042
2043/*
2044 * Flush any data not yet written
2045 */
2046int ssl_flush_output( ssl_context *ssl )
2047{
2048 int ret;
2049 unsigned char *buf;
2050
2051 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2052
2053 while( ssl->out_left > 0 )
2054 {
2055 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2056 5 + ssl->out_msglen, ssl->out_left ) );
2057
Paul Bakker5bd42292012-12-19 14:40:42 +01002058 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00002059 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002060
Paul Bakker5121ce52009-01-03 21:22:43 +00002061 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2062
2063 if( ret <= 0 )
2064 return( ret );
2065
2066 ssl->out_left -= ret;
2067 }
2068
2069 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2070
2071 return( 0 );
2072}
2073
2074/*
2075 * Record layer functions
2076 */
2077int ssl_write_record( ssl_context *ssl )
2078{
Paul Bakker05ef8352012-05-08 09:17:57 +00002079 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002080 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002081
2082 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2083
Paul Bakker5121ce52009-01-03 21:22:43 +00002084 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2085 {
2086 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2087 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2088 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2089
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002090 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2091 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 }
2093
Paul Bakker2770fbd2012-07-03 13:30:23 +00002094#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002095 if( ssl->transform_out != NULL &&
2096 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002097 {
2098 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2099 {
2100 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2101 return( ret );
2102 }
2103
2104 len = ssl->out_msglen;
2105 }
2106#endif /*POLARSSL_ZLIB_SUPPORT */
2107
Paul Bakker05ef8352012-05-08 09:17:57 +00002108#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002109 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002111 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002112
Paul Bakker05ef8352012-05-08 09:17:57 +00002113 ret = ssl_hw_record_write( ssl );
2114 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2115 {
2116 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002117 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002118 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002119
2120 if( ret == 0 )
2121 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002122 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002123#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002124 if( !done )
2125 {
2126 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2127 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2128 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002129 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2130 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002131
Paul Bakker48916f92012-09-16 19:57:18 +00002132 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002133 {
2134 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2135 {
2136 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2137 return( ret );
2138 }
2139
2140 len = ssl->out_msglen;
2141 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2142 ssl->out_hdr[4] = (unsigned char)( len );
2143 }
2144
2145 ssl->out_left = 5 + ssl->out_msglen;
2146
2147 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2148 "version = [%d:%d], msglen = %d",
2149 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2150 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2151
2152 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002153 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002154 }
2155
Paul Bakker5121ce52009-01-03 21:22:43 +00002156 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2157 {
2158 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2159 return( ret );
2160 }
2161
2162 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2163
2164 return( 0 );
2165}
2166
2167int ssl_read_record( ssl_context *ssl )
2168{
Paul Bakker05ef8352012-05-08 09:17:57 +00002169 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002170
2171 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2172
2173 if( ssl->in_hslen != 0 &&
2174 ssl->in_hslen < ssl->in_msglen )
2175 {
2176 /*
2177 * Get next Handshake message in the current record
2178 */
2179 ssl->in_msglen -= ssl->in_hslen;
2180
Paul Bakker8934a982011-08-05 11:11:53 +00002181 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002182 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002183
2184 ssl->in_hslen = 4;
2185 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2186
2187 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2188 " %d, type = %d, hslen = %d",
2189 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2190
2191 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2192 {
2193 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002194 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002195 }
2196
2197 if( ssl->in_msglen < ssl->in_hslen )
2198 {
2199 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002200 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002201 }
2202
Paul Bakker4224bc02014-04-08 14:36:50 +02002203 if( ssl->state != SSL_HANDSHAKE_OVER )
2204 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002205
2206 return( 0 );
2207 }
2208
2209 ssl->in_hslen = 0;
2210
2211 /*
2212 * Read the record header and validate it
2213 */
Manuel Pégourié-Gonnard0aaefce2015-10-27 13:42:11 +01002214read_record_header:
Paul Bakker5121ce52009-01-03 21:22:43 +00002215 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2216 {
2217 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2218 return( ret );
2219 }
2220
2221 ssl->in_msgtype = ssl->in_hdr[0];
2222 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2223
2224 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2225 "version = [%d:%d], msglen = %d",
2226 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2227 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2228
2229 if( ssl->in_hdr[1] != ssl->major_ver )
2230 {
2231 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002232 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002233 }
2234
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002235 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 {
2237 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002238 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 }
2240
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002241 /* Sanity check (outer boundaries) */
2242 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2243 {
2244 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2245 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2246 }
2247
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002249 * Make sure the message length is acceptable for the current transform
2250 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 */
Paul Bakker48916f92012-09-16 19:57:18 +00002252 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002254 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 {
2256 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002257 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002258 }
2259 }
2260 else
2261 {
Paul Bakker48916f92012-09-16 19:57:18 +00002262 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002263 {
2264 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002265 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002266 }
2267
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002268#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002269 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002270 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002271 {
2272 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002273 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002274 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002275#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002276
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002277#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2278 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002279 /*
2280 * TLS encrypted messages can have up to 256 bytes of padding
2281 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002282 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002283 ssl->in_msglen > ssl->transform_in->minlen +
2284 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002285 {
2286 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002287 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002288 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002289#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002290 }
2291
2292 /*
2293 * Read and optionally decrypt the message contents
2294 */
2295 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2296 {
2297 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2298 return( ret );
2299 }
2300
2301 SSL_DEBUG_BUF( 4, "input record from network",
2302 ssl->in_hdr, 5 + ssl->in_msglen );
2303
Paul Bakker05ef8352012-05-08 09:17:57 +00002304#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002305 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002306 {
2307 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2308
2309 ret = ssl_hw_record_read( ssl );
2310 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2311 {
2312 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002313 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002314 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002315
2316 if( ret == 0 )
2317 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002318 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002319#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002320 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002321 {
2322 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2323 {
Paul Bakker40865c82013-01-31 17:13:13 +01002324#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2325 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2326 {
2327 ssl_send_alert_message( ssl,
2328 SSL_ALERT_LEVEL_FATAL,
2329 SSL_ALERT_MSG_BAD_RECORD_MAC );
2330 }
2331#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002332 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2333 return( ret );
2334 }
2335
2336 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2337 ssl->in_msg, ssl->in_msglen );
2338
2339 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2340 {
2341 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002342 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002343 }
2344 }
2345
Paul Bakker2770fbd2012-07-03 13:30:23 +00002346#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002347 if( ssl->transform_in != NULL &&
2348 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002349 {
2350 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2351 {
2352 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2353 return( ret );
2354 }
2355
2356 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2357 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2358 }
2359#endif /* POLARSSL_ZLIB_SUPPORT */
2360
Paul Bakker0a925182012-04-16 06:46:41 +00002361 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2362 ssl->in_msgtype != SSL_MSG_ALERT &&
2363 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2364 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2365 {
2366 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2367
Paul Bakker48916f92012-09-16 19:57:18 +00002368 if( ( ret = ssl_send_alert_message( ssl,
2369 SSL_ALERT_LEVEL_FATAL,
2370 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002371 {
2372 return( ret );
2373 }
2374
2375 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2376 }
2377
Paul Bakker5121ce52009-01-03 21:22:43 +00002378 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2379 {
2380 ssl->in_hslen = 4;
2381 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2382
2383 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2384 " %d, type = %d, hslen = %d",
2385 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2386
2387 /*
2388 * Additional checks to validate the handshake header
2389 */
2390 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2391 {
2392 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002393 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002394 }
2395
2396 if( ssl->in_msglen < ssl->in_hslen )
2397 {
2398 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002399 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 }
2401
Paul Bakker48916f92012-09-16 19:57:18 +00002402 if( ssl->state != SSL_HANDSHAKE_OVER )
2403 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 }
2405
2406 if( ssl->in_msgtype == SSL_MSG_ALERT )
2407 {
2408 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2409 ssl->in_msg[0], ssl->in_msg[1] ) );
2410
2411 /*
Manuel Pégourié-Gonnard0aaefce2015-10-27 13:42:11 +01002412 * Ignore non-fatal alerts, except close_notify and no_renego
Paul Bakker5121ce52009-01-03 21:22:43 +00002413 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002414 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002415 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002416 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2417 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002418 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002419 }
2420
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002421 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2422 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002423 {
2424 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002425 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 }
Manuel Pégourié-Gonnard0aaefce2015-10-27 13:42:11 +01002427
2428 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2429 ssl->in_msg[1] == SSL_ALERT_MSG_NO_RENEGOTIATION )
2430 {
2431 SSL_DEBUG_MSG( 2, ( "is a no_renegotiation" ) );
2432 /* Will be handled when trying to parse ServerHello */
2433 ssl->in_left = 0;
2434 return( 0 );
2435 }
2436
2437 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
2438 ssl->endpoint == SSL_IS_SERVER &&
2439 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2440 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
2441 {
2442 SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
2443 /* Will be handled in ssl_parse_certificate() */
2444 ssl->in_left = 0;
2445 return( 0 );
2446 }
2447
2448 /* Silently discard: fetch new message */
2449 goto read_record_header;
Paul Bakker5121ce52009-01-03 21:22:43 +00002450 }
2451
2452 ssl->in_left = 0;
2453
2454 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2455
2456 return( 0 );
2457}
2458
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002459int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2460{
2461 int ret;
2462
2463 if( ( ret = ssl_send_alert_message( ssl,
2464 SSL_ALERT_LEVEL_FATAL,
2465 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2466 {
2467 return( ret );
2468 }
2469
2470 return( 0 );
2471}
2472
Paul Bakker0a925182012-04-16 06:46:41 +00002473int ssl_send_alert_message( ssl_context *ssl,
2474 unsigned char level,
2475 unsigned char message )
2476{
2477 int ret;
2478
2479 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2480
2481 ssl->out_msgtype = SSL_MSG_ALERT;
2482 ssl->out_msglen = 2;
2483 ssl->out_msg[0] = level;
2484 ssl->out_msg[1] = message;
2485
2486 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2487 {
2488 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2489 return( ret );
2490 }
2491
2492 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2493
2494 return( 0 );
2495}
2496
Paul Bakker5121ce52009-01-03 21:22:43 +00002497/*
2498 * Handshake functions
2499 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002500#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2501 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2502 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2503 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2504 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2505 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2506 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002507int ssl_write_certificate( ssl_context *ssl )
2508{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002509 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002510
2511 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2512
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002513 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002514 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2515 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002516 {
2517 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2518 ssl->state++;
2519 return( 0 );
2520 }
2521
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002522 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2523 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002524}
2525
2526int ssl_parse_certificate( ssl_context *ssl )
2527{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002528 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2529
2530 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2531
2532 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002533 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2534 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002535 {
2536 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2537 ssl->state++;
2538 return( 0 );
2539 }
2540
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002541 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2542 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002543}
2544#else
2545int ssl_write_certificate( ssl_context *ssl )
2546{
2547 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2548 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002549 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002550 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2551
2552 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2553
2554 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002555 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2556 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002557 {
2558 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2559 ssl->state++;
2560 return( 0 );
2561 }
2562
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002563#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002564 if( ssl->endpoint == SSL_IS_CLIENT )
2565 {
2566 if( ssl->client_auth == 0 )
2567 {
2568 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2569 ssl->state++;
2570 return( 0 );
2571 }
2572
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002573#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 /*
2575 * If using SSLv3 and got no cert, send an Alert message
2576 * (otherwise an empty Certificate message will be sent).
2577 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002578 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002579 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2580 {
2581 ssl->out_msglen = 2;
2582 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002583 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2584 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002585
2586 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2587 goto write_msg;
2588 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002589#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002591#endif /* POLARSSL_SSL_CLI_C */
2592#if defined(POLARSSL_SSL_SRV_C)
2593 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00002594 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002595 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002596 {
2597 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002598 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 }
2600 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002601#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002602
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002603 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002604
2605 /*
2606 * 0 . 0 handshake type
2607 * 1 . 3 handshake length
2608 * 4 . 6 length of all certs
2609 * 7 . 9 length of cert. 1
2610 * 10 . n-1 peer certificate
2611 * n . n+2 length of cert. 2
2612 * n+3 . ... upper level cert, etc.
2613 */
2614 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002615 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002616
Paul Bakker29087132010-03-21 21:03:34 +00002617 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 {
2619 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002620 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002621 {
2622 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2623 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002624 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 }
2626
2627 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2628 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2629 ssl->out_msg[i + 2] = (unsigned char)( n );
2630
2631 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2632 i += n; crt = crt->next;
2633 }
2634
2635 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2636 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2637 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2638
2639 ssl->out_msglen = i;
2640 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2641 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2642
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002643#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002644write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002645#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002646
2647 ssl->state++;
2648
2649 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2650 {
2651 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2652 return( ret );
2653 }
2654
2655 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2656
Paul Bakkered27a042013-04-18 22:46:23 +02002657 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002658}
2659
2660int ssl_parse_certificate( ssl_context *ssl )
2661{
Paul Bakkered27a042013-04-18 22:46:23 +02002662 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002663 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002664 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
2666 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2667
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002668 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002669 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2670 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002671 {
2672 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2673 ssl->state++;
2674 return( 0 );
2675 }
2676
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002677#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002678 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002679 ( ssl->authmode == SSL_VERIFY_NONE ||
2680 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002681 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002682 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002683 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2684 ssl->state++;
2685 return( 0 );
2686 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002687#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002688
2689 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2690 {
2691 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2692 return( ret );
2693 }
2694
2695 ssl->state++;
2696
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002697#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002698#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 /*
2700 * Check if the client sent an empty certificate
2701 */
2702 if( ssl->endpoint == SSL_IS_SERVER &&
2703 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2704 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002705 if( ssl->in_msglen == 2 &&
2706 ssl->in_msgtype == SSL_MSG_ALERT &&
2707 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2708 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002709 {
2710 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2711
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002712 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002713 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2714 return( 0 );
2715 else
Paul Bakker40e46942009-01-03 21:51:57 +00002716 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002717 }
2718 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002719#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002720
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002721#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2722 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002723 if( ssl->endpoint == SSL_IS_SERVER &&
2724 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2725 {
2726 if( ssl->in_hslen == 7 &&
2727 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2728 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2729 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2730 {
2731 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2732
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002733 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002734 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002735 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002736 else
2737 return( 0 );
2738 }
2739 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002740#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2741 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002742#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002743
2744 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2745 {
2746 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002747 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002748 }
2749
2750 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2751 {
2752 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002753 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002754 }
2755
2756 /*
2757 * Same message structure as in ssl_write_certificate()
2758 */
2759 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2760
2761 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2762 {
2763 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002764 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002765 }
2766
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002767 /* In case we tried to reuse a session but it failed */
2768 if( ssl->session_negotiate->peer_cert != NULL )
2769 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002770 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002771 polarssl_free( ssl->session_negotiate->peer_cert );
2772 }
2773
Mansour Moufidc531b4a2015-02-15 17:35:38 -05002774 if( ( ssl->session_negotiate->peer_cert = polarssl_malloc(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002775 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002776 {
2777 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002778 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002779 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002780 }
2781
Paul Bakkerb6b09562013-09-18 14:17:41 +02002782 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002783
2784 i = 7;
2785
2786 while( i < ssl->in_hslen )
2787 {
2788 if( ssl->in_msg[i] != 0 )
2789 {
2790 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002791 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002792 }
2793
2794 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2795 | (unsigned int) ssl->in_msg[i + 2];
2796 i += 3;
2797
2798 if( n < 128 || i + n > ssl->in_hslen )
2799 {
2800 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002801 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002802 }
2803
Paul Bakkerddf26b42013-09-18 13:46:23 +02002804 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2805 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002806 if( ret != 0 )
2807 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002808 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002809 return( ret );
2810 }
2811
2812 i += n;
2813 }
2814
Paul Bakker48916f92012-09-16 19:57:18 +00002815 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002816
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002817 /*
2818 * On client, make sure the server cert doesn't change during renego to
2819 * avoid "triple handshake" attack: https://secure-resumption.com/
2820 */
Paul Bakkerf6080b82015-01-13 16:18:23 +01002821#if defined(POLARSSL_SSL_RENEGOTIATION) && defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002822 if( ssl->endpoint == SSL_IS_CLIENT &&
2823 ssl->renegotiation == SSL_RENEGOTIATION )
2824 {
2825 if( ssl->session->peer_cert == NULL )
2826 {
2827 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2828 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2829 }
2830
2831 if( ssl->session->peer_cert->raw.len !=
2832 ssl->session_negotiate->peer_cert->raw.len ||
2833 memcmp( ssl->session->peer_cert->raw.p,
2834 ssl->session_negotiate->peer_cert->raw.p,
2835 ssl->session->peer_cert->raw.len ) != 0 )
2836 {
2837 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2838 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2839 }
2840 }
Paul Bakkerf6080b82015-01-13 16:18:23 +01002841#endif /* POLARSSL_SSL_RENEGOTIATION && POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002842
Paul Bakker5121ce52009-01-03 21:22:43 +00002843 if( ssl->authmode != SSL_VERIFY_NONE )
2844 {
2845 if( ssl->ca_chain == NULL )
2846 {
2847 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002848 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002849 }
2850
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002851 /*
2852 * Main check: verify certificate
2853 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002854 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2855 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2856 &ssl->session_negotiate->verify_result,
2857 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002858
2859 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002860 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002861 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002862 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002863
2864 /*
2865 * Secondary checks: always done, but change 'ret' only if it was 0
2866 */
2867
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002868#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002869 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002870 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002871
2872 /* If certificate uses an EC key, make sure the curve is OK */
2873 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2874 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2875 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002876 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002877 if( ret == 0 )
2878 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002879 }
2880 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002881#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002882
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002883 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2884 ciphersuite_info,
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02002885 ! ssl->endpoint,
2886 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002887 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002888 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002889 if( ret == 0 )
2890 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2891 }
2892
Paul Bakker5121ce52009-01-03 21:22:43 +00002893 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2894 ret = 0;
2895 }
2896
2897 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2898
2899 return( ret );
2900}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002901#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2902 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2903 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2904 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2905 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2906 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2907 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002908
2909int ssl_write_change_cipher_spec( ssl_context *ssl )
2910{
2911 int ret;
2912
2913 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2914
2915 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2916 ssl->out_msglen = 1;
2917 ssl->out_msg[0] = 1;
2918
Paul Bakker5121ce52009-01-03 21:22:43 +00002919 ssl->state++;
2920
2921 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2922 {
2923 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2924 return( ret );
2925 }
2926
2927 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2928
2929 return( 0 );
2930}
2931
2932int ssl_parse_change_cipher_spec( ssl_context *ssl )
2933{
2934 int ret;
2935
2936 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2937
Paul Bakker5121ce52009-01-03 21:22:43 +00002938 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2939 {
2940 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2941 return( ret );
2942 }
2943
2944 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2945 {
2946 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002947 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002948 }
2949
2950 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2951 {
2952 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002953 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002954 }
2955
2956 ssl->state++;
2957
2958 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2959
2960 return( 0 );
2961}
2962
Paul Bakker41c83d32013-03-20 14:39:14 +01002963void ssl_optimize_checksum( ssl_context *ssl,
2964 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002965{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002966 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002967
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002968#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2969 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002970 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002971 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002972 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002973#endif
2974#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2975#if defined(POLARSSL_SHA512_C)
2976 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2977 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2978 else
2979#endif
2980#if defined(POLARSSL_SHA256_C)
2981 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002982 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002983 else
2984#endif
2985#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002986 {
2987 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002988 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002989 }
Paul Bakker380da532012-04-18 16:10:25 +00002990}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002991
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002992static void ssl_update_checksum_start( ssl_context *ssl,
2993 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002994{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002995#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2996 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002997 md5_update( &ssl->handshake->fin_md5 , buf, len );
2998 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002999#endif
3000#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3001#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003002 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003003#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003004#if defined(POLARSSL_SHA512_C)
3005 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003006#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003007#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003008}
3009
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003010#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3011 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003012static void ssl_update_checksum_md5sha1( ssl_context *ssl,
3013 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003014{
Paul Bakker48916f92012-09-16 19:57:18 +00003015 md5_update( &ssl->handshake->fin_md5 , buf, len );
3016 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003017}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003018#endif
Paul Bakker380da532012-04-18 16:10:25 +00003019
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003020#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3021#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003022static void ssl_update_checksum_sha256( ssl_context *ssl,
3023 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003024{
Paul Bakker9e36f042013-06-30 14:34:05 +02003025 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003026}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003027#endif
Paul Bakker380da532012-04-18 16:10:25 +00003028
Paul Bakker9e36f042013-06-30 14:34:05 +02003029#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003030static void ssl_update_checksum_sha384( ssl_context *ssl,
3031 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003032{
Paul Bakker9e36f042013-06-30 14:34:05 +02003033 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003034}
Paul Bakker769075d2012-11-24 11:26:46 +01003035#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003036#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003037
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003038#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003039static void ssl_calc_finished_ssl(
3040 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003041{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003042 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003043 md5_context md5;
3044 sha1_context sha1;
3045
Paul Bakker5121ce52009-01-03 21:22:43 +00003046 unsigned char padbuf[48];
3047 unsigned char md5sum[16];
3048 unsigned char sha1sum[20];
3049
Paul Bakker48916f92012-09-16 19:57:18 +00003050 ssl_session *session = ssl->session_negotiate;
3051 if( !session )
3052 session = ssl->session;
3053
Paul Bakker1ef83d62012-04-11 12:09:53 +00003054 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3055
Paul Bakker48916f92012-09-16 19:57:18 +00003056 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3057 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003058
3059 /*
3060 * SSLv3:
3061 * hash =
3062 * MD5( master + pad2 +
3063 * MD5( handshake + sender + master + pad1 ) )
3064 * + SHA1( master + pad2 +
3065 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003066 */
3067
Paul Bakker90995b52013-06-24 19:20:35 +02003068#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003069 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003070 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003071#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003072
Paul Bakker90995b52013-06-24 19:20:35 +02003073#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003074 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003075 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003076#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003077
Paul Bakker3c2122f2013-06-24 19:03:14 +02003078 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3079 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003080
Paul Bakker1ef83d62012-04-11 12:09:53 +00003081 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003082
Paul Bakker3c2122f2013-06-24 19:03:14 +02003083 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003084 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003085 md5_update( &md5, padbuf, 48 );
3086 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003087
Paul Bakker3c2122f2013-06-24 19:03:14 +02003088 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003089 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003090 sha1_update( &sha1, padbuf, 40 );
3091 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003092
Paul Bakker1ef83d62012-04-11 12:09:53 +00003093 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003094
Paul Bakker1ef83d62012-04-11 12:09:53 +00003095 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003096 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003097 md5_update( &md5, padbuf, 48 );
3098 md5_update( &md5, md5sum, 16 );
3099 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003100
Paul Bakker1ef83d62012-04-11 12:09:53 +00003101 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003102 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003103 sha1_update( &sha1, padbuf , 40 );
3104 sha1_update( &sha1, sha1sum, 20 );
3105 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003106
Paul Bakker1ef83d62012-04-11 12:09:53 +00003107 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003108
Paul Bakker5b4af392014-06-26 12:09:34 +02003109 md5_free( &md5 );
3110 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003111
Paul Bakker34617722014-06-13 17:20:13 +02003112 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3113 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3114 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003115
3116 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3117}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003118#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003119
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003120#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003121static void ssl_calc_finished_tls(
3122 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003123{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003124 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003125 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003126 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003127 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003128 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003129
Paul Bakker48916f92012-09-16 19:57:18 +00003130 ssl_session *session = ssl->session_negotiate;
3131 if( !session )
3132 session = ssl->session;
3133
Paul Bakker1ef83d62012-04-11 12:09:53 +00003134 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003135
Paul Bakker48916f92012-09-16 19:57:18 +00003136 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3137 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003138
Paul Bakker1ef83d62012-04-11 12:09:53 +00003139 /*
3140 * TLSv1:
3141 * hash = PRF( master, finished_label,
3142 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3143 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003144
Paul Bakker90995b52013-06-24 19:20:35 +02003145#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003146 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3147 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003148#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003149
Paul Bakker90995b52013-06-24 19:20:35 +02003150#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003151 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3152 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003153#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003154
3155 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003156 ? "client finished"
3157 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003158
3159 md5_finish( &md5, padbuf );
3160 sha1_finish( &sha1, padbuf + 16 );
3161
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003162 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003163 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003164
3165 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3166
Paul Bakker5b4af392014-06-26 12:09:34 +02003167 md5_free( &md5 );
3168 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003169
Paul Bakker34617722014-06-13 17:20:13 +02003170 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003171
3172 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3173}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003174#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003175
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003176#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3177#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003178static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003179 ssl_context *ssl, unsigned char *buf, int from )
3180{
3181 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003182 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003183 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003184 unsigned char padbuf[32];
3185
Paul Bakker48916f92012-09-16 19:57:18 +00003186 ssl_session *session = ssl->session_negotiate;
3187 if( !session )
3188 session = ssl->session;
3189
Paul Bakker380da532012-04-18 16:10:25 +00003190 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003191
Paul Bakker9e36f042013-06-30 14:34:05 +02003192 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003193
3194 /*
3195 * TLSv1.2:
3196 * hash = PRF( master, finished_label,
3197 * Hash( handshake ) )[0.11]
3198 */
3199
Paul Bakker9e36f042013-06-30 14:34:05 +02003200#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003201 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003202 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003203#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003204
3205 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003206 ? "client finished"
3207 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003208
Paul Bakker9e36f042013-06-30 14:34:05 +02003209 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003210
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003211 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003212 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003213
3214 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3215
Paul Bakker5b4af392014-06-26 12:09:34 +02003216 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003217
Paul Bakker34617722014-06-13 17:20:13 +02003218 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003219
3220 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3221}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003222#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003223
Paul Bakker9e36f042013-06-30 14:34:05 +02003224#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003225static void ssl_calc_finished_tls_sha384(
3226 ssl_context *ssl, unsigned char *buf, int from )
3227{
3228 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003229 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003230 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003231 unsigned char padbuf[48];
3232
Paul Bakker48916f92012-09-16 19:57:18 +00003233 ssl_session *session = ssl->session_negotiate;
3234 if( !session )
3235 session = ssl->session;
3236
Paul Bakker380da532012-04-18 16:10:25 +00003237 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003238
Paul Bakker9e36f042013-06-30 14:34:05 +02003239 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003240
3241 /*
3242 * TLSv1.2:
3243 * hash = PRF( master, finished_label,
3244 * Hash( handshake ) )[0.11]
3245 */
3246
Paul Bakker9e36f042013-06-30 14:34:05 +02003247#if !defined(POLARSSL_SHA512_ALT)
3248 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3249 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003250#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003251
3252 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003253 ? "client finished"
3254 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003255
Paul Bakker9e36f042013-06-30 14:34:05 +02003256 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003257
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003258 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003259 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003260
3261 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3262
Paul Bakker5b4af392014-06-26 12:09:34 +02003263 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003264
Paul Bakker34617722014-06-13 17:20:13 +02003265 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003266
3267 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3268}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003269#endif /* POLARSSL_SHA512_C */
3270#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003271
Paul Bakker48916f92012-09-16 19:57:18 +00003272void ssl_handshake_wrapup( ssl_context *ssl )
3273{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003274 int resume = ssl->handshake->resume;
3275
Paul Bakker48916f92012-09-16 19:57:18 +00003276 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3277
3278 /*
3279 * Free our handshake params
3280 */
3281 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003282 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003283 ssl->handshake = NULL;
3284
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003285#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003286 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003287 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003288 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003289 ssl->renego_records_seen = 0;
3290 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003291#endif
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003292
Paul Bakker48916f92012-09-16 19:57:18 +00003293 /*
3294 * Switch in our now active transform context
3295 */
3296 if( ssl->transform )
3297 {
3298 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003299 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003300 }
3301 ssl->transform = ssl->transform_negotiate;
3302 ssl->transform_negotiate = NULL;
3303
Paul Bakker0a597072012-09-25 21:55:46 +00003304 if( ssl->session )
3305 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003306#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3307 /* RFC 7366 3.1: keep the EtM state */
3308 ssl->session_negotiate->encrypt_then_mac =
3309 ssl->session->encrypt_then_mac;
3310#endif
3311
Paul Bakker0a597072012-09-25 21:55:46 +00003312 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003313 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003314 }
3315 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003316 ssl->session_negotiate = NULL;
3317
Paul Bakker0a597072012-09-25 21:55:46 +00003318 /*
3319 * Add cache entry
3320 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003321 if( ssl->f_set_cache != NULL &&
3322 ssl->session->length != 0 &&
3323 resume == 0 )
3324 {
Paul Bakker0a597072012-09-25 21:55:46 +00003325 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3326 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003327 }
Paul Bakker0a597072012-09-25 21:55:46 +00003328
Paul Bakker48916f92012-09-16 19:57:18 +00003329 ssl->state++;
3330
3331 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3332}
3333
Paul Bakker1ef83d62012-04-11 12:09:53 +00003334int ssl_write_finished( ssl_context *ssl )
3335{
3336 int ret, hash_len;
3337
3338 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3339
Paul Bakker92be97b2013-01-02 17:30:03 +01003340 /*
3341 * Set the out_msg pointer to the correct location based on IV length
3342 */
3343 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3344 {
3345 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3346 ssl->transform_negotiate->fixed_ivlen;
3347 }
3348 else
3349 ssl->out_msg = ssl->out_iv;
3350
Paul Bakker48916f92012-09-16 19:57:18 +00003351 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003352
3353 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003354 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3355
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003356#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003357 ssl->verify_data_len = hash_len;
3358 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003359#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003360
Paul Bakker5121ce52009-01-03 21:22:43 +00003361 ssl->out_msglen = 4 + hash_len;
3362 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3363 ssl->out_msg[0] = SSL_HS_FINISHED;
3364
3365 /*
3366 * In case of session resuming, invert the client and server
3367 * ChangeCipherSpec messages order.
3368 */
Paul Bakker0a597072012-09-25 21:55:46 +00003369 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003370 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003371#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003372 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003373 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003374#endif
3375#if defined(POLARSSL_SSL_SRV_C)
3376 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003377 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003378#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003379 }
3380 else
3381 ssl->state++;
3382
Paul Bakker48916f92012-09-16 19:57:18 +00003383 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003384 * Switch to our negotiated transform and session parameters for outbound
3385 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003386 */
3387 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3388 ssl->transform_out = ssl->transform_negotiate;
3389 ssl->session_out = ssl->session_negotiate;
3390 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003391
Paul Bakker07eb38b2012-12-19 14:42:06 +01003392#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003393 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003394 {
3395 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3396 {
3397 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3398 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3399 }
3400 }
3401#endif
3402
Paul Bakker5121ce52009-01-03 21:22:43 +00003403 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3404 {
3405 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3406 return( ret );
3407 }
3408
3409 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3410
3411 return( 0 );
3412}
3413
3414int ssl_parse_finished( ssl_context *ssl )
3415{
Paul Bakker23986e52011-04-24 08:57:21 +00003416 int ret;
3417 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003418 unsigned char buf[36];
3419
3420 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3421
Paul Bakker48916f92012-09-16 19:57:18 +00003422 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003423
Paul Bakker48916f92012-09-16 19:57:18 +00003424 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003425 * Switch to our negotiated transform and session parameters for inbound
3426 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003427 */
3428 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3429 ssl->transform_in = ssl->transform_negotiate;
3430 ssl->session_in = ssl->session_negotiate;
3431 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003432
Paul Bakker92be97b2013-01-02 17:30:03 +01003433 /*
3434 * Set the in_msg pointer to the correct location based on IV length
3435 */
3436 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3437 {
3438 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3439 ssl->transform_negotiate->fixed_ivlen;
3440 }
3441 else
3442 ssl->in_msg = ssl->in_iv;
3443
Paul Bakker07eb38b2012-12-19 14:42:06 +01003444#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003445 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003446 {
3447 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3448 {
3449 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3450 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3451 }
3452 }
3453#endif
3454
Paul Bakker5121ce52009-01-03 21:22:43 +00003455 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3456 {
3457 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3458 return( ret );
3459 }
3460
3461 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3462 {
3463 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003464 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003465 }
3466
Paul Bakker1ef83d62012-04-11 12:09:53 +00003467 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003468 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3469
3470 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3471 ssl->in_hslen != 4 + hash_len )
3472 {
3473 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003474 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003475 }
3476
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003477 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003478 {
3479 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003480 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003481 }
3482
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003483#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003484 ssl->verify_data_len = hash_len;
3485 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003486#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003487
Paul Bakker0a597072012-09-25 21:55:46 +00003488 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003489 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003490#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003491 if( ssl->endpoint == SSL_IS_CLIENT )
3492 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003493#endif
3494#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003495 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003496 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003497#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003498 }
3499 else
3500 ssl->state++;
3501
3502 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3503
3504 return( 0 );
3505}
3506
Paul Bakker968afaa2014-07-09 11:09:24 +02003507static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003508{
3509 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3510
3511#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3512 defined(POLARSSL_SSL_PROTO_TLS1_1)
3513 md5_init( &handshake->fin_md5 );
3514 sha1_init( &handshake->fin_sha1 );
3515 md5_starts( &handshake->fin_md5 );
3516 sha1_starts( &handshake->fin_sha1 );
3517#endif
3518#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3519#if defined(POLARSSL_SHA256_C)
3520 sha256_init( &handshake->fin_sha256 );
3521 sha256_starts( &handshake->fin_sha256, 0 );
3522#endif
3523#if defined(POLARSSL_SHA512_C)
3524 sha512_init( &handshake->fin_sha512 );
3525 sha512_starts( &handshake->fin_sha512, 1 );
3526#endif
3527#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3528
3529 handshake->update_checksum = ssl_update_checksum_start;
3530 handshake->sig_alg = SSL_HASH_SHA1;
3531
3532#if defined(POLARSSL_DHM_C)
3533 dhm_init( &handshake->dhm_ctx );
3534#endif
3535#if defined(POLARSSL_ECDH_C)
3536 ecdh_init( &handshake->ecdh_ctx );
3537#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003538}
3539
3540static void ssl_transform_init( ssl_transform *transform )
3541{
3542 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003543
3544 cipher_init( &transform->cipher_ctx_enc );
3545 cipher_init( &transform->cipher_ctx_dec );
3546
3547 md_init( &transform->md_ctx_enc );
3548 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003549}
3550
3551void ssl_session_init( ssl_session *session )
3552{
3553 memset( session, 0, sizeof(ssl_session) );
3554}
3555
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003556static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003557{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003558 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003559 if( ssl->transform_negotiate )
3560 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003561 if( ssl->session_negotiate )
3562 ssl_session_free( ssl->session_negotiate );
3563 if( ssl->handshake )
3564 ssl_handshake_free( ssl->handshake );
3565
3566 /*
3567 * Either the pointers are now NULL or cleared properly and can be freed.
3568 * Now allocate missing structures.
3569 */
3570 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003571 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003572 ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003573 }
Paul Bakker48916f92012-09-16 19:57:18 +00003574
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003575 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003576 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003577 ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003578 }
Paul Bakker48916f92012-09-16 19:57:18 +00003579
Paul Bakker82788fb2014-10-20 13:59:19 +02003580 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003581 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003582 ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003583 }
Paul Bakker48916f92012-09-16 19:57:18 +00003584
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003585 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003586 if( ssl->handshake == NULL ||
3587 ssl->transform_negotiate == NULL ||
3588 ssl->session_negotiate == NULL )
3589 {
3590 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003591
3592 polarssl_free( ssl->handshake );
3593 polarssl_free( ssl->transform_negotiate );
3594 polarssl_free( ssl->session_negotiate );
3595
3596 ssl->handshake = NULL;
3597 ssl->transform_negotiate = NULL;
3598 ssl->session_negotiate = NULL;
3599
Paul Bakker48916f92012-09-16 19:57:18 +00003600 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3601 }
3602
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003603 /* Initialize structures */
3604 ssl_session_init( ssl->session_negotiate );
3605 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003606 ssl_handshake_params_init( ssl->handshake );
3607
3608#if defined(POLARSSL_X509_CRT_PARSE_C)
3609 ssl->handshake->key_cert = ssl->key_cert;
3610#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003611
Paul Bakker48916f92012-09-16 19:57:18 +00003612 return( 0 );
3613}
3614
Paul Bakker5121ce52009-01-03 21:22:43 +00003615/*
3616 * Initialize an SSL context
3617 */
3618int ssl_init( ssl_context *ssl )
3619{
Paul Bakker48916f92012-09-16 19:57:18 +00003620 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003621 int len = SSL_BUFFER_LEN;
3622
3623 memset( ssl, 0, sizeof( ssl_context ) );
3624
Paul Bakker62f2dee2012-09-28 07:31:51 +00003625 /*
3626 * Sane defaults
3627 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003628 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3629 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3630 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3631 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003632
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003633 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003634
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003635#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003636 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003637 memset( ssl->renego_period, 0xFF, 7 );
3638 ssl->renego_period[7] = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003639#endif
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003640
Paul Bakker62f2dee2012-09-28 07:31:51 +00003641#if defined(POLARSSL_DHM_C)
3642 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
Manuel Pégourié-Gonnardf0f399d2015-07-03 17:45:57 +02003643 POLARSSL_DHM_RFC5114_MODP_2048_P) ) != 0 ||
Paul Bakker62f2dee2012-09-28 07:31:51 +00003644 ( ret = mpi_read_string( &ssl->dhm_G, 16,
Manuel Pégourié-Gonnardf0f399d2015-07-03 17:45:57 +02003645 POLARSSL_DHM_RFC5114_MODP_2048_G) ) != 0 )
Paul Bakker62f2dee2012-09-28 07:31:51 +00003646 {
3647 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3648 return( ret );
3649 }
3650#endif
3651
3652 /*
3653 * Prepare base structures
3654 */
Manuel Pégourié-Gonnardf7db5e02015-02-18 10:32:41 +00003655 if( ( ssl->in_ctr = polarssl_malloc( len ) ) == NULL ||
3656 ( ssl->out_ctr = polarssl_malloc( len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003657 {
3658 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003659 polarssl_free( ssl->in_ctr );
3660 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003661 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003662 }
3663
3664 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3665 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3666
Manuel Pégourié-Gonnardf7db5e02015-02-18 10:32:41 +00003667 ssl->in_hdr = ssl->in_ctr + 8;
3668 ssl->in_iv = ssl->in_ctr + 13;
3669 ssl->in_msg = ssl->in_ctr + 13;
3670
3671 ssl->out_hdr = ssl->out_ctr + 8;
3672 ssl->out_iv = ssl->out_ctr + 13;
3673 ssl->out_msg = ssl->out_ctr + 13;
3674
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003675#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3676 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
3677#endif
3678
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003679#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3680 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3681#endif
3682
Paul Bakker606b4ba2013-08-14 16:52:14 +02003683#if defined(POLARSSL_SSL_SESSION_TICKETS)
3684 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3685#endif
3686
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003687#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003688 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003689#endif
3690
Paul Bakker48916f92012-09-16 19:57:18 +00003691 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3692 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003693
3694 return( 0 );
3695}
3696
3697/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003698 * Reset an initialized and used SSL context for re-use while retaining
3699 * all application-set variables, function pointers and data.
3700 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003701int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003702{
Paul Bakker48916f92012-09-16 19:57:18 +00003703 int ret;
3704
Paul Bakker7eb013f2011-10-06 12:37:39 +00003705 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003706
3707#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003708 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003709 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003710
3711 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard61860192014-11-04 13:05:42 +01003712 memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
3713 memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003714#endif
3715 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00003716
Paul Bakker7eb013f2011-10-06 12:37:39 +00003717 ssl->in_offt = NULL;
3718
Paul Bakker92be97b2013-01-02 17:30:03 +01003719 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003720 ssl->in_msgtype = 0;
3721 ssl->in_msglen = 0;
3722 ssl->in_left = 0;
3723
3724 ssl->in_hslen = 0;
3725 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003726 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003727
Paul Bakker92be97b2013-01-02 17:30:03 +01003728 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003729 ssl->out_msgtype = 0;
3730 ssl->out_msglen = 0;
3731 ssl->out_left = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003732#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01003733 if( ssl->split_done != SSL_CBC_RECORD_SPLITTING_DISABLED )
3734 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003735#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00003736
Paul Bakker48916f92012-09-16 19:57:18 +00003737 ssl->transform_in = NULL;
3738 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003739
3740 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3741 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003742
3743#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003744 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003745 {
3746 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003747 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003748 {
3749 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3750 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3751 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003752 }
3753#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003754
Paul Bakker48916f92012-09-16 19:57:18 +00003755 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003756 {
Paul Bakker48916f92012-09-16 19:57:18 +00003757 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003758 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003759 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003760 }
Paul Bakker48916f92012-09-16 19:57:18 +00003761
Paul Bakkerc0463502013-02-14 11:19:38 +01003762 if( ssl->session )
3763 {
3764 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003765 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003766 ssl->session = NULL;
3767 }
3768
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003769#if defined(POLARSSL_SSL_ALPN)
3770 ssl->alpn_chosen = NULL;
3771#endif
3772
Paul Bakker48916f92012-09-16 19:57:18 +00003773 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3774 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003775
3776 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003777}
3778
Paul Bakkera503a632013-08-14 13:48:06 +02003779#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003780static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3781{
3782 aes_free( &tkeys->enc );
3783 aes_free( &tkeys->dec );
3784
3785 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3786}
3787
Paul Bakker7eb013f2011-10-06 12:37:39 +00003788/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003789 * Allocate and initialize ticket keys
3790 */
3791static int ssl_ticket_keys_init( ssl_context *ssl )
3792{
3793 int ret;
3794 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003795 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003796
3797 if( ssl->ticket_keys != NULL )
3798 return( 0 );
3799
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003800 tkeys = polarssl_malloc( sizeof(ssl_ticket_keys) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003801 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003802 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3803
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003804 aes_init( &tkeys->enc );
3805 aes_init( &tkeys->dec );
3806
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003807 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003808 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003809 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003810 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003811 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003812 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003813
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003814 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3815 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3816 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3817 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003818 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003819 polarssl_free( tkeys );
3820 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003821 }
3822
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003823 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003824 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003825 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003826 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003827 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003828 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003829
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003830 ssl->ticket_keys = tkeys;
3831
3832 return( 0 );
3833}
Paul Bakkera503a632013-08-14 13:48:06 +02003834#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003835
3836/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003837 * SSL set accessors
3838 */
3839void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3840{
3841 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003842
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003843#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
3844 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003845 if( endpoint == SSL_IS_CLIENT )
3846 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003847#endif
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01003848
3849#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
3850 if( endpoint == SSL_IS_SERVER )
3851 ssl->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
3852#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003853}
3854
3855void ssl_set_authmode( ssl_context *ssl, int authmode )
3856{
3857 ssl->authmode = authmode;
3858}
3859
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003860#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003861void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003862 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003863 void *p_vrfy )
3864{
3865 ssl->f_vrfy = f_vrfy;
3866 ssl->p_vrfy = p_vrfy;
3867}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003868#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003869
Paul Bakker5121ce52009-01-03 21:22:43 +00003870void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003871 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003872 void *p_rng )
3873{
3874 ssl->f_rng = f_rng;
3875 ssl->p_rng = p_rng;
3876}
3877
3878void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003879 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003880 void *p_dbg )
3881{
3882 ssl->f_dbg = f_dbg;
3883 ssl->p_dbg = p_dbg;
3884}
3885
3886void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003887 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003888 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003889{
3890 ssl->f_recv = f_recv;
3891 ssl->f_send = f_send;
3892 ssl->p_recv = p_recv;
3893 ssl->p_send = p_send;
3894}
3895
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003896#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00003897void ssl_set_session_cache( ssl_context *ssl,
3898 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3899 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003900{
Paul Bakker0a597072012-09-25 21:55:46 +00003901 ssl->f_get_cache = f_get_cache;
3902 ssl->p_get_cache = p_get_cache;
3903 ssl->f_set_cache = f_set_cache;
3904 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003905}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003906#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003907
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003908#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003909int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003910{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003911 int ret;
3912
3913 if( ssl == NULL ||
3914 session == NULL ||
3915 ssl->session_negotiate == NULL ||
3916 ssl->endpoint != SSL_IS_CLIENT )
3917 {
3918 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3919 }
3920
3921 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3922 return( ret );
3923
Paul Bakker0a597072012-09-25 21:55:46 +00003924 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003925
3926 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003927}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003928#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003929
Paul Bakkerb68cad62012-08-23 08:34:18 +00003930void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003931{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003932 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3933 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3934 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3935 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3936}
3937
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003938void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3939 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003940 int major, int minor )
3941{
3942 if( major != SSL_MAJOR_VERSION_3 )
3943 return;
3944
3945 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3946 return;
3947
3948 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003949}
3950
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003951#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003952/* Add a new (empty) key_cert entry an return a pointer to it */
3953static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3954{
3955 ssl_key_cert *key_cert, *last;
3956
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003957 key_cert = polarssl_malloc( sizeof(ssl_key_cert) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003958 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003959 return( NULL );
3960
3961 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3962
3963 /* Append the new key_cert to the (possibly empty) current list */
3964 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003965 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003966 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003967 if( ssl->handshake != NULL )
3968 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003969 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003970 else
3971 {
3972 last = ssl->key_cert;
3973 while( last->next != NULL )
3974 last = last->next;
3975 last->next = key_cert;
3976 }
3977
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003978 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003979}
3980
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003981void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003982 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003983{
3984 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003985 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003986 ssl->peer_cn = peer_cn;
3987}
3988
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003989int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003990 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003991{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003992 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3993
3994 if( key_cert == NULL )
3995 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3996
3997 key_cert->cert = own_cert;
3998 key_cert->key = pk_key;
3999
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004000 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004001}
4002
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +01004003#if ! defined(POLARSSL_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004004#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004005int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004006 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00004007{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004008 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004009 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004010
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004011 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004012 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4013
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004014 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004015 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004016 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004017
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004018 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004019
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004020 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004021 if( ret != 0 )
4022 return( ret );
4023
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004024 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004025 return( ret );
4026
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004027 key_cert->cert = own_cert;
4028 key_cert->key_own_alloc = 1;
4029
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004030 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004031}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004032#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004033
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004034int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02004035 void *rsa_key,
4036 rsa_decrypt_func rsa_decrypt,
4037 rsa_sign_func rsa_sign,
4038 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00004039{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004040 int ret;
4041 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004042
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004043 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004044 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4045
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004046 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004047 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004048 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004049
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004050 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004051
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004052 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
4053 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
4054 return( ret );
4055
4056 key_cert->cert = own_cert;
4057 key_cert->key_own_alloc = 1;
4058
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004059 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00004060}
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +01004061#endif /* POLARSSL_DEPRECATED_REMOVED */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004062#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004063
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004064#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004065int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
4066 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004067{
Paul Bakker6db455e2013-09-18 17:29:31 +02004068 if( psk == NULL || psk_identity == NULL )
4069 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4070
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02004071 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01004072 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4073
Manuel Pégourié-Gonnard65125542015-08-27 16:37:35 +02004074 /* Identity len will be encoded on two bytes */
4075 if( ( psk_identity_len >> 16 ) != 0 ||
4076 psk_identity_len > SSL_MAX_CONTENT_LEN )
4077 {
4078 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4079 }
4080
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004081 if( ssl->psk != NULL || ssl->psk_identity != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02004082 {
4083 polarssl_free( ssl->psk );
4084 polarssl_free( ssl->psk_identity );
4085 }
4086
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004087 if( ( ssl->psk = polarssl_malloc( psk_len ) ) == NULL ||
4088 ( ssl->psk_identity = polarssl_malloc( psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05004089 {
4090 polarssl_free( ssl->psk );
Manuel Pégourié-Gonnard5aff0292015-09-28 18:09:45 +02004091 polarssl_free( ssl->psk_identity );
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004092 ssl->psk = NULL;
Manuel Pégourié-Gonnard5aff0292015-09-28 18:09:45 +02004093 ssl->psk_identity = NULL;
Mansour Moufidf81088b2015-02-17 13:10:21 -05004094 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4095 }
Paul Bakker6db455e2013-09-18 17:29:31 +02004096
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004097 ssl->psk_len = psk_len;
4098 ssl->psk_identity_len = psk_identity_len;
4099
Paul Bakker6db455e2013-09-18 17:29:31 +02004100 memcpy( ssl->psk, psk, ssl->psk_len );
4101 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004102
4103 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004104}
4105
4106void ssl_set_psk_cb( ssl_context *ssl,
4107 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4108 size_t),
4109 void *p_psk )
4110{
4111 ssl->f_psk = f_psk;
4112 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004113}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004114#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004115
Paul Bakker48916f92012-09-16 19:57:18 +00004116#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004117int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004118{
4119 int ret;
4120
Paul Bakker48916f92012-09-16 19:57:18 +00004121 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004122 {
4123 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4124 return( ret );
4125 }
4126
Paul Bakker48916f92012-09-16 19:57:18 +00004127 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004128 {
4129 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4130 return( ret );
4131 }
4132
4133 return( 0 );
4134}
4135
Paul Bakker1b57b062011-01-06 15:48:19 +00004136int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4137{
4138 int ret;
4139
Paul Bakker66d5d072014-06-17 16:39:18 +02004140 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004141 {
4142 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4143 return( ret );
4144 }
4145
Paul Bakker66d5d072014-06-17 16:39:18 +02004146 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004147 {
4148 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4149 return( ret );
4150 }
4151
4152 return( 0 );
4153}
Paul Bakker48916f92012-09-16 19:57:18 +00004154#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004155
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004156#if defined(POLARSSL_SSL_SET_CURVES)
4157/*
4158 * Set the allowed elliptic curves
4159 */
4160void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4161{
4162 ssl->curve_list = curve_list;
4163}
4164#endif
4165
Paul Bakker0be444a2013-08-27 21:55:01 +02004166#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004167int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004168{
4169 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00004170 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004171
4172 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004173
4174 if( ssl->hostname_len + 1 == 0 )
4175 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4176
Simon Butcherc988f322015-09-29 23:27:20 +01004177 if( ssl->hostname_len > SSL_MAX_HOST_NAME_LEN )
4178 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4179
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004180 ssl->hostname = polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004181
Paul Bakkerb15b8512012-01-13 13:44:06 +00004182 if( ssl->hostname == NULL )
4183 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4184
Paul Bakker3c2122f2013-06-24 19:03:14 +02004185 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004186 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004187
Paul Bakker40ea7de2009-05-03 10:18:48 +00004188 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004189
4190 return( 0 );
4191}
4192
Paul Bakker5701cdc2012-09-27 21:49:42 +00004193void ssl_set_sni( ssl_context *ssl,
4194 int (*f_sni)(void *, ssl_context *,
4195 const unsigned char *, size_t),
4196 void *p_sni )
4197{
4198 ssl->f_sni = f_sni;
4199 ssl->p_sni = p_sni;
4200}
Paul Bakker0be444a2013-08-27 21:55:01 +02004201#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004202
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004203#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004204int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004205{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004206 size_t cur_len, tot_len;
4207 const char **p;
4208
4209 /*
4210 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4211 * truncated". Check lengths now rather than later.
4212 */
4213 tot_len = 0;
4214 for( p = protos; *p != NULL; p++ )
4215 {
4216 cur_len = strlen( *p );
4217 tot_len += cur_len;
4218
4219 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4220 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4221 }
4222
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004223 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004224
4225 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004226}
4227
4228const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4229{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004230 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004231}
4232#endif /* POLARSSL_SSL_ALPN */
4233
Paul Bakker490ecc82011-10-06 13:04:09 +00004234void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4235{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004236 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4237 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4238 {
4239 ssl->max_major_ver = major;
4240 ssl->max_minor_ver = minor;
4241 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004242}
4243
Paul Bakker1d29fb52012-09-28 13:28:45 +00004244void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4245{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004246 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4247 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4248 {
4249 ssl->min_major_ver = major;
4250 ssl->min_minor_ver = minor;
4251 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004252}
4253
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004254#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4255void ssl_set_fallback( ssl_context *ssl, char fallback )
4256{
4257 ssl->fallback = fallback;
4258}
4259#endif
4260
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004261#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4262void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
4263{
4264 ssl->encrypt_then_mac = etm;
4265}
4266#endif
4267
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004268#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4269void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4270{
4271 ssl->extended_ms = ems;
4272}
4273#endif
4274
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004275void ssl_set_arc4_support( ssl_context *ssl, char arc4 )
4276{
4277 ssl->arc4_disabled = arc4;
4278}
4279
Paul Bakker05decb22013-08-15 13:33:48 +02004280#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004281int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4282{
Paul Bakker77e257e2013-12-16 15:29:52 +01004283 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004284 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004285 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004286 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004287 }
4288
4289 ssl->mfl_code = mfl_code;
4290
4291 return( 0 );
4292}
Paul Bakker05decb22013-08-15 13:33:48 +02004293#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004294
Paul Bakker1f2bc622013-08-15 13:45:55 +02004295#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004296int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004297{
Paul Bakker8c1ede62013-07-19 14:14:37 +02004298 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004299
4300 return( 0 );
4301}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004302#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004303
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004304#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4305void ssl_set_cbc_record_splitting( ssl_context *ssl, char split )
4306{
4307 ssl->split_done = split;
4308}
4309#endif
4310
Paul Bakker48916f92012-09-16 19:57:18 +00004311void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4312{
4313 ssl->allow_legacy_renegotiation = allow_legacy;
4314}
4315
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004316#if defined(POLARSSL_SSL_RENEGOTIATION)
4317void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4318{
4319 ssl->disable_renegotiation = renegotiation;
4320}
4321
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004322void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4323{
4324 ssl->renego_max_records = max_records;
4325}
4326
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004327void ssl_set_renegotiation_period( ssl_context *ssl,
4328 const unsigned char period[8] )
4329{
4330 memcpy( ssl->renego_period, period, 8 );
4331}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004332#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004333
Paul Bakkera503a632013-08-14 13:48:06 +02004334#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004335int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4336{
4337 ssl->session_tickets = use_tickets;
4338
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004339#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004340 if( ssl->endpoint == SSL_IS_CLIENT )
4341 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004342#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004343
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01004344 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
4345 return( 0 );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004346
4347 if( ssl->f_rng == NULL )
4348 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4349
4350 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004351}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004352
4353void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4354{
4355 ssl->ticket_lifetime = lifetime;
4356}
Paul Bakkera503a632013-08-14 13:48:06 +02004357#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004358
Paul Bakker5121ce52009-01-03 21:22:43 +00004359/*
4360 * SSL get accessors
4361 */
4362size_t ssl_get_bytes_avail( const ssl_context *ssl )
4363{
4364 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4365}
4366
Paul Bakkerff60ee62010-03-16 21:09:09 +00004367int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004368{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00004369 if( ssl->session != NULL )
4370 return( ssl->session->verify_result );
4371
4372 if( ssl->session_negotiate != NULL )
4373 return( ssl->session_negotiate->verify_result );
4374
4375 return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004376}
4377
Paul Bakkere3166ce2011-01-27 17:40:50 +00004378const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004379{
Paul Bakker926c8e42013-03-06 10:23:34 +01004380 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004381 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004382
Paul Bakkere3166ce2011-01-27 17:40:50 +00004383 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004384}
4385
Paul Bakker43ca69c2011-01-15 17:35:19 +00004386const char *ssl_get_version( const ssl_context *ssl )
4387{
4388 switch( ssl->minor_ver )
4389 {
4390 case SSL_MINOR_VERSION_0:
4391 return( "SSLv3.0" );
4392
4393 case SSL_MINOR_VERSION_1:
4394 return( "TLSv1.0" );
4395
4396 case SSL_MINOR_VERSION_2:
4397 return( "TLSv1.1" );
4398
Paul Bakker1ef83d62012-04-11 12:09:53 +00004399 case SSL_MINOR_VERSION_3:
4400 return( "TLSv1.2" );
4401
Paul Bakker43ca69c2011-01-15 17:35:19 +00004402 default:
4403 break;
4404 }
4405 return( "unknown" );
4406}
4407
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004408#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004409const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004410{
4411 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004412 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004413
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004414 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004415}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004416#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004417
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004418#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004419int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4420{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004421 if( ssl == NULL ||
4422 dst == NULL ||
4423 ssl->session == NULL ||
4424 ssl->endpoint != SSL_IS_CLIENT )
4425 {
4426 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4427 }
4428
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004429 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004430}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004431#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004432
Paul Bakker5121ce52009-01-03 21:22:43 +00004433/*
Paul Bakker1961b702013-01-25 14:49:24 +01004434 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004435 */
Paul Bakker1961b702013-01-25 14:49:24 +01004436int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004437{
Paul Bakker40e46942009-01-03 21:51:57 +00004438 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004439
Paul Bakker40e46942009-01-03 21:51:57 +00004440#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004441 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004442 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004443#endif
Paul Bakker40e46942009-01-03 21:51:57 +00004444#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004445 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004446 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004447#endif
4448
Paul Bakker1961b702013-01-25 14:49:24 +01004449 return( ret );
4450}
4451
4452/*
4453 * Perform the SSL handshake
4454 */
4455int ssl_handshake( ssl_context *ssl )
4456{
4457 int ret = 0;
4458
4459 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4460
4461 while( ssl->state != SSL_HANDSHAKE_OVER )
4462 {
4463 ret = ssl_handshake_step( ssl );
4464
4465 if( ret != 0 )
4466 break;
4467 }
4468
Paul Bakker5121ce52009-01-03 21:22:43 +00004469 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4470
4471 return( ret );
4472}
4473
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004474#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004475#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004476/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004477 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004478 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004479static int ssl_write_hello_request( ssl_context *ssl )
4480{
4481 int ret;
4482
4483 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4484
4485 ssl->out_msglen = 4;
4486 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4487 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4488
4489 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4490 {
4491 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4492 return( ret );
4493 }
4494
4495 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4496
4497 return( 0 );
4498}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004499#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004500
4501/*
4502 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004503 * - any side: calling ssl_renegotiate(),
4504 * - client: receiving a HelloRequest during ssl_read(),
4505 * - server: receiving any handshake message on server during ssl_read() after
4506 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004507 * If the handshake doesn't complete due to waiting for I/O, it will continue
4508 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004509 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004510static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004511{
4512 int ret;
4513
4514 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4515
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004516 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4517 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004518
4519 ssl->state = SSL_HELLO_REQUEST;
4520 ssl->renegotiation = SSL_RENEGOTIATION;
4521
Paul Bakker48916f92012-09-16 19:57:18 +00004522 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4523 {
4524 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4525 return( ret );
4526 }
4527
4528 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4529
4530 return( 0 );
4531}
4532
4533/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004534 * Renegotiate current connection on client,
4535 * or request renegotiation on server
4536 */
4537int ssl_renegotiate( ssl_context *ssl )
4538{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004539 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004540
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004541#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004542 /* On server, just send the request */
4543 if( ssl->endpoint == SSL_IS_SERVER )
4544 {
4545 if( ssl->state != SSL_HANDSHAKE_OVER )
4546 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4547
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004548 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4549
4550 /* Did we already try/start sending HelloRequest? */
4551 if( ssl->out_left != 0 )
4552 return( ssl_flush_output( ssl ) );
4553
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004554 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004555 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004556#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004557
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004558#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004559 /*
4560 * On client, either start the renegotiation process or,
4561 * if already in progress, continue the handshake
4562 */
4563 if( ssl->renegotiation != SSL_RENEGOTIATION )
4564 {
4565 if( ssl->state != SSL_HANDSHAKE_OVER )
4566 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4567
4568 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4569 {
4570 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4571 return( ret );
4572 }
4573 }
4574 else
4575 {
4576 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4577 {
4578 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4579 return( ret );
4580 }
4581 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004582#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004583
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004584 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004585}
4586
4587/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004588 * Check record counters and renegotiate if they're above the limit.
4589 */
4590static int ssl_check_ctr_renegotiate( ssl_context *ssl )
4591{
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004592 if( ssl->state != SSL_HANDSHAKE_OVER ||
4593 ssl->renegotiation == SSL_RENEGOTIATION_PENDING ||
4594 ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED )
4595 {
4596 return( 0 );
4597 }
4598
4599 // TODO: adapt for DTLS
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004600 if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 &&
4601 memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004602 {
4603 return( 0 );
4604 }
4605
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004606 SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004607 return( ssl_renegotiate( ssl ) );
4608}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004609#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004610
4611/*
4612 * Receive application data decrypted from the SSL layer
4613 */
Paul Bakker23986e52011-04-24 08:57:21 +00004614int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004615{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004616 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004617 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004618
4619 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4620
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004621#if defined(POLARSSL_SSL_RENEGOTIATION)
4622 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4623 {
4624 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4625 return( ret );
4626 }
4627#endif
4628
Paul Bakker5121ce52009-01-03 21:22:43 +00004629 if( ssl->state != SSL_HANDSHAKE_OVER )
4630 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004631 ret = ssl_handshake( ssl );
4632 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4633 {
4634 record_read = 1;
4635 }
4636 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004637 {
4638 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4639 return( ret );
4640 }
4641 }
4642
4643 if( ssl->in_offt == NULL )
4644 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004645 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004646 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004647 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4648 {
4649 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4650 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004651
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004652 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4653 return( ret );
4654 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004655 }
4656
4657 if( ssl->in_msglen == 0 &&
4658 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4659 {
4660 /*
4661 * OpenSSL sends empty messages to randomize the IV
4662 */
4663 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4664 {
Paul Bakker831a7552011-05-18 13:32:51 +00004665 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4666 return( 0 );
4667
Paul Bakker5121ce52009-01-03 21:22:43 +00004668 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4669 return( ret );
4670 }
4671 }
4672
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004673#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004674 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4675 {
4676 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4677
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004678#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004679 if( ssl->endpoint == SSL_IS_CLIENT &&
4680 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4681 ssl->in_hslen != 4 ) )
4682 {
4683 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4684 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4685 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004686#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004687
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004688 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4689 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004690 ssl->allow_legacy_renegotiation ==
4691 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004692 {
4693 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4694
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004695#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004696 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004697 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004698 /*
4699 * SSLv3 does not have a "no_renegotiation" alert
4700 */
4701 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4702 return( ret );
4703 }
4704 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004705#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004706#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4707 defined(POLARSSL_SSL_PROTO_TLS1_2)
4708 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004709 {
4710 if( ( ret = ssl_send_alert_message( ssl,
4711 SSL_ALERT_LEVEL_WARNING,
4712 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4713 {
4714 return( ret );
4715 }
Paul Bakker48916f92012-09-16 19:57:18 +00004716 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004717 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004718#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4719 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004720 {
4721 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004722 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004723 }
Paul Bakker48916f92012-09-16 19:57:18 +00004724 }
4725 else
4726 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004727 ret = ssl_start_renegotiation( ssl );
4728 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4729 {
4730 record_read = 1;
4731 }
4732 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004733 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004734 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004735 return( ret );
4736 }
Paul Bakker48916f92012-09-16 19:57:18 +00004737 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004738
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004739 /* If a non-handshake record was read during renego, fallthrough,
4740 * else tell the user they should call ssl_read() again */
4741 if( ! record_read )
4742 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004743 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004744 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4745 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004746 ssl->renego_records_seen++;
4747
4748 if( ssl->renego_max_records >= 0 &&
4749 ssl->renego_records_seen > ssl->renego_max_records )
4750 {
4751 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4752 "but not honored by client" ) );
4753 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4754 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004755 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004756#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004757
4758 /* Fatal and closure alerts handled by ssl_read_record() */
4759 if( ssl->in_msgtype == SSL_MSG_ALERT )
4760 {
4761 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4762 return( POLARSSL_ERR_NET_WANT_READ );
4763 }
4764
4765 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004766 {
4767 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004768 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004769 }
4770
4771 ssl->in_offt = ssl->in_msg;
4772 }
4773
4774 n = ( len < ssl->in_msglen )
4775 ? len : ssl->in_msglen;
4776
4777 memcpy( buf, ssl->in_offt, n );
4778 ssl->in_msglen -= n;
4779
4780 if( ssl->in_msglen == 0 )
4781 /* all bytes consumed */
4782 ssl->in_offt = NULL;
4783 else
4784 /* more data available */
4785 ssl->in_offt += n;
4786
4787 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4788
Paul Bakker23986e52011-04-24 08:57:21 +00004789 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004790}
4791
4792/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004793 * Send application data to be encrypted by the SSL layer,
4794 * taking care of max fragment length and buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +00004795 */
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004796static int ssl_write_real( ssl_context *ssl,
4797 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004798{
Paul Bakker23986e52011-04-24 08:57:21 +00004799 int ret;
4800 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004801 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004802
Paul Bakker05decb22013-08-15 13:33:48 +02004803#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004804 /*
4805 * Assume mfl_code is correct since it was checked when set
4806 */
4807 max_len = mfl_code_to_length[ssl->mfl_code];
4808
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004809 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004810 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004811 */
4812 if( ssl->session_out != NULL &&
4813 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4814 {
4815 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4816 }
Paul Bakker05decb22013-08-15 13:33:48 +02004817#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004818
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004819 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004820
Paul Bakker5121ce52009-01-03 21:22:43 +00004821 if( ssl->out_left != 0 )
4822 {
4823 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4824 {
4825 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4826 return( ret );
4827 }
4828 }
Paul Bakker887bd502011-06-08 13:10:54 +00004829 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004830 {
Paul Bakker887bd502011-06-08 13:10:54 +00004831 ssl->out_msglen = n;
4832 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4833 memcpy( ssl->out_msg, buf, n );
4834
4835 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4836 {
4837 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4838 return( ret );
4839 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004840 }
4841
Paul Bakker23986e52011-04-24 08:57:21 +00004842 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004843}
4844
4845/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004846 * Write application data, doing 1/n-1 splitting if necessary.
4847 *
4848 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004849 * then the caller will call us again with the same arguments, so
4850 * remember wether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004851 */
4852#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004853static int ssl_write_split( ssl_context *ssl,
4854 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004855{
4856 int ret;
4857
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004858 if( ssl->split_done == SSL_CBC_RECORD_SPLITTING_DISABLED ||
4859 len <= 1 ||
4860 ssl->minor_ver > SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004861 cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
4862 != POLARSSL_MODE_CBC )
4863 {
4864 return( ssl_write_real( ssl, buf, len ) );
4865 }
4866
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004867 if( ssl->split_done == 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004868 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004869 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004870 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004871 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004872 }
4873
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004874 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
4875 return( ret );
4876 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004877
4878 return( ret + 1 );
4879}
4880#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */
4881
4882/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004883 * Write application data (public-facing wrapper)
4884 */
4885int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
4886{
4887 int ret;
4888
4889 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4890
4891#if defined(POLARSSL_SSL_RENEGOTIATION)
4892 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4893 {
4894 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4895 return( ret );
4896 }
4897#endif
4898
4899 if( ssl->state != SSL_HANDSHAKE_OVER )
4900 {
4901 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4902 {
4903 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4904 return( ret );
4905 }
4906 }
4907
4908#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4909 ret = ssl_write_split( ssl, buf, len );
4910#else
4911 ret = ssl_write_real( ssl, buf, len );
4912#endif
4913
4914 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4915
4916 return( ret );
4917}
4918
4919/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004920 * Notify the peer that the connection is being closed
4921 */
4922int ssl_close_notify( ssl_context *ssl )
4923{
4924 int ret;
4925
4926 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4927
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004928 if( ssl->out_left != 0 )
4929 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004930
4931 if( ssl->state == SSL_HANDSHAKE_OVER )
4932 {
Paul Bakker48916f92012-09-16 19:57:18 +00004933 if( ( ret = ssl_send_alert_message( ssl,
4934 SSL_ALERT_LEVEL_WARNING,
4935 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004936 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004937 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004938 return( ret );
4939 }
4940 }
4941
4942 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4943
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004944 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004945}
4946
Paul Bakker48916f92012-09-16 19:57:18 +00004947void ssl_transform_free( ssl_transform *transform )
4948{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004949 if( transform == NULL )
4950 return;
4951
Paul Bakker48916f92012-09-16 19:57:18 +00004952#if defined(POLARSSL_ZLIB_SUPPORT)
4953 deflateEnd( &transform->ctx_deflate );
4954 inflateEnd( &transform->ctx_inflate );
4955#endif
4956
Paul Bakker84bbeb52014-07-01 14:53:22 +02004957 cipher_free( &transform->cipher_ctx_enc );
4958 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004959
Paul Bakker84bbeb52014-07-01 14:53:22 +02004960 md_free( &transform->md_ctx_enc );
4961 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004962
Paul Bakker34617722014-06-13 17:20:13 +02004963 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004964}
4965
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004966#if defined(POLARSSL_X509_CRT_PARSE_C)
4967static void ssl_key_cert_free( ssl_key_cert *key_cert )
4968{
4969 ssl_key_cert *cur = key_cert, *next;
4970
4971 while( cur != NULL )
4972 {
4973 next = cur->next;
4974
4975 if( cur->key_own_alloc )
4976 {
4977 pk_free( cur->key );
4978 polarssl_free( cur->key );
4979 }
4980 polarssl_free( cur );
4981
4982 cur = next;
4983 }
4984}
4985#endif /* POLARSSL_X509_CRT_PARSE_C */
4986
Paul Bakker48916f92012-09-16 19:57:18 +00004987void ssl_handshake_free( ssl_handshake_params *handshake )
4988{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004989 if( handshake == NULL )
4990 return;
4991
Paul Bakker48916f92012-09-16 19:57:18 +00004992#if defined(POLARSSL_DHM_C)
4993 dhm_free( &handshake->dhm_ctx );
4994#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004995#if defined(POLARSSL_ECDH_C)
4996 ecdh_free( &handshake->ecdh_ctx );
4997#endif
4998
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004999#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02005000 /* explicit void pointer cast for buggy MS compiler */
5001 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005002#endif
5003
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02005004#if defined(POLARSSL_X509_CRT_PARSE_C) && \
5005 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
5006 /*
5007 * Free only the linked list wrapper, not the keys themselves
5008 * since the belong to the SNI callback
5009 */
5010 if( handshake->sni_key_cert != NULL )
5011 {
5012 ssl_key_cert *cur = handshake->sni_key_cert, *next;
5013
5014 while( cur != NULL )
5015 {
5016 next = cur->next;
5017 polarssl_free( cur );
5018 cur = next;
5019 }
5020 }
Paul Bakker9af723c2014-05-01 13:03:14 +02005021#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005022
Paul Bakker34617722014-06-13 17:20:13 +02005023 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005024}
5025
5026void ssl_session_free( ssl_session *session )
5027{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005028 if( session == NULL )
5029 return;
5030
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005031#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00005032 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00005033 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005034 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02005035 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00005036 }
Paul Bakkered27a042013-04-18 22:46:23 +02005037#endif
Paul Bakker0a597072012-09-25 21:55:46 +00005038
Paul Bakkera503a632013-08-14 13:48:06 +02005039#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005040 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02005041#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005042
Paul Bakker34617722014-06-13 17:20:13 +02005043 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005044}
5045
Paul Bakker5121ce52009-01-03 21:22:43 +00005046/*
5047 * Free an SSL context
5048 */
5049void ssl_free( ssl_context *ssl )
5050{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005051 if( ssl == NULL )
5052 return;
5053
Paul Bakker5121ce52009-01-03 21:22:43 +00005054 SSL_DEBUG_MSG( 2, ( "=> free" ) );
5055
Paul Bakker5121ce52009-01-03 21:22:43 +00005056 if( ssl->out_ctr != NULL )
5057 {
Paul Bakker34617722014-06-13 17:20:13 +02005058 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005059 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005060 }
5061
5062 if( ssl->in_ctr != NULL )
5063 {
Paul Bakker34617722014-06-13 17:20:13 +02005064 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005065 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005066 }
5067
Paul Bakker16770332013-10-11 09:59:44 +02005068#if defined(POLARSSL_ZLIB_SUPPORT)
5069 if( ssl->compress_buf != NULL )
5070 {
Paul Bakker34617722014-06-13 17:20:13 +02005071 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02005072 polarssl_free( ssl->compress_buf );
5073 }
5074#endif
5075
Paul Bakker40e46942009-01-03 21:51:57 +00005076#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00005077 mpi_free( &ssl->dhm_P );
5078 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00005079#endif
5080
Paul Bakker48916f92012-09-16 19:57:18 +00005081 if( ssl->transform )
5082 {
5083 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005084 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005085 }
5086
5087 if( ssl->handshake )
5088 {
5089 ssl_handshake_free( ssl->handshake );
5090 ssl_transform_free( ssl->transform_negotiate );
5091 ssl_session_free( ssl->session_negotiate );
5092
Paul Bakker6e339b52013-07-03 13:37:05 +02005093 polarssl_free( ssl->handshake );
5094 polarssl_free( ssl->transform_negotiate );
5095 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00005096 }
5097
Paul Bakkerc0463502013-02-14 11:19:38 +01005098 if( ssl->session )
5099 {
5100 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005101 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005102 }
5103
Paul Bakkera503a632013-08-14 13:48:06 +02005104#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005105 if( ssl->ticket_keys )
5106 {
5107 ssl_ticket_keys_free( ssl->ticket_keys );
5108 polarssl_free( ssl->ticket_keys );
5109 }
Paul Bakkera503a632013-08-14 13:48:06 +02005110#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005111
Paul Bakker0be444a2013-08-27 21:55:01 +02005112#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02005113 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005114 {
Paul Bakker34617722014-06-13 17:20:13 +02005115 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02005116 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00005117 ssl->hostname_len = 0;
5118 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005119#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005120
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005121#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005122 if( ssl->psk != NULL )
5123 {
Paul Bakker34617722014-06-13 17:20:13 +02005124 polarssl_zeroize( ssl->psk, ssl->psk_len );
5125 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005126 polarssl_free( ssl->psk );
5127 polarssl_free( ssl->psk_identity );
5128 ssl->psk_len = 0;
5129 ssl->psk_identity_len = 0;
5130 }
5131#endif
5132
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005133#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005134 ssl_key_cert_free( ssl->key_cert );
5135#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005136
Paul Bakker05ef8352012-05-08 09:17:57 +00005137#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
5138 if( ssl_hw_record_finish != NULL )
5139 {
5140 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
5141 ssl_hw_record_finish( ssl );
5142 }
5143#endif
5144
Paul Bakker5121ce52009-01-03 21:22:43 +00005145 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00005146
Paul Bakker86f04f42013-02-14 11:20:09 +01005147 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02005148 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005149}
5150
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005151#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005152/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005153 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005154 */
5155unsigned char ssl_sig_from_pk( pk_context *pk )
5156{
5157#if defined(POLARSSL_RSA_C)
5158 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
5159 return( SSL_SIG_RSA );
5160#endif
5161#if defined(POLARSSL_ECDSA_C)
5162 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
5163 return( SSL_SIG_ECDSA );
5164#endif
5165 return( SSL_SIG_ANON );
5166}
5167
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005168pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
5169{
5170 switch( sig )
5171 {
5172#if defined(POLARSSL_RSA_C)
5173 case SSL_SIG_RSA:
5174 return( POLARSSL_PK_RSA );
5175#endif
5176#if defined(POLARSSL_ECDSA_C)
5177 case SSL_SIG_ECDSA:
5178 return( POLARSSL_PK_ECDSA );
5179#endif
5180 default:
5181 return( POLARSSL_PK_NONE );
5182 }
5183}
Paul Bakker9af723c2014-05-01 13:03:14 +02005184#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005185
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005186/*
5187 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
5188 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005189md_type_t ssl_md_alg_from_hash( unsigned char hash )
5190{
5191 switch( hash )
5192 {
5193#if defined(POLARSSL_MD5_C)
5194 case SSL_HASH_MD5:
5195 return( POLARSSL_MD_MD5 );
5196#endif
5197#if defined(POLARSSL_SHA1_C)
5198 case SSL_HASH_SHA1:
5199 return( POLARSSL_MD_SHA1 );
5200#endif
5201#if defined(POLARSSL_SHA256_C)
5202 case SSL_HASH_SHA224:
5203 return( POLARSSL_MD_SHA224 );
5204 case SSL_HASH_SHA256:
5205 return( POLARSSL_MD_SHA256 );
5206#endif
5207#if defined(POLARSSL_SHA512_C)
5208 case SSL_HASH_SHA384:
5209 return( POLARSSL_MD_SHA384 );
5210 case SSL_HASH_SHA512:
5211 return( POLARSSL_MD_SHA512 );
5212#endif
5213 default:
5214 return( POLARSSL_MD_NONE );
5215 }
5216}
5217
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005218#if defined(POLARSSL_SSL_SET_CURVES)
5219/*
5220 * Check is a curve proposed by the peer is in our list.
5221 * Return 1 if we're willing to use it, 0 otherwise.
5222 */
5223int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
5224{
5225 const ecp_group_id *gid;
5226
5227 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5228 if( *gid == grp_id )
5229 return( 1 );
5230
5231 return( 0 );
5232}
Paul Bakker9af723c2014-05-01 13:03:14 +02005233#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005234
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005235#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005236int ssl_check_cert_usage( const x509_crt *cert,
5237 const ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005238 int cert_endpoint,
5239 int *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005240{
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005241 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005242#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5243 int usage = 0;
5244#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005245#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5246 const char *ext_oid;
5247 size_t ext_len;
5248#endif
5249
5250#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5251 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5252 ((void) cert);
5253 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005254 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005255#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005256
5257#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5258 if( cert_endpoint == SSL_IS_SERVER )
5259 {
5260 /* Server part of the key exchange */
5261 switch( ciphersuite->key_exchange )
5262 {
5263 case POLARSSL_KEY_EXCHANGE_RSA:
5264 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5265 usage = KU_KEY_ENCIPHERMENT;
5266 break;
5267
5268 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5269 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5270 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5271 usage = KU_DIGITAL_SIGNATURE;
5272 break;
5273
5274 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5275 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5276 usage = KU_KEY_AGREEMENT;
5277 break;
5278
5279 /* Don't use default: we want warnings when adding new values */
5280 case POLARSSL_KEY_EXCHANGE_NONE:
5281 case POLARSSL_KEY_EXCHANGE_PSK:
5282 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5283 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5284 usage = 0;
5285 }
5286 }
5287 else
5288 {
5289 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5290 usage = KU_DIGITAL_SIGNATURE;
5291 }
5292
5293 if( x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005294 {
5295 *flags |= BADCERT_KEY_USAGE;
5296 ret = -1;
5297 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005298#else
5299 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005300#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5301
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005302#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5303 if( cert_endpoint == SSL_IS_SERVER )
5304 {
5305 ext_oid = OID_SERVER_AUTH;
5306 ext_len = OID_SIZE( OID_SERVER_AUTH );
5307 }
5308 else
5309 {
5310 ext_oid = OID_CLIENT_AUTH;
5311 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5312 }
5313
5314 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005315 {
5316 *flags |= BADCERT_EXT_KEY_USAGE;
5317 ret = -1;
5318 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005319#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5320
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005321 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005322}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005323#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005324
5325#endif /* POLARSSL_SSL_TLS_C */