blob: 82d03805997aabed1b65deed67af2c223e2d2af4 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * The SSL 3.0 specification was drafted by Netscape in 1996,
24 * and became an IETF standard in 1999.
25 *
26 * http://wp.netscape.com/eng/ssl3/
27 * http://www.ietf.org/rfc/rfc2246.txt
28 * http://www.ietf.org/rfc/rfc4346.txt
29 */
30
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#else
34#include POLARSSL_CONFIG_FILE
35#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker40e46942009-01-03 21:51:57 +000037#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker0be444a2013-08-27 21:55:01 +020039#include "polarssl/debug.h"
40#include "polarssl/ssl.h"
41
Rich Evans00ab4702015-02-06 13:43:58 +000042#include <string.h>
43
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020044#if defined(POLARSSL_X509_CRT_PARSE_C) && \
45 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
46#include "polarssl/oid.h"
47#endif
48
Paul Bakker7dc4c442014-02-01 22:50:26 +010049#if defined(POLARSSL_PLATFORM_C)
50#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020051#else
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <stdlib.h>
Paul Bakker6e339b52013-07-03 13:37:05 +020053#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker6edcd412013-10-29 15:22:54 +010057#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
58 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000059#define strcasecmp _stricmp
60#endif
61
Paul Bakker34617722014-06-13 17:20:13 +020062/* Implementation that should never be optimized out by the compiler */
63static void polarssl_zeroize( void *v, size_t n ) {
64 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
65}
66
Paul Bakker05decb22013-08-15 13:33:48 +020067#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020068/*
69 * Convert max_fragment_length codes to length.
70 * RFC 6066 says:
71 * enum{
72 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
73 * } MaxFragmentLength;
74 * and we add 0 -> extension unused
75 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020076static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020077{
78 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
79 512, /* SSL_MAX_FRAG_LEN_512 */
80 1024, /* SSL_MAX_FRAG_LEN_1024 */
81 2048, /* SSL_MAX_FRAG_LEN_2048 */
82 4096, /* SSL_MAX_FRAG_LEN_4096 */
83};
Paul Bakker05decb22013-08-15 13:33:48 +020084#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020085
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020086static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
87{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020088 ssl_session_free( dst );
89 memcpy( dst, src, sizeof( ssl_session ) );
90
Paul Bakker7c6b2c32013-09-16 13:49:26 +020091#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020092 if( src->peer_cert != NULL )
93 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020094 int ret;
95
Mansour Moufidc531b4a2015-02-15 17:35:38 -050096 dst->peer_cert = polarssl_malloc( sizeof(x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020097 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020098 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
99
Paul Bakkerb6b09562013-09-18 14:17:41 +0200100 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200101
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200102 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
103 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200104 {
105 polarssl_free( dst->peer_cert );
106 dst->peer_cert = NULL;
107 return( ret );
108 }
109 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200110#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200111
Paul Bakkera503a632013-08-14 13:48:06 +0200112#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200113 if( src->ticket != NULL )
114 {
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500115 dst->ticket = polarssl_malloc( src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200116 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200117 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
118
119 memcpy( dst->ticket, src->ticket, src->ticket_len );
120 }
Paul Bakkera503a632013-08-14 13:48:06 +0200121#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200122
123 return( 0 );
124}
125
Paul Bakker05ef8352012-05-08 09:17:57 +0000126#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200127int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200128 const unsigned char *key_enc, const unsigned char *key_dec,
129 size_t keylen,
130 const unsigned char *iv_enc, const unsigned char *iv_dec,
131 size_t ivlen,
132 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200133 size_t maclen ) = NULL;
134int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
135int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
136int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
137int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
138int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200139#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000140
Paul Bakker5121ce52009-01-03 21:22:43 +0000141/*
142 * Key material generation
143 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200144#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200145static int ssl3_prf( const unsigned char *secret, size_t slen,
146 const char *label,
147 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000148 unsigned char *dstbuf, size_t dlen )
149{
150 size_t i;
151 md5_context md5;
152 sha1_context sha1;
153 unsigned char padding[16];
154 unsigned char sha1sum[20];
155 ((void)label);
156
Paul Bakker5b4af392014-06-26 12:09:34 +0200157 md5_init( &md5 );
158 sha1_init( &sha1 );
159
Paul Bakker5f70b252012-09-13 14:23:06 +0000160 /*
161 * SSLv3:
162 * block =
163 * MD5( secret + SHA1( 'A' + secret + random ) ) +
164 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
165 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
166 * ...
167 */
168 for( i = 0; i < dlen / 16; i++ )
169 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200170 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000171
172 sha1_starts( &sha1 );
173 sha1_update( &sha1, padding, 1 + i );
174 sha1_update( &sha1, secret, slen );
175 sha1_update( &sha1, random, rlen );
176 sha1_finish( &sha1, sha1sum );
177
178 md5_starts( &md5 );
179 md5_update( &md5, secret, slen );
180 md5_update( &md5, sha1sum, 20 );
181 md5_finish( &md5, dstbuf + i * 16 );
182 }
183
Paul Bakker5b4af392014-06-26 12:09:34 +0200184 md5_free( &md5 );
185 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000186
Paul Bakker34617722014-06-13 17:20:13 +0200187 polarssl_zeroize( padding, sizeof( padding ) );
188 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000189
190 return( 0 );
191}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200192#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000193
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200194#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200195static int tls1_prf( const unsigned char *secret, size_t slen,
196 const char *label,
197 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000198 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000199{
Paul Bakker23986e52011-04-24 08:57:21 +0000200 size_t nb, hs;
201 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200202 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 unsigned char tmp[128];
204 unsigned char h_i[20];
205
206 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000207 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000208
209 hs = ( slen + 1 ) / 2;
210 S1 = secret;
211 S2 = secret + slen - hs;
212
213 nb = strlen( label );
214 memcpy( tmp + 20, label, nb );
215 memcpy( tmp + 20 + nb, random, rlen );
216 nb += rlen;
217
218 /*
219 * First compute P_md5(secret,label+random)[0..dlen]
220 */
221 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
222
223 for( i = 0; i < dlen; i += 16 )
224 {
225 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
226 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
227
228 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
229
230 for( j = 0; j < k; j++ )
231 dstbuf[i + j] = h_i[j];
232 }
233
234 /*
235 * XOR out with P_sha1(secret,label+random)[0..dlen]
236 */
237 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
238
239 for( i = 0; i < dlen; i += 20 )
240 {
241 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
242 sha1_hmac( S2, hs, tmp, 20, tmp );
243
244 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
245
246 for( j = 0; j < k; j++ )
247 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
248 }
249
Paul Bakker34617722014-06-13 17:20:13 +0200250 polarssl_zeroize( tmp, sizeof( tmp ) );
251 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000252
253 return( 0 );
254}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200255#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000256
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200257#if defined(POLARSSL_SSL_PROTO_TLS1_2)
258#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200259static int tls_prf_sha256( const unsigned char *secret, size_t slen,
260 const char *label,
261 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000262 unsigned char *dstbuf, size_t dlen )
263{
264 size_t nb;
265 size_t i, j, k;
266 unsigned char tmp[128];
267 unsigned char h_i[32];
268
269 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
270 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
271
272 nb = strlen( label );
273 memcpy( tmp + 32, label, nb );
274 memcpy( tmp + 32 + nb, random, rlen );
275 nb += rlen;
276
277 /*
278 * Compute P_<hash>(secret, label + random)[0..dlen]
279 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200280 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000281
282 for( i = 0; i < dlen; i += 32 )
283 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200284 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
285 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000286
287 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
288
289 for( j = 0; j < k; j++ )
290 dstbuf[i + j] = h_i[j];
291 }
292
Paul Bakker34617722014-06-13 17:20:13 +0200293 polarssl_zeroize( tmp, sizeof( tmp ) );
294 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000295
296 return( 0 );
297}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200298#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000299
Paul Bakker9e36f042013-06-30 14:34:05 +0200300#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200301static int tls_prf_sha384( const unsigned char *secret, size_t slen,
302 const char *label,
303 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000304 unsigned char *dstbuf, size_t dlen )
305{
306 size_t nb;
307 size_t i, j, k;
308 unsigned char tmp[128];
309 unsigned char h_i[48];
310
311 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
312 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
313
314 nb = strlen( label );
315 memcpy( tmp + 48, label, nb );
316 memcpy( tmp + 48 + nb, random, rlen );
317 nb += rlen;
318
319 /*
320 * Compute P_<hash>(secret, label + random)[0..dlen]
321 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200322 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000323
324 for( i = 0; i < dlen; i += 48 )
325 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200326 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
327 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000328
329 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
330
331 for( j = 0; j < k; j++ )
332 dstbuf[i + j] = h_i[j];
333 }
334
Paul Bakker34617722014-06-13 17:20:13 +0200335 polarssl_zeroize( tmp, sizeof( tmp ) );
336 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000337
338 return( 0 );
339}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200340#endif /* POLARSSL_SHA512_C */
341#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000342
Paul Bakker66d5d072014-06-17 16:39:18 +0200343static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200344
345#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
346 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200347static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200348#endif
Paul Bakker380da532012-04-18 16:10:25 +0000349
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200350#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200351static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
352static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200353#endif
354
355#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200356static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
357static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200358#endif
359
360#if defined(POLARSSL_SSL_PROTO_TLS1_2)
361#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200362static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
363static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
364static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200365#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100366
Paul Bakker9e36f042013-06-30 14:34:05 +0200367#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200368static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
369static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
370static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100371#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200372#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000373
Paul Bakker5121ce52009-01-03 21:22:43 +0000374int ssl_derive_keys( ssl_context *ssl )
375{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200376 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000378 unsigned char keyblk[256];
379 unsigned char *key1;
380 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100381 unsigned char *mac_enc;
382 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200383 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100384 const cipher_info_t *cipher_info;
385 const md_info_t *md_info;
386
Paul Bakker48916f92012-09-16 19:57:18 +0000387 ssl_session *session = ssl->session_negotiate;
388 ssl_transform *transform = ssl->transform_negotiate;
389 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000390
391 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
392
Paul Bakker68884e32013-01-07 18:20:04 +0100393 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
394 if( cipher_info == NULL )
395 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200396 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100397 transform->ciphersuite_info->cipher ) );
398 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
399 }
400
401 md_info = md_info_from_type( transform->ciphersuite_info->mac );
402 if( md_info == NULL )
403 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200404 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100405 transform->ciphersuite_info->mac ) );
406 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
407 }
408
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000410 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000411 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200412#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000413 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000414 {
Paul Bakker48916f92012-09-16 19:57:18 +0000415 handshake->tls_prf = ssl3_prf;
416 handshake->calc_verify = ssl_calc_verify_ssl;
417 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000418 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200419 else
420#endif
421#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
422 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000423 {
Paul Bakker48916f92012-09-16 19:57:18 +0000424 handshake->tls_prf = tls1_prf;
425 handshake->calc_verify = ssl_calc_verify_tls;
426 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000427 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200428 else
429#endif
430#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200431#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200432 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
433 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000434 {
Paul Bakker48916f92012-09-16 19:57:18 +0000435 handshake->tls_prf = tls_prf_sha384;
436 handshake->calc_verify = ssl_calc_verify_tls_sha384;
437 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000438 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000439 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200440#endif
441#if defined(POLARSSL_SHA256_C)
442 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000443 {
Paul Bakker48916f92012-09-16 19:57:18 +0000444 handshake->tls_prf = tls_prf_sha256;
445 handshake->calc_verify = ssl_calc_verify_tls_sha256;
446 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000447 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200448 else
449#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200450#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200451 {
452 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200453 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200454 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000455
456 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000457 * SSLv3:
458 * master =
459 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
460 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
461 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200462 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200463 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000464 * master = PRF( premaster, "master secret", randbytes )[0..47]
465 */
Paul Bakker0a597072012-09-25 21:55:46 +0000466 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000467 {
Paul Bakker48916f92012-09-16 19:57:18 +0000468 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
469 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200471#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
472 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200473 {
474 unsigned char session_hash[48];
475 size_t hash_len;
476
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200477 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200478
479 ssl->handshake->calc_verify( ssl, session_hash );
480
481#if defined(POLARSSL_SSL_PROTO_TLS1_2)
482 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
483 {
484#if defined(POLARSSL_SHA512_C)
485 if( ssl->transform_negotiate->ciphersuite_info->mac ==
486 POLARSSL_MD_SHA384 )
487 {
488 hash_len = 48;
489 }
490 else
491#endif
492 hash_len = 32;
493 }
494 else
495#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
496 hash_len = 36;
497
498 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
499
500 handshake->tls_prf( handshake->premaster, handshake->pmslen,
501 "extended master secret",
502 session_hash, hash_len, session->master, 48 );
503
504 }
505 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200506#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000507 handshake->tls_prf( handshake->premaster, handshake->pmslen,
508 "master secret",
509 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200511
Paul Bakker34617722014-06-13 17:20:13 +0200512 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 }
514 else
515 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
516
517 /*
518 * Swap the client and server random values.
519 */
Paul Bakker48916f92012-09-16 19:57:18 +0000520 memcpy( tmp, handshake->randbytes, 64 );
521 memcpy( handshake->randbytes, tmp + 32, 32 );
522 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200523 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525 /*
526 * SSLv3:
527 * key block =
528 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
529 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
530 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
531 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
532 * ...
533 *
534 * TLSv1:
535 * key block = PRF( master, "key expansion", randbytes )
536 */
Paul Bakker48916f92012-09-16 19:57:18 +0000537 handshake->tls_prf( session->master, 48, "key expansion",
538 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
Paul Bakker48916f92012-09-16 19:57:18 +0000540 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
541 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
542 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
543 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
545
Paul Bakker34617722014-06-13 17:20:13 +0200546 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
548 /*
549 * Determine the appropriate key, IV and MAC length.
550 */
Paul Bakker68884e32013-01-07 18:20:04 +0100551
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200552 transform->keylen = cipher_info->key_length / 8;
553
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200554 if( cipher_info->mode == POLARSSL_MODE_GCM ||
555 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200557 transform->maclen = 0;
558
Paul Bakker68884e32013-01-07 18:20:04 +0100559 transform->ivlen = 12;
560 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200561
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200562 /* Minimum length is expicit IV + tag */
563 transform->minlen = transform->ivlen - transform->fixed_ivlen
564 + ( transform->ciphersuite_info->flags &
565 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100566 }
567 else
568 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200569 /* Initialize HMAC contexts */
570 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
571 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100572 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200573 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
574 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100575 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200577 /* Get MAC length */
578 transform->maclen = md_get_size( md_info );
579
580#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
581 /*
582 * If HMAC is to be truncated, we shall keep the leftmost bytes,
583 * (rfc 6066 page 13 or rfc 2104 section 4),
584 * so we only need to adjust the length here.
585 */
586 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
587 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
588#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
589
590 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100591 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000592
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200593 /* Minimum length */
594 if( cipher_info->mode == POLARSSL_MODE_STREAM )
595 transform->minlen = transform->maclen;
596 else
Paul Bakker68884e32013-01-07 18:20:04 +0100597 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200598 /*
599 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100600 * 1. if EtM is in use: one block plus MAC
601 * otherwise: * first multiple of blocklen greater than maclen
602 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200603 */
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100604#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
605 if( session->encrypt_then_mac == SSL_ETM_ENABLED )
606 {
607 transform->minlen = transform->maclen
608 + cipher_info->block_size;
609 }
610 else
611#endif
612 {
613 transform->minlen = transform->maclen
614 + cipher_info->block_size
615 - transform->maclen % cipher_info->block_size;
616 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200617
618#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
619 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
620 ssl->minor_ver == SSL_MINOR_VERSION_1 )
621 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100622 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200623#endif
624#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
625 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
626 ssl->minor_ver == SSL_MINOR_VERSION_3 )
627 {
628 transform->minlen += transform->ivlen;
629 }
630 else
631#endif
632 {
633 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
634 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
635 }
Paul Bakker68884e32013-01-07 18:20:04 +0100636 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000637 }
638
639 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000640 transform->keylen, transform->minlen, transform->ivlen,
641 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000642
643 /*
644 * Finally setup the cipher contexts, IVs and MAC secrets.
645 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100646#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000647 if( ssl->endpoint == SSL_IS_CLIENT )
648 {
Paul Bakker48916f92012-09-16 19:57:18 +0000649 key1 = keyblk + transform->maclen * 2;
650 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000651
Paul Bakker68884e32013-01-07 18:20:04 +0100652 mac_enc = keyblk;
653 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000654
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000655 /*
656 * This is not used in TLS v1.1.
657 */
Paul Bakker48916f92012-09-16 19:57:18 +0000658 iv_copy_len = ( transform->fixed_ivlen ) ?
659 transform->fixed_ivlen : transform->ivlen;
660 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
661 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000662 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 }
664 else
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100665#endif /* POLARSSL_SSL_CLI_C */
666#if defined(POLARSSL_SSL_SRV_C)
667 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 {
Paul Bakker48916f92012-09-16 19:57:18 +0000669 key1 = keyblk + transform->maclen * 2 + transform->keylen;
670 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
Paul Bakker68884e32013-01-07 18:20:04 +0100672 mac_enc = keyblk + transform->maclen;
673 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000674
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000675 /*
676 * This is not used in TLS v1.1.
677 */
Paul Bakker48916f92012-09-16 19:57:18 +0000678 iv_copy_len = ( transform->fixed_ivlen ) ?
679 transform->fixed_ivlen : transform->ivlen;
680 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
681 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000682 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100684 else
685#endif /* POLARSSL_SSL_SRV_C */
686 {
687 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
688 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
689 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000690
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200691#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100692 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
693 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100694 if( transform->maclen > sizeof transform->mac_enc )
695 {
696 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200697 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100698 }
699
Paul Bakker68884e32013-01-07 18:20:04 +0100700 memcpy( transform->mac_enc, mac_enc, transform->maclen );
701 memcpy( transform->mac_dec, mac_dec, transform->maclen );
702 }
703 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200704#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200705#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
706 defined(POLARSSL_SSL_PROTO_TLS1_2)
707 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100708 {
709 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
710 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
711 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200712 else
713#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200714 {
715 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200716 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200717 }
Paul Bakker68884e32013-01-07 18:20:04 +0100718
Paul Bakker05ef8352012-05-08 09:17:57 +0000719#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200720 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000721 {
722 int ret = 0;
723
724 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
725
Paul Bakker07eb38b2012-12-19 14:42:06 +0100726 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
727 transform->iv_enc, transform->iv_dec,
728 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100729 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100730 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000731 {
732 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200733 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000734 }
735 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200736#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000737
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200738 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
739 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000740 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200741 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
742 return( ret );
743 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200744
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200745 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
746 cipher_info ) ) != 0 )
747 {
748 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
749 return( ret );
750 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200751
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200752 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
753 cipher_info->key_length,
754 POLARSSL_ENCRYPT ) ) != 0 )
755 {
756 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
757 return( ret );
758 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200759
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200760 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
761 cipher_info->key_length,
762 POLARSSL_DECRYPT ) ) != 0 )
763 {
764 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
765 return( ret );
766 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200767
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200768#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200769 if( cipher_info->mode == POLARSSL_MODE_CBC )
770 {
771 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
772 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200773 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200774 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
775 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200776 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200777
778 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
779 POLARSSL_PADDING_NONE ) ) != 0 )
780 {
781 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
782 return( ret );
783 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000784 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200785#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000786
Paul Bakker34617722014-06-13 17:20:13 +0200787 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000788
Paul Bakker2770fbd2012-07-03 13:30:23 +0000789#if defined(POLARSSL_ZLIB_SUPPORT)
790 // Initialize compression
791 //
Paul Bakker48916f92012-09-16 19:57:18 +0000792 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000793 {
Paul Bakker16770332013-10-11 09:59:44 +0200794 if( ssl->compress_buf == NULL )
795 {
796 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
797 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
798 if( ssl->compress_buf == NULL )
799 {
800 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
801 SSL_BUFFER_LEN ) );
802 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
803 }
804 }
805
Paul Bakker2770fbd2012-07-03 13:30:23 +0000806 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
807
Paul Bakker48916f92012-09-16 19:57:18 +0000808 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
809 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000810
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200811 if( deflateInit( &transform->ctx_deflate,
812 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000813 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000814 {
815 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
816 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
817 }
818 }
819#endif /* POLARSSL_ZLIB_SUPPORT */
820
Paul Bakker5121ce52009-01-03 21:22:43 +0000821 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
822
823 return( 0 );
824}
825
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200826#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000827void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000828{
829 md5_context md5;
830 sha1_context sha1;
831 unsigned char pad_1[48];
832 unsigned char pad_2[48];
833
Paul Bakker380da532012-04-18 16:10:25 +0000834 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000835
Paul Bakker48916f92012-09-16 19:57:18 +0000836 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
837 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000838
Paul Bakker380da532012-04-18 16:10:25 +0000839 memset( pad_1, 0x36, 48 );
840 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000841
Paul Bakker48916f92012-09-16 19:57:18 +0000842 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000843 md5_update( &md5, pad_1, 48 );
844 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000845
Paul Bakker380da532012-04-18 16:10:25 +0000846 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000847 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000848 md5_update( &md5, pad_2, 48 );
849 md5_update( &md5, hash, 16 );
850 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000851
Paul Bakker48916f92012-09-16 19:57:18 +0000852 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000853 sha1_update( &sha1, pad_1, 40 );
854 sha1_finish( &sha1, hash + 16 );
855
856 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000857 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000858 sha1_update( &sha1, pad_2, 40 );
859 sha1_update( &sha1, hash + 16, 20 );
860 sha1_finish( &sha1, hash + 16 );
861
862 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
863 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
864
Paul Bakker5b4af392014-06-26 12:09:34 +0200865 md5_free( &md5 );
866 sha1_free( &sha1 );
867
Paul Bakker380da532012-04-18 16:10:25 +0000868 return;
869}
Paul Bakker9af723c2014-05-01 13:03:14 +0200870#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000871
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200872#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000873void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
874{
875 md5_context md5;
876 sha1_context sha1;
877
878 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
879
Paul Bakker48916f92012-09-16 19:57:18 +0000880 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
881 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000882
Paul Bakker48916f92012-09-16 19:57:18 +0000883 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000884 sha1_finish( &sha1, hash + 16 );
885
886 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
887 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
888
Paul Bakker5b4af392014-06-26 12:09:34 +0200889 md5_free( &md5 );
890 sha1_free( &sha1 );
891
Paul Bakker380da532012-04-18 16:10:25 +0000892 return;
893}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200894#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000895
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200896#if defined(POLARSSL_SSL_PROTO_TLS1_2)
897#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000898void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
899{
Paul Bakker9e36f042013-06-30 14:34:05 +0200900 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000901
902 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
903
Paul Bakker9e36f042013-06-30 14:34:05 +0200904 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
905 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000906
907 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
908 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
909
Paul Bakker5b4af392014-06-26 12:09:34 +0200910 sha256_free( &sha256 );
911
Paul Bakker380da532012-04-18 16:10:25 +0000912 return;
913}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200914#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000915
Paul Bakker9e36f042013-06-30 14:34:05 +0200916#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000917void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
918{
Paul Bakker9e36f042013-06-30 14:34:05 +0200919 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000920
921 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
922
Paul Bakker9e36f042013-06-30 14:34:05 +0200923 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
924 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000925
Paul Bakkerca4ab492012-04-18 14:23:57 +0000926 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000927 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
928
Paul Bakker5b4af392014-06-26 12:09:34 +0200929 sha512_free( &sha512 );
930
Paul Bakker5121ce52009-01-03 21:22:43 +0000931 return;
932}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200933#endif /* POLARSSL_SHA512_C */
934#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000935
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200936#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200937int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
938{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200939 unsigned char *p = ssl->handshake->premaster;
940 unsigned char *end = p + sizeof( ssl->handshake->premaster );
941
942 /*
943 * PMS = struct {
944 * opaque other_secret<0..2^16-1>;
945 * opaque psk<0..2^16-1>;
946 * };
947 * with "other_secret" depending on the particular key exchange
948 */
949#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
950 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
951 {
Manuel Pégourié-Gonnard5ca36402015-10-21 12:35:29 +0200952 if( end - p < 2 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200953 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
954
955 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
956 *(p++) = (unsigned char)( ssl->psk_len );
Manuel Pégourié-Gonnard5ca36402015-10-21 12:35:29 +0200957
958 if( end < p || (size_t)( end - p ) < ssl->psk_len )
959 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
960
961 memset( p, 0, ssl->psk_len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200962 p += ssl->psk_len;
963 }
964 else
965#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200966#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
967 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
968 {
969 /*
970 * other_secret already set by the ClientKeyExchange message,
971 * and is 48 bytes long
972 */
973 *p++ = 0;
974 *p++ = 48;
975 p += 48;
976 }
977 else
978#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200979#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
980 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
981 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200982 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200983 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200984
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200985 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200986 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200987 p + 2, &len,
988 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200989 {
990 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
991 return( ret );
992 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200993 *(p++) = (unsigned char)( len >> 8 );
994 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200995 p += len;
996
997 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
998 }
999 else
1000#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1001#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1002 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1003 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001004 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001005 size_t zlen;
1006
1007 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001008 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001009 ssl->f_rng, ssl->p_rng ) ) != 0 )
1010 {
1011 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1012 return( ret );
1013 }
1014
1015 *(p++) = (unsigned char)( zlen >> 8 );
1016 *(p++) = (unsigned char)( zlen );
1017 p += zlen;
1018
1019 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1020 }
1021 else
1022#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1023 {
1024 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001025 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001026 }
1027
1028 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnard5ca36402015-10-21 12:35:29 +02001029 if( end - p < 2 )
1030 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001031
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001032 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1033 *(p++) = (unsigned char)( ssl->psk_len );
Manuel Pégourié-Gonnard5ca36402015-10-21 12:35:29 +02001034
1035 if( end < p || (size_t)( end - p ) < ssl->psk_len )
1036 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1037
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001038 memcpy( p, ssl->psk, ssl->psk_len );
1039 p += ssl->psk_len;
1040
1041 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1042
1043 return( 0 );
1044}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001045#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001046
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001047#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001048/*
1049 * SSLv3.0 MAC functions
1050 */
Paul Bakker68884e32013-01-07 18:20:04 +01001051static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1052 unsigned char *buf, size_t len,
1053 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001054{
1055 unsigned char header[11];
1056 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001057 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001058 int md_size = md_get_size( md_ctx->md_info );
1059 int md_type = md_get_type( md_ctx->md_info );
1060
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001061 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001062 if( md_type == POLARSSL_MD_MD5 )
1063 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001064 else
Paul Bakker68884e32013-01-07 18:20:04 +01001065 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001066
1067 memcpy( header, ctr, 8 );
1068 header[ 8] = (unsigned char) type;
1069 header[ 9] = (unsigned char)( len >> 8 );
1070 header[10] = (unsigned char)( len );
1071
Paul Bakker68884e32013-01-07 18:20:04 +01001072 memset( padding, 0x36, padlen );
1073 md_starts( md_ctx );
1074 md_update( md_ctx, secret, md_size );
1075 md_update( md_ctx, padding, padlen );
1076 md_update( md_ctx, header, 11 );
1077 md_update( md_ctx, buf, len );
1078 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001079
Paul Bakker68884e32013-01-07 18:20:04 +01001080 memset( padding, 0x5C, padlen );
1081 md_starts( md_ctx );
1082 md_update( md_ctx, secret, md_size );
1083 md_update( md_ctx, padding, padlen );
1084 md_update( md_ctx, buf + len, md_size );
1085 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001086}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001087#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001088
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001089#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1090 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1091 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1092#define POLARSSL_SOME_MODES_USE_MAC
1093#endif
1094
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001095/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001096 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001097 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001098static int ssl_encrypt_buf( ssl_context *ssl )
1099{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001100 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001101 cipher_mode_t mode;
1102 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001103
1104 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1105
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001106 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1107 {
1108 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1109 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1110 }
1111
1112 mode = cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
1113
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001114 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1115 ssl->out_msg, ssl->out_msglen );
1116
Paul Bakker5121ce52009-01-03 21:22:43 +00001117 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001118 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001119 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001120#if defined(POLARSSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001121 if( mode == POLARSSL_MODE_STREAM ||
1122 ( mode == POLARSSL_MODE_CBC
1123#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1124 && ssl->session_out->encrypt_then_mac == SSL_ETM_DISABLED
1125#endif
1126 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001127 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001128#if defined(POLARSSL_SSL_PROTO_SSL3)
1129 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1130 {
1131 ssl_mac( &ssl->transform_out->md_ctx_enc,
1132 ssl->transform_out->mac_enc,
1133 ssl->out_msg, ssl->out_msglen,
1134 ssl->out_ctr, ssl->out_msgtype );
1135 }
1136 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001137#endif
1138#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001139 defined(POLARSSL_SSL_PROTO_TLS1_2)
1140 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1141 {
1142 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1143 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1144 ssl->out_msg, ssl->out_msglen );
1145 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1146 ssl->out_msg + ssl->out_msglen );
1147 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1148 }
1149 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001150#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001151 {
1152 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001153 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001154 }
1155
1156 SSL_DEBUG_BUF( 4, "computed mac",
1157 ssl->out_msg + ssl->out_msglen,
1158 ssl->transform_out->maclen );
1159
1160 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001161 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001162 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001163#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001164
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001165 /*
1166 * Encrypt
1167 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001168#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001169 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001171 int ret;
1172 size_t olen = 0;
1173
Paul Bakker5121ce52009-01-03 21:22:43 +00001174 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1175 "including %d bytes of padding",
1176 ssl->out_msglen, 0 ) );
1177
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001178 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001179 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001180 ssl->transform_out->ivlen,
1181 ssl->out_msg, ssl->out_msglen,
1182 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001183 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001184 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001185 return( ret );
1186 }
1187
1188 if( ssl->out_msglen != olen )
1189 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001190 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001191 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001192 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 }
Paul Bakker68884e32013-01-07 18:20:04 +01001194 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001195#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001196#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1197 if( mode == POLARSSL_MODE_GCM ||
1198 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001199 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001200 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001201 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001202 unsigned char *enc_msg;
1203 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001204 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1205 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001206
Paul Bakkerca4ab492012-04-18 14:23:57 +00001207 memcpy( add_data, ssl->out_ctr, 8 );
1208 add_data[8] = ssl->out_msgtype;
1209 add_data[9] = ssl->major_ver;
1210 add_data[10] = ssl->minor_ver;
1211 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1212 add_data[12] = ssl->out_msglen & 0xFF;
1213
1214 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1215 add_data, 13 );
1216
Paul Bakker68884e32013-01-07 18:20:04 +01001217 /*
1218 * Generate IV
1219 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001220#if defined(POLARSSL_SSL_AEAD_RANDOM_IV)
Paul Bakker68884e32013-01-07 18:20:04 +01001221 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001222 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1223 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001224 if( ret != 0 )
1225 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001226
Paul Bakker68884e32013-01-07 18:20:04 +01001227 memcpy( ssl->out_iv,
1228 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1229 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001230#else
1231 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1232 {
1233 /* Reminder if we ever add an AEAD mode with a different size */
1234 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1235 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1236 }
1237
1238 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1239 ssl->out_ctr, 8 );
1240 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1241#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00001242
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001243 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001244 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001245
Paul Bakker68884e32013-01-07 18:20:04 +01001246 /*
1247 * Fix pointer positions and message length with added IV
1248 */
1249 enc_msg = ssl->out_msg;
1250 enc_msglen = ssl->out_msglen;
1251 ssl->out_msglen += ssl->transform_out->ivlen -
1252 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001253
Paul Bakker68884e32013-01-07 18:20:04 +01001254 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1255 "including %d bytes of padding",
1256 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001257
Paul Bakker68884e32013-01-07 18:20:04 +01001258 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001259 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001260 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001261 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1262 ssl->transform_out->iv_enc,
1263 ssl->transform_out->ivlen,
1264 add_data, 13,
1265 enc_msg, enc_msglen,
1266 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001267 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001268 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001269 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001270 return( ret );
1271 }
1272
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001273 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001274 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001275 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001276 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001277 }
1278
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001279 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001280 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001281
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001282 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001283 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001284 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001285#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001286#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1287 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001288 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001289 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001290 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001291 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001292 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001293
Paul Bakker48916f92012-09-16 19:57:18 +00001294 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1295 ssl->transform_out->ivlen;
1296 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001297 padlen = 0;
1298
1299 for( i = 0; i <= padlen; i++ )
1300 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1301
1302 ssl->out_msglen += padlen + 1;
1303
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001304 enc_msglen = ssl->out_msglen;
1305 enc_msg = ssl->out_msg;
1306
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001307#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001308 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001309 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1310 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001311 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001312 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001313 {
1314 /*
1315 * Generate IV
1316 */
Manuel Pégourié-Gonnarda67fd792015-08-27 12:02:40 +02001317 ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001318 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001319 if( ret != 0 )
1320 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001321
Paul Bakker92be97b2013-01-02 17:30:03 +01001322 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001323 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001324
1325 /*
1326 * Fix pointer positions and message length with added IV
1327 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001328 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001329 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001330 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001331 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001332#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001333
Paul Bakker5121ce52009-01-03 21:22:43 +00001334 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001335 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001336 ssl->out_msglen, ssl->transform_out->ivlen,
1337 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001338
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001339 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001340 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001341 ssl->transform_out->ivlen,
1342 enc_msg, enc_msglen,
1343 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001344 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001345 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001346 return( ret );
1347 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001348
Paul Bakkercca5b812013-08-31 17:40:26 +02001349 if( enc_msglen != olen )
1350 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001351 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001352 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001353 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001354
1355#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001356 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1357 {
1358 /*
1359 * Save IV in SSL3 and TLS1
1360 */
1361 memcpy( ssl->transform_out->iv_enc,
1362 ssl->transform_out->cipher_ctx_enc.iv,
1363 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001365#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001366
1367#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001368 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001369 {
1370 /*
1371 * MAC(MAC_write_key, seq_num +
1372 * TLSCipherText.type +
1373 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001374 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001375 * IV + // except for TLS 1.0
1376 * ENC(content + padding + padding_length));
1377 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001378 unsigned char pseudo_hdr[13];
1379
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001380 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1381
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001382 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1383 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001384 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1385 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1386
1387 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001388
1389 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1390 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1391 ssl->out_iv, ssl->out_msglen );
1392 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1393 ssl->out_iv + ssl->out_msglen );
1394 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1395
1396 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001397 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001398 }
1399#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001400 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001401 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001402#endif /* POLARSSL_CIPHER_MODE_CBC &&
1403 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001404 {
1405 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001406 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001407 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001408
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001409 /* Make extra sure authentication was performed, exactly once */
1410 if( auth_done != 1 )
1411 {
1412 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1413 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1414 }
1415
Paul Bakkerca4ab492012-04-18 14:23:57 +00001416 for( i = 8; i > 0; i-- )
1417 if( ++ssl->out_ctr[i - 1] != 0 )
1418 break;
1419
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001420 /* The loops goes to its end iff the counter is wrapping */
1421 if( i == 0 )
1422 {
1423 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1424 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1425 }
1426
Paul Bakker5121ce52009-01-03 21:22:43 +00001427 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1428
1429 return( 0 );
1430}
1431
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001432#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001433
Paul Bakker5121ce52009-01-03 21:22:43 +00001434static int ssl_decrypt_buf( ssl_context *ssl )
1435{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001436 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001437 cipher_mode_t mode;
1438 int auth_done = 0;
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001439#if defined(POLARSSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001440 size_t padlen = 0, correct = 1;
1441#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001442
1443 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1444
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001445 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1446 {
1447 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1448 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1449 }
1450
1451 mode = cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
1452
Paul Bakker48916f92012-09-16 19:57:18 +00001453 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001454 {
1455 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001456 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001457 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001458 }
1459
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001460#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001461 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001462 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001463 int ret;
1464 size_t olen = 0;
1465
Paul Bakker68884e32013-01-07 18:20:04 +01001466 padlen = 0;
1467
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001468 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001469 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001470 ssl->transform_in->ivlen,
1471 ssl->in_msg, ssl->in_msglen,
1472 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001473 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001474 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001475 return( ret );
1476 }
1477
1478 if( ssl->in_msglen != olen )
1479 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001480 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001481 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001482 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001483 }
Paul Bakker68884e32013-01-07 18:20:04 +01001484 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001485#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001486#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1487 if( mode == POLARSSL_MODE_GCM ||
1488 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001489 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001490 int ret;
1491 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001492 unsigned char *dec_msg;
1493 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001494 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001495 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1496 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001497 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1498 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001499
Manuel Pégourié-Gonnard06d75192015-02-11 14:54:11 +00001500 if( ssl->in_msglen < (size_t) explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001501 {
1502 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1503 "+ taglen (%d)", ssl->in_msglen,
1504 explicit_iv_len, taglen ) );
1505 return( POLARSSL_ERR_SSL_INVALID_MAC );
1506 }
1507 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1508
Paul Bakker68884e32013-01-07 18:20:04 +01001509 dec_msg = ssl->in_msg;
1510 dec_msg_result = ssl->in_msg;
1511 ssl->in_msglen = dec_msglen;
1512
1513 memcpy( add_data, ssl->in_ctr, 8 );
1514 add_data[8] = ssl->in_msgtype;
1515 add_data[9] = ssl->major_ver;
1516 add_data[10] = ssl->minor_ver;
1517 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1518 add_data[12] = ssl->in_msglen & 0xFF;
1519
1520 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1521 add_data, 13 );
1522
1523 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1524 ssl->in_iv,
1525 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1526
1527 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1528 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001529 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001530
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001531 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001532 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001533 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001534 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1535 ssl->transform_in->iv_dec,
1536 ssl->transform_in->ivlen,
1537 add_data, 13,
1538 dec_msg, dec_msglen,
1539 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001540 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001541 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001542 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1543
1544 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1545 return( POLARSSL_ERR_SSL_INVALID_MAC );
1546
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001547 return( ret );
1548 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001549 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001550
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001551 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001552 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001553 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001554 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001555 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001556 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001557 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001558#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001559#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1560 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001561 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001562 {
Paul Bakker45829992013-01-03 14:52:21 +01001563 /*
1564 * Decrypt and check the padding
1565 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001566 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001567 unsigned char *dec_msg;
1568 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001569 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001570 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001571 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001572
Paul Bakker5121ce52009-01-03 21:22:43 +00001573 /*
Paul Bakker45829992013-01-03 14:52:21 +01001574 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001575 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001576#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001577 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1578 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001579#endif
Paul Bakker45829992013-01-03 14:52:21 +01001580
1581 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1582 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1583 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001584 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1585 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1586 ssl->transform_in->ivlen,
1587 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001588 return( POLARSSL_ERR_SSL_INVALID_MAC );
1589 }
1590
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001591 dec_msglen = ssl->in_msglen;
1592 dec_msg = ssl->in_msg;
1593 dec_msg_result = ssl->in_msg;
1594
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001595 /*
1596 * Authenticate before decrypt if enabled
1597 */
1598#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001599 if( ssl->session_in->encrypt_then_mac == SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001600 {
1601 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001602 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001603
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001604 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1605
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001606 dec_msglen -= ssl->transform_in->maclen;
1607 ssl->in_msglen -= ssl->transform_in->maclen;
1608
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001609 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1610 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1611 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1612 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1613
1614 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1615
1616 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001617 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1618 ssl->in_iv, ssl->in_msglen );
1619 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1620 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1621
1622 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1623 ssl->transform_in->maclen );
1624 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1625 ssl->transform_in->maclen );
1626
1627 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1628 ssl->transform_in->maclen ) != 0 )
1629 {
1630 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1631
1632 return( POLARSSL_ERR_SSL_INVALID_MAC );
1633 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001634 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001635 }
1636#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1637
1638 /*
1639 * Check length sanity
1640 */
1641 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1642 {
1643 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1644 ssl->in_msglen, ssl->transform_in->ivlen ) );
1645 return( POLARSSL_ERR_SSL_INVALID_MAC );
1646 }
1647
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001648#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001649 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001650 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001651 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001652 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001653 {
Paul Bakker48916f92012-09-16 19:57:18 +00001654 dec_msglen -= ssl->transform_in->ivlen;
1655 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001656
Paul Bakker48916f92012-09-16 19:57:18 +00001657 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001658 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001659 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001660#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001661
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001662 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001663 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001664 ssl->transform_in->ivlen,
1665 dec_msg, dec_msglen,
1666 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001667 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001668 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001669 return( ret );
1670 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001671
Paul Bakkercca5b812013-08-31 17:40:26 +02001672 if( dec_msglen != olen )
1673 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001674 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001675 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001676 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001677
1678#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001679 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1680 {
1681 /*
1682 * Save IV in SSL3 and TLS1
1683 */
1684 memcpy( ssl->transform_in->iv_dec,
1685 ssl->transform_in->cipher_ctx_dec.iv,
1686 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001687 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001688#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001689
1690 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001691
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001692 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001693 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001694 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001695#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001696 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1697 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001698#endif
Paul Bakker45829992013-01-03 14:52:21 +01001699 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001700 correct = 0;
1701 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001702
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001703#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001704 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1705 {
Paul Bakker48916f92012-09-16 19:57:18 +00001706 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001707 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001708#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001709 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1710 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001711 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001712#endif
Paul Bakker45829992013-01-03 14:52:21 +01001713 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001714 }
1715 }
1716 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001717#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001718#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1719 defined(POLARSSL_SSL_PROTO_TLS1_2)
1720 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001721 {
1722 /*
Paul Bakker45829992013-01-03 14:52:21 +01001723 * TLSv1+: always check the padding up to the first failure
1724 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001725 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001726 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001727 size_t padding_idx = ssl->in_msglen - padlen - 1;
1728
Paul Bakker956c9e02013-12-19 14:42:28 +01001729 /*
1730 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001731 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001732 *
Paul Bakker61885c72014-04-25 12:59:03 +02001733 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1734 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001735 *
1736 * In both cases we reset padding_idx to a safe value (0) to
1737 * prevent out-of-buffer reads.
1738 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001739 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001740 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1741 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001742
1743 padding_idx *= correct;
1744
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001745 for( i = 1; i <= 256; i++ )
1746 {
1747 real_count &= ( i <= padlen );
1748 pad_count += real_count *
1749 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1750 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001751
1752 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001753
Paul Bakkerd66f0702013-01-31 16:57:45 +01001754#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001755 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001756 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001757#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001758 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001759 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001760 else
1761#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1762 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001763 {
1764 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001765 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001766 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001767
1768 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001769 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001770 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001771#endif /* POLARSSL_CIPHER_MODE_CBC &&
1772 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001773 {
1774 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001775 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001776 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001777
1778 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1779 ssl->in_msg, ssl->in_msglen );
1780
1781 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001782 * Authenticate if not done yet.
1783 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001784 */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001785#if defined(POLARSSL_SOME_MODES_USE_MAC)
1786 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001787 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001788 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1789
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001790 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001791
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001792 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1793 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001794
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001795 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001796
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001797#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001798 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1799 {
1800 ssl_mac( &ssl->transform_in->md_ctx_dec,
1801 ssl->transform_in->mac_dec,
1802 ssl->in_msg, ssl->in_msglen,
1803 ssl->in_ctr, ssl->in_msgtype );
1804 }
1805 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001806#endif /* POLARSSL_SSL_PROTO_SSL3 */
1807#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001808 defined(POLARSSL_SSL_PROTO_TLS1_2)
1809 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1810 {
1811 /*
1812 * Process MAC and always update for padlen afterwards to make
1813 * total time independent of padlen
1814 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001815 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001816 *
1817 * Known timing attacks:
1818 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1819 *
1820 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1821 * correctly. (We round down instead of up, so -56 is the correct
1822 * value for our calculations instead of -55)
1823 */
1824 size_t j, extra_run = 0;
1825 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1826 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001827
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001828 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001829
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001830 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1831 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1832 ssl->in_msglen );
1833 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1834 ssl->in_msg + ssl->in_msglen );
Manuel Pégourié-Gonnard7d1e95c2015-04-29 01:35:48 +02001835 /* Call md_process at least once due to cache attacks */
1836 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001837 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001838
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001839 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1840 }
1841 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001842#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001843 POLARSSL_SSL_PROTO_TLS1_2 */
1844 {
1845 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001846 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001847 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001848
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001849 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1850 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1851 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001852
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001853 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001854 ssl->transform_in->maclen ) != 0 )
1855 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001856#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001857 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001858#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001859 correct = 0;
1860 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001861 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001862
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001863 /*
1864 * Finally check the correct flag
1865 */
1866 if( correct == 0 )
1867 return( POLARSSL_ERR_SSL_INVALID_MAC );
1868 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001869#endif /* POLARSSL_SOME_MODES_USE_MAC */
1870
1871 /* Make extra sure authentication was performed, exactly once */
1872 if( auth_done != 1 )
1873 {
1874 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1875 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1876 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001877
1878 if( ssl->in_msglen == 0 )
1879 {
1880 ssl->nb_zero++;
1881
1882 /*
1883 * Three or more empty messages may be a DoS attack
1884 * (excessive CPU consumption).
1885 */
1886 if( ssl->nb_zero > 3 )
1887 {
1888 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1889 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001890 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001891 }
1892 }
1893 else
1894 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001895
Paul Bakker23986e52011-04-24 08:57:21 +00001896 for( i = 8; i > 0; i-- )
1897 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001898 break;
1899
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001900 /* The loops goes to its end iff the counter is wrapping */
1901 if( i == 0 )
1902 {
1903 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1904 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1905 }
1906
Paul Bakker5121ce52009-01-03 21:22:43 +00001907 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1908
1909 return( 0 );
1910}
1911
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001912#undef MAC_NONE
1913#undef MAC_PLAINTEXT
1914#undef MAC_CIPHERTEXT
1915
Paul Bakker2770fbd2012-07-03 13:30:23 +00001916#if defined(POLARSSL_ZLIB_SUPPORT)
1917/*
1918 * Compression/decompression functions
1919 */
1920static int ssl_compress_buf( ssl_context *ssl )
1921{
1922 int ret;
1923 unsigned char *msg_post = ssl->out_msg;
1924 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001925 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001926
1927 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1928
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001929 if( len_pre == 0 )
1930 return( 0 );
1931
Paul Bakker2770fbd2012-07-03 13:30:23 +00001932 memcpy( msg_pre, ssl->out_msg, len_pre );
1933
1934 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1935 ssl->out_msglen ) );
1936
1937 SSL_DEBUG_BUF( 4, "before compression: output payload",
1938 ssl->out_msg, ssl->out_msglen );
1939
Paul Bakker48916f92012-09-16 19:57:18 +00001940 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1941 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1942 ssl->transform_out->ctx_deflate.next_out = msg_post;
1943 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001944
Paul Bakker48916f92012-09-16 19:57:18 +00001945 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001946 if( ret != Z_OK )
1947 {
1948 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1949 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1950 }
1951
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001952 ssl->out_msglen = SSL_BUFFER_LEN -
1953 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001954
Paul Bakker2770fbd2012-07-03 13:30:23 +00001955 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1956 ssl->out_msglen ) );
1957
1958 SSL_DEBUG_BUF( 4, "after compression: output payload",
1959 ssl->out_msg, ssl->out_msglen );
1960
1961 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1962
1963 return( 0 );
1964}
1965
1966static int ssl_decompress_buf( ssl_context *ssl )
1967{
1968 int ret;
1969 unsigned char *msg_post = ssl->in_msg;
1970 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001971 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001972
1973 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1974
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001975 if( len_pre == 0 )
1976 return( 0 );
1977
Paul Bakker2770fbd2012-07-03 13:30:23 +00001978 memcpy( msg_pre, ssl->in_msg, len_pre );
1979
1980 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1981 ssl->in_msglen ) );
1982
1983 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1984 ssl->in_msg, ssl->in_msglen );
1985
Paul Bakker48916f92012-09-16 19:57:18 +00001986 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1987 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1988 ssl->transform_in->ctx_inflate.next_out = msg_post;
1989 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001990
Paul Bakker48916f92012-09-16 19:57:18 +00001991 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001992 if( ret != Z_OK )
1993 {
1994 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1995 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1996 }
1997
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001998 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1999 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002000
Paul Bakker2770fbd2012-07-03 13:30:23 +00002001 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
2002 ssl->in_msglen ) );
2003
2004 SSL_DEBUG_BUF( 4, "after decompression: input payload",
2005 ssl->in_msg, ssl->in_msglen );
2006
2007 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
2008
2009 return( 0 );
2010}
2011#endif /* POLARSSL_ZLIB_SUPPORT */
2012
Paul Bakker5121ce52009-01-03 21:22:43 +00002013/*
2014 * Fill the input message buffer
2015 */
Paul Bakker23986e52011-04-24 08:57:21 +00002016int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002017{
Paul Bakker23986e52011-04-24 08:57:21 +00002018 int ret;
2019 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002020
2021 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
2022
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002023 if( nb_want > SSL_BUFFER_LEN - 8 )
2024 {
2025 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2026 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2027 }
2028
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 while( ssl->in_left < nb_want )
2030 {
2031 len = nb_want - ssl->in_left;
2032 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
2033
2034 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2035 ssl->in_left, nb_want ) );
2036 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2037
Paul Bakker831a7552011-05-18 13:32:51 +00002038 if( ret == 0 )
2039 return( POLARSSL_ERR_SSL_CONN_EOF );
2040
Paul Bakker5121ce52009-01-03 21:22:43 +00002041 if( ret < 0 )
2042 return( ret );
2043
2044 ssl->in_left += ret;
2045 }
2046
2047 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2048
2049 return( 0 );
2050}
2051
2052/*
2053 * Flush any data not yet written
2054 */
2055int ssl_flush_output( ssl_context *ssl )
2056{
2057 int ret;
2058 unsigned char *buf;
2059
2060 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2061
2062 while( ssl->out_left > 0 )
2063 {
2064 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2065 5 + ssl->out_msglen, ssl->out_left ) );
2066
Paul Bakker5bd42292012-12-19 14:40:42 +01002067 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002069
Paul Bakker5121ce52009-01-03 21:22:43 +00002070 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2071
2072 if( ret <= 0 )
2073 return( ret );
2074
2075 ssl->out_left -= ret;
2076 }
2077
2078 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2079
2080 return( 0 );
2081}
2082
2083/*
2084 * Record layer functions
2085 */
2086int ssl_write_record( ssl_context *ssl )
2087{
Paul Bakker05ef8352012-05-08 09:17:57 +00002088 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002089 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002090
2091 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2092
Paul Bakker5121ce52009-01-03 21:22:43 +00002093 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2094 {
2095 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2096 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2097 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2098
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002099 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2100 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 }
2102
Paul Bakker2770fbd2012-07-03 13:30:23 +00002103#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002104 if( ssl->transform_out != NULL &&
2105 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002106 {
2107 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2108 {
2109 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2110 return( ret );
2111 }
2112
2113 len = ssl->out_msglen;
2114 }
2115#endif /*POLARSSL_ZLIB_SUPPORT */
2116
Paul Bakker05ef8352012-05-08 09:17:57 +00002117#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002118 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002119 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002120 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002121
Paul Bakker05ef8352012-05-08 09:17:57 +00002122 ret = ssl_hw_record_write( ssl );
2123 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2124 {
2125 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002126 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002127 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002128
2129 if( ret == 0 )
2130 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002131 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002132#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002133 if( !done )
2134 {
2135 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2136 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2137 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2139 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002140
Paul Bakker48916f92012-09-16 19:57:18 +00002141 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002142 {
2143 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2144 {
2145 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2146 return( ret );
2147 }
2148
2149 len = ssl->out_msglen;
2150 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2151 ssl->out_hdr[4] = (unsigned char)( len );
2152 }
2153
2154 ssl->out_left = 5 + ssl->out_msglen;
2155
2156 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2157 "version = [%d:%d], msglen = %d",
2158 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2159 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2160
2161 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002162 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 }
2164
Paul Bakker5121ce52009-01-03 21:22:43 +00002165 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2166 {
2167 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2168 return( ret );
2169 }
2170
2171 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2172
2173 return( 0 );
2174}
2175
2176int ssl_read_record( ssl_context *ssl )
2177{
Paul Bakker05ef8352012-05-08 09:17:57 +00002178 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002179
2180 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2181
2182 if( ssl->in_hslen != 0 &&
2183 ssl->in_hslen < ssl->in_msglen )
2184 {
2185 /*
2186 * Get next Handshake message in the current record
2187 */
2188 ssl->in_msglen -= ssl->in_hslen;
2189
Paul Bakker8934a982011-08-05 11:11:53 +00002190 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002191 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002192
2193 ssl->in_hslen = 4;
2194 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2195
2196 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2197 " %d, type = %d, hslen = %d",
2198 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2199
2200 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2201 {
2202 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002203 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 }
2205
2206 if( ssl->in_msglen < ssl->in_hslen )
2207 {
2208 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002209 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002210 }
2211
Paul Bakker4224bc02014-04-08 14:36:50 +02002212 if( ssl->state != SSL_HANDSHAKE_OVER )
2213 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002214
2215 return( 0 );
2216 }
2217
2218 ssl->in_hslen = 0;
2219
2220 /*
2221 * Read the record header and validate it
2222 */
2223 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2224 {
2225 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2226 return( ret );
2227 }
2228
2229 ssl->in_msgtype = ssl->in_hdr[0];
2230 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2231
2232 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2233 "version = [%d:%d], msglen = %d",
2234 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2235 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2236
2237 if( ssl->in_hdr[1] != ssl->major_ver )
2238 {
2239 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002240 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 }
2242
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002243 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002244 {
2245 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002246 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002247 }
2248
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002249 /* Sanity check (outer boundaries) */
2250 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2251 {
2252 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2253 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2254 }
2255
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002257 * Make sure the message length is acceptable for the current transform
2258 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002259 */
Paul Bakker48916f92012-09-16 19:57:18 +00002260 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002262 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
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 }
2268 else
2269 {
Paul Bakker48916f92012-09-16 19:57:18 +00002270 if( ssl->in_msglen < ssl->transform_in->minlen )
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 }
2275
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002276#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002277 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002278 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002279 {
2280 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002281 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002282 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002283#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002284
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002285#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2286 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 /*
2288 * TLS encrypted messages can have up to 256 bytes of padding
2289 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002290 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002291 ssl->in_msglen > ssl->transform_in->minlen +
2292 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002293 {
2294 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002295 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002296 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002297#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002298 }
2299
2300 /*
2301 * Read and optionally decrypt the message contents
2302 */
2303 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2304 {
2305 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2306 return( ret );
2307 }
2308
2309 SSL_DEBUG_BUF( 4, "input record from network",
2310 ssl->in_hdr, 5 + ssl->in_msglen );
2311
Paul Bakker05ef8352012-05-08 09:17:57 +00002312#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002313 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002314 {
2315 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2316
2317 ret = ssl_hw_record_read( ssl );
2318 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2319 {
2320 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002321 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002322 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002323
2324 if( ret == 0 )
2325 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002326 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002327#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002328 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002329 {
2330 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2331 {
Paul Bakker40865c82013-01-31 17:13:13 +01002332#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2333 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2334 {
2335 ssl_send_alert_message( ssl,
2336 SSL_ALERT_LEVEL_FATAL,
2337 SSL_ALERT_MSG_BAD_RECORD_MAC );
2338 }
2339#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002340 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2341 return( ret );
2342 }
2343
2344 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2345 ssl->in_msg, ssl->in_msglen );
2346
2347 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2348 {
2349 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002350 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002351 }
2352 }
2353
Paul Bakker2770fbd2012-07-03 13:30:23 +00002354#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002355 if( ssl->transform_in != NULL &&
2356 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002357 {
2358 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2359 {
2360 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2361 return( ret );
2362 }
2363
2364 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2365 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2366 }
2367#endif /* POLARSSL_ZLIB_SUPPORT */
2368
Paul Bakker0a925182012-04-16 06:46:41 +00002369 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2370 ssl->in_msgtype != SSL_MSG_ALERT &&
2371 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2372 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2373 {
2374 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2375
Paul Bakker48916f92012-09-16 19:57:18 +00002376 if( ( ret = ssl_send_alert_message( ssl,
2377 SSL_ALERT_LEVEL_FATAL,
2378 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002379 {
2380 return( ret );
2381 }
2382
2383 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2384 }
2385
Paul Bakker5121ce52009-01-03 21:22:43 +00002386 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2387 {
2388 ssl->in_hslen = 4;
2389 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2390
2391 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2392 " %d, type = %d, hslen = %d",
2393 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2394
2395 /*
2396 * Additional checks to validate the handshake header
2397 */
2398 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2399 {
2400 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002401 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002402 }
2403
2404 if( ssl->in_msglen < ssl->in_hslen )
2405 {
2406 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002407 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 }
2409
Paul Bakker48916f92012-09-16 19:57:18 +00002410 if( ssl->state != SSL_HANDSHAKE_OVER )
2411 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002412 }
2413
2414 if( ssl->in_msgtype == SSL_MSG_ALERT )
2415 {
2416 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2417 ssl->in_msg[0], ssl->in_msg[1] ) );
2418
2419 /*
2420 * Ignore non-fatal alerts, except close_notify
2421 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002422 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002423 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002424 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2425 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002426 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002427 }
2428
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002429 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2430 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002431 {
2432 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002433 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002434 }
2435 }
2436
2437 ssl->in_left = 0;
2438
2439 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2440
2441 return( 0 );
2442}
2443
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002444int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2445{
2446 int ret;
2447
2448 if( ( ret = ssl_send_alert_message( ssl,
2449 SSL_ALERT_LEVEL_FATAL,
2450 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2451 {
2452 return( ret );
2453 }
2454
2455 return( 0 );
2456}
2457
Paul Bakker0a925182012-04-16 06:46:41 +00002458int ssl_send_alert_message( ssl_context *ssl,
2459 unsigned char level,
2460 unsigned char message )
2461{
2462 int ret;
2463
2464 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2465
2466 ssl->out_msgtype = SSL_MSG_ALERT;
2467 ssl->out_msglen = 2;
2468 ssl->out_msg[0] = level;
2469 ssl->out_msg[1] = message;
2470
2471 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2472 {
2473 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2474 return( ret );
2475 }
2476
2477 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2478
2479 return( 0 );
2480}
2481
Paul Bakker5121ce52009-01-03 21:22:43 +00002482/*
2483 * Handshake functions
2484 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002485#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2486 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2487 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2488 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2489 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2490 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2491 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002492int ssl_write_certificate( ssl_context *ssl )
2493{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002494 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002495
2496 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2497
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002498 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002499 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2500 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002501 {
2502 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2503 ssl->state++;
2504 return( 0 );
2505 }
2506
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002507 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2508 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002509}
2510
2511int ssl_parse_certificate( ssl_context *ssl )
2512{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002513 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2514
2515 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2516
2517 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002518 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2519 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002520 {
2521 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2522 ssl->state++;
2523 return( 0 );
2524 }
2525
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002526 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2527 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002528}
2529#else
2530int ssl_write_certificate( ssl_context *ssl )
2531{
2532 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2533 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002534 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002535 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2536
2537 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2538
2539 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002540 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2541 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002542 {
2543 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2544 ssl->state++;
2545 return( 0 );
2546 }
2547
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002548#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002549 if( ssl->endpoint == SSL_IS_CLIENT )
2550 {
2551 if( ssl->client_auth == 0 )
2552 {
2553 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2554 ssl->state++;
2555 return( 0 );
2556 }
2557
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002558#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002559 /*
2560 * If using SSLv3 and got no cert, send an Alert message
2561 * (otherwise an empty Certificate message will be sent).
2562 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002563 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002564 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2565 {
2566 ssl->out_msglen = 2;
2567 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002568 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2569 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002570
2571 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2572 goto write_msg;
2573 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002574#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002576#endif /* POLARSSL_SSL_CLI_C */
2577#if defined(POLARSSL_SSL_SRV_C)
2578 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00002579 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002580 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002581 {
2582 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002583 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002584 }
2585 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002586#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002587
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002588 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002589
2590 /*
2591 * 0 . 0 handshake type
2592 * 1 . 3 handshake length
2593 * 4 . 6 length of all certs
2594 * 7 . 9 length of cert. 1
2595 * 10 . n-1 peer certificate
2596 * n . n+2 length of cert. 2
2597 * n+3 . ... upper level cert, etc.
2598 */
2599 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002600 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002601
Paul Bakker29087132010-03-21 21:03:34 +00002602 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002603 {
2604 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002605 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002606 {
2607 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2608 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002609 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002610 }
2611
2612 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2613 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2614 ssl->out_msg[i + 2] = (unsigned char)( n );
2615
2616 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2617 i += n; crt = crt->next;
2618 }
2619
2620 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2621 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2622 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2623
2624 ssl->out_msglen = i;
2625 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2626 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2627
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002628#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002629write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002630#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002631
2632 ssl->state++;
2633
2634 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2635 {
2636 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2637 return( ret );
2638 }
2639
2640 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2641
Paul Bakkered27a042013-04-18 22:46:23 +02002642 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002643}
2644
2645int ssl_parse_certificate( ssl_context *ssl )
2646{
Paul Bakkered27a042013-04-18 22:46:23 +02002647 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002648 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002649 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002650
2651 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2652
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002653 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002654 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2655 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002656 {
2657 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2658 ssl->state++;
2659 return( 0 );
2660 }
2661
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002662#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002663 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002664 ( ssl->authmode == SSL_VERIFY_NONE ||
2665 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002666 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002667 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002668 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2669 ssl->state++;
2670 return( 0 );
2671 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002672#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002673
2674 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2675 {
2676 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2677 return( ret );
2678 }
2679
2680 ssl->state++;
2681
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002682#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002683#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002684 /*
2685 * Check if the client sent an empty certificate
2686 */
2687 if( ssl->endpoint == SSL_IS_SERVER &&
2688 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2689 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002690 if( ssl->in_msglen == 2 &&
2691 ssl->in_msgtype == SSL_MSG_ALERT &&
2692 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2693 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002694 {
2695 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2696
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002697 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002698 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2699 return( 0 );
2700 else
Paul Bakker40e46942009-01-03 21:51:57 +00002701 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 }
2703 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002704#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002705
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002706#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2707 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002708 if( ssl->endpoint == SSL_IS_SERVER &&
2709 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2710 {
2711 if( ssl->in_hslen == 7 &&
2712 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2713 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2714 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2715 {
2716 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2717
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002718 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002719 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002720 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002721 else
2722 return( 0 );
2723 }
2724 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002725#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2726 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002727#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002728
2729 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2730 {
2731 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002732 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 }
2734
2735 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2736 {
2737 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002738 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002739 }
2740
2741 /*
2742 * Same message structure as in ssl_write_certificate()
2743 */
2744 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2745
2746 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2747 {
2748 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002749 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002750 }
2751
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002752 /* In case we tried to reuse a session but it failed */
2753 if( ssl->session_negotiate->peer_cert != NULL )
2754 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002755 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002756 polarssl_free( ssl->session_negotiate->peer_cert );
2757 }
2758
Mansour Moufidc531b4a2015-02-15 17:35:38 -05002759 if( ( ssl->session_negotiate->peer_cert = polarssl_malloc(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002760 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002761 {
2762 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002763 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002764 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002765 }
2766
Paul Bakkerb6b09562013-09-18 14:17:41 +02002767 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002768
2769 i = 7;
2770
2771 while( i < ssl->in_hslen )
2772 {
2773 if( ssl->in_msg[i] != 0 )
2774 {
2775 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002776 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002777 }
2778
2779 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2780 | (unsigned int) ssl->in_msg[i + 2];
2781 i += 3;
2782
2783 if( n < 128 || i + n > ssl->in_hslen )
2784 {
2785 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002786 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002787 }
2788
Paul Bakkerddf26b42013-09-18 13:46:23 +02002789 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2790 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002791 if( ret != 0 )
2792 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002793 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002794 return( ret );
2795 }
2796
2797 i += n;
2798 }
2799
Paul Bakker48916f92012-09-16 19:57:18 +00002800 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002801
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002802 /*
2803 * On client, make sure the server cert doesn't change during renego to
2804 * avoid "triple handshake" attack: https://secure-resumption.com/
2805 */
Paul Bakkerf6080b82015-01-13 16:18:23 +01002806#if defined(POLARSSL_SSL_RENEGOTIATION) && defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002807 if( ssl->endpoint == SSL_IS_CLIENT &&
2808 ssl->renegotiation == SSL_RENEGOTIATION )
2809 {
2810 if( ssl->session->peer_cert == NULL )
2811 {
2812 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2813 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2814 }
2815
2816 if( ssl->session->peer_cert->raw.len !=
2817 ssl->session_negotiate->peer_cert->raw.len ||
2818 memcmp( ssl->session->peer_cert->raw.p,
2819 ssl->session_negotiate->peer_cert->raw.p,
2820 ssl->session->peer_cert->raw.len ) != 0 )
2821 {
2822 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2823 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2824 }
2825 }
Paul Bakkerf6080b82015-01-13 16:18:23 +01002826#endif /* POLARSSL_SSL_RENEGOTIATION && POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002827
Paul Bakker5121ce52009-01-03 21:22:43 +00002828 if( ssl->authmode != SSL_VERIFY_NONE )
2829 {
2830 if( ssl->ca_chain == NULL )
2831 {
2832 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002833 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002834 }
2835
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002836 /*
2837 * Main check: verify certificate
2838 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002839 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2840 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2841 &ssl->session_negotiate->verify_result,
2842 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002843
2844 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002845 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002846 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002847 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002848
2849 /*
2850 * Secondary checks: always done, but change 'ret' only if it was 0
2851 */
2852
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002853#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002854 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002855 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002856
2857 /* If certificate uses an EC key, make sure the curve is OK */
2858 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2859 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2860 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002861 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002862 if( ret == 0 )
2863 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002864 }
2865 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002866#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002867
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002868 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2869 ciphersuite_info,
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02002870 ! ssl->endpoint,
2871 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002872 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002873 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002874 if( ret == 0 )
2875 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2876 }
2877
Paul Bakker5121ce52009-01-03 21:22:43 +00002878 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2879 ret = 0;
2880 }
2881
2882 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2883
2884 return( ret );
2885}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002886#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2887 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2888 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2889 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2890 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2891 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2892 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002893
2894int ssl_write_change_cipher_spec( ssl_context *ssl )
2895{
2896 int ret;
2897
2898 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2899
2900 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2901 ssl->out_msglen = 1;
2902 ssl->out_msg[0] = 1;
2903
Paul Bakker5121ce52009-01-03 21:22:43 +00002904 ssl->state++;
2905
2906 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2907 {
2908 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2909 return( ret );
2910 }
2911
2912 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2913
2914 return( 0 );
2915}
2916
2917int ssl_parse_change_cipher_spec( ssl_context *ssl )
2918{
2919 int ret;
2920
2921 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2922
Paul Bakker5121ce52009-01-03 21:22:43 +00002923 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2924 {
2925 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2926 return( ret );
2927 }
2928
2929 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2930 {
2931 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002932 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002933 }
2934
2935 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2936 {
2937 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002938 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002939 }
2940
2941 ssl->state++;
2942
2943 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2944
2945 return( 0 );
2946}
2947
Paul Bakker41c83d32013-03-20 14:39:14 +01002948void ssl_optimize_checksum( ssl_context *ssl,
2949 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002950{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002951 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002952
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002953#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2954 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002955 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002956 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002957 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002958#endif
2959#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2960#if defined(POLARSSL_SHA512_C)
2961 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2962 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2963 else
2964#endif
2965#if defined(POLARSSL_SHA256_C)
2966 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002967 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002968 else
2969#endif
2970#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002971 {
2972 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002973 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002974 }
Paul Bakker380da532012-04-18 16:10:25 +00002975}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002976
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002977static void ssl_update_checksum_start( ssl_context *ssl,
2978 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002979{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002980#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2981 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002982 md5_update( &ssl->handshake->fin_md5 , buf, len );
2983 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002984#endif
2985#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2986#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002987 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002988#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002989#if defined(POLARSSL_SHA512_C)
2990 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002991#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002992#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002993}
2994
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 Bakkerb6c5d2e2013-06-25 16:25:17 +02002997static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2998 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002999{
Paul Bakker48916f92012-09-16 19:57:18 +00003000 md5_update( &ssl->handshake->fin_md5 , buf, len );
3001 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003002}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003003#endif
Paul Bakker380da532012-04-18 16:10:25 +00003004
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003005#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3006#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003007static void ssl_update_checksum_sha256( ssl_context *ssl,
3008 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003009{
Paul Bakker9e36f042013-06-30 14:34:05 +02003010 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003011}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003012#endif
Paul Bakker380da532012-04-18 16:10:25 +00003013
Paul Bakker9e36f042013-06-30 14:34:05 +02003014#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003015static void ssl_update_checksum_sha384( ssl_context *ssl,
3016 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003017{
Paul Bakker9e36f042013-06-30 14:34:05 +02003018 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003019}
Paul Bakker769075d2012-11-24 11:26:46 +01003020#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003021#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003022
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003023#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003024static void ssl_calc_finished_ssl(
3025 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003026{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003027 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003028 md5_context md5;
3029 sha1_context sha1;
3030
Paul Bakker5121ce52009-01-03 21:22:43 +00003031 unsigned char padbuf[48];
3032 unsigned char md5sum[16];
3033 unsigned char sha1sum[20];
3034
Paul Bakker48916f92012-09-16 19:57:18 +00003035 ssl_session *session = ssl->session_negotiate;
3036 if( !session )
3037 session = ssl->session;
3038
Paul Bakker1ef83d62012-04-11 12:09:53 +00003039 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3040
Paul Bakker48916f92012-09-16 19:57:18 +00003041 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3042 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003043
3044 /*
3045 * SSLv3:
3046 * hash =
3047 * MD5( master + pad2 +
3048 * MD5( handshake + sender + master + pad1 ) )
3049 * + SHA1( master + pad2 +
3050 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003051 */
3052
Paul Bakker90995b52013-06-24 19:20:35 +02003053#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003054 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003055 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003056#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003057
Paul Bakker90995b52013-06-24 19:20:35 +02003058#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003059 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003060 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003061#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003062
Paul Bakker3c2122f2013-06-24 19:03:14 +02003063 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3064 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003065
Paul Bakker1ef83d62012-04-11 12:09:53 +00003066 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003067
Paul Bakker3c2122f2013-06-24 19:03:14 +02003068 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003069 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003070 md5_update( &md5, padbuf, 48 );
3071 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003072
Paul Bakker3c2122f2013-06-24 19:03:14 +02003073 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003074 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003075 sha1_update( &sha1, padbuf, 40 );
3076 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003077
Paul Bakker1ef83d62012-04-11 12:09:53 +00003078 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003079
Paul Bakker1ef83d62012-04-11 12:09:53 +00003080 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003081 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003082 md5_update( &md5, padbuf, 48 );
3083 md5_update( &md5, md5sum, 16 );
3084 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003085
Paul Bakker1ef83d62012-04-11 12:09:53 +00003086 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003087 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003088 sha1_update( &sha1, padbuf , 40 );
3089 sha1_update( &sha1, sha1sum, 20 );
3090 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003091
Paul Bakker1ef83d62012-04-11 12:09:53 +00003092 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003093
Paul Bakker5b4af392014-06-26 12:09:34 +02003094 md5_free( &md5 );
3095 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003096
Paul Bakker34617722014-06-13 17:20:13 +02003097 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3098 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3099 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003100
3101 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3102}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003103#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003104
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003105#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003106static void ssl_calc_finished_tls(
3107 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003108{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003109 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003110 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003111 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003112 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003113 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003114
Paul Bakker48916f92012-09-16 19:57:18 +00003115 ssl_session *session = ssl->session_negotiate;
3116 if( !session )
3117 session = ssl->session;
3118
Paul Bakker1ef83d62012-04-11 12:09:53 +00003119 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003120
Paul Bakker48916f92012-09-16 19:57:18 +00003121 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3122 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003123
Paul Bakker1ef83d62012-04-11 12:09:53 +00003124 /*
3125 * TLSv1:
3126 * hash = PRF( master, finished_label,
3127 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3128 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003129
Paul Bakker90995b52013-06-24 19:20:35 +02003130#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003131 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3132 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003133#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003134
Paul Bakker90995b52013-06-24 19:20:35 +02003135#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003136 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3137 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003138#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003139
3140 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003141 ? "client finished"
3142 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003143
3144 md5_finish( &md5, padbuf );
3145 sha1_finish( &sha1, padbuf + 16 );
3146
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003147 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003148 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003149
3150 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3151
Paul Bakker5b4af392014-06-26 12:09:34 +02003152 md5_free( &md5 );
3153 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003154
Paul Bakker34617722014-06-13 17:20:13 +02003155 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003156
3157 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3158}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003159#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003160
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003161#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3162#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003163static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003164 ssl_context *ssl, unsigned char *buf, int from )
3165{
3166 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003167 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003168 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003169 unsigned char padbuf[32];
3170
Paul Bakker48916f92012-09-16 19:57:18 +00003171 ssl_session *session = ssl->session_negotiate;
3172 if( !session )
3173 session = ssl->session;
3174
Paul Bakker380da532012-04-18 16:10:25 +00003175 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003176
Paul Bakker9e36f042013-06-30 14:34:05 +02003177 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003178
3179 /*
3180 * TLSv1.2:
3181 * hash = PRF( master, finished_label,
3182 * Hash( handshake ) )[0.11]
3183 */
3184
Paul Bakker9e36f042013-06-30 14:34:05 +02003185#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003186 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003187 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003188#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003189
3190 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003191 ? "client finished"
3192 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003193
Paul Bakker9e36f042013-06-30 14:34:05 +02003194 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003195
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003196 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003197 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003198
3199 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3200
Paul Bakker5b4af392014-06-26 12:09:34 +02003201 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003202
Paul Bakker34617722014-06-13 17:20:13 +02003203 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003204
3205 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3206}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003207#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003208
Paul Bakker9e36f042013-06-30 14:34:05 +02003209#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003210static void ssl_calc_finished_tls_sha384(
3211 ssl_context *ssl, unsigned char *buf, int from )
3212{
3213 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003214 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003215 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003216 unsigned char padbuf[48];
3217
Paul Bakker48916f92012-09-16 19:57:18 +00003218 ssl_session *session = ssl->session_negotiate;
3219 if( !session )
3220 session = ssl->session;
3221
Paul Bakker380da532012-04-18 16:10:25 +00003222 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003223
Paul Bakker9e36f042013-06-30 14:34:05 +02003224 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003225
3226 /*
3227 * TLSv1.2:
3228 * hash = PRF( master, finished_label,
3229 * Hash( handshake ) )[0.11]
3230 */
3231
Paul Bakker9e36f042013-06-30 14:34:05 +02003232#if !defined(POLARSSL_SHA512_ALT)
3233 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3234 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003235#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003236
3237 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003238 ? "client finished"
3239 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003240
Paul Bakker9e36f042013-06-30 14:34:05 +02003241 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003242
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003243 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003244 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003245
3246 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3247
Paul Bakker5b4af392014-06-26 12:09:34 +02003248 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003249
Paul Bakker34617722014-06-13 17:20:13 +02003250 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003251
3252 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3253}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003254#endif /* POLARSSL_SHA512_C */
3255#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003256
Paul Bakker48916f92012-09-16 19:57:18 +00003257void ssl_handshake_wrapup( ssl_context *ssl )
3258{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003259 int resume = ssl->handshake->resume;
3260
Paul Bakker48916f92012-09-16 19:57:18 +00003261 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3262
3263 /*
3264 * Free our handshake params
3265 */
3266 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003267 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003268 ssl->handshake = NULL;
3269
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003270#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003271 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003272 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003273 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003274 ssl->renego_records_seen = 0;
3275 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003276#endif
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003277
Paul Bakker48916f92012-09-16 19:57:18 +00003278 /*
3279 * Switch in our now active transform context
3280 */
3281 if( ssl->transform )
3282 {
3283 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003284 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003285 }
3286 ssl->transform = ssl->transform_negotiate;
3287 ssl->transform_negotiate = NULL;
3288
Paul Bakker0a597072012-09-25 21:55:46 +00003289 if( ssl->session )
3290 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003291#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3292 /* RFC 7366 3.1: keep the EtM state */
3293 ssl->session_negotiate->encrypt_then_mac =
3294 ssl->session->encrypt_then_mac;
3295#endif
3296
Paul Bakker0a597072012-09-25 21:55:46 +00003297 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003298 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003299 }
3300 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003301 ssl->session_negotiate = NULL;
3302
Paul Bakker0a597072012-09-25 21:55:46 +00003303 /*
3304 * Add cache entry
3305 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003306 if( ssl->f_set_cache != NULL &&
3307 ssl->session->length != 0 &&
3308 resume == 0 )
3309 {
Paul Bakker0a597072012-09-25 21:55:46 +00003310 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3311 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003312 }
Paul Bakker0a597072012-09-25 21:55:46 +00003313
Paul Bakker48916f92012-09-16 19:57:18 +00003314 ssl->state++;
3315
3316 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3317}
3318
Paul Bakker1ef83d62012-04-11 12:09:53 +00003319int ssl_write_finished( ssl_context *ssl )
3320{
3321 int ret, hash_len;
3322
3323 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3324
Paul Bakker92be97b2013-01-02 17:30:03 +01003325 /*
3326 * Set the out_msg pointer to the correct location based on IV length
3327 */
3328 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3329 {
3330 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3331 ssl->transform_negotiate->fixed_ivlen;
3332 }
3333 else
3334 ssl->out_msg = ssl->out_iv;
3335
Paul Bakker48916f92012-09-16 19:57:18 +00003336 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003337
3338 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003339 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3340
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003341#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003342 ssl->verify_data_len = hash_len;
3343 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003344#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003345
Paul Bakker5121ce52009-01-03 21:22:43 +00003346 ssl->out_msglen = 4 + hash_len;
3347 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3348 ssl->out_msg[0] = SSL_HS_FINISHED;
3349
3350 /*
3351 * In case of session resuming, invert the client and server
3352 * ChangeCipherSpec messages order.
3353 */
Paul Bakker0a597072012-09-25 21:55:46 +00003354 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003355 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003356#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003357 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003358 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003359#endif
3360#if defined(POLARSSL_SSL_SRV_C)
3361 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003362 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003363#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003364 }
3365 else
3366 ssl->state++;
3367
Paul Bakker48916f92012-09-16 19:57:18 +00003368 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003369 * Switch to our negotiated transform and session parameters for outbound
3370 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003371 */
3372 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3373 ssl->transform_out = ssl->transform_negotiate;
3374 ssl->session_out = ssl->session_negotiate;
3375 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003376
Paul Bakker07eb38b2012-12-19 14:42:06 +01003377#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003378 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003379 {
3380 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3381 {
3382 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3383 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3384 }
3385 }
3386#endif
3387
Paul Bakker5121ce52009-01-03 21:22:43 +00003388 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3389 {
3390 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3391 return( ret );
3392 }
3393
3394 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3395
3396 return( 0 );
3397}
3398
3399int ssl_parse_finished( ssl_context *ssl )
3400{
Paul Bakker23986e52011-04-24 08:57:21 +00003401 int ret;
3402 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003403 unsigned char buf[36];
3404
3405 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3406
Paul Bakker48916f92012-09-16 19:57:18 +00003407 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003408
Paul Bakker48916f92012-09-16 19:57:18 +00003409 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003410 * Switch to our negotiated transform and session parameters for inbound
3411 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003412 */
3413 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3414 ssl->transform_in = ssl->transform_negotiate;
3415 ssl->session_in = ssl->session_negotiate;
3416 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003417
Paul Bakker92be97b2013-01-02 17:30:03 +01003418 /*
3419 * Set the in_msg pointer to the correct location based on IV length
3420 */
3421 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3422 {
3423 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3424 ssl->transform_negotiate->fixed_ivlen;
3425 }
3426 else
3427 ssl->in_msg = ssl->in_iv;
3428
Paul Bakker07eb38b2012-12-19 14:42:06 +01003429#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003430 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003431 {
3432 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3433 {
3434 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3435 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3436 }
3437 }
3438#endif
3439
Paul Bakker5121ce52009-01-03 21:22:43 +00003440 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3441 {
3442 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3443 return( ret );
3444 }
3445
3446 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3447 {
3448 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003449 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003450 }
3451
Paul Bakker1ef83d62012-04-11 12:09:53 +00003452 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003453 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3454
3455 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3456 ssl->in_hslen != 4 + hash_len )
3457 {
3458 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003459 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003460 }
3461
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003462 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003463 {
3464 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003465 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003466 }
3467
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003468#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003469 ssl->verify_data_len = hash_len;
3470 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003471#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003472
Paul Bakker0a597072012-09-25 21:55:46 +00003473 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003474 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003475#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003476 if( ssl->endpoint == SSL_IS_CLIENT )
3477 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003478#endif
3479#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003480 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003481 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003482#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003483 }
3484 else
3485 ssl->state++;
3486
3487 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3488
3489 return( 0 );
3490}
3491
Paul Bakker968afaa2014-07-09 11:09:24 +02003492static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003493{
3494 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3495
3496#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3497 defined(POLARSSL_SSL_PROTO_TLS1_1)
3498 md5_init( &handshake->fin_md5 );
3499 sha1_init( &handshake->fin_sha1 );
3500 md5_starts( &handshake->fin_md5 );
3501 sha1_starts( &handshake->fin_sha1 );
3502#endif
3503#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3504#if defined(POLARSSL_SHA256_C)
3505 sha256_init( &handshake->fin_sha256 );
3506 sha256_starts( &handshake->fin_sha256, 0 );
3507#endif
3508#if defined(POLARSSL_SHA512_C)
3509 sha512_init( &handshake->fin_sha512 );
3510 sha512_starts( &handshake->fin_sha512, 1 );
3511#endif
3512#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3513
3514 handshake->update_checksum = ssl_update_checksum_start;
3515 handshake->sig_alg = SSL_HASH_SHA1;
3516
3517#if defined(POLARSSL_DHM_C)
3518 dhm_init( &handshake->dhm_ctx );
3519#endif
3520#if defined(POLARSSL_ECDH_C)
3521 ecdh_init( &handshake->ecdh_ctx );
3522#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003523}
3524
3525static void ssl_transform_init( ssl_transform *transform )
3526{
3527 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003528
3529 cipher_init( &transform->cipher_ctx_enc );
3530 cipher_init( &transform->cipher_ctx_dec );
3531
3532 md_init( &transform->md_ctx_enc );
3533 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003534}
3535
3536void ssl_session_init( ssl_session *session )
3537{
3538 memset( session, 0, sizeof(ssl_session) );
3539}
3540
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003541static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003542{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003543 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003544 if( ssl->transform_negotiate )
3545 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003546 if( ssl->session_negotiate )
3547 ssl_session_free( ssl->session_negotiate );
3548 if( ssl->handshake )
3549 ssl_handshake_free( ssl->handshake );
3550
3551 /*
3552 * Either the pointers are now NULL or cleared properly and can be freed.
3553 * Now allocate missing structures.
3554 */
3555 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003556 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003557 ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003558 }
Paul Bakker48916f92012-09-16 19:57:18 +00003559
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003560 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003561 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003562 ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003563 }
Paul Bakker48916f92012-09-16 19:57:18 +00003564
Paul Bakker82788fb2014-10-20 13:59:19 +02003565 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003566 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003567 ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003568 }
Paul Bakker48916f92012-09-16 19:57:18 +00003569
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003570 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003571 if( ssl->handshake == NULL ||
3572 ssl->transform_negotiate == NULL ||
3573 ssl->session_negotiate == NULL )
3574 {
3575 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003576
3577 polarssl_free( ssl->handshake );
3578 polarssl_free( ssl->transform_negotiate );
3579 polarssl_free( ssl->session_negotiate );
3580
3581 ssl->handshake = NULL;
3582 ssl->transform_negotiate = NULL;
3583 ssl->session_negotiate = NULL;
3584
Paul Bakker48916f92012-09-16 19:57:18 +00003585 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3586 }
3587
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003588 /* Initialize structures */
3589 ssl_session_init( ssl->session_negotiate );
3590 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003591 ssl_handshake_params_init( ssl->handshake );
3592
3593#if defined(POLARSSL_X509_CRT_PARSE_C)
3594 ssl->handshake->key_cert = ssl->key_cert;
3595#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003596
Paul Bakker48916f92012-09-16 19:57:18 +00003597 return( 0 );
3598}
3599
Paul Bakker5121ce52009-01-03 21:22:43 +00003600/*
3601 * Initialize an SSL context
3602 */
3603int ssl_init( ssl_context *ssl )
3604{
Paul Bakker48916f92012-09-16 19:57:18 +00003605 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003606 int len = SSL_BUFFER_LEN;
3607
3608 memset( ssl, 0, sizeof( ssl_context ) );
3609
Paul Bakker62f2dee2012-09-28 07:31:51 +00003610 /*
3611 * Sane defaults
3612 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003613 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3614 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3615 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3616 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003617
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003618 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003619
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003620#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003621 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003622 memset( ssl->renego_period, 0xFF, 7 );
3623 ssl->renego_period[7] = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003624#endif
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003625
Paul Bakker62f2dee2012-09-28 07:31:51 +00003626#if defined(POLARSSL_DHM_C)
3627 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
Manuel Pégourié-Gonnardf0f399d2015-07-03 17:45:57 +02003628 POLARSSL_DHM_RFC5114_MODP_2048_P) ) != 0 ||
Paul Bakker62f2dee2012-09-28 07:31:51 +00003629 ( ret = mpi_read_string( &ssl->dhm_G, 16,
Manuel Pégourié-Gonnardf0f399d2015-07-03 17:45:57 +02003630 POLARSSL_DHM_RFC5114_MODP_2048_G) ) != 0 )
Paul Bakker62f2dee2012-09-28 07:31:51 +00003631 {
3632 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3633 return( ret );
3634 }
3635#endif
3636
3637 /*
3638 * Prepare base structures
3639 */
Manuel Pégourié-Gonnardf7db5e02015-02-18 10:32:41 +00003640 if( ( ssl->in_ctr = polarssl_malloc( len ) ) == NULL ||
3641 ( ssl->out_ctr = polarssl_malloc( len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003642 {
3643 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003644 polarssl_free( ssl->in_ctr );
3645 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003646 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003647 }
3648
3649 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3650 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3651
Manuel Pégourié-Gonnardf7db5e02015-02-18 10:32:41 +00003652 ssl->in_hdr = ssl->in_ctr + 8;
3653 ssl->in_iv = ssl->in_ctr + 13;
3654 ssl->in_msg = ssl->in_ctr + 13;
3655
3656 ssl->out_hdr = ssl->out_ctr + 8;
3657 ssl->out_iv = ssl->out_ctr + 13;
3658 ssl->out_msg = ssl->out_ctr + 13;
3659
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003660#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3661 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
3662#endif
3663
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003664#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3665 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3666#endif
3667
Paul Bakker606b4ba2013-08-14 16:52:14 +02003668#if defined(POLARSSL_SSL_SESSION_TICKETS)
3669 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3670#endif
3671
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003672#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003673 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003674#endif
3675
Paul Bakker48916f92012-09-16 19:57:18 +00003676 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3677 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003678
3679 return( 0 );
3680}
3681
3682/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003683 * Reset an initialized and used SSL context for re-use while retaining
3684 * all application-set variables, function pointers and data.
3685 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003686int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003687{
Paul Bakker48916f92012-09-16 19:57:18 +00003688 int ret;
3689
Paul Bakker7eb013f2011-10-06 12:37:39 +00003690 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003691
3692#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003693 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003694 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003695
3696 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard61860192014-11-04 13:05:42 +01003697 memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
3698 memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003699#endif
3700 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00003701
Paul Bakker7eb013f2011-10-06 12:37:39 +00003702 ssl->in_offt = NULL;
3703
Paul Bakker92be97b2013-01-02 17:30:03 +01003704 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003705 ssl->in_msgtype = 0;
3706 ssl->in_msglen = 0;
3707 ssl->in_left = 0;
3708
3709 ssl->in_hslen = 0;
3710 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003711 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003712
Paul Bakker92be97b2013-01-02 17:30:03 +01003713 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003714 ssl->out_msgtype = 0;
3715 ssl->out_msglen = 0;
3716 ssl->out_left = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003717#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01003718 if( ssl->split_done != SSL_CBC_RECORD_SPLITTING_DISABLED )
3719 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003720#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00003721
Paul Bakker48916f92012-09-16 19:57:18 +00003722 ssl->transform_in = NULL;
3723 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003724
3725 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3726 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003727
3728#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003729 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003730 {
3731 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003732 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003733 {
3734 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3735 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3736 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003737 }
3738#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003739
Paul Bakker48916f92012-09-16 19:57:18 +00003740 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003741 {
Paul Bakker48916f92012-09-16 19:57:18 +00003742 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003743 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003744 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003745 }
Paul Bakker48916f92012-09-16 19:57:18 +00003746
Paul Bakkerc0463502013-02-14 11:19:38 +01003747 if( ssl->session )
3748 {
3749 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003750 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003751 ssl->session = NULL;
3752 }
3753
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003754#if defined(POLARSSL_SSL_ALPN)
3755 ssl->alpn_chosen = NULL;
3756#endif
3757
Paul Bakker48916f92012-09-16 19:57:18 +00003758 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3759 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003760
3761 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003762}
3763
Paul Bakkera503a632013-08-14 13:48:06 +02003764#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003765static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3766{
3767 aes_free( &tkeys->enc );
3768 aes_free( &tkeys->dec );
3769
3770 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3771}
3772
Paul Bakker7eb013f2011-10-06 12:37:39 +00003773/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003774 * Allocate and initialize ticket keys
3775 */
3776static int ssl_ticket_keys_init( ssl_context *ssl )
3777{
3778 int ret;
3779 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003780 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003781
3782 if( ssl->ticket_keys != NULL )
3783 return( 0 );
3784
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003785 tkeys = polarssl_malloc( sizeof(ssl_ticket_keys) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003786 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003787 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3788
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003789 aes_init( &tkeys->enc );
3790 aes_init( &tkeys->dec );
3791
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003792 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003793 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003794 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003795 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003796 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003797 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003798
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003799 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3800 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3801 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3802 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003803 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003804 polarssl_free( tkeys );
3805 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003806 }
3807
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003808 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003809 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003810 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003811 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003812 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003813 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003814
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003815 ssl->ticket_keys = tkeys;
3816
3817 return( 0 );
3818}
Paul Bakkera503a632013-08-14 13:48:06 +02003819#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003820
3821/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003822 * SSL set accessors
3823 */
3824void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3825{
3826 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003827
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003828#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
3829 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003830 if( endpoint == SSL_IS_CLIENT )
3831 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003832#endif
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01003833
3834#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
3835 if( endpoint == SSL_IS_SERVER )
3836 ssl->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
3837#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003838}
3839
3840void ssl_set_authmode( ssl_context *ssl, int authmode )
3841{
3842 ssl->authmode = authmode;
3843}
3844
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003845#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003846void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003847 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003848 void *p_vrfy )
3849{
3850 ssl->f_vrfy = f_vrfy;
3851 ssl->p_vrfy = p_vrfy;
3852}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003853#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003854
Paul Bakker5121ce52009-01-03 21:22:43 +00003855void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003856 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003857 void *p_rng )
3858{
3859 ssl->f_rng = f_rng;
3860 ssl->p_rng = p_rng;
3861}
3862
3863void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003864 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003865 void *p_dbg )
3866{
3867 ssl->f_dbg = f_dbg;
3868 ssl->p_dbg = p_dbg;
3869}
3870
3871void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003872 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003873 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003874{
3875 ssl->f_recv = f_recv;
3876 ssl->f_send = f_send;
3877 ssl->p_recv = p_recv;
3878 ssl->p_send = p_send;
3879}
3880
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003881#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00003882void ssl_set_session_cache( ssl_context *ssl,
3883 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3884 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003885{
Paul Bakker0a597072012-09-25 21:55:46 +00003886 ssl->f_get_cache = f_get_cache;
3887 ssl->p_get_cache = p_get_cache;
3888 ssl->f_set_cache = f_set_cache;
3889 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003890}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003891#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003892
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003893#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003894int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003895{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003896 int ret;
3897
3898 if( ssl == NULL ||
3899 session == NULL ||
3900 ssl->session_negotiate == NULL ||
3901 ssl->endpoint != SSL_IS_CLIENT )
3902 {
3903 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3904 }
3905
3906 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3907 return( ret );
3908
Paul Bakker0a597072012-09-25 21:55:46 +00003909 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003910
3911 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003912}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003913#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003914
Paul Bakkerb68cad62012-08-23 08:34:18 +00003915void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003916{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003917 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3918 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3919 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3920 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3921}
3922
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003923void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3924 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003925 int major, int minor )
3926{
3927 if( major != SSL_MAJOR_VERSION_3 )
3928 return;
3929
3930 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3931 return;
3932
3933 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003934}
3935
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003936#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003937/* Add a new (empty) key_cert entry an return a pointer to it */
3938static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3939{
3940 ssl_key_cert *key_cert, *last;
3941
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003942 key_cert = polarssl_malloc( sizeof(ssl_key_cert) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003943 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003944 return( NULL );
3945
3946 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3947
3948 /* Append the new key_cert to the (possibly empty) current list */
3949 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003950 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003951 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003952 if( ssl->handshake != NULL )
3953 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003954 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003955 else
3956 {
3957 last = ssl->key_cert;
3958 while( last->next != NULL )
3959 last = last->next;
3960 last->next = key_cert;
3961 }
3962
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003963 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003964}
3965
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003966void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003967 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003968{
3969 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003970 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003971 ssl->peer_cn = peer_cn;
3972}
3973
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003974int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003975 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003976{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003977 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3978
3979 if( key_cert == NULL )
3980 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3981
3982 key_cert->cert = own_cert;
3983 key_cert->key = pk_key;
3984
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00003985 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003986}
3987
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +01003988#if ! defined(POLARSSL_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003989#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003990int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003991 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003992{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003993 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003994 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003995
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003996 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003997 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3998
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003999 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004000 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004001 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004002
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004003 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004004
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004005 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004006 if( ret != 0 )
4007 return( ret );
4008
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004009 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004010 return( ret );
4011
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004012 key_cert->cert = own_cert;
4013 key_cert->key_own_alloc = 1;
4014
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004015 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004016}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004017#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004018
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004019int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02004020 void *rsa_key,
4021 rsa_decrypt_func rsa_decrypt,
4022 rsa_sign_func rsa_sign,
4023 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00004024{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004025 int ret;
4026 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004027
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004028 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004029 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4030
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004031 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004032 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004033 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004034
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004035 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004036
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004037 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
4038 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
4039 return( ret );
4040
4041 key_cert->cert = own_cert;
4042 key_cert->key_own_alloc = 1;
4043
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004044 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00004045}
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +01004046#endif /* POLARSSL_DEPRECATED_REMOVED */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004047#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004048
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004049#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004050int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
4051 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004052{
Paul Bakker6db455e2013-09-18 17:29:31 +02004053 if( psk == NULL || psk_identity == NULL )
4054 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4055
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02004056 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01004057 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4058
Manuel Pégourié-Gonnard65125542015-08-27 16:37:35 +02004059 /* Identity len will be encoded on two bytes */
4060 if( ( psk_identity_len >> 16 ) != 0 ||
4061 psk_identity_len > SSL_MAX_CONTENT_LEN )
4062 {
4063 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4064 }
4065
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004066 if( ssl->psk != NULL || ssl->psk_identity != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02004067 {
4068 polarssl_free( ssl->psk );
4069 polarssl_free( ssl->psk_identity );
Manuel Pégourié-Gonnard9c521762015-10-27 10:54:53 +01004070 ssl->psk = NULL;
4071 ssl->psk_identity = NULL;
Paul Bakker6db455e2013-09-18 17:29:31 +02004072 }
4073
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004074 if( ( ssl->psk = polarssl_malloc( psk_len ) ) == NULL ||
4075 ( ssl->psk_identity = polarssl_malloc( psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05004076 {
4077 polarssl_free( ssl->psk );
Manuel Pégourié-Gonnard5aff0292015-09-28 18:09:45 +02004078 polarssl_free( ssl->psk_identity );
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004079 ssl->psk = NULL;
Manuel Pégourié-Gonnard5aff0292015-09-28 18:09:45 +02004080 ssl->psk_identity = NULL;
Mansour Moufidf81088b2015-02-17 13:10:21 -05004081 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4082 }
Paul Bakker6db455e2013-09-18 17:29:31 +02004083
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004084 ssl->psk_len = psk_len;
4085 ssl->psk_identity_len = psk_identity_len;
4086
Paul Bakker6db455e2013-09-18 17:29:31 +02004087 memcpy( ssl->psk, psk, ssl->psk_len );
4088 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004089
4090 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004091}
4092
4093void ssl_set_psk_cb( ssl_context *ssl,
4094 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4095 size_t),
4096 void *p_psk )
4097{
4098 ssl->f_psk = f_psk;
4099 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004100}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004101#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004102
Paul Bakker48916f92012-09-16 19:57:18 +00004103#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004104int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004105{
4106 int ret;
4107
Paul Bakker48916f92012-09-16 19:57:18 +00004108 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004109 {
4110 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4111 return( ret );
4112 }
4113
Paul Bakker48916f92012-09-16 19:57:18 +00004114 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004115 {
4116 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4117 return( ret );
4118 }
4119
4120 return( 0 );
4121}
4122
Paul Bakker1b57b062011-01-06 15:48:19 +00004123int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4124{
4125 int ret;
4126
Paul Bakker66d5d072014-06-17 16:39:18 +02004127 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004128 {
4129 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4130 return( ret );
4131 }
4132
Paul Bakker66d5d072014-06-17 16:39:18 +02004133 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004134 {
4135 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4136 return( ret );
4137 }
4138
4139 return( 0 );
4140}
Paul Bakker48916f92012-09-16 19:57:18 +00004141#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004142
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004143#if defined(POLARSSL_SSL_SET_CURVES)
4144/*
4145 * Set the allowed elliptic curves
4146 */
4147void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4148{
4149 ssl->curve_list = curve_list;
4150}
4151#endif
4152
Paul Bakker0be444a2013-08-27 21:55:01 +02004153#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004154int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004155{
4156 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00004157 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004158
4159 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004160
4161 if( ssl->hostname_len + 1 == 0 )
4162 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4163
Simon Butcherc988f322015-09-29 23:27:20 +01004164 if( ssl->hostname_len > SSL_MAX_HOST_NAME_LEN )
4165 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4166
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004167 ssl->hostname = polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004168
Paul Bakkerb15b8512012-01-13 13:44:06 +00004169 if( ssl->hostname == NULL )
4170 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4171
Paul Bakker3c2122f2013-06-24 19:03:14 +02004172 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004173 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004174
Paul Bakker40ea7de2009-05-03 10:18:48 +00004175 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004176
4177 return( 0 );
4178}
4179
Paul Bakker5701cdc2012-09-27 21:49:42 +00004180void ssl_set_sni( ssl_context *ssl,
4181 int (*f_sni)(void *, ssl_context *,
4182 const unsigned char *, size_t),
4183 void *p_sni )
4184{
4185 ssl->f_sni = f_sni;
4186 ssl->p_sni = p_sni;
4187}
Paul Bakker0be444a2013-08-27 21:55:01 +02004188#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004189
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004190#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004191int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004192{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004193 size_t cur_len, tot_len;
4194 const char **p;
4195
4196 /*
4197 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4198 * truncated". Check lengths now rather than later.
4199 */
4200 tot_len = 0;
4201 for( p = protos; *p != NULL; p++ )
4202 {
4203 cur_len = strlen( *p );
4204 tot_len += cur_len;
4205
4206 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4207 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4208 }
4209
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004210 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004211
4212 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004213}
4214
4215const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4216{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004217 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004218}
4219#endif /* POLARSSL_SSL_ALPN */
4220
Paul Bakker490ecc82011-10-06 13:04:09 +00004221void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4222{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004223 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4224 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4225 {
4226 ssl->max_major_ver = major;
4227 ssl->max_minor_ver = minor;
4228 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004229}
4230
Paul Bakker1d29fb52012-09-28 13:28:45 +00004231void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4232{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004233 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4234 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4235 {
4236 ssl->min_major_ver = major;
4237 ssl->min_minor_ver = minor;
4238 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004239}
4240
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004241#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4242void ssl_set_fallback( ssl_context *ssl, char fallback )
4243{
4244 ssl->fallback = fallback;
4245}
4246#endif
4247
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004248#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4249void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
4250{
4251 ssl->encrypt_then_mac = etm;
4252}
4253#endif
4254
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004255#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4256void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4257{
4258 ssl->extended_ms = ems;
4259}
4260#endif
4261
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004262void ssl_set_arc4_support( ssl_context *ssl, char arc4 )
4263{
4264 ssl->arc4_disabled = arc4;
4265}
4266
Paul Bakker05decb22013-08-15 13:33:48 +02004267#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004268int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4269{
Paul Bakker77e257e2013-12-16 15:29:52 +01004270 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004271 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004272 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004273 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004274 }
4275
4276 ssl->mfl_code = mfl_code;
4277
4278 return( 0 );
4279}
Paul Bakker05decb22013-08-15 13:33:48 +02004280#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004281
Paul Bakker1f2bc622013-08-15 13:45:55 +02004282#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004283int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004284{
Paul Bakker8c1ede62013-07-19 14:14:37 +02004285 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004286
4287 return( 0 );
4288}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004289#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004290
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004291#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4292void ssl_set_cbc_record_splitting( ssl_context *ssl, char split )
4293{
4294 ssl->split_done = split;
4295}
4296#endif
4297
Paul Bakker48916f92012-09-16 19:57:18 +00004298void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4299{
4300 ssl->allow_legacy_renegotiation = allow_legacy;
4301}
4302
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004303#if defined(POLARSSL_SSL_RENEGOTIATION)
4304void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4305{
4306 ssl->disable_renegotiation = renegotiation;
4307}
4308
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004309void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4310{
4311 ssl->renego_max_records = max_records;
4312}
4313
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004314void ssl_set_renegotiation_period( ssl_context *ssl,
4315 const unsigned char period[8] )
4316{
4317 memcpy( ssl->renego_period, period, 8 );
4318}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004319#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004320
Paul Bakkera503a632013-08-14 13:48:06 +02004321#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004322int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4323{
4324 ssl->session_tickets = use_tickets;
4325
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004326#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004327 if( ssl->endpoint == SSL_IS_CLIENT )
4328 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004329#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004330
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01004331 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
4332 return( 0 );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004333
4334 if( ssl->f_rng == NULL )
4335 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4336
4337 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004338}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004339
4340void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4341{
4342 ssl->ticket_lifetime = lifetime;
4343}
Paul Bakkera503a632013-08-14 13:48:06 +02004344#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004345
Paul Bakker5121ce52009-01-03 21:22:43 +00004346/*
4347 * SSL get accessors
4348 */
4349size_t ssl_get_bytes_avail( const ssl_context *ssl )
4350{
4351 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4352}
4353
Paul Bakkerff60ee62010-03-16 21:09:09 +00004354int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004355{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00004356 if( ssl->session != NULL )
4357 return( ssl->session->verify_result );
4358
4359 if( ssl->session_negotiate != NULL )
4360 return( ssl->session_negotiate->verify_result );
4361
4362 return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004363}
4364
Paul Bakkere3166ce2011-01-27 17:40:50 +00004365const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004366{
Paul Bakker926c8e42013-03-06 10:23:34 +01004367 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004368 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004369
Paul Bakkere3166ce2011-01-27 17:40:50 +00004370 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004371}
4372
Paul Bakker43ca69c2011-01-15 17:35:19 +00004373const char *ssl_get_version( const ssl_context *ssl )
4374{
4375 switch( ssl->minor_ver )
4376 {
4377 case SSL_MINOR_VERSION_0:
4378 return( "SSLv3.0" );
4379
4380 case SSL_MINOR_VERSION_1:
4381 return( "TLSv1.0" );
4382
4383 case SSL_MINOR_VERSION_2:
4384 return( "TLSv1.1" );
4385
Paul Bakker1ef83d62012-04-11 12:09:53 +00004386 case SSL_MINOR_VERSION_3:
4387 return( "TLSv1.2" );
4388
Paul Bakker43ca69c2011-01-15 17:35:19 +00004389 default:
4390 break;
4391 }
4392 return( "unknown" );
4393}
4394
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004395#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004396const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004397{
4398 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004399 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004400
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004401 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004402}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004403#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004404
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004405#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004406int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4407{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004408 if( ssl == NULL ||
4409 dst == NULL ||
4410 ssl->session == NULL ||
4411 ssl->endpoint != SSL_IS_CLIENT )
4412 {
4413 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4414 }
4415
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004416 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004417}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004418#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004419
Paul Bakker5121ce52009-01-03 21:22:43 +00004420/*
Paul Bakker1961b702013-01-25 14:49:24 +01004421 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004422 */
Paul Bakker1961b702013-01-25 14:49:24 +01004423int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004424{
Paul Bakker40e46942009-01-03 21:51:57 +00004425 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004426
Paul Bakker40e46942009-01-03 21:51:57 +00004427#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004428 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004429 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004430#endif
Paul Bakker40e46942009-01-03 21:51:57 +00004431#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004432 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004433 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004434#endif
4435
Paul Bakker1961b702013-01-25 14:49:24 +01004436 return( ret );
4437}
4438
4439/*
4440 * Perform the SSL handshake
4441 */
4442int ssl_handshake( ssl_context *ssl )
4443{
4444 int ret = 0;
4445
4446 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4447
4448 while( ssl->state != SSL_HANDSHAKE_OVER )
4449 {
4450 ret = ssl_handshake_step( ssl );
4451
4452 if( ret != 0 )
4453 break;
4454 }
4455
Paul Bakker5121ce52009-01-03 21:22:43 +00004456 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4457
4458 return( ret );
4459}
4460
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004461#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004462#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004463/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004464 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004465 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004466static int ssl_write_hello_request( ssl_context *ssl )
4467{
4468 int ret;
4469
4470 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4471
4472 ssl->out_msglen = 4;
4473 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4474 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4475
4476 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4477 {
4478 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4479 return( ret );
4480 }
4481
4482 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4483
4484 return( 0 );
4485}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004486#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004487
4488/*
4489 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004490 * - any side: calling ssl_renegotiate(),
4491 * - client: receiving a HelloRequest during ssl_read(),
4492 * - server: receiving any handshake message on server during ssl_read() after
4493 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004494 * If the handshake doesn't complete due to waiting for I/O, it will continue
4495 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004496 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004497static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004498{
4499 int ret;
4500
4501 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4502
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004503 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4504 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004505
4506 ssl->state = SSL_HELLO_REQUEST;
4507 ssl->renegotiation = SSL_RENEGOTIATION;
4508
Paul Bakker48916f92012-09-16 19:57:18 +00004509 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4510 {
4511 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4512 return( ret );
4513 }
4514
4515 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4516
4517 return( 0 );
4518}
4519
4520/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004521 * Renegotiate current connection on client,
4522 * or request renegotiation on server
4523 */
4524int ssl_renegotiate( ssl_context *ssl )
4525{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004526 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004527
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004528#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004529 /* On server, just send the request */
4530 if( ssl->endpoint == SSL_IS_SERVER )
4531 {
4532 if( ssl->state != SSL_HANDSHAKE_OVER )
4533 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4534
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004535 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4536
4537 /* Did we already try/start sending HelloRequest? */
4538 if( ssl->out_left != 0 )
4539 return( ssl_flush_output( ssl ) );
4540
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004541 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004542 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004543#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004544
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004545#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004546 /*
4547 * On client, either start the renegotiation process or,
4548 * if already in progress, continue the handshake
4549 */
4550 if( ssl->renegotiation != SSL_RENEGOTIATION )
4551 {
4552 if( ssl->state != SSL_HANDSHAKE_OVER )
4553 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4554
4555 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4556 {
4557 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4558 return( ret );
4559 }
4560 }
4561 else
4562 {
4563 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4564 {
4565 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4566 return( ret );
4567 }
4568 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004569#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004570
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004571 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004572}
4573
4574/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004575 * Check record counters and renegotiate if they're above the limit.
4576 */
4577static int ssl_check_ctr_renegotiate( ssl_context *ssl )
4578{
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004579 if( ssl->state != SSL_HANDSHAKE_OVER ||
4580 ssl->renegotiation == SSL_RENEGOTIATION_PENDING ||
4581 ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED )
4582 {
4583 return( 0 );
4584 }
4585
4586 // TODO: adapt for DTLS
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004587 if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 &&
4588 memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004589 {
4590 return( 0 );
4591 }
4592
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004593 SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004594 return( ssl_renegotiate( ssl ) );
4595}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004596#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004597
4598/*
4599 * Receive application data decrypted from the SSL layer
4600 */
Paul Bakker23986e52011-04-24 08:57:21 +00004601int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004602{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004603 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004604 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004605
4606 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4607
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004608#if defined(POLARSSL_SSL_RENEGOTIATION)
4609 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4610 {
4611 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4612 return( ret );
4613 }
4614#endif
4615
Paul Bakker5121ce52009-01-03 21:22:43 +00004616 if( ssl->state != SSL_HANDSHAKE_OVER )
4617 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004618 ret = ssl_handshake( ssl );
4619 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4620 {
4621 record_read = 1;
4622 }
4623 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004624 {
4625 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4626 return( ret );
4627 }
4628 }
4629
4630 if( ssl->in_offt == NULL )
4631 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004632 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004633 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004634 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4635 {
4636 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4637 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004638
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004639 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4640 return( ret );
4641 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004642 }
4643
4644 if( ssl->in_msglen == 0 &&
4645 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4646 {
4647 /*
4648 * OpenSSL sends empty messages to randomize the IV
4649 */
4650 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4651 {
Paul Bakker831a7552011-05-18 13:32:51 +00004652 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4653 return( 0 );
4654
Paul Bakker5121ce52009-01-03 21:22:43 +00004655 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4656 return( ret );
4657 }
4658 }
4659
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004660#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004661 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4662 {
4663 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4664
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004665#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004666 if( ssl->endpoint == SSL_IS_CLIENT &&
4667 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4668 ssl->in_hslen != 4 ) )
4669 {
4670 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4671 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4672 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004673#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004674
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004675 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4676 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004677 ssl->allow_legacy_renegotiation ==
4678 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004679 {
4680 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4681
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004682#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004683 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004684 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004685 /*
4686 * SSLv3 does not have a "no_renegotiation" alert
4687 */
4688 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4689 return( ret );
4690 }
4691 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004692#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004693#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4694 defined(POLARSSL_SSL_PROTO_TLS1_2)
4695 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004696 {
4697 if( ( ret = ssl_send_alert_message( ssl,
4698 SSL_ALERT_LEVEL_WARNING,
4699 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4700 {
4701 return( ret );
4702 }
Paul Bakker48916f92012-09-16 19:57:18 +00004703 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004704 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004705#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4706 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004707 {
4708 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004709 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004710 }
Paul Bakker48916f92012-09-16 19:57:18 +00004711 }
4712 else
4713 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004714 ret = ssl_start_renegotiation( ssl );
4715 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4716 {
4717 record_read = 1;
4718 }
4719 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004720 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004721 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004722 return( ret );
4723 }
Paul Bakker48916f92012-09-16 19:57:18 +00004724 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004725
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004726 /* If a non-handshake record was read during renego, fallthrough,
4727 * else tell the user they should call ssl_read() again */
4728 if( ! record_read )
4729 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004730 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004731 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4732 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004733 ssl->renego_records_seen++;
4734
4735 if( ssl->renego_max_records >= 0 &&
4736 ssl->renego_records_seen > ssl->renego_max_records )
4737 {
4738 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4739 "but not honored by client" ) );
4740 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4741 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004742 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004743#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004744
4745 /* Fatal and closure alerts handled by ssl_read_record() */
4746 if( ssl->in_msgtype == SSL_MSG_ALERT )
4747 {
4748 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4749 return( POLARSSL_ERR_NET_WANT_READ );
4750 }
4751
4752 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004753 {
4754 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004755 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004756 }
4757
4758 ssl->in_offt = ssl->in_msg;
4759 }
4760
4761 n = ( len < ssl->in_msglen )
4762 ? len : ssl->in_msglen;
4763
4764 memcpy( buf, ssl->in_offt, n );
4765 ssl->in_msglen -= n;
4766
4767 if( ssl->in_msglen == 0 )
4768 /* all bytes consumed */
4769 ssl->in_offt = NULL;
4770 else
4771 /* more data available */
4772 ssl->in_offt += n;
4773
4774 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4775
Paul Bakker23986e52011-04-24 08:57:21 +00004776 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004777}
4778
4779/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004780 * Send application data to be encrypted by the SSL layer,
4781 * taking care of max fragment length and buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +00004782 */
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004783static int ssl_write_real( ssl_context *ssl,
4784 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004785{
Paul Bakker23986e52011-04-24 08:57:21 +00004786 int ret;
4787 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004788 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004789
Paul Bakker05decb22013-08-15 13:33:48 +02004790#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004791 /*
4792 * Assume mfl_code is correct since it was checked when set
4793 */
4794 max_len = mfl_code_to_length[ssl->mfl_code];
4795
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004796 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004797 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004798 */
4799 if( ssl->session_out != NULL &&
4800 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4801 {
4802 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4803 }
Paul Bakker05decb22013-08-15 13:33:48 +02004804#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004805
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004806 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004807
Paul Bakker5121ce52009-01-03 21:22:43 +00004808 if( ssl->out_left != 0 )
4809 {
4810 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4811 {
4812 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4813 return( ret );
4814 }
4815 }
Paul Bakker887bd502011-06-08 13:10:54 +00004816 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004817 {
Paul Bakker887bd502011-06-08 13:10:54 +00004818 ssl->out_msglen = n;
4819 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4820 memcpy( ssl->out_msg, buf, n );
4821
4822 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4823 {
4824 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4825 return( ret );
4826 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004827 }
4828
Paul Bakker23986e52011-04-24 08:57:21 +00004829 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004830}
4831
4832/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004833 * Write application data, doing 1/n-1 splitting if necessary.
4834 *
4835 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004836 * then the caller will call us again with the same arguments, so
4837 * remember wether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004838 */
4839#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004840static int ssl_write_split( ssl_context *ssl,
4841 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004842{
4843 int ret;
4844
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004845 if( ssl->split_done == SSL_CBC_RECORD_SPLITTING_DISABLED ||
4846 len <= 1 ||
4847 ssl->minor_ver > SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004848 cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
4849 != POLARSSL_MODE_CBC )
4850 {
4851 return( ssl_write_real( ssl, buf, len ) );
4852 }
4853
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004854 if( ssl->split_done == 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004855 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004856 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004857 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004858 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004859 }
4860
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004861 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
4862 return( ret );
4863 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004864
4865 return( ret + 1 );
4866}
4867#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */
4868
4869/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004870 * Write application data (public-facing wrapper)
4871 */
4872int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
4873{
4874 int ret;
4875
4876 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4877
4878#if defined(POLARSSL_SSL_RENEGOTIATION)
4879 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4880 {
4881 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4882 return( ret );
4883 }
4884#endif
4885
4886 if( ssl->state != SSL_HANDSHAKE_OVER )
4887 {
4888 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4889 {
4890 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4891 return( ret );
4892 }
4893 }
4894
4895#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4896 ret = ssl_write_split( ssl, buf, len );
4897#else
4898 ret = ssl_write_real( ssl, buf, len );
4899#endif
4900
4901 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4902
4903 return( ret );
4904}
4905
4906/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004907 * Notify the peer that the connection is being closed
4908 */
4909int ssl_close_notify( ssl_context *ssl )
4910{
4911 int ret;
4912
4913 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4914
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004915 if( ssl->out_left != 0 )
4916 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004917
4918 if( ssl->state == SSL_HANDSHAKE_OVER )
4919 {
Paul Bakker48916f92012-09-16 19:57:18 +00004920 if( ( ret = ssl_send_alert_message( ssl,
4921 SSL_ALERT_LEVEL_WARNING,
4922 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004923 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004924 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004925 return( ret );
4926 }
4927 }
4928
4929 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4930
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004931 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004932}
4933
Paul Bakker48916f92012-09-16 19:57:18 +00004934void ssl_transform_free( ssl_transform *transform )
4935{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004936 if( transform == NULL )
4937 return;
4938
Paul Bakker48916f92012-09-16 19:57:18 +00004939#if defined(POLARSSL_ZLIB_SUPPORT)
4940 deflateEnd( &transform->ctx_deflate );
4941 inflateEnd( &transform->ctx_inflate );
4942#endif
4943
Paul Bakker84bbeb52014-07-01 14:53:22 +02004944 cipher_free( &transform->cipher_ctx_enc );
4945 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004946
Paul Bakker84bbeb52014-07-01 14:53:22 +02004947 md_free( &transform->md_ctx_enc );
4948 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004949
Paul Bakker34617722014-06-13 17:20:13 +02004950 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004951}
4952
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004953#if defined(POLARSSL_X509_CRT_PARSE_C)
4954static void ssl_key_cert_free( ssl_key_cert *key_cert )
4955{
4956 ssl_key_cert *cur = key_cert, *next;
4957
4958 while( cur != NULL )
4959 {
4960 next = cur->next;
4961
4962 if( cur->key_own_alloc )
4963 {
4964 pk_free( cur->key );
4965 polarssl_free( cur->key );
4966 }
4967 polarssl_free( cur );
4968
4969 cur = next;
4970 }
4971}
4972#endif /* POLARSSL_X509_CRT_PARSE_C */
4973
Paul Bakker48916f92012-09-16 19:57:18 +00004974void ssl_handshake_free( ssl_handshake_params *handshake )
4975{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004976 if( handshake == NULL )
4977 return;
4978
Paul Bakker48916f92012-09-16 19:57:18 +00004979#if defined(POLARSSL_DHM_C)
4980 dhm_free( &handshake->dhm_ctx );
4981#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004982#if defined(POLARSSL_ECDH_C)
4983 ecdh_free( &handshake->ecdh_ctx );
4984#endif
4985
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004986#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004987 /* explicit void pointer cast for buggy MS compiler */
4988 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004989#endif
4990
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004991#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4992 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4993 /*
4994 * Free only the linked list wrapper, not the keys themselves
4995 * since the belong to the SNI callback
4996 */
4997 if( handshake->sni_key_cert != NULL )
4998 {
4999 ssl_key_cert *cur = handshake->sni_key_cert, *next;
5000
5001 while( cur != NULL )
5002 {
5003 next = cur->next;
5004 polarssl_free( cur );
5005 cur = next;
5006 }
5007 }
Paul Bakker9af723c2014-05-01 13:03:14 +02005008#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005009
Paul Bakker34617722014-06-13 17:20:13 +02005010 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005011}
5012
5013void ssl_session_free( ssl_session *session )
5014{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005015 if( session == NULL )
5016 return;
5017
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005018#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00005019 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00005020 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005021 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02005022 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00005023 }
Paul Bakkered27a042013-04-18 22:46:23 +02005024#endif
Paul Bakker0a597072012-09-25 21:55:46 +00005025
Paul Bakkera503a632013-08-14 13:48:06 +02005026#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005027 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02005028#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005029
Paul Bakker34617722014-06-13 17:20:13 +02005030 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005031}
5032
Paul Bakker5121ce52009-01-03 21:22:43 +00005033/*
5034 * Free an SSL context
5035 */
5036void ssl_free( ssl_context *ssl )
5037{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005038 if( ssl == NULL )
5039 return;
5040
Paul Bakker5121ce52009-01-03 21:22:43 +00005041 SSL_DEBUG_MSG( 2, ( "=> free" ) );
5042
Paul Bakker5121ce52009-01-03 21:22:43 +00005043 if( ssl->out_ctr != NULL )
5044 {
Paul Bakker34617722014-06-13 17:20:13 +02005045 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005046 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005047 }
5048
5049 if( ssl->in_ctr != NULL )
5050 {
Paul Bakker34617722014-06-13 17:20:13 +02005051 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005052 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005053 }
5054
Paul Bakker16770332013-10-11 09:59:44 +02005055#if defined(POLARSSL_ZLIB_SUPPORT)
5056 if( ssl->compress_buf != NULL )
5057 {
Paul Bakker34617722014-06-13 17:20:13 +02005058 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02005059 polarssl_free( ssl->compress_buf );
5060 }
5061#endif
5062
Paul Bakker40e46942009-01-03 21:51:57 +00005063#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00005064 mpi_free( &ssl->dhm_P );
5065 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00005066#endif
5067
Paul Bakker48916f92012-09-16 19:57:18 +00005068 if( ssl->transform )
5069 {
5070 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005071 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005072 }
5073
5074 if( ssl->handshake )
5075 {
5076 ssl_handshake_free( ssl->handshake );
5077 ssl_transform_free( ssl->transform_negotiate );
5078 ssl_session_free( ssl->session_negotiate );
5079
Paul Bakker6e339b52013-07-03 13:37:05 +02005080 polarssl_free( ssl->handshake );
5081 polarssl_free( ssl->transform_negotiate );
5082 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00005083 }
5084
Paul Bakkerc0463502013-02-14 11:19:38 +01005085 if( ssl->session )
5086 {
5087 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005088 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005089 }
5090
Paul Bakkera503a632013-08-14 13:48:06 +02005091#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005092 if( ssl->ticket_keys )
5093 {
5094 ssl_ticket_keys_free( ssl->ticket_keys );
5095 polarssl_free( ssl->ticket_keys );
5096 }
Paul Bakkera503a632013-08-14 13:48:06 +02005097#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005098
Paul Bakker0be444a2013-08-27 21:55:01 +02005099#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02005100 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005101 {
Paul Bakker34617722014-06-13 17:20:13 +02005102 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02005103 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00005104 ssl->hostname_len = 0;
5105 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005106#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005107
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005108#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005109 if( ssl->psk != NULL )
5110 {
Paul Bakker34617722014-06-13 17:20:13 +02005111 polarssl_zeroize( ssl->psk, ssl->psk_len );
5112 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005113 polarssl_free( ssl->psk );
5114 polarssl_free( ssl->psk_identity );
5115 ssl->psk_len = 0;
5116 ssl->psk_identity_len = 0;
5117 }
5118#endif
5119
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005120#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005121 ssl_key_cert_free( ssl->key_cert );
5122#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005123
Paul Bakker05ef8352012-05-08 09:17:57 +00005124#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
5125 if( ssl_hw_record_finish != NULL )
5126 {
5127 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
5128 ssl_hw_record_finish( ssl );
5129 }
5130#endif
5131
Paul Bakker5121ce52009-01-03 21:22:43 +00005132 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00005133
Paul Bakker86f04f42013-02-14 11:20:09 +01005134 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02005135 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005136}
5137
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005138#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005139/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005140 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005141 */
5142unsigned char ssl_sig_from_pk( pk_context *pk )
5143{
5144#if defined(POLARSSL_RSA_C)
5145 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
5146 return( SSL_SIG_RSA );
5147#endif
5148#if defined(POLARSSL_ECDSA_C)
5149 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
5150 return( SSL_SIG_ECDSA );
5151#endif
5152 return( SSL_SIG_ANON );
5153}
5154
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005155pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
5156{
5157 switch( sig )
5158 {
5159#if defined(POLARSSL_RSA_C)
5160 case SSL_SIG_RSA:
5161 return( POLARSSL_PK_RSA );
5162#endif
5163#if defined(POLARSSL_ECDSA_C)
5164 case SSL_SIG_ECDSA:
5165 return( POLARSSL_PK_ECDSA );
5166#endif
5167 default:
5168 return( POLARSSL_PK_NONE );
5169 }
5170}
Paul Bakker9af723c2014-05-01 13:03:14 +02005171#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005172
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005173/*
5174 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
5175 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005176md_type_t ssl_md_alg_from_hash( unsigned char hash )
5177{
5178 switch( hash )
5179 {
5180#if defined(POLARSSL_MD5_C)
5181 case SSL_HASH_MD5:
5182 return( POLARSSL_MD_MD5 );
5183#endif
5184#if defined(POLARSSL_SHA1_C)
5185 case SSL_HASH_SHA1:
5186 return( POLARSSL_MD_SHA1 );
5187#endif
5188#if defined(POLARSSL_SHA256_C)
5189 case SSL_HASH_SHA224:
5190 return( POLARSSL_MD_SHA224 );
5191 case SSL_HASH_SHA256:
5192 return( POLARSSL_MD_SHA256 );
5193#endif
5194#if defined(POLARSSL_SHA512_C)
5195 case SSL_HASH_SHA384:
5196 return( POLARSSL_MD_SHA384 );
5197 case SSL_HASH_SHA512:
5198 return( POLARSSL_MD_SHA512 );
5199#endif
5200 default:
5201 return( POLARSSL_MD_NONE );
5202 }
5203}
5204
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005205#if defined(POLARSSL_SSL_SET_CURVES)
5206/*
5207 * Check is a curve proposed by the peer is in our list.
5208 * Return 1 if we're willing to use it, 0 otherwise.
5209 */
5210int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
5211{
5212 const ecp_group_id *gid;
5213
5214 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5215 if( *gid == grp_id )
5216 return( 1 );
5217
5218 return( 0 );
5219}
Paul Bakker9af723c2014-05-01 13:03:14 +02005220#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005221
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005222#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005223int ssl_check_cert_usage( const x509_crt *cert,
5224 const ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005225 int cert_endpoint,
5226 int *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005227{
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005228 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005229#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5230 int usage = 0;
5231#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005232#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5233 const char *ext_oid;
5234 size_t ext_len;
5235#endif
5236
5237#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5238 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5239 ((void) cert);
5240 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005241 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005242#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005243
5244#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5245 if( cert_endpoint == SSL_IS_SERVER )
5246 {
5247 /* Server part of the key exchange */
5248 switch( ciphersuite->key_exchange )
5249 {
5250 case POLARSSL_KEY_EXCHANGE_RSA:
5251 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5252 usage = KU_KEY_ENCIPHERMENT;
5253 break;
5254
5255 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5256 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5257 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5258 usage = KU_DIGITAL_SIGNATURE;
5259 break;
5260
5261 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5262 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5263 usage = KU_KEY_AGREEMENT;
5264 break;
5265
5266 /* Don't use default: we want warnings when adding new values */
5267 case POLARSSL_KEY_EXCHANGE_NONE:
5268 case POLARSSL_KEY_EXCHANGE_PSK:
5269 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5270 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5271 usage = 0;
5272 }
5273 }
5274 else
5275 {
5276 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5277 usage = KU_DIGITAL_SIGNATURE;
5278 }
5279
5280 if( x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005281 {
5282 *flags |= BADCERT_KEY_USAGE;
5283 ret = -1;
5284 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005285#else
5286 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005287#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5288
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005289#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5290 if( cert_endpoint == SSL_IS_SERVER )
5291 {
5292 ext_oid = OID_SERVER_AUTH;
5293 ext_len = OID_SIZE( OID_SERVER_AUTH );
5294 }
5295 else
5296 {
5297 ext_oid = OID_CLIENT_AUTH;
5298 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5299 }
5300
5301 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005302 {
5303 *flags |= BADCERT_EXT_KEY_USAGE;
5304 ret = -1;
5305 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005306#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5307
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005308 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005309}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005310#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005311
5312#endif /* POLARSSL_SSL_TLS_C */