blob: 08b0470c84bdf94fba947d9ed8dc4f2175bdaeed [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
Simon Butcher149950d2016-10-15 22:08:08 +010086#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020087static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
88{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020089 ssl_session_free( dst );
90 memcpy( dst, src, sizeof( ssl_session ) );
91
Paul Bakker7c6b2c32013-09-16 13:49:26 +020092#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020093 if( src->peer_cert != NULL )
94 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020095 int ret;
96
Mansour Moufidc531b4a2015-02-15 17:35:38 -050097 dst->peer_cert = polarssl_malloc( sizeof(x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020098 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020099 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
100
Paul Bakkerb6b09562013-09-18 14:17:41 +0200101 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200102
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200103 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
104 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200105 {
106 polarssl_free( dst->peer_cert );
107 dst->peer_cert = NULL;
108 return( ret );
109 }
110 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200111#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200112
Paul Bakkera503a632013-08-14 13:48:06 +0200113#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200114 if( src->ticket != NULL )
115 {
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500116 dst->ticket = polarssl_malloc( src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200117 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200118 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
119
120 memcpy( dst->ticket, src->ticket, src->ticket_len );
121 }
Paul Bakkera503a632013-08-14 13:48:06 +0200122#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200123
124 return( 0 );
125}
Simon Butcher149950d2016-10-15 22:08:08 +0100126#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200127
Paul Bakker05ef8352012-05-08 09:17:57 +0000128#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200129int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200130 const unsigned char *key_enc, const unsigned char *key_dec,
131 size_t keylen,
132 const unsigned char *iv_enc, const unsigned char *iv_dec,
133 size_t ivlen,
134 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200135 size_t maclen ) = NULL;
136int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
137int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
138int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
139int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
140int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200141#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000142
Paul Bakker5121ce52009-01-03 21:22:43 +0000143/*
144 * Key material generation
145 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200146#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200147static int ssl3_prf( const unsigned char *secret, size_t slen,
148 const char *label,
149 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000150 unsigned char *dstbuf, size_t dlen )
151{
152 size_t i;
153 md5_context md5;
154 sha1_context sha1;
155 unsigned char padding[16];
156 unsigned char sha1sum[20];
157 ((void)label);
158
Paul Bakker5b4af392014-06-26 12:09:34 +0200159 md5_init( &md5 );
160 sha1_init( &sha1 );
161
Paul Bakker5f70b252012-09-13 14:23:06 +0000162 /*
163 * SSLv3:
164 * block =
165 * MD5( secret + SHA1( 'A' + secret + random ) ) +
166 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
167 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
168 * ...
169 */
170 for( i = 0; i < dlen / 16; i++ )
171 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200172 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000173
174 sha1_starts( &sha1 );
175 sha1_update( &sha1, padding, 1 + i );
176 sha1_update( &sha1, secret, slen );
177 sha1_update( &sha1, random, rlen );
178 sha1_finish( &sha1, sha1sum );
179
180 md5_starts( &md5 );
181 md5_update( &md5, secret, slen );
182 md5_update( &md5, sha1sum, 20 );
183 md5_finish( &md5, dstbuf + i * 16 );
184 }
185
Paul Bakker5b4af392014-06-26 12:09:34 +0200186 md5_free( &md5 );
187 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000188
Paul Bakker34617722014-06-13 17:20:13 +0200189 polarssl_zeroize( padding, sizeof( padding ) );
190 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000191
192 return( 0 );
193}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200194#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000195
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200196#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200197static int tls1_prf( const unsigned char *secret, size_t slen,
198 const char *label,
199 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000200 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000201{
Paul Bakker23986e52011-04-24 08:57:21 +0000202 size_t nb, hs;
203 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200204 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 unsigned char tmp[128];
206 unsigned char h_i[20];
207
208 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000209 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
211 hs = ( slen + 1 ) / 2;
212 S1 = secret;
213 S2 = secret + slen - hs;
214
215 nb = strlen( label );
216 memcpy( tmp + 20, label, nb );
217 memcpy( tmp + 20 + nb, random, rlen );
218 nb += rlen;
219
220 /*
221 * First compute P_md5(secret,label+random)[0..dlen]
222 */
223 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
224
225 for( i = 0; i < dlen; i += 16 )
226 {
227 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
228 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
229
230 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
231
232 for( j = 0; j < k; j++ )
233 dstbuf[i + j] = h_i[j];
234 }
235
236 /*
237 * XOR out with P_sha1(secret,label+random)[0..dlen]
238 */
239 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
240
241 for( i = 0; i < dlen; i += 20 )
242 {
243 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
244 sha1_hmac( S2, hs, tmp, 20, tmp );
245
246 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
247
248 for( j = 0; j < k; j++ )
249 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
250 }
251
Paul Bakker34617722014-06-13 17:20:13 +0200252 polarssl_zeroize( tmp, sizeof( tmp ) );
253 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
255 return( 0 );
256}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200257#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000258
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200259#if defined(POLARSSL_SSL_PROTO_TLS1_2)
260#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200261static int tls_prf_sha256( const unsigned char *secret, size_t slen,
262 const char *label,
263 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000264 unsigned char *dstbuf, size_t dlen )
265{
266 size_t nb;
267 size_t i, j, k;
268 unsigned char tmp[128];
269 unsigned char h_i[32];
270
271 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
272 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
273
274 nb = strlen( label );
275 memcpy( tmp + 32, label, nb );
276 memcpy( tmp + 32 + nb, random, rlen );
277 nb += rlen;
278
279 /*
280 * Compute P_<hash>(secret, label + random)[0..dlen]
281 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200282 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000283
284 for( i = 0; i < dlen; i += 32 )
285 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200286 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
287 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000288
289 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
290
291 for( j = 0; j < k; j++ )
292 dstbuf[i + j] = h_i[j];
293 }
294
Paul Bakker34617722014-06-13 17:20:13 +0200295 polarssl_zeroize( tmp, sizeof( tmp ) );
296 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000297
298 return( 0 );
299}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200300#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000301
Paul Bakker9e36f042013-06-30 14:34:05 +0200302#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200303static int tls_prf_sha384( const unsigned char *secret, size_t slen,
304 const char *label,
305 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000306 unsigned char *dstbuf, size_t dlen )
307{
308 size_t nb;
309 size_t i, j, k;
310 unsigned char tmp[128];
311 unsigned char h_i[48];
312
313 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
314 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
315
316 nb = strlen( label );
317 memcpy( tmp + 48, label, nb );
318 memcpy( tmp + 48 + nb, random, rlen );
319 nb += rlen;
320
321 /*
322 * Compute P_<hash>(secret, label + random)[0..dlen]
323 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200324 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000325
326 for( i = 0; i < dlen; i += 48 )
327 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200328 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
329 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000330
331 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
332
333 for( j = 0; j < k; j++ )
334 dstbuf[i + j] = h_i[j];
335 }
336
Paul Bakker34617722014-06-13 17:20:13 +0200337 polarssl_zeroize( tmp, sizeof( tmp ) );
338 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000339
340 return( 0 );
341}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200342#endif /* POLARSSL_SHA512_C */
343#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000344
Paul Bakker66d5d072014-06-17 16:39:18 +0200345static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200346
347#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
348 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200349static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200350#endif
Paul Bakker380da532012-04-18 16:10:25 +0000351
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200353static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
354static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200355#endif
356
357#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200358static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
359static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200360#endif
361
362#if defined(POLARSSL_SSL_PROTO_TLS1_2)
363#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200364static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
365static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
366static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200367#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100368
Paul Bakker9e36f042013-06-30 14:34:05 +0200369#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200370static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
371static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
372static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100373#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200374#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000375
Paul Bakker5121ce52009-01-03 21:22:43 +0000376int ssl_derive_keys( ssl_context *ssl )
377{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200378 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 unsigned char keyblk[256];
381 unsigned char *key1;
382 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100383 unsigned char *mac_enc;
384 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200385 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100386 const cipher_info_t *cipher_info;
387 const md_info_t *md_info;
388
Paul Bakker48916f92012-09-16 19:57:18 +0000389 ssl_session *session = ssl->session_negotiate;
390 ssl_transform *transform = ssl->transform_negotiate;
391 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000392
393 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
394
Paul Bakker68884e32013-01-07 18:20:04 +0100395 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
396 if( cipher_info == NULL )
397 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200398 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100399 transform->ciphersuite_info->cipher ) );
400 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
401 }
402
403 md_info = md_info_from_type( transform->ciphersuite_info->mac );
404 if( md_info == NULL )
405 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200406 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100407 transform->ciphersuite_info->mac ) );
408 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
409 }
410
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000412 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000413 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200414#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000415 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000416 {
Paul Bakker48916f92012-09-16 19:57:18 +0000417 handshake->tls_prf = ssl3_prf;
418 handshake->calc_verify = ssl_calc_verify_ssl;
419 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000420 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200421 else
422#endif
423#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
424 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000425 {
Paul Bakker48916f92012-09-16 19:57:18 +0000426 handshake->tls_prf = tls1_prf;
427 handshake->calc_verify = ssl_calc_verify_tls;
428 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000429 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200430 else
431#endif
432#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200433#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200434 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
435 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000436 {
Paul Bakker48916f92012-09-16 19:57:18 +0000437 handshake->tls_prf = tls_prf_sha384;
438 handshake->calc_verify = ssl_calc_verify_tls_sha384;
439 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000440 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000441 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200442#endif
443#if defined(POLARSSL_SHA256_C)
444 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000445 {
Paul Bakker48916f92012-09-16 19:57:18 +0000446 handshake->tls_prf = tls_prf_sha256;
447 handshake->calc_verify = ssl_calc_verify_tls_sha256;
448 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000449 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200450 else
451#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200452#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200453 {
454 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200455 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200456 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000457
458 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 * SSLv3:
460 * master =
461 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
462 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
463 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200464 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200465 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 * master = PRF( premaster, "master secret", randbytes )[0..47]
467 */
Paul Bakker0a597072012-09-25 21:55:46 +0000468 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 {
Paul Bakker48916f92012-09-16 19:57:18 +0000470 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
471 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200473#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
474 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200475 {
476 unsigned char session_hash[48];
477 size_t hash_len;
478
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200479 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200480
481 ssl->handshake->calc_verify( ssl, session_hash );
482
483#if defined(POLARSSL_SSL_PROTO_TLS1_2)
484 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
485 {
486#if defined(POLARSSL_SHA512_C)
487 if( ssl->transform_negotiate->ciphersuite_info->mac ==
488 POLARSSL_MD_SHA384 )
489 {
490 hash_len = 48;
491 }
492 else
493#endif
494 hash_len = 32;
495 }
496 else
497#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
498 hash_len = 36;
499
500 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
501
502 handshake->tls_prf( handshake->premaster, handshake->pmslen,
503 "extended master secret",
504 session_hash, hash_len, session->master, 48 );
505
506 }
507 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200508#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000509 handshake->tls_prf( handshake->premaster, handshake->pmslen,
510 "master secret",
511 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200513
Paul Bakker34617722014-06-13 17:20:13 +0200514 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 }
516 else
517 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
518
519 /*
520 * Swap the client and server random values.
521 */
Paul Bakker48916f92012-09-16 19:57:18 +0000522 memcpy( tmp, handshake->randbytes, 64 );
523 memcpy( handshake->randbytes, tmp + 32, 32 );
524 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200525 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527 /*
528 * SSLv3:
529 * key block =
530 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
531 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
532 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
533 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
534 * ...
535 *
536 * TLSv1:
537 * key block = PRF( master, "key expansion", randbytes )
538 */
Paul Bakker48916f92012-09-16 19:57:18 +0000539 handshake->tls_prf( session->master, 48, "key expansion",
540 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000541
Paul Bakker48916f92012-09-16 19:57:18 +0000542 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
543 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
544 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
545 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
547
Paul Bakker34617722014-06-13 17:20:13 +0200548 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
550 /*
551 * Determine the appropriate key, IV and MAC length.
552 */
Paul Bakker68884e32013-01-07 18:20:04 +0100553
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200554 transform->keylen = cipher_info->key_length / 8;
555
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200556 if( cipher_info->mode == POLARSSL_MODE_GCM ||
557 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200559 transform->maclen = 0;
560
Paul Bakker68884e32013-01-07 18:20:04 +0100561 transform->ivlen = 12;
562 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200563
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200564 /* Minimum length is expicit IV + tag */
565 transform->minlen = transform->ivlen - transform->fixed_ivlen
566 + ( transform->ciphersuite_info->flags &
567 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100568 }
569 else
570 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200571 /* Initialize HMAC contexts */
572 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
573 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100574 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200575 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
576 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100577 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200579 /* Get MAC length */
580 transform->maclen = md_get_size( md_info );
581
582#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
583 /*
584 * If HMAC is to be truncated, we shall keep the leftmost bytes,
585 * (rfc 6066 page 13 or rfc 2104 section 4),
586 * so we only need to adjust the length here.
587 */
588 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
589 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
590#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
591
592 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100593 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200595 /* Minimum length */
596 if( cipher_info->mode == POLARSSL_MODE_STREAM )
597 transform->minlen = transform->maclen;
598 else
Paul Bakker68884e32013-01-07 18:20:04 +0100599 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200600 /*
601 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100602 * 1. if EtM is in use: one block plus MAC
603 * otherwise: * first multiple of blocklen greater than maclen
604 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200605 */
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100606#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
607 if( session->encrypt_then_mac == SSL_ETM_ENABLED )
608 {
609 transform->minlen = transform->maclen
610 + cipher_info->block_size;
611 }
612 else
613#endif
614 {
615 transform->minlen = transform->maclen
616 + cipher_info->block_size
617 - transform->maclen % cipher_info->block_size;
618 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200619
620#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
621 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
622 ssl->minor_ver == SSL_MINOR_VERSION_1 )
623 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100624 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200625#endif
626#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
627 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
628 ssl->minor_ver == SSL_MINOR_VERSION_3 )
629 {
630 transform->minlen += transform->ivlen;
631 }
632 else
633#endif
634 {
635 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
636 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
637 }
Paul Bakker68884e32013-01-07 18:20:04 +0100638 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 }
640
641 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000642 transform->keylen, transform->minlen, transform->ivlen,
643 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000644
645 /*
646 * Finally setup the cipher contexts, IVs and MAC secrets.
647 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100648#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000649 if( ssl->endpoint == SSL_IS_CLIENT )
650 {
Paul Bakker48916f92012-09-16 19:57:18 +0000651 key1 = keyblk + transform->maclen * 2;
652 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000653
Paul Bakker68884e32013-01-07 18:20:04 +0100654 mac_enc = keyblk;
655 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000656
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000657 /*
658 * This is not used in TLS v1.1.
659 */
Paul Bakker48916f92012-09-16 19:57:18 +0000660 iv_copy_len = ( transform->fixed_ivlen ) ?
661 transform->fixed_ivlen : transform->ivlen;
662 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
663 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000664 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000665 }
666 else
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100667#endif /* POLARSSL_SSL_CLI_C */
668#if defined(POLARSSL_SSL_SRV_C)
669 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000670 {
Paul Bakker48916f92012-09-16 19:57:18 +0000671 key1 = keyblk + transform->maclen * 2 + transform->keylen;
672 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000673
Paul Bakker68884e32013-01-07 18:20:04 +0100674 mac_enc = keyblk + transform->maclen;
675 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000676
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000677 /*
678 * This is not used in TLS v1.1.
679 */
Paul Bakker48916f92012-09-16 19:57:18 +0000680 iv_copy_len = ( transform->fixed_ivlen ) ?
681 transform->fixed_ivlen : transform->ivlen;
682 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
683 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000684 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100686 else
687#endif /* POLARSSL_SSL_SRV_C */
688 {
689 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
690 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
691 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000692
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200693#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100694 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
695 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100696 if( transform->maclen > sizeof transform->mac_enc )
697 {
698 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200699 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100700 }
701
Paul Bakker68884e32013-01-07 18:20:04 +0100702 memcpy( transform->mac_enc, mac_enc, transform->maclen );
703 memcpy( transform->mac_dec, mac_dec, transform->maclen );
704 }
705 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200706#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200707#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
708 defined(POLARSSL_SSL_PROTO_TLS1_2)
709 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100710 {
711 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
712 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
713 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200714 else
715#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200716 {
717 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200718 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200719 }
Paul Bakker68884e32013-01-07 18:20:04 +0100720
Paul Bakker05ef8352012-05-08 09:17:57 +0000721#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200722 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000723 {
724 int ret = 0;
725
726 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
727
Paul Bakker07eb38b2012-12-19 14:42:06 +0100728 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
729 transform->iv_enc, transform->iv_dec,
730 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100731 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100732 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000733 {
734 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200735 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000736 }
737 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200738#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000739
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200740 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
741 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000742 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200743 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
744 return( ret );
745 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200746
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200747 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
748 cipher_info ) ) != 0 )
749 {
750 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
751 return( ret );
752 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200753
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200754 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
755 cipher_info->key_length,
756 POLARSSL_ENCRYPT ) ) != 0 )
757 {
758 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
759 return( ret );
760 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200761
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200762 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
763 cipher_info->key_length,
764 POLARSSL_DECRYPT ) ) != 0 )
765 {
766 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
767 return( ret );
768 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200769
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200770#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200771 if( cipher_info->mode == POLARSSL_MODE_CBC )
772 {
773 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
774 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200775 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200776 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
777 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200778 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200779
780 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
781 POLARSSL_PADDING_NONE ) ) != 0 )
782 {
783 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
784 return( ret );
785 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000786 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200787#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000788
Paul Bakker34617722014-06-13 17:20:13 +0200789 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000790
Paul Bakker2770fbd2012-07-03 13:30:23 +0000791#if defined(POLARSSL_ZLIB_SUPPORT)
792 // Initialize compression
793 //
Paul Bakker48916f92012-09-16 19:57:18 +0000794 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000795 {
Paul Bakker16770332013-10-11 09:59:44 +0200796 if( ssl->compress_buf == NULL )
797 {
798 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
799 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
800 if( ssl->compress_buf == NULL )
801 {
802 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
803 SSL_BUFFER_LEN ) );
804 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
805 }
806 }
807
Paul Bakker2770fbd2012-07-03 13:30:23 +0000808 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
809
Paul Bakker48916f92012-09-16 19:57:18 +0000810 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
811 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000812
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200813 if( deflateInit( &transform->ctx_deflate,
814 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000815 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000816 {
817 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
818 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
819 }
820 }
821#endif /* POLARSSL_ZLIB_SUPPORT */
822
Paul Bakker5121ce52009-01-03 21:22:43 +0000823 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
824
825 return( 0 );
826}
827
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200828#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000829void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000830{
831 md5_context md5;
832 sha1_context sha1;
833 unsigned char pad_1[48];
834 unsigned char pad_2[48];
835
Paul Bakker380da532012-04-18 16:10:25 +0000836 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000837
Paul Bakker48916f92012-09-16 19:57:18 +0000838 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
839 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000840
Paul Bakker380da532012-04-18 16:10:25 +0000841 memset( pad_1, 0x36, 48 );
842 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000843
Paul Bakker48916f92012-09-16 19:57:18 +0000844 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000845 md5_update( &md5, pad_1, 48 );
846 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000847
Paul Bakker380da532012-04-18 16:10:25 +0000848 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000849 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000850 md5_update( &md5, pad_2, 48 );
851 md5_update( &md5, hash, 16 );
852 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000853
Paul Bakker48916f92012-09-16 19:57:18 +0000854 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000855 sha1_update( &sha1, pad_1, 40 );
856 sha1_finish( &sha1, hash + 16 );
857
858 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000859 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000860 sha1_update( &sha1, pad_2, 40 );
861 sha1_update( &sha1, hash + 16, 20 );
862 sha1_finish( &sha1, hash + 16 );
863
864 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
865 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
866
Paul Bakker5b4af392014-06-26 12:09:34 +0200867 md5_free( &md5 );
868 sha1_free( &sha1 );
869
Paul Bakker380da532012-04-18 16:10:25 +0000870 return;
871}
Paul Bakker9af723c2014-05-01 13:03:14 +0200872#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000873
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200874#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000875void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
876{
877 md5_context md5;
878 sha1_context sha1;
879
880 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
881
Paul Bakker48916f92012-09-16 19:57:18 +0000882 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
883 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000884
Paul Bakker48916f92012-09-16 19:57:18 +0000885 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000886 sha1_finish( &sha1, hash + 16 );
887
888 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
889 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
890
Paul Bakker5b4af392014-06-26 12:09:34 +0200891 md5_free( &md5 );
892 sha1_free( &sha1 );
893
Paul Bakker380da532012-04-18 16:10:25 +0000894 return;
895}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200896#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000897
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200898#if defined(POLARSSL_SSL_PROTO_TLS1_2)
899#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000900void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
901{
Paul Bakker9e36f042013-06-30 14:34:05 +0200902 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000903
904 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
905
Paul Bakker9e36f042013-06-30 14:34:05 +0200906 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
907 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000908
909 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
910 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
911
Paul Bakker5b4af392014-06-26 12:09:34 +0200912 sha256_free( &sha256 );
913
Paul Bakker380da532012-04-18 16:10:25 +0000914 return;
915}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200916#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000917
Paul Bakker9e36f042013-06-30 14:34:05 +0200918#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000919void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
920{
Paul Bakker9e36f042013-06-30 14:34:05 +0200921 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000922
923 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
924
Paul Bakker9e36f042013-06-30 14:34:05 +0200925 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
926 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000927
Paul Bakkerca4ab492012-04-18 14:23:57 +0000928 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
930
Paul Bakker5b4af392014-06-26 12:09:34 +0200931 sha512_free( &sha512 );
932
Paul Bakker5121ce52009-01-03 21:22:43 +0000933 return;
934}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200935#endif /* POLARSSL_SHA512_C */
936#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000937
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200938#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200939int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
940{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200941 unsigned char *p = ssl->handshake->premaster;
942 unsigned char *end = p + sizeof( ssl->handshake->premaster );
943
944 /*
945 * PMS = struct {
946 * opaque other_secret<0..2^16-1>;
947 * opaque psk<0..2^16-1>;
948 * };
949 * with "other_secret" depending on the particular key exchange
950 */
951#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
952 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
953 {
Manuel Pégourié-Gonnard5ca36402015-10-21 12:35:29 +0200954 if( end - p < 2 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200955 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
956
957 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
958 *(p++) = (unsigned char)( ssl->psk_len );
Manuel Pégourié-Gonnard5ca36402015-10-21 12:35:29 +0200959
960 if( end < p || (size_t)( end - p ) < ssl->psk_len )
961 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
962
963 memset( p, 0, ssl->psk_len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200964 p += ssl->psk_len;
965 }
966 else
967#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200968#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
969 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
970 {
971 /*
972 * other_secret already set by the ClientKeyExchange message,
973 * and is 48 bytes long
974 */
975 *p++ = 0;
976 *p++ = 48;
977 p += 48;
978 }
979 else
980#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200981#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
982 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
983 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200984 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200985 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200986
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200987 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200988 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200989 p + 2, &len,
990 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200991 {
992 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
993 return( ret );
994 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200995 *(p++) = (unsigned char)( len >> 8 );
996 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200997 p += len;
998
999 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1000 }
1001 else
1002#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1003#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1004 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1005 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001006 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001007 size_t zlen;
1008
1009 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001010 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001011 ssl->f_rng, ssl->p_rng ) ) != 0 )
1012 {
1013 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1014 return( ret );
1015 }
1016
1017 *(p++) = (unsigned char)( zlen >> 8 );
1018 *(p++) = (unsigned char)( zlen );
1019 p += zlen;
1020
1021 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1022 }
1023 else
1024#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1025 {
1026 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001027 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001028 }
1029
1030 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnard5ca36402015-10-21 12:35:29 +02001031 if( end - p < 2 )
1032 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001033
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001034 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1035 *(p++) = (unsigned char)( ssl->psk_len );
Manuel Pégourié-Gonnard5ca36402015-10-21 12:35:29 +02001036
1037 if( end < p || (size_t)( end - p ) < ssl->psk_len )
1038 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1039
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001040 memcpy( p, ssl->psk, ssl->psk_len );
1041 p += ssl->psk_len;
1042
1043 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1044
1045 return( 0 );
1046}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001047#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001048
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001049#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001050/*
1051 * SSLv3.0 MAC functions
1052 */
Paul Bakker68884e32013-01-07 18:20:04 +01001053static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1054 unsigned char *buf, size_t len,
1055 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001056{
1057 unsigned char header[11];
1058 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001059 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001060 int md_size = md_get_size( md_ctx->md_info );
1061 int md_type = md_get_type( md_ctx->md_info );
1062
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001063 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001064 if( md_type == POLARSSL_MD_MD5 )
1065 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001066 else
Paul Bakker68884e32013-01-07 18:20:04 +01001067 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001068
1069 memcpy( header, ctr, 8 );
1070 header[ 8] = (unsigned char) type;
1071 header[ 9] = (unsigned char)( len >> 8 );
1072 header[10] = (unsigned char)( len );
1073
Paul Bakker68884e32013-01-07 18:20:04 +01001074 memset( padding, 0x36, padlen );
1075 md_starts( md_ctx );
1076 md_update( md_ctx, secret, md_size );
1077 md_update( md_ctx, padding, padlen );
1078 md_update( md_ctx, header, 11 );
1079 md_update( md_ctx, buf, len );
1080 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001081
Paul Bakker68884e32013-01-07 18:20:04 +01001082 memset( padding, 0x5C, padlen );
1083 md_starts( md_ctx );
1084 md_update( md_ctx, secret, md_size );
1085 md_update( md_ctx, padding, padlen );
1086 md_update( md_ctx, buf + len, md_size );
1087 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001088}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001089#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001090
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001091#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1092 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1093 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1094#define POLARSSL_SOME_MODES_USE_MAC
1095#endif
1096
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001097/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001098 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001099 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001100static int ssl_encrypt_buf( ssl_context *ssl )
1101{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001102 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001103 cipher_mode_t mode;
1104 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001105
1106 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1107
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001108 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1109 {
1110 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1111 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1112 }
1113
1114 mode = cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
1115
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001116 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1117 ssl->out_msg, ssl->out_msglen );
1118
Paul Bakker5121ce52009-01-03 21:22:43 +00001119 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001120 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001121 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001122#if defined(POLARSSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001123 if( mode == POLARSSL_MODE_STREAM ||
1124 ( mode == POLARSSL_MODE_CBC
1125#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1126 && ssl->session_out->encrypt_then_mac == SSL_ETM_DISABLED
1127#endif
1128 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001129 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001130#if defined(POLARSSL_SSL_PROTO_SSL3)
1131 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1132 {
1133 ssl_mac( &ssl->transform_out->md_ctx_enc,
1134 ssl->transform_out->mac_enc,
1135 ssl->out_msg, ssl->out_msglen,
1136 ssl->out_ctr, ssl->out_msgtype );
1137 }
1138 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001139#endif
1140#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001141 defined(POLARSSL_SSL_PROTO_TLS1_2)
1142 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1143 {
1144 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1145 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1146 ssl->out_msg, ssl->out_msglen );
1147 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1148 ssl->out_msg + ssl->out_msglen );
1149 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1150 }
1151 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001152#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001153 {
1154 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001155 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001156 }
1157
1158 SSL_DEBUG_BUF( 4, "computed mac",
1159 ssl->out_msg + ssl->out_msglen,
1160 ssl->transform_out->maclen );
1161
1162 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001163 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001164 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001165#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001166
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001167 /*
1168 * Encrypt
1169 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001170#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001171 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001172 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001173 int ret;
1174 size_t olen = 0;
1175
Paul Bakker5121ce52009-01-03 21:22:43 +00001176 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1177 "including %d bytes of padding",
1178 ssl->out_msglen, 0 ) );
1179
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001180 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001181 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001182 ssl->transform_out->ivlen,
1183 ssl->out_msg, ssl->out_msglen,
1184 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001185 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001186 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001187 return( ret );
1188 }
1189
1190 if( ssl->out_msglen != olen )
1191 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001192 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001193 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001194 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001195 }
Paul Bakker68884e32013-01-07 18:20:04 +01001196 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001197#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001198#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1199 if( mode == POLARSSL_MODE_GCM ||
1200 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001201 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001202 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001203 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001204 unsigned char *enc_msg;
1205 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001206 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1207 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001208
Paul Bakkerca4ab492012-04-18 14:23:57 +00001209 memcpy( add_data, ssl->out_ctr, 8 );
1210 add_data[8] = ssl->out_msgtype;
1211 add_data[9] = ssl->major_ver;
1212 add_data[10] = ssl->minor_ver;
1213 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1214 add_data[12] = ssl->out_msglen & 0xFF;
1215
1216 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1217 add_data, 13 );
1218
Paul Bakker68884e32013-01-07 18:20:04 +01001219 /*
1220 * Generate IV
1221 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001222 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1223 {
1224 /* Reminder if we ever add an AEAD mode with a different size */
1225 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1226 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1227 }
1228
1229 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1230 ssl->out_ctr, 8 );
1231 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001232
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001233 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001234 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001235
Paul Bakker68884e32013-01-07 18:20:04 +01001236 /*
1237 * Fix pointer positions and message length with added IV
1238 */
1239 enc_msg = ssl->out_msg;
1240 enc_msglen = ssl->out_msglen;
1241 ssl->out_msglen += ssl->transform_out->ivlen -
1242 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001243
Paul Bakker68884e32013-01-07 18:20:04 +01001244 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1245 "including %d bytes of padding",
1246 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001247
Paul Bakker68884e32013-01-07 18:20:04 +01001248 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001249 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001250 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001251 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1252 ssl->transform_out->iv_enc,
1253 ssl->transform_out->ivlen,
1254 add_data, 13,
1255 enc_msg, enc_msglen,
1256 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001257 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001258 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001259 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001260 return( ret );
1261 }
1262
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001263 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001264 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001265 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001266 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001267 }
1268
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001269 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001270 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001271
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001272 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001273 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001274 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001275#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001276#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1277 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001278 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001279 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001280 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001281 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001282 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001283
Paul Bakker48916f92012-09-16 19:57:18 +00001284 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1285 ssl->transform_out->ivlen;
1286 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001287 padlen = 0;
1288
1289 for( i = 0; i <= padlen; i++ )
1290 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1291
1292 ssl->out_msglen += padlen + 1;
1293
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001294 enc_msglen = ssl->out_msglen;
1295 enc_msg = ssl->out_msg;
1296
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001297#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001298 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001299 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1300 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001301 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001302 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001303 {
1304 /*
1305 * Generate IV
1306 */
Manuel Pégourié-Gonnarda67fd792015-08-27 12:02:40 +02001307 ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001308 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001309 if( ret != 0 )
1310 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001311
Paul Bakker92be97b2013-01-02 17:30:03 +01001312 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001313 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001314
1315 /*
1316 * Fix pointer positions and message length with added IV
1317 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001318 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001319 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001320 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001321 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001322#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001323
Paul Bakker5121ce52009-01-03 21:22:43 +00001324 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001325 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001326 ssl->out_msglen, ssl->transform_out->ivlen,
1327 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001329 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001330 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001331 ssl->transform_out->ivlen,
1332 enc_msg, enc_msglen,
1333 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001334 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001335 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001336 return( ret );
1337 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001338
Paul Bakkercca5b812013-08-31 17:40:26 +02001339 if( enc_msglen != olen )
1340 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001341 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001342 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001343 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001344
1345#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001346 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1347 {
1348 /*
1349 * Save IV in SSL3 and TLS1
1350 */
1351 memcpy( ssl->transform_out->iv_enc,
1352 ssl->transform_out->cipher_ctx_enc.iv,
1353 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001355#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001356
1357#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001358 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001359 {
1360 /*
1361 * MAC(MAC_write_key, seq_num +
1362 * TLSCipherText.type +
1363 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001364 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001365 * IV + // except for TLS 1.0
1366 * ENC(content + padding + padding_length));
1367 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001368 unsigned char pseudo_hdr[13];
1369
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001370 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1371
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001372 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1373 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001374 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1375 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1376
1377 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001378
1379 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1380 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1381 ssl->out_iv, ssl->out_msglen );
1382 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1383 ssl->out_iv + ssl->out_msglen );
1384 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1385
1386 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001387 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001388 }
1389#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001390 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001391 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001392#endif /* POLARSSL_CIPHER_MODE_CBC &&
1393 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001394 {
1395 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001396 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001397 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001399 /* Make extra sure authentication was performed, exactly once */
1400 if( auth_done != 1 )
1401 {
1402 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1403 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1404 }
1405
Paul Bakkerca4ab492012-04-18 14:23:57 +00001406 for( i = 8; i > 0; i-- )
1407 if( ++ssl->out_ctr[i - 1] != 0 )
1408 break;
1409
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001410 /* The loops goes to its end iff the counter is wrapping */
1411 if( i == 0 )
1412 {
1413 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1414 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1415 }
1416
Paul Bakker5121ce52009-01-03 21:22:43 +00001417 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1418
1419 return( 0 );
1420}
1421
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001422#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001423
Paul Bakker5121ce52009-01-03 21:22:43 +00001424static int ssl_decrypt_buf( ssl_context *ssl )
1425{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001426 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001427 cipher_mode_t mode;
1428 int auth_done = 0;
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001429#if defined(POLARSSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001430 size_t padlen = 0, correct = 1;
1431#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001432
1433 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1434
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001435 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1436 {
1437 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1438 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1439 }
1440
1441 mode = cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
1442
Paul Bakker48916f92012-09-16 19:57:18 +00001443 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001444 {
1445 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001446 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001447 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001448 }
1449
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001450#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001451 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001452 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001453 int ret;
1454 size_t olen = 0;
1455
Paul Bakker68884e32013-01-07 18:20:04 +01001456 padlen = 0;
1457
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001458 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001459 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001460 ssl->transform_in->ivlen,
1461 ssl->in_msg, ssl->in_msglen,
1462 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001463 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001464 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001465 return( ret );
1466 }
1467
1468 if( ssl->in_msglen != olen )
1469 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001470 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001471 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001472 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001473 }
Paul Bakker68884e32013-01-07 18:20:04 +01001474 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001475#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001476#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1477 if( mode == POLARSSL_MODE_GCM ||
1478 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001479 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001480 int ret;
1481 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001482 unsigned char *dec_msg;
1483 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001484 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001485 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1486 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Simon Ba697bf52016-11-10 13:19:42 +00001487 size_t explicit_iv_len = ssl->transform_in->ivlen -
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001488 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001489
Manuel Pégourié-Gonnard06d75192015-02-11 14:54:11 +00001490 if( ssl->in_msglen < (size_t) explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001491 {
1492 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1493 "+ taglen (%d)", ssl->in_msglen,
1494 explicit_iv_len, taglen ) );
1495 return( POLARSSL_ERR_SSL_INVALID_MAC );
1496 }
1497 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1498
Paul Bakker68884e32013-01-07 18:20:04 +01001499 dec_msg = ssl->in_msg;
1500 dec_msg_result = ssl->in_msg;
1501 ssl->in_msglen = dec_msglen;
1502
1503 memcpy( add_data, ssl->in_ctr, 8 );
1504 add_data[8] = ssl->in_msgtype;
1505 add_data[9] = ssl->major_ver;
1506 add_data[10] = ssl->minor_ver;
1507 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1508 add_data[12] = ssl->in_msglen & 0xFF;
1509
1510 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1511 add_data, 13 );
1512
1513 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1514 ssl->in_iv,
1515 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1516
1517 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1518 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001519 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001520
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001521 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001522 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001523 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001524 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1525 ssl->transform_in->iv_dec,
1526 ssl->transform_in->ivlen,
1527 add_data, 13,
1528 dec_msg, dec_msglen,
1529 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001530 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001531 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001532 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1533
1534 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1535 return( POLARSSL_ERR_SSL_INVALID_MAC );
1536
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001537 return( ret );
1538 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001539 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001540
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001541 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001542 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001543 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001544 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001545 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001546 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001547 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001548#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001549#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1550 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001551 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001552 {
Paul Bakker45829992013-01-03 14:52:21 +01001553 /*
1554 * Decrypt and check the padding
1555 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001556 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001557 unsigned char *dec_msg;
1558 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001559 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001560 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001561 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001562
Paul Bakker5121ce52009-01-03 21:22:43 +00001563 /*
Paul Bakker45829992013-01-03 14:52:21 +01001564 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001565 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001566#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001567 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1568 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001569#endif
Paul Bakker45829992013-01-03 14:52:21 +01001570
1571 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1572 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1573 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001574 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1575 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1576 ssl->transform_in->ivlen,
1577 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001578 return( POLARSSL_ERR_SSL_INVALID_MAC );
1579 }
1580
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001581 dec_msglen = ssl->in_msglen;
1582 dec_msg = ssl->in_msg;
1583 dec_msg_result = ssl->in_msg;
1584
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001585 /*
1586 * Authenticate before decrypt if enabled
1587 */
1588#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001589 if( ssl->session_in->encrypt_then_mac == SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001590 {
1591 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001592 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001593
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001594 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1595
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001596 dec_msglen -= ssl->transform_in->maclen;
1597 ssl->in_msglen -= ssl->transform_in->maclen;
1598
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001599 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1600 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1601 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1602 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1603
1604 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1605
1606 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001607 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1608 ssl->in_iv, ssl->in_msglen );
1609 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1610 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1611
1612 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1613 ssl->transform_in->maclen );
1614 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1615 ssl->transform_in->maclen );
1616
1617 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1618 ssl->transform_in->maclen ) != 0 )
1619 {
1620 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1621
1622 return( POLARSSL_ERR_SSL_INVALID_MAC );
1623 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001624 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001625 }
1626#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1627
1628 /*
1629 * Check length sanity
1630 */
1631 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1632 {
1633 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1634 ssl->in_msglen, ssl->transform_in->ivlen ) );
1635 return( POLARSSL_ERR_SSL_INVALID_MAC );
1636 }
1637
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001638#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001639 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001640 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001641 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001642 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001643 {
Paul Bakker48916f92012-09-16 19:57:18 +00001644 dec_msglen -= ssl->transform_in->ivlen;
1645 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001646
Paul Bakker48916f92012-09-16 19:57:18 +00001647 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001648 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001649 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001650#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001651
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001652 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001653 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001654 ssl->transform_in->ivlen,
1655 dec_msg, dec_msglen,
1656 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001657 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001658 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001659 return( ret );
1660 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001661
Paul Bakkercca5b812013-08-31 17:40:26 +02001662 if( dec_msglen != olen )
1663 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001664 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001665 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001666 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001667
1668#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001669 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1670 {
1671 /*
1672 * Save IV in SSL3 and TLS1
1673 */
1674 memcpy( ssl->transform_in->iv_dec,
1675 ssl->transform_in->cipher_ctx_dec.iv,
1676 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001677 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001678#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001679
1680 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001681
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001682 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001683 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001684 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001685#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001686 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1687 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001688#endif
Paul Bakker45829992013-01-03 14:52:21 +01001689 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001690 correct = 0;
1691 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001692
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001693#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001694 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1695 {
Paul Bakker48916f92012-09-16 19:57:18 +00001696 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001697 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001698#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001699 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1700 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001701 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001702#endif
Paul Bakker45829992013-01-03 14:52:21 +01001703 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001704 }
1705 }
1706 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001707#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001708#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1709 defined(POLARSSL_SSL_PROTO_TLS1_2)
1710 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001711 {
1712 /*
Paul Bakker45829992013-01-03 14:52:21 +01001713 * TLSv1+: always check the padding up to the first failure
1714 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001715 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001716 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001717 size_t padding_idx = ssl->in_msglen - padlen - 1;
1718
Paul Bakker956c9e02013-12-19 14:42:28 +01001719 /*
1720 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001721 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001722 *
Paul Bakker61885c72014-04-25 12:59:03 +02001723 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1724 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001725 *
1726 * In both cases we reset padding_idx to a safe value (0) to
1727 * prevent out-of-buffer reads.
1728 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001729 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001730 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1731 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001732
1733 padding_idx *= correct;
1734
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001735 for( i = 1; i <= 256; i++ )
1736 {
1737 real_count &= ( i <= padlen );
1738 pad_count += real_count *
1739 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1740 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001741
1742 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001743
Paul Bakkerd66f0702013-01-31 16:57:45 +01001744#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001745 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001746 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001747#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001748 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001749 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001750 else
1751#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1752 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001753 {
1754 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001755 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001756 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001757
1758 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001759 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001760 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001761#endif /* POLARSSL_CIPHER_MODE_CBC &&
1762 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +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 );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001766 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001767
1768 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1769 ssl->in_msg, ssl->in_msglen );
1770
1771 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001772 * Authenticate if not done yet.
1773 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001774 */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001775#if defined(POLARSSL_SOME_MODES_USE_MAC)
1776 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001777 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001778 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1779
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001780 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001781
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001782 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1783 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001784
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001785 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001786
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001787#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001788 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1789 {
1790 ssl_mac( &ssl->transform_in->md_ctx_dec,
1791 ssl->transform_in->mac_dec,
1792 ssl->in_msg, ssl->in_msglen,
1793 ssl->in_ctr, ssl->in_msgtype );
1794 }
1795 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001796#endif /* POLARSSL_SSL_PROTO_SSL3 */
1797#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001798 defined(POLARSSL_SSL_PROTO_TLS1_2)
1799 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1800 {
1801 /*
1802 * Process MAC and always update for padlen afterwards to make
1803 * total time independent of padlen
1804 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001805 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001806 *
1807 * Known timing attacks:
1808 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1809 *
1810 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1811 * correctly. (We round down instead of up, so -56 is the correct
1812 * value for our calculations instead of -55)
1813 */
1814 size_t j, extra_run = 0;
1815 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1816 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001817
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001818 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001819
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001820 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1821 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1822 ssl->in_msglen );
1823 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1824 ssl->in_msg + ssl->in_msglen );
Manuel Pégourié-Gonnard7d1e95c2015-04-29 01:35:48 +02001825 /* Call md_process at least once due to cache attacks */
1826 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001827 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001828
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001829 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1830 }
1831 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001832#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001833 POLARSSL_SSL_PROTO_TLS1_2 */
1834 {
1835 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001836 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001837 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001838
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001839 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1840 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1841 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001842
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001843 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001844 ssl->transform_in->maclen ) != 0 )
1845 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001846#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001847 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001848#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001849 correct = 0;
1850 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001851 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001852
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001853 /*
1854 * Finally check the correct flag
1855 */
1856 if( correct == 0 )
1857 return( POLARSSL_ERR_SSL_INVALID_MAC );
1858 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001859#endif /* POLARSSL_SOME_MODES_USE_MAC */
1860
1861 /* Make extra sure authentication was performed, exactly once */
1862 if( auth_done != 1 )
1863 {
1864 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1865 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1866 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001867
1868 if( ssl->in_msglen == 0 )
1869 {
1870 ssl->nb_zero++;
1871
1872 /*
1873 * Three or more empty messages may be a DoS attack
1874 * (excessive CPU consumption).
1875 */
1876 if( ssl->nb_zero > 3 )
1877 {
1878 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1879 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001880 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001881 }
1882 }
1883 else
1884 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001885
Paul Bakker23986e52011-04-24 08:57:21 +00001886 for( i = 8; i > 0; i-- )
1887 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001888 break;
1889
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001890 /* The loops goes to its end iff the counter is wrapping */
1891 if( i == 0 )
1892 {
1893 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1894 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1895 }
1896
Paul Bakker5121ce52009-01-03 21:22:43 +00001897 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1898
1899 return( 0 );
1900}
1901
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001902#undef MAC_NONE
1903#undef MAC_PLAINTEXT
1904#undef MAC_CIPHERTEXT
1905
Paul Bakker2770fbd2012-07-03 13:30:23 +00001906#if defined(POLARSSL_ZLIB_SUPPORT)
1907/*
1908 * Compression/decompression functions
1909 */
1910static int ssl_compress_buf( ssl_context *ssl )
1911{
1912 int ret;
1913 unsigned char *msg_post = ssl->out_msg;
1914 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001915 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001916
1917 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1918
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001919 if( len_pre == 0 )
1920 return( 0 );
1921
Paul Bakker2770fbd2012-07-03 13:30:23 +00001922 memcpy( msg_pre, ssl->out_msg, len_pre );
1923
1924 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1925 ssl->out_msglen ) );
1926
1927 SSL_DEBUG_BUF( 4, "before compression: output payload",
1928 ssl->out_msg, ssl->out_msglen );
1929
Paul Bakker48916f92012-09-16 19:57:18 +00001930 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1931 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1932 ssl->transform_out->ctx_deflate.next_out = msg_post;
1933 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001934
Paul Bakker48916f92012-09-16 19:57:18 +00001935 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001936 if( ret != Z_OK )
1937 {
1938 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1939 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1940 }
1941
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001942 ssl->out_msglen = SSL_BUFFER_LEN -
1943 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001944
Paul Bakker2770fbd2012-07-03 13:30:23 +00001945 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1946 ssl->out_msglen ) );
1947
1948 SSL_DEBUG_BUF( 4, "after compression: output payload",
1949 ssl->out_msg, ssl->out_msglen );
1950
1951 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1952
1953 return( 0 );
1954}
1955
1956static int ssl_decompress_buf( ssl_context *ssl )
1957{
1958 int ret;
1959 unsigned char *msg_post = ssl->in_msg;
1960 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001961 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001962
1963 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1964
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001965 if( len_pre == 0 )
1966 return( 0 );
1967
Paul Bakker2770fbd2012-07-03 13:30:23 +00001968 memcpy( msg_pre, ssl->in_msg, len_pre );
1969
1970 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1971 ssl->in_msglen ) );
1972
1973 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1974 ssl->in_msg, ssl->in_msglen );
1975
Paul Bakker48916f92012-09-16 19:57:18 +00001976 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1977 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1978 ssl->transform_in->ctx_inflate.next_out = msg_post;
1979 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001980
Paul Bakker48916f92012-09-16 19:57:18 +00001981 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001982 if( ret != Z_OK )
1983 {
1984 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1985 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1986 }
1987
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001988 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1989 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001990
Paul Bakker2770fbd2012-07-03 13:30:23 +00001991 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1992 ssl->in_msglen ) );
1993
1994 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1995 ssl->in_msg, ssl->in_msglen );
1996
1997 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1998
1999 return( 0 );
2000}
2001#endif /* POLARSSL_ZLIB_SUPPORT */
2002
Paul Bakker5121ce52009-01-03 21:22:43 +00002003/*
2004 * Fill the input message buffer
2005 */
Paul Bakker23986e52011-04-24 08:57:21 +00002006int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002007{
Paul Bakker23986e52011-04-24 08:57:21 +00002008 int ret;
2009 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002010
2011 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
2012
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002013 if( nb_want > SSL_BUFFER_LEN - 8 )
2014 {
2015 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2016 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2017 }
2018
Paul Bakker5121ce52009-01-03 21:22:43 +00002019 while( ssl->in_left < nb_want )
2020 {
2021 len = nb_want - ssl->in_left;
2022 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
2023
2024 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2025 ssl->in_left, nb_want ) );
2026 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2027
Paul Bakker831a7552011-05-18 13:32:51 +00002028 if( ret == 0 )
2029 return( POLARSSL_ERR_SSL_CONN_EOF );
2030
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 if( ret < 0 )
2032 return( ret );
2033
2034 ssl->in_left += ret;
2035 }
2036
2037 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2038
2039 return( 0 );
2040}
2041
2042/*
2043 * Flush any data not yet written
2044 */
2045int ssl_flush_output( ssl_context *ssl )
2046{
2047 int ret;
2048 unsigned char *buf;
2049
2050 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2051
2052 while( ssl->out_left > 0 )
2053 {
2054 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2055 5 + ssl->out_msglen, ssl->out_left ) );
2056
Paul Bakker5bd42292012-12-19 14:40:42 +01002057 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00002058 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002059
Paul Bakker5121ce52009-01-03 21:22:43 +00002060 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2061
2062 if( ret <= 0 )
2063 return( ret );
2064
2065 ssl->out_left -= ret;
2066 }
2067
2068 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2069
2070 return( 0 );
2071}
2072
2073/*
2074 * Record layer functions
2075 */
2076int ssl_write_record( ssl_context *ssl )
2077{
Paul Bakker05ef8352012-05-08 09:17:57 +00002078 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002079 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002080
2081 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2082
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2084 {
2085 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2086 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2087 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2088
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002089 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2090 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 }
2092
Paul Bakker2770fbd2012-07-03 13:30:23 +00002093#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002094 if( ssl->transform_out != NULL &&
2095 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002096 {
2097 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2098 {
2099 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2100 return( ret );
2101 }
2102
2103 len = ssl->out_msglen;
2104 }
2105#endif /*POLARSSL_ZLIB_SUPPORT */
2106
Paul Bakker05ef8352012-05-08 09:17:57 +00002107#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002108 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002109 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002110 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002111
Paul Bakker05ef8352012-05-08 09:17:57 +00002112 ret = ssl_hw_record_write( ssl );
2113 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2114 {
2115 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002116 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002117 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002118
2119 if( ret == 0 )
2120 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002121 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002122#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002123 if( !done )
2124 {
2125 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2126 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2127 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2129 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002130
Paul Bakker48916f92012-09-16 19:57:18 +00002131 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002132 {
2133 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2134 {
2135 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2136 return( ret );
2137 }
2138
2139 len = ssl->out_msglen;
2140 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2141 ssl->out_hdr[4] = (unsigned char)( len );
2142 }
2143
2144 ssl->out_left = 5 + ssl->out_msglen;
2145
2146 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2147 "version = [%d:%d], msglen = %d",
2148 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2149 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2150
2151 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002152 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002153 }
2154
Paul Bakker5121ce52009-01-03 21:22:43 +00002155 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2156 {
2157 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2158 return( ret );
2159 }
2160
2161 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2162
2163 return( 0 );
2164}
2165
2166int ssl_read_record( ssl_context *ssl )
2167{
Paul Bakker05ef8352012-05-08 09:17:57 +00002168 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002169
2170 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2171
Hanno Becker10699cc2017-06-08 15:41:02 +01002172 if( ssl->keep_current_message == 1 )
2173 {
2174 SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
2175 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2176 ssl->keep_current_message = 0;
2177
2178 return( 0 );
2179 }
2180
Paul Bakker5121ce52009-01-03 21:22:43 +00002181 if( ssl->in_hslen != 0 &&
2182 ssl->in_hslen < ssl->in_msglen )
2183 {
2184 /*
2185 * Get next Handshake message in the current record
2186 */
2187 ssl->in_msglen -= ssl->in_hslen;
2188
Paul Bakker8934a982011-08-05 11:11:53 +00002189 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002190 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002191
2192 ssl->in_hslen = 4;
2193 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2194
2195 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2196 " %d, type = %d, hslen = %d",
2197 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2198
2199 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2200 {
2201 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002202 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002203 }
2204
2205 if( ssl->in_msglen < ssl->in_hslen )
2206 {
2207 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002208 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002209 }
2210
Paul Bakker4224bc02014-04-08 14:36:50 +02002211 if( ssl->state != SSL_HANDSHAKE_OVER )
2212 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002213
2214 return( 0 );
2215 }
2216
2217 ssl->in_hslen = 0;
2218
2219 /*
2220 * Read the record header and validate it
2221 */
Manuel Pégourié-Gonnard0aaefce2015-10-27 13:42:11 +01002222read_record_header:
Paul Bakker5121ce52009-01-03 21:22:43 +00002223 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 /*
Manuel Pégourié-Gonnard0aaefce2015-10-27 13:42:11 +01002420 * Ignore non-fatal alerts, except close_notify and no_renego
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 */
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 }
Manuel Pégourié-Gonnard0aaefce2015-10-27 13:42:11 +01002435
2436 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2437 ssl->in_msg[1] == SSL_ALERT_MSG_NO_RENEGOTIATION )
2438 {
2439 SSL_DEBUG_MSG( 2, ( "is a no_renegotiation" ) );
2440 /* Will be handled when trying to parse ServerHello */
2441 ssl->in_left = 0;
2442 return( 0 );
2443 }
2444
2445 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
2446 ssl->endpoint == SSL_IS_SERVER &&
2447 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2448 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
2449 {
2450 SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
2451 /* Will be handled in ssl_parse_certificate() */
2452 ssl->in_left = 0;
2453 return( 0 );
2454 }
2455
2456 /* Silently discard: fetch new message */
2457 goto read_record_header;
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 }
2459
2460 ssl->in_left = 0;
2461
2462 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2463
2464 return( 0 );
2465}
2466
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002467int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2468{
2469 int ret;
2470
2471 if( ( ret = ssl_send_alert_message( ssl,
2472 SSL_ALERT_LEVEL_FATAL,
2473 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2474 {
2475 return( ret );
2476 }
2477
2478 return( 0 );
2479}
2480
Paul Bakker0a925182012-04-16 06:46:41 +00002481int ssl_send_alert_message( ssl_context *ssl,
2482 unsigned char level,
2483 unsigned char message )
2484{
2485 int ret;
2486
2487 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2488
2489 ssl->out_msgtype = SSL_MSG_ALERT;
2490 ssl->out_msglen = 2;
2491 ssl->out_msg[0] = level;
2492 ssl->out_msg[1] = message;
2493
2494 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2495 {
2496 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2497 return( ret );
2498 }
2499
2500 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2501
2502 return( 0 );
2503}
2504
Paul Bakker5121ce52009-01-03 21:22:43 +00002505/*
2506 * Handshake functions
2507 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002508#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2509 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2510 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2511 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2512 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2513 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2514 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002515int ssl_write_certificate( ssl_context *ssl )
2516{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002517 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002518
2519 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2520
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002521 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002522 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2523 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002524 {
2525 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2526 ssl->state++;
2527 return( 0 );
2528 }
2529
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002530 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2531 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002532}
2533
2534int ssl_parse_certificate( ssl_context *ssl )
2535{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002536 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2537
2538 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2539
2540 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002541 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2542 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002543 {
2544 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2545 ssl->state++;
2546 return( 0 );
2547 }
2548
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002549 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2550 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002551}
2552#else
2553int ssl_write_certificate( ssl_context *ssl )
2554{
2555 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2556 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002557 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002558 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2559
2560 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2561
2562 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002563 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2564 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002565 {
2566 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2567 ssl->state++;
2568 return( 0 );
2569 }
2570
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002571#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002572 if( ssl->endpoint == SSL_IS_CLIENT )
2573 {
2574 if( ssl->client_auth == 0 )
2575 {
2576 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2577 ssl->state++;
2578 return( 0 );
2579 }
2580
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002581#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002582 /*
2583 * If using SSLv3 and got no cert, send an Alert message
2584 * (otherwise an empty Certificate message will be sent).
2585 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002586 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2588 {
2589 ssl->out_msglen = 2;
2590 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002591 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2592 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002593
2594 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2595 goto write_msg;
2596 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002597#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002599#endif /* POLARSSL_SSL_CLI_C */
2600#if defined(POLARSSL_SSL_SRV_C)
2601 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00002602 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002603 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002604 {
2605 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002606 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002607 }
2608 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002609#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002610
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002611 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002612
2613 /*
2614 * 0 . 0 handshake type
2615 * 1 . 3 handshake length
2616 * 4 . 6 length of all certs
2617 * 7 . 9 length of cert. 1
2618 * 10 . n-1 peer certificate
2619 * n . n+2 length of cert. 2
2620 * n+3 . ... upper level cert, etc.
2621 */
2622 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002623 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002624
Paul Bakker29087132010-03-21 21:03:34 +00002625 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002626 {
2627 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002628 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002629 {
2630 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2631 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002632 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002633 }
2634
2635 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2636 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2637 ssl->out_msg[i + 2] = (unsigned char)( n );
2638
2639 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2640 i += n; crt = crt->next;
2641 }
2642
2643 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2644 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2645 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2646
2647 ssl->out_msglen = i;
2648 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2649 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2650
Simon Butcher149950d2016-10-15 22:08:08 +01002651#if defined(POLARSSL_SSL_PROTO_SSL3) && defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002652write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002653#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
2655 ssl->state++;
2656
2657 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2658 {
2659 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2660 return( ret );
2661 }
2662
2663 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2664
Paul Bakkered27a042013-04-18 22:46:23 +02002665 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002666}
2667
2668int ssl_parse_certificate( ssl_context *ssl )
2669{
Paul Bakkered27a042013-04-18 22:46:23 +02002670 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002671 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002672 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002673
2674 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2675
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002676 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002677 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2678 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002679 {
2680 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2681 ssl->state++;
2682 return( 0 );
2683 }
2684
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002685#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002686 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002687 ( ssl->authmode == SSL_VERIFY_NONE ||
2688 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002690 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2692 ssl->state++;
2693 return( 0 );
2694 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002695#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002696
2697 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2698 {
2699 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2700 return( ret );
2701 }
2702
2703 ssl->state++;
2704
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002705#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002706#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002707 /*
2708 * Check if the client sent an empty certificate
2709 */
2710 if( ssl->endpoint == SSL_IS_SERVER &&
2711 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2712 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002713 if( ssl->in_msglen == 2 &&
2714 ssl->in_msgtype == SSL_MSG_ALERT &&
2715 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2716 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002717 {
2718 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2719
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002720 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002721 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2722 return( 0 );
2723 else
Paul Bakker40e46942009-01-03 21:51:57 +00002724 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002725 }
2726 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002727#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002728
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002729#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2730 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002731 if( ssl->endpoint == SSL_IS_SERVER &&
2732 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2733 {
2734 if( ssl->in_hslen == 7 &&
2735 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2736 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2737 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2738 {
2739 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2740
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002741 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002742 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002743 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002744 else
2745 return( 0 );
2746 }
2747 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002748#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2749 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002750#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002751
2752 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2753 {
2754 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002755 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002756 }
2757
2758 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2759 {
2760 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002761 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002762 }
2763
2764 /*
2765 * Same message structure as in ssl_write_certificate()
2766 */
2767 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2768
2769 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2770 {
2771 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002772 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002773 }
2774
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002775 /* In case we tried to reuse a session but it failed */
2776 if( ssl->session_negotiate->peer_cert != NULL )
2777 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002778 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002779 polarssl_free( ssl->session_negotiate->peer_cert );
2780 }
2781
Mansour Moufidc531b4a2015-02-15 17:35:38 -05002782 if( ( ssl->session_negotiate->peer_cert = polarssl_malloc(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002783 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002784 {
2785 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002786 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002787 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002788 }
2789
Paul Bakkerb6b09562013-09-18 14:17:41 +02002790 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002791
2792 i = 7;
2793
2794 while( i < ssl->in_hslen )
2795 {
2796 if( ssl->in_msg[i] != 0 )
2797 {
2798 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002799 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002800 }
2801
2802 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2803 | (unsigned int) ssl->in_msg[i + 2];
2804 i += 3;
2805
2806 if( n < 128 || i + n > ssl->in_hslen )
2807 {
2808 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002809 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002810 }
2811
Paul Bakkerddf26b42013-09-18 13:46:23 +02002812 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2813 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002814 if( ret != 0 )
2815 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002816 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002817 return( ret );
2818 }
2819
2820 i += n;
2821 }
2822
Paul Bakker48916f92012-09-16 19:57:18 +00002823 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002824
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002825 /*
2826 * On client, make sure the server cert doesn't change during renego to
2827 * avoid "triple handshake" attack: https://secure-resumption.com/
2828 */
Paul Bakkerf6080b82015-01-13 16:18:23 +01002829#if defined(POLARSSL_SSL_RENEGOTIATION) && defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002830 if( ssl->endpoint == SSL_IS_CLIENT &&
2831 ssl->renegotiation == SSL_RENEGOTIATION )
2832 {
2833 if( ssl->session->peer_cert == NULL )
2834 {
2835 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2836 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2837 }
2838
2839 if( ssl->session->peer_cert->raw.len !=
2840 ssl->session_negotiate->peer_cert->raw.len ||
2841 memcmp( ssl->session->peer_cert->raw.p,
2842 ssl->session_negotiate->peer_cert->raw.p,
2843 ssl->session->peer_cert->raw.len ) != 0 )
2844 {
2845 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2846 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2847 }
2848 }
Paul Bakkerf6080b82015-01-13 16:18:23 +01002849#endif /* POLARSSL_SSL_RENEGOTIATION && POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002850
Paul Bakker5121ce52009-01-03 21:22:43 +00002851 if( ssl->authmode != SSL_VERIFY_NONE )
2852 {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002853 /*
2854 * Main check: verify certificate
2855 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002856 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2857 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2858 &ssl->session_negotiate->verify_result,
2859 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002860
2861 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002862 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002863 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002864 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002865
2866 /*
2867 * Secondary checks: always done, but change 'ret' only if it was 0
2868 */
2869
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002870#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002871 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002872 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002873
2874 /* If certificate uses an EC key, make sure the curve is OK */
2875 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2876 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2877 {
Hanno Becker888c2fd2017-05-11 11:12:40 +01002878 ssl->session_negotiate->verify_result |= BADCERT_BAD_KEY;
2879
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002880 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002881 if( ret == 0 )
2882 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002883 }
2884 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002885#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002886
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002887 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2888 ciphersuite_info,
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02002889 ! ssl->endpoint,
2890 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002891 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002892 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002893 if( ret == 0 )
2894 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2895 }
2896
Hanno Becker888c2fd2017-05-11 11:12:40 +01002897 /* x509_crt_verify_with_profile is supposed to report a
2898 * verification failure through POLARSSL_ERR_X509_CERT_VERIFY_FAILED,
2899 * with details encoded in the verification flags. All other kinds
2900 * of error codes, including those from the user provided f_vrfy
2901 * functions, are treated as fatal and lead to a failure of
2902 * ssl_parse_certificate even if verification was optional. */
2903 if( ssl->authmode == SSL_VERIFY_OPTIONAL &&
2904 ( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED ||
2905 ret == POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE ) )
2906 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002907 ret = 0;
Hanno Becker888c2fd2017-05-11 11:12:40 +01002908 }
2909
2910 if( ssl->ca_chain == NULL && ssl->authmode == SSL_VERIFY_REQUIRED )
2911 {
2912 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
2913 ret = POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED;
2914 }
2915
2916#if defined(POLARSSL_DEBUG_C)
2917 if( ssl->session_negotiate->verify_result != 0 )
2918 {
2919 SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
2920 ssl->session_negotiate->verify_result ) );
2921 }
2922 else
2923 {
2924 SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
2925 }
2926#endif /* POLARSSL_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002927 }
2928
2929 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2930
2931 return( ret );
2932}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002933#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2934 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2935 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2936 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2937 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2938 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2939 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002940
2941int ssl_write_change_cipher_spec( ssl_context *ssl )
2942{
2943 int ret;
2944
2945 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2946
2947 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2948 ssl->out_msglen = 1;
2949 ssl->out_msg[0] = 1;
2950
Paul Bakker5121ce52009-01-03 21:22:43 +00002951 ssl->state++;
2952
2953 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2954 {
2955 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2956 return( ret );
2957 }
2958
2959 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2960
2961 return( 0 );
2962}
2963
2964int ssl_parse_change_cipher_spec( ssl_context *ssl )
2965{
2966 int ret;
2967
2968 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2969
Paul Bakker5121ce52009-01-03 21:22:43 +00002970 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2971 {
2972 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2973 return( ret );
2974 }
2975
2976 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2977 {
2978 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002979 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002980 }
2981
2982 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2983 {
2984 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002985 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002986 }
2987
2988 ssl->state++;
2989
2990 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2991
2992 return( 0 );
2993}
2994
Paul Bakker41c83d32013-03-20 14:39:14 +01002995void ssl_optimize_checksum( ssl_context *ssl,
2996 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002997{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002998 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002999
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003000#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3001 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00003002 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00003003 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00003004 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003005#endif
3006#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3007#if defined(POLARSSL_SHA512_C)
3008 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
3009 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
3010 else
3011#endif
3012#if defined(POLARSSL_SHA256_C)
3013 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00003014 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003015 else
3016#endif
3017#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003018 {
3019 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003020 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003021 }
Paul Bakker380da532012-04-18 16:10:25 +00003022}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003023
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003024static void ssl_update_checksum_start( ssl_context *ssl,
3025 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003026{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003027#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3028 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003029 md5_update( &ssl->handshake->fin_md5 , buf, len );
3030 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003031#endif
3032#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3033#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003034 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003035#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003036#if defined(POLARSSL_SHA512_C)
3037 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003038#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003039#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003040}
3041
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003042#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3043 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003044static void ssl_update_checksum_md5sha1( ssl_context *ssl,
3045 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003046{
Paul Bakker48916f92012-09-16 19:57:18 +00003047 md5_update( &ssl->handshake->fin_md5 , buf, len );
3048 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003049}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003050#endif
Paul Bakker380da532012-04-18 16:10:25 +00003051
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003052#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3053#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003054static void ssl_update_checksum_sha256( ssl_context *ssl,
3055 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003056{
Paul Bakker9e36f042013-06-30 14:34:05 +02003057 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003058}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003059#endif
Paul Bakker380da532012-04-18 16:10:25 +00003060
Paul Bakker9e36f042013-06-30 14:34:05 +02003061#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003062static void ssl_update_checksum_sha384( ssl_context *ssl,
3063 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003064{
Paul Bakker9e36f042013-06-30 14:34:05 +02003065 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003066}
Paul Bakker769075d2012-11-24 11:26:46 +01003067#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003068#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003069
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003070#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003071static void ssl_calc_finished_ssl(
3072 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003073{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003074 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003075 md5_context md5;
3076 sha1_context sha1;
3077
Paul Bakker5121ce52009-01-03 21:22:43 +00003078 unsigned char padbuf[48];
3079 unsigned char md5sum[16];
3080 unsigned char sha1sum[20];
3081
Paul Bakker48916f92012-09-16 19:57:18 +00003082 ssl_session *session = ssl->session_negotiate;
3083 if( !session )
3084 session = ssl->session;
3085
Paul Bakker1ef83d62012-04-11 12:09:53 +00003086 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3087
Paul Bakker48916f92012-09-16 19:57:18 +00003088 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3089 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003090
3091 /*
3092 * SSLv3:
3093 * hash =
3094 * MD5( master + pad2 +
3095 * MD5( handshake + sender + master + pad1 ) )
3096 * + SHA1( master + pad2 +
3097 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003098 */
3099
Paul Bakker90995b52013-06-24 19:20:35 +02003100#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003101 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003102 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003103#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003104
Paul Bakker90995b52013-06-24 19:20:35 +02003105#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003106 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003107 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003108#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003109
Paul Bakker3c2122f2013-06-24 19:03:14 +02003110 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3111 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003112
Paul Bakker1ef83d62012-04-11 12:09:53 +00003113 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003114
Paul Bakker3c2122f2013-06-24 19:03:14 +02003115 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003116 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003117 md5_update( &md5, padbuf, 48 );
3118 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003119
Paul Bakker3c2122f2013-06-24 19:03:14 +02003120 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003121 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003122 sha1_update( &sha1, padbuf, 40 );
3123 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003124
Paul Bakker1ef83d62012-04-11 12:09:53 +00003125 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003126
Paul Bakker1ef83d62012-04-11 12:09:53 +00003127 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003128 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003129 md5_update( &md5, padbuf, 48 );
3130 md5_update( &md5, md5sum, 16 );
3131 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003132
Paul Bakker1ef83d62012-04-11 12:09:53 +00003133 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003134 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003135 sha1_update( &sha1, padbuf , 40 );
3136 sha1_update( &sha1, sha1sum, 20 );
3137 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003138
Paul Bakker1ef83d62012-04-11 12:09:53 +00003139 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003140
Paul Bakker5b4af392014-06-26 12:09:34 +02003141 md5_free( &md5 );
3142 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003143
Paul Bakker34617722014-06-13 17:20:13 +02003144 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3145 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3146 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003147
3148 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3149}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003150#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003151
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003152#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003153static void ssl_calc_finished_tls(
3154 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003155{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003156 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003157 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003158 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003159 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003160 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003161
Paul Bakker48916f92012-09-16 19:57:18 +00003162 ssl_session *session = ssl->session_negotiate;
3163 if( !session )
3164 session = ssl->session;
3165
Paul Bakker1ef83d62012-04-11 12:09:53 +00003166 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003167
Paul Bakker48916f92012-09-16 19:57:18 +00003168 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3169 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003170
Paul Bakker1ef83d62012-04-11 12:09:53 +00003171 /*
3172 * TLSv1:
3173 * hash = PRF( master, finished_label,
3174 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3175 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003176
Paul Bakker90995b52013-06-24 19:20:35 +02003177#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003178 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3179 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003180#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003181
Paul Bakker90995b52013-06-24 19:20:35 +02003182#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003183 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3184 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003185#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003186
3187 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003188 ? "client finished"
3189 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003190
3191 md5_finish( &md5, padbuf );
3192 sha1_finish( &sha1, padbuf + 16 );
3193
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003194 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003195 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003196
3197 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3198
Paul Bakker5b4af392014-06-26 12:09:34 +02003199 md5_free( &md5 );
3200 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003201
Paul Bakker34617722014-06-13 17:20:13 +02003202 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003203
3204 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3205}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003206#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003207
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003208#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3209#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003210static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003211 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 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003216 unsigned char padbuf[32];
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 sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003223
Paul Bakker9e36f042013-06-30 14:34:05 +02003224 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +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_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003233 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003234 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003235#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003236
3237 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003238 ? "client finished"
3239 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003240
Paul Bakker9e36f042013-06-30 14:34:05 +02003241 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +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, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003245
3246 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3247
Paul Bakker5b4af392014-06-26 12:09:34 +02003248 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003249
Paul Bakker34617722014-06-13 17:20:13 +02003250 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003251
3252 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3253}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003254#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003255
Paul Bakker9e36f042013-06-30 14:34:05 +02003256#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003257static void ssl_calc_finished_tls_sha384(
3258 ssl_context *ssl, unsigned char *buf, int from )
3259{
3260 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003261 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003262 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003263 unsigned char padbuf[48];
3264
Paul Bakker48916f92012-09-16 19:57:18 +00003265 ssl_session *session = ssl->session_negotiate;
3266 if( !session )
3267 session = ssl->session;
3268
Paul Bakker380da532012-04-18 16:10:25 +00003269 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003270
Paul Bakker9e36f042013-06-30 14:34:05 +02003271 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003272
3273 /*
3274 * TLSv1.2:
3275 * hash = PRF( master, finished_label,
3276 * Hash( handshake ) )[0.11]
3277 */
3278
Paul Bakker9e36f042013-06-30 14:34:05 +02003279#if !defined(POLARSSL_SHA512_ALT)
3280 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3281 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003282#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003283
3284 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003285 ? "client finished"
3286 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003287
Paul Bakker9e36f042013-06-30 14:34:05 +02003288 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003289
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003290 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003291 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003292
3293 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3294
Paul Bakker5b4af392014-06-26 12:09:34 +02003295 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003296
Paul Bakker34617722014-06-13 17:20:13 +02003297 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003298
3299 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3300}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003301#endif /* POLARSSL_SHA512_C */
3302#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003303
Paul Bakker48916f92012-09-16 19:57:18 +00003304void ssl_handshake_wrapup( ssl_context *ssl )
3305{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003306 int resume = ssl->handshake->resume;
3307
Paul Bakker48916f92012-09-16 19:57:18 +00003308 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3309
3310 /*
3311 * Free our handshake params
3312 */
3313 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003314 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003315 ssl->handshake = NULL;
3316
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003317#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003318 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003319 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003320 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003321 ssl->renego_records_seen = 0;
3322 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003323#endif
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003324
Paul Bakker48916f92012-09-16 19:57:18 +00003325 /*
3326 * Switch in our now active transform context
3327 */
3328 if( ssl->transform )
3329 {
3330 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003331 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003332 }
3333 ssl->transform = ssl->transform_negotiate;
3334 ssl->transform_negotiate = NULL;
3335
Paul Bakker0a597072012-09-25 21:55:46 +00003336 if( ssl->session )
3337 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003338#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3339 /* RFC 7366 3.1: keep the EtM state */
3340 ssl->session_negotiate->encrypt_then_mac =
3341 ssl->session->encrypt_then_mac;
3342#endif
3343
Paul Bakker0a597072012-09-25 21:55:46 +00003344 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003345 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003346 }
3347 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003348 ssl->session_negotiate = NULL;
3349
Paul Bakker0a597072012-09-25 21:55:46 +00003350 /*
3351 * Add cache entry
3352 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003353 if( ssl->f_set_cache != NULL &&
3354 ssl->session->length != 0 &&
3355 resume == 0 )
3356 {
Paul Bakker0a597072012-09-25 21:55:46 +00003357 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3358 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003359 }
Paul Bakker0a597072012-09-25 21:55:46 +00003360
Paul Bakker48916f92012-09-16 19:57:18 +00003361 ssl->state++;
3362
3363 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3364}
3365
Paul Bakker1ef83d62012-04-11 12:09:53 +00003366int ssl_write_finished( ssl_context *ssl )
3367{
3368 int ret, hash_len;
3369
3370 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3371
Paul Bakker92be97b2013-01-02 17:30:03 +01003372 /*
3373 * Set the out_msg pointer to the correct location based on IV length
3374 */
3375 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3376 {
3377 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3378 ssl->transform_negotiate->fixed_ivlen;
3379 }
3380 else
3381 ssl->out_msg = ssl->out_iv;
3382
Paul Bakker48916f92012-09-16 19:57:18 +00003383 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003384
3385 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003386 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3387
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003388#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003389 ssl->verify_data_len = hash_len;
3390 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003391#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003392
Paul Bakker5121ce52009-01-03 21:22:43 +00003393 ssl->out_msglen = 4 + hash_len;
3394 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3395 ssl->out_msg[0] = SSL_HS_FINISHED;
3396
3397 /*
3398 * In case of session resuming, invert the client and server
3399 * ChangeCipherSpec messages order.
3400 */
Paul Bakker0a597072012-09-25 21:55:46 +00003401 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003402 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003403#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003404 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003405 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003406#endif
3407#if defined(POLARSSL_SSL_SRV_C)
3408 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003409 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003410#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003411 }
3412 else
3413 ssl->state++;
3414
Paul Bakker48916f92012-09-16 19:57:18 +00003415 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003416 * Switch to our negotiated transform and session parameters for outbound
3417 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003418 */
3419 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3420 ssl->transform_out = ssl->transform_negotiate;
3421 ssl->session_out = ssl->session_negotiate;
3422 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003423
Paul Bakker07eb38b2012-12-19 14:42:06 +01003424#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003425 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003426 {
3427 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3428 {
3429 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3430 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3431 }
3432 }
3433#endif
3434
Paul Bakker5121ce52009-01-03 21:22:43 +00003435 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3436 {
3437 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3438 return( ret );
3439 }
3440
3441 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3442
3443 return( 0 );
3444}
3445
3446int ssl_parse_finished( ssl_context *ssl )
3447{
Paul Bakker23986e52011-04-24 08:57:21 +00003448 int ret;
3449 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003450 unsigned char buf[36];
3451
3452 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3453
Paul Bakker48916f92012-09-16 19:57:18 +00003454 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003455
Paul Bakker48916f92012-09-16 19:57:18 +00003456 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003457 * Switch to our negotiated transform and session parameters for inbound
3458 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003459 */
3460 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3461 ssl->transform_in = ssl->transform_negotiate;
3462 ssl->session_in = ssl->session_negotiate;
3463 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003464
Paul Bakker92be97b2013-01-02 17:30:03 +01003465 /*
3466 * Set the in_msg pointer to the correct location based on IV length
3467 */
3468 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3469 {
3470 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3471 ssl->transform_negotiate->fixed_ivlen;
3472 }
3473 else
3474 ssl->in_msg = ssl->in_iv;
3475
Paul Bakker07eb38b2012-12-19 14:42:06 +01003476#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003477 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003478 {
3479 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3480 {
3481 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3482 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3483 }
3484 }
3485#endif
3486
Paul Bakker5121ce52009-01-03 21:22:43 +00003487 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3488 {
3489 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3490 return( ret );
3491 }
3492
3493 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3494 {
3495 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003496 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003497 }
3498
Paul Bakker1ef83d62012-04-11 12:09:53 +00003499 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003500 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3501
3502 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3503 ssl->in_hslen != 4 + hash_len )
3504 {
3505 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003506 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003507 }
3508
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003509 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003510 {
3511 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003512 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003513 }
3514
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003515#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003516 ssl->verify_data_len = hash_len;
3517 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003518#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003519
Paul Bakker0a597072012-09-25 21:55:46 +00003520 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003521 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003522#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003523 if( ssl->endpoint == SSL_IS_CLIENT )
3524 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003525#endif
3526#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003527 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003528 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003529#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003530 }
3531 else
3532 ssl->state++;
3533
3534 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3535
3536 return( 0 );
3537}
3538
Paul Bakker968afaa2014-07-09 11:09:24 +02003539static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003540{
3541 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3542
3543#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3544 defined(POLARSSL_SSL_PROTO_TLS1_1)
3545 md5_init( &handshake->fin_md5 );
3546 sha1_init( &handshake->fin_sha1 );
3547 md5_starts( &handshake->fin_md5 );
3548 sha1_starts( &handshake->fin_sha1 );
3549#endif
3550#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3551#if defined(POLARSSL_SHA256_C)
3552 sha256_init( &handshake->fin_sha256 );
3553 sha256_starts( &handshake->fin_sha256, 0 );
3554#endif
3555#if defined(POLARSSL_SHA512_C)
3556 sha512_init( &handshake->fin_sha512 );
3557 sha512_starts( &handshake->fin_sha512, 1 );
3558#endif
3559#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3560
3561 handshake->update_checksum = ssl_update_checksum_start;
Hanno Beckerc2b9d982017-05-10 16:37:21 +01003562
3563#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
3564 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
3565 ssl_sig_hash_set_init( &handshake->hash_algs );
3566#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003567
3568#if defined(POLARSSL_DHM_C)
3569 dhm_init( &handshake->dhm_ctx );
3570#endif
3571#if defined(POLARSSL_ECDH_C)
3572 ecdh_init( &handshake->ecdh_ctx );
3573#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003574}
3575
3576static void ssl_transform_init( ssl_transform *transform )
3577{
3578 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003579
3580 cipher_init( &transform->cipher_ctx_enc );
3581 cipher_init( &transform->cipher_ctx_dec );
3582
3583 md_init( &transform->md_ctx_enc );
3584 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003585}
3586
3587void ssl_session_init( ssl_session *session )
3588{
3589 memset( session, 0, sizeof(ssl_session) );
3590}
3591
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003592static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003593{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003594 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003595 if( ssl->transform_negotiate )
3596 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003597 if( ssl->session_negotiate )
3598 ssl_session_free( ssl->session_negotiate );
3599 if( ssl->handshake )
3600 ssl_handshake_free( ssl->handshake );
3601
3602 /*
3603 * Either the pointers are now NULL or cleared properly and can be freed.
3604 * Now allocate missing structures.
3605 */
3606 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003607 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003608 ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003609 }
Paul Bakker48916f92012-09-16 19:57:18 +00003610
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003611 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003612 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003613 ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003614 }
Paul Bakker48916f92012-09-16 19:57:18 +00003615
Paul Bakker82788fb2014-10-20 13:59:19 +02003616 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003617 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003618 ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003619 }
Paul Bakker48916f92012-09-16 19:57:18 +00003620
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003621 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003622 if( ssl->handshake == NULL ||
3623 ssl->transform_negotiate == NULL ||
3624 ssl->session_negotiate == NULL )
3625 {
3626 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003627
3628 polarssl_free( ssl->handshake );
3629 polarssl_free( ssl->transform_negotiate );
3630 polarssl_free( ssl->session_negotiate );
3631
3632 ssl->handshake = NULL;
3633 ssl->transform_negotiate = NULL;
3634 ssl->session_negotiate = NULL;
3635
Paul Bakker48916f92012-09-16 19:57:18 +00003636 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3637 }
3638
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003639 /* Initialize structures */
3640 ssl_session_init( ssl->session_negotiate );
3641 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003642 ssl_handshake_params_init( ssl->handshake );
3643
3644#if defined(POLARSSL_X509_CRT_PARSE_C)
3645 ssl->handshake->key_cert = ssl->key_cert;
3646#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003647
Paul Bakker48916f92012-09-16 19:57:18 +00003648 return( 0 );
3649}
3650
Paul Bakker5121ce52009-01-03 21:22:43 +00003651/*
3652 * Initialize an SSL context
3653 */
3654int ssl_init( ssl_context *ssl )
3655{
Paul Bakker48916f92012-09-16 19:57:18 +00003656 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003657 int len = SSL_BUFFER_LEN;
3658
3659 memset( ssl, 0, sizeof( ssl_context ) );
3660
Paul Bakker62f2dee2012-09-28 07:31:51 +00003661 /*
3662 * Sane defaults
3663 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003664 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3665 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3666 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3667 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003668
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003669 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003670
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003671#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003672 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003673 memset( ssl->renego_period, 0xFF, 7 );
3674 ssl->renego_period[7] = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003675#endif
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003676
Paul Bakker62f2dee2012-09-28 07:31:51 +00003677#if defined(POLARSSL_DHM_C)
3678 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
Manuel Pégourié-Gonnardf0f399d2015-07-03 17:45:57 +02003679 POLARSSL_DHM_RFC5114_MODP_2048_P) ) != 0 ||
Paul Bakker62f2dee2012-09-28 07:31:51 +00003680 ( ret = mpi_read_string( &ssl->dhm_G, 16,
Manuel Pégourié-Gonnardf0f399d2015-07-03 17:45:57 +02003681 POLARSSL_DHM_RFC5114_MODP_2048_G) ) != 0 )
Paul Bakker62f2dee2012-09-28 07:31:51 +00003682 {
3683 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3684 return( ret );
3685 }
3686#endif
3687
3688 /*
3689 * Prepare base structures
3690 */
Manuel Pégourié-Gonnardf7db5e02015-02-18 10:32:41 +00003691 if( ( ssl->in_ctr = polarssl_malloc( len ) ) == NULL ||
3692 ( ssl->out_ctr = polarssl_malloc( len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003693 {
3694 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003695 polarssl_free( ssl->in_ctr );
3696 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003697 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003698 }
3699
3700 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3701 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3702
Manuel Pégourié-Gonnardf7db5e02015-02-18 10:32:41 +00003703 ssl->in_hdr = ssl->in_ctr + 8;
3704 ssl->in_iv = ssl->in_ctr + 13;
3705 ssl->in_msg = ssl->in_ctr + 13;
3706
3707 ssl->out_hdr = ssl->out_ctr + 8;
3708 ssl->out_iv = ssl->out_ctr + 13;
3709 ssl->out_msg = ssl->out_ctr + 13;
3710
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003711#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3712 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
3713#endif
3714
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003715#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3716 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3717#endif
3718
Paul Bakker606b4ba2013-08-14 16:52:14 +02003719#if defined(POLARSSL_SSL_SESSION_TICKETS)
3720 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3721#endif
3722
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003723#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003724 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003725#endif
3726
Paul Bakker48916f92012-09-16 19:57:18 +00003727 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3728 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003729
3730 return( 0 );
3731}
3732
3733/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003734 * Reset an initialized and used SSL context for re-use while retaining
3735 * all application-set variables, function pointers and data.
3736 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003737int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003738{
Paul Bakker48916f92012-09-16 19:57:18 +00003739 int ret;
3740
Paul Bakker7eb013f2011-10-06 12:37:39 +00003741 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003742
3743#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003744 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003745 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003746
3747 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard61860192014-11-04 13:05:42 +01003748 memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
3749 memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003750#endif
3751 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00003752
Paul Bakker7eb013f2011-10-06 12:37:39 +00003753 ssl->in_offt = NULL;
3754
Paul Bakker92be97b2013-01-02 17:30:03 +01003755 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003756 ssl->in_msgtype = 0;
3757 ssl->in_msglen = 0;
3758 ssl->in_left = 0;
3759
3760 ssl->in_hslen = 0;
3761 ssl->nb_zero = 0;
Hanno Becker10699cc2017-06-08 15:41:02 +01003762 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003763
Paul Bakker92be97b2013-01-02 17:30:03 +01003764 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003765 ssl->out_msgtype = 0;
3766 ssl->out_msglen = 0;
3767 ssl->out_left = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003768#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01003769 if( ssl->split_done != SSL_CBC_RECORD_SPLITTING_DISABLED )
3770 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003771#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00003772
Paul Bakker48916f92012-09-16 19:57:18 +00003773 ssl->transform_in = NULL;
3774 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003775
3776 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3777 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003778
3779#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003780 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003781 {
3782 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003783 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003784 {
3785 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3786 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3787 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003788 }
3789#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003790
Paul Bakker48916f92012-09-16 19:57:18 +00003791 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003792 {
Paul Bakker48916f92012-09-16 19:57:18 +00003793 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003794 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003795 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003796 }
Paul Bakker48916f92012-09-16 19:57:18 +00003797
Paul Bakkerc0463502013-02-14 11:19:38 +01003798 if( ssl->session )
3799 {
3800 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003801 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003802 ssl->session = NULL;
3803 }
3804
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003805#if defined(POLARSSL_SSL_ALPN)
3806 ssl->alpn_chosen = NULL;
3807#endif
3808
Paul Bakker48916f92012-09-16 19:57:18 +00003809 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3810 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003811
3812 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003813}
3814
Paul Bakkera503a632013-08-14 13:48:06 +02003815#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003816static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3817{
3818 aes_free( &tkeys->enc );
3819 aes_free( &tkeys->dec );
3820
3821 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3822}
3823
Paul Bakker7eb013f2011-10-06 12:37:39 +00003824/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003825 * Allocate and initialize ticket keys
3826 */
3827static int ssl_ticket_keys_init( ssl_context *ssl )
3828{
3829 int ret;
3830 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003831 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003832
3833 if( ssl->ticket_keys != NULL )
3834 return( 0 );
3835
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003836 tkeys = polarssl_malloc( sizeof(ssl_ticket_keys) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003837 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003838 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3839
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003840 aes_init( &tkeys->enc );
3841 aes_init( &tkeys->dec );
3842
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003843 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003844 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003845 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003846 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003847 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003848 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003849
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003850 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3851 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3852 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3853 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003854 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003855 polarssl_free( tkeys );
3856 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003857 }
3858
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003859 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003860 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003861 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003862 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003863 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003864 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003865
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003866 ssl->ticket_keys = tkeys;
3867
3868 return( 0 );
3869}
Paul Bakkera503a632013-08-14 13:48:06 +02003870#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003871
3872/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003873 * SSL set accessors
3874 */
3875void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3876{
3877 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003878
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003879#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
3880 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003881 if( endpoint == SSL_IS_CLIENT )
3882 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003883#endif
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01003884
3885#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
3886 if( endpoint == SSL_IS_SERVER )
3887 ssl->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
3888#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003889}
3890
3891void ssl_set_authmode( ssl_context *ssl, int authmode )
3892{
3893 ssl->authmode = authmode;
3894}
3895
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003896#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003897void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003898 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003899 void *p_vrfy )
3900{
3901 ssl->f_vrfy = f_vrfy;
3902 ssl->p_vrfy = p_vrfy;
3903}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003904#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003905
Paul Bakker5121ce52009-01-03 21:22:43 +00003906void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003907 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003908 void *p_rng )
3909{
3910 ssl->f_rng = f_rng;
3911 ssl->p_rng = p_rng;
3912}
3913
3914void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003915 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003916 void *p_dbg )
3917{
3918 ssl->f_dbg = f_dbg;
3919 ssl->p_dbg = p_dbg;
3920}
3921
3922void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003923 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003924 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003925{
3926 ssl->f_recv = f_recv;
3927 ssl->f_send = f_send;
3928 ssl->p_recv = p_recv;
3929 ssl->p_send = p_send;
3930}
3931
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003932#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00003933void ssl_set_session_cache( ssl_context *ssl,
3934 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3935 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003936{
Paul Bakker0a597072012-09-25 21:55:46 +00003937 ssl->f_get_cache = f_get_cache;
3938 ssl->p_get_cache = p_get_cache;
3939 ssl->f_set_cache = f_set_cache;
3940 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003941}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003942#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003943
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003944#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003945int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003946{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003947 int ret;
3948
3949 if( ssl == NULL ||
3950 session == NULL ||
3951 ssl->session_negotiate == NULL ||
3952 ssl->endpoint != SSL_IS_CLIENT )
3953 {
3954 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3955 }
3956
3957 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3958 return( ret );
3959
Paul Bakker0a597072012-09-25 21:55:46 +00003960 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003961
3962 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003963}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003964#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003965
Paul Bakkerb68cad62012-08-23 08:34:18 +00003966void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003967{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003968 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3969 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3970 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3971 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3972}
3973
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003974void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3975 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003976 int major, int minor )
3977{
3978 if( major != SSL_MAJOR_VERSION_3 )
3979 return;
3980
3981 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3982 return;
3983
3984 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003985}
3986
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003987#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003988/* Add a new (empty) key_cert entry an return a pointer to it */
3989static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3990{
3991 ssl_key_cert *key_cert, *last;
3992
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003993 key_cert = polarssl_malloc( sizeof(ssl_key_cert) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003994 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003995 return( NULL );
3996
3997 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3998
3999 /* Append the new key_cert to the (possibly empty) current list */
4000 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01004001 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004002 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01004003 if( ssl->handshake != NULL )
4004 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01004005 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004006 else
4007 {
4008 last = ssl->key_cert;
4009 while( last->next != NULL )
4010 last = last->next;
4011 last->next = key_cert;
4012 }
4013
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004014 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004015}
4016
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004017void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00004018 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00004019{
4020 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00004021 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00004022 ssl->peer_cn = peer_cn;
4023}
4024
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004025int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004026 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00004027{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004028 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
4029
4030 if( key_cert == NULL )
4031 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4032
4033 key_cert->cert = own_cert;
4034 key_cert->key = pk_key;
4035
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004036 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004037}
4038
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +01004039#if ! defined(POLARSSL_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004040#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004041int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004042 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00004043{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004044 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004045 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004046
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004047 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004048 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4049
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004050 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004051 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004052 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004053
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004054 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004055
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004056 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004057 if( ret != 0 )
4058 return( ret );
4059
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004060 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004061 return( ret );
4062
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004063 key_cert->cert = own_cert;
4064 key_cert->key_own_alloc = 1;
4065
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004066 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004067}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004068#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004069
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004070int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02004071 void *rsa_key,
4072 rsa_decrypt_func rsa_decrypt,
4073 rsa_sign_func rsa_sign,
4074 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00004075{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004076 int ret;
4077 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004078
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004079 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004080 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4081
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004082 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004083 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004084 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004085
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004086 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004087
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004088 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
4089 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
4090 return( ret );
4091
4092 key_cert->cert = own_cert;
4093 key_cert->key_own_alloc = 1;
4094
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004095 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00004096}
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +01004097#endif /* POLARSSL_DEPRECATED_REMOVED */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004098#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004099
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004100#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004101int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
4102 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004103{
Paul Bakker6db455e2013-09-18 17:29:31 +02004104 if( psk == NULL || psk_identity == NULL )
4105 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4106
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02004107 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01004108 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4109
Manuel Pégourié-Gonnard65125542015-08-27 16:37:35 +02004110 /* Identity len will be encoded on two bytes */
4111 if( ( psk_identity_len >> 16 ) != 0 ||
4112 psk_identity_len > SSL_MAX_CONTENT_LEN )
4113 {
4114 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4115 }
4116
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004117 if( ssl->psk != NULL || ssl->psk_identity != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02004118 {
4119 polarssl_free( ssl->psk );
4120 polarssl_free( ssl->psk_identity );
Manuel Pégourié-Gonnard9c521762015-10-27 10:54:53 +01004121 ssl->psk = NULL;
4122 ssl->psk_identity = NULL;
Paul Bakker6db455e2013-09-18 17:29:31 +02004123 }
4124
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004125 if( ( ssl->psk = polarssl_malloc( psk_len ) ) == NULL ||
4126 ( ssl->psk_identity = polarssl_malloc( psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05004127 {
4128 polarssl_free( ssl->psk );
Manuel Pégourié-Gonnard5aff0292015-09-28 18:09:45 +02004129 polarssl_free( ssl->psk_identity );
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004130 ssl->psk = NULL;
Manuel Pégourié-Gonnard5aff0292015-09-28 18:09:45 +02004131 ssl->psk_identity = NULL;
Mansour Moufidf81088b2015-02-17 13:10:21 -05004132 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4133 }
Paul Bakker6db455e2013-09-18 17:29:31 +02004134
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004135 ssl->psk_len = psk_len;
4136 ssl->psk_identity_len = psk_identity_len;
4137
Paul Bakker6db455e2013-09-18 17:29:31 +02004138 memcpy( ssl->psk, psk, ssl->psk_len );
4139 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004140
4141 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004142}
4143
4144void ssl_set_psk_cb( ssl_context *ssl,
4145 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4146 size_t),
4147 void *p_psk )
4148{
4149 ssl->f_psk = f_psk;
4150 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004151}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004152#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004153
Paul Bakker48916f92012-09-16 19:57:18 +00004154#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004155int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004156{
4157 int ret;
4158
Paul Bakker48916f92012-09-16 19:57:18 +00004159 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004160 {
4161 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4162 return( ret );
4163 }
4164
Paul Bakker48916f92012-09-16 19:57:18 +00004165 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004166 {
4167 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4168 return( ret );
4169 }
4170
4171 return( 0 );
4172}
4173
Paul Bakker1b57b062011-01-06 15:48:19 +00004174int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4175{
4176 int ret;
4177
Paul Bakker66d5d072014-06-17 16:39:18 +02004178 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004179 {
4180 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4181 return( ret );
4182 }
4183
Paul Bakker66d5d072014-06-17 16:39:18 +02004184 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004185 {
4186 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4187 return( ret );
4188 }
4189
4190 return( 0 );
4191}
Paul Bakker48916f92012-09-16 19:57:18 +00004192#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004193
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004194#if defined(POLARSSL_SSL_SET_CURVES)
4195/*
4196 * Set the allowed elliptic curves
4197 */
4198void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4199{
4200 ssl->curve_list = curve_list;
4201}
4202#endif
4203
Paul Bakker0be444a2013-08-27 21:55:01 +02004204#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004205int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004206{
4207 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00004208 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004209
4210 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004211
4212 if( ssl->hostname_len + 1 == 0 )
4213 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4214
Simon Butcherc988f322015-09-29 23:27:20 +01004215 if( ssl->hostname_len > SSL_MAX_HOST_NAME_LEN )
4216 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4217
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004218 ssl->hostname = polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004219
Paul Bakkerb15b8512012-01-13 13:44:06 +00004220 if( ssl->hostname == NULL )
4221 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4222
Paul Bakker3c2122f2013-06-24 19:03:14 +02004223 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004224 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004225
Paul Bakker40ea7de2009-05-03 10:18:48 +00004226 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004227
4228 return( 0 );
4229}
4230
Paul Bakker5701cdc2012-09-27 21:49:42 +00004231void ssl_set_sni( ssl_context *ssl,
4232 int (*f_sni)(void *, ssl_context *,
4233 const unsigned char *, size_t),
4234 void *p_sni )
4235{
4236 ssl->f_sni = f_sni;
4237 ssl->p_sni = p_sni;
4238}
Paul Bakker0be444a2013-08-27 21:55:01 +02004239#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004240
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004241#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004242int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004243{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004244 size_t cur_len, tot_len;
4245 const char **p;
4246
4247 /*
4248 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4249 * truncated". Check lengths now rather than later.
4250 */
4251 tot_len = 0;
4252 for( p = protos; *p != NULL; p++ )
4253 {
4254 cur_len = strlen( *p );
4255 tot_len += cur_len;
4256
4257 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4258 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4259 }
4260
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004261 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004262
4263 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004264}
4265
4266const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4267{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004268 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004269}
4270#endif /* POLARSSL_SSL_ALPN */
4271
Paul Bakker490ecc82011-10-06 13:04:09 +00004272void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4273{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004274 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4275 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4276 {
4277 ssl->max_major_ver = major;
4278 ssl->max_minor_ver = minor;
4279 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004280}
4281
Paul Bakker1d29fb52012-09-28 13:28:45 +00004282void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4283{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004284 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4285 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4286 {
4287 ssl->min_major_ver = major;
4288 ssl->min_minor_ver = minor;
4289 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004290}
4291
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004292#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4293void ssl_set_fallback( ssl_context *ssl, char fallback )
4294{
4295 ssl->fallback = fallback;
4296}
4297#endif
4298
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004299#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4300void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
4301{
4302 ssl->encrypt_then_mac = etm;
4303}
4304#endif
4305
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004306#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4307void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4308{
4309 ssl->extended_ms = ems;
4310}
4311#endif
4312
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004313void ssl_set_arc4_support( ssl_context *ssl, char arc4 )
4314{
4315 ssl->arc4_disabled = arc4;
4316}
4317
Paul Bakker05decb22013-08-15 13:33:48 +02004318#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004319int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4320{
Paul Bakker77e257e2013-12-16 15:29:52 +01004321 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004322 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004323 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004324 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004325 }
4326
4327 ssl->mfl_code = mfl_code;
4328
4329 return( 0 );
4330}
Paul Bakker05decb22013-08-15 13:33:48 +02004331#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004332
Paul Bakker1f2bc622013-08-15 13:45:55 +02004333#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004334int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004335{
Paul Bakker8c1ede62013-07-19 14:14:37 +02004336 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004337
4338 return( 0 );
4339}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004340#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004341
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004342#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4343void ssl_set_cbc_record_splitting( ssl_context *ssl, char split )
4344{
4345 ssl->split_done = split;
4346}
4347#endif
4348
Paul Bakker48916f92012-09-16 19:57:18 +00004349void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4350{
4351 ssl->allow_legacy_renegotiation = allow_legacy;
4352}
4353
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004354#if defined(POLARSSL_SSL_RENEGOTIATION)
4355void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4356{
4357 ssl->disable_renegotiation = renegotiation;
4358}
4359
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004360void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4361{
4362 ssl->renego_max_records = max_records;
4363}
4364
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004365void ssl_set_renegotiation_period( ssl_context *ssl,
4366 const unsigned char period[8] )
4367{
4368 memcpy( ssl->renego_period, period, 8 );
4369}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004370#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004371
Paul Bakkera503a632013-08-14 13:48:06 +02004372#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004373int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4374{
4375 ssl->session_tickets = use_tickets;
4376
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004377#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004378 if( ssl->endpoint == SSL_IS_CLIENT )
4379 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004380#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004381
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01004382 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
4383 return( 0 );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004384
4385 if( ssl->f_rng == NULL )
4386 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4387
4388 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004389}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004390
4391void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4392{
4393 ssl->ticket_lifetime = lifetime;
4394}
Paul Bakkera503a632013-08-14 13:48:06 +02004395#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004396
Paul Bakker5121ce52009-01-03 21:22:43 +00004397/*
4398 * SSL get accessors
4399 */
4400size_t ssl_get_bytes_avail( const ssl_context *ssl )
4401{
4402 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4403}
4404
Paul Bakkerff60ee62010-03-16 21:09:09 +00004405int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004406{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00004407 if( ssl->session != NULL )
4408 return( ssl->session->verify_result );
4409
4410 if( ssl->session_negotiate != NULL )
4411 return( ssl->session_negotiate->verify_result );
4412
4413 return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004414}
4415
Paul Bakkere3166ce2011-01-27 17:40:50 +00004416const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004417{
Paul Bakker926c8e42013-03-06 10:23:34 +01004418 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004419 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004420
Paul Bakkere3166ce2011-01-27 17:40:50 +00004421 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004422}
4423
Paul Bakker43ca69c2011-01-15 17:35:19 +00004424const char *ssl_get_version( const ssl_context *ssl )
4425{
4426 switch( ssl->minor_ver )
4427 {
4428 case SSL_MINOR_VERSION_0:
4429 return( "SSLv3.0" );
4430
4431 case SSL_MINOR_VERSION_1:
4432 return( "TLSv1.0" );
4433
4434 case SSL_MINOR_VERSION_2:
4435 return( "TLSv1.1" );
4436
Paul Bakker1ef83d62012-04-11 12:09:53 +00004437 case SSL_MINOR_VERSION_3:
4438 return( "TLSv1.2" );
4439
Paul Bakker43ca69c2011-01-15 17:35:19 +00004440 default:
4441 break;
4442 }
4443 return( "unknown" );
4444}
4445
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004446#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004447const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004448{
4449 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004450 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004451
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004452 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004453}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004454#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004455
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004456#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004457int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4458{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004459 if( ssl == NULL ||
4460 dst == NULL ||
4461 ssl->session == NULL ||
4462 ssl->endpoint != SSL_IS_CLIENT )
4463 {
4464 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4465 }
4466
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004467 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004468}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004469#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004470
Paul Bakker5121ce52009-01-03 21:22:43 +00004471/*
Paul Bakker1961b702013-01-25 14:49:24 +01004472 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004473 */
Paul Bakker1961b702013-01-25 14:49:24 +01004474int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004475{
Paul Bakker40e46942009-01-03 21:51:57 +00004476 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004477
Paul Bakker40e46942009-01-03 21:51:57 +00004478#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004479 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004480 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004481#endif
Paul Bakker40e46942009-01-03 21:51:57 +00004482#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004483 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004484 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004485#endif
4486
Paul Bakker1961b702013-01-25 14:49:24 +01004487 return( ret );
4488}
4489
4490/*
4491 * Perform the SSL handshake
4492 */
4493int ssl_handshake( ssl_context *ssl )
4494{
4495 int ret = 0;
4496
4497 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4498
4499 while( ssl->state != SSL_HANDSHAKE_OVER )
4500 {
4501 ret = ssl_handshake_step( ssl );
4502
4503 if( ret != 0 )
4504 break;
4505 }
4506
Paul Bakker5121ce52009-01-03 21:22:43 +00004507 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4508
4509 return( ret );
4510}
4511
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004512#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004513#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004514/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004515 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004516 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004517static int ssl_write_hello_request( ssl_context *ssl )
4518{
4519 int ret;
4520
4521 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4522
4523 ssl->out_msglen = 4;
4524 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4525 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4526
4527 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4528 {
4529 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4530 return( ret );
4531 }
4532
4533 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4534
4535 return( 0 );
4536}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004537#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004538
4539/*
4540 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004541 * - any side: calling ssl_renegotiate(),
4542 * - client: receiving a HelloRequest during ssl_read(),
4543 * - server: receiving any handshake message on server during ssl_read() after
4544 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004545 * If the handshake doesn't complete due to waiting for I/O, it will continue
4546 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004547 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004548static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004549{
4550 int ret;
4551
4552 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4553
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004554 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4555 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004556
4557 ssl->state = SSL_HELLO_REQUEST;
4558 ssl->renegotiation = SSL_RENEGOTIATION;
4559
Paul Bakker48916f92012-09-16 19:57:18 +00004560 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4561 {
4562 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4563 return( ret );
4564 }
4565
4566 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4567
4568 return( 0 );
4569}
4570
4571/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004572 * Renegotiate current connection on client,
4573 * or request renegotiation on server
4574 */
4575int ssl_renegotiate( ssl_context *ssl )
4576{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004577 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004578
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004579#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004580 /* On server, just send the request */
4581 if( ssl->endpoint == SSL_IS_SERVER )
4582 {
4583 if( ssl->state != SSL_HANDSHAKE_OVER )
4584 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4585
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004586 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4587
4588 /* Did we already try/start sending HelloRequest? */
4589 if( ssl->out_left != 0 )
4590 return( ssl_flush_output( ssl ) );
4591
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004592 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004593 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004594#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004595
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004596#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004597 /*
4598 * On client, either start the renegotiation process or,
4599 * if already in progress, continue the handshake
4600 */
4601 if( ssl->renegotiation != SSL_RENEGOTIATION )
4602 {
4603 if( ssl->state != SSL_HANDSHAKE_OVER )
4604 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4605
4606 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4607 {
4608 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4609 return( ret );
4610 }
4611 }
4612 else
4613 {
4614 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4615 {
4616 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4617 return( ret );
4618 }
4619 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004620#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004621
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004622 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004623}
4624
4625/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004626 * Check record counters and renegotiate if they're above the limit.
4627 */
4628static int ssl_check_ctr_renegotiate( ssl_context *ssl )
4629{
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004630 if( ssl->state != SSL_HANDSHAKE_OVER ||
4631 ssl->renegotiation == SSL_RENEGOTIATION_PENDING ||
4632 ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED )
4633 {
4634 return( 0 );
4635 }
4636
4637 // TODO: adapt for DTLS
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004638 if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 &&
4639 memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004640 {
4641 return( 0 );
4642 }
4643
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004644 SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004645 return( ssl_renegotiate( ssl ) );
4646}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004647#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004648
4649/*
4650 * Receive application data decrypted from the SSL layer
4651 */
Paul Bakker23986e52011-04-24 08:57:21 +00004652int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004653{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004654 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004655 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004656
4657 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4658
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004659#if defined(POLARSSL_SSL_RENEGOTIATION)
4660 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4661 {
4662 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4663 return( ret );
4664 }
4665#endif
4666
Paul Bakker5121ce52009-01-03 21:22:43 +00004667 if( ssl->state != SSL_HANDSHAKE_OVER )
4668 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004669 ret = ssl_handshake( ssl );
4670 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4671 {
4672 record_read = 1;
4673 }
4674 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004675 {
4676 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4677 return( ret );
4678 }
4679 }
4680
4681 if( ssl->in_offt == NULL )
4682 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004683 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004684 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004685 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4686 {
4687 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4688 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004689
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004690 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4691 return( ret );
4692 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004693 }
4694
4695 if( ssl->in_msglen == 0 &&
4696 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4697 {
4698 /*
4699 * OpenSSL sends empty messages to randomize the IV
4700 */
4701 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4702 {
Paul Bakker831a7552011-05-18 13:32:51 +00004703 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4704 return( 0 );
4705
Paul Bakker5121ce52009-01-03 21:22:43 +00004706 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4707 return( ret );
4708 }
4709 }
4710
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004711#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004712 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4713 {
4714 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4715
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004716#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004717 if( ssl->endpoint == SSL_IS_CLIENT &&
4718 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4719 ssl->in_hslen != 4 ) )
4720 {
4721 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4722 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4723 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004724#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004725
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004726 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4727 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004728 ssl->allow_legacy_renegotiation ==
4729 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004730 {
4731 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4732
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004733#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004734 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004735 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004736 /*
4737 * SSLv3 does not have a "no_renegotiation" alert
4738 */
4739 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4740 return( ret );
4741 }
4742 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004743#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004744#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4745 defined(POLARSSL_SSL_PROTO_TLS1_2)
4746 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004747 {
4748 if( ( ret = ssl_send_alert_message( ssl,
4749 SSL_ALERT_LEVEL_WARNING,
4750 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4751 {
4752 return( ret );
4753 }
Paul Bakker48916f92012-09-16 19:57:18 +00004754 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004755 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004756#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4757 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004758 {
4759 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004760 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004761 }
Paul Bakker48916f92012-09-16 19:57:18 +00004762 }
4763 else
4764 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004765 ret = ssl_start_renegotiation( ssl );
4766 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4767 {
4768 record_read = 1;
4769 }
4770 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004771 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004772 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004773 return( ret );
4774 }
Paul Bakker48916f92012-09-16 19:57:18 +00004775 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004776
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004777 /* If a non-handshake record was read during renego, fallthrough,
4778 * else tell the user they should call ssl_read() again */
4779 if( ! record_read )
4780 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004781 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004782 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4783 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004784 ssl->renego_records_seen++;
4785
4786 if( ssl->renego_max_records >= 0 &&
4787 ssl->renego_records_seen > ssl->renego_max_records )
4788 {
4789 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4790 "but not honored by client" ) );
4791 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4792 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004793 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004794#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004795
4796 /* Fatal and closure alerts handled by ssl_read_record() */
4797 if( ssl->in_msgtype == SSL_MSG_ALERT )
4798 {
4799 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4800 return( POLARSSL_ERR_NET_WANT_READ );
4801 }
4802
4803 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004804 {
4805 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004806 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004807 }
4808
4809 ssl->in_offt = ssl->in_msg;
4810 }
4811
4812 n = ( len < ssl->in_msglen )
4813 ? len : ssl->in_msglen;
4814
4815 memcpy( buf, ssl->in_offt, n );
4816 ssl->in_msglen -= n;
4817
4818 if( ssl->in_msglen == 0 )
4819 /* all bytes consumed */
4820 ssl->in_offt = NULL;
4821 else
4822 /* more data available */
4823 ssl->in_offt += n;
4824
4825 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4826
Paul Bakker23986e52011-04-24 08:57:21 +00004827 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004828}
4829
4830/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004831 * Send application data to be encrypted by the SSL layer,
4832 * taking care of max fragment length and buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +00004833 */
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004834static int ssl_write_real( ssl_context *ssl,
4835 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004836{
Paul Bakker23986e52011-04-24 08:57:21 +00004837 int ret;
4838 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004839 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004840
Paul Bakker05decb22013-08-15 13:33:48 +02004841#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004842 /*
4843 * Assume mfl_code is correct since it was checked when set
4844 */
4845 max_len = mfl_code_to_length[ssl->mfl_code];
4846
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004847 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004848 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004849 */
4850 if( ssl->session_out != NULL &&
4851 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4852 {
4853 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4854 }
Paul Bakker05decb22013-08-15 13:33:48 +02004855#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004856
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004857 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004858
Paul Bakker5121ce52009-01-03 21:22:43 +00004859 if( ssl->out_left != 0 )
4860 {
4861 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4862 {
4863 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4864 return( ret );
4865 }
4866 }
Paul Bakker887bd502011-06-08 13:10:54 +00004867 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004868 {
Paul Bakker887bd502011-06-08 13:10:54 +00004869 ssl->out_msglen = n;
4870 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4871 memcpy( ssl->out_msg, buf, n );
4872
4873 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4874 {
4875 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4876 return( ret );
4877 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004878 }
4879
Paul Bakker23986e52011-04-24 08:57:21 +00004880 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004881}
4882
4883/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004884 * Write application data, doing 1/n-1 splitting if necessary.
4885 *
4886 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004887 * then the caller will call us again with the same arguments, so
4888 * remember wether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004889 */
4890#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004891static int ssl_write_split( ssl_context *ssl,
4892 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004893{
4894 int ret;
4895
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004896 if( ssl->split_done == SSL_CBC_RECORD_SPLITTING_DISABLED ||
4897 len <= 1 ||
4898 ssl->minor_ver > SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004899 cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
4900 != POLARSSL_MODE_CBC )
4901 {
4902 return( ssl_write_real( ssl, buf, len ) );
4903 }
4904
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004905 if( ssl->split_done == 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004906 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004907 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004908 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004909 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004910 }
4911
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004912 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
4913 return( ret );
4914 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004915
4916 return( ret + 1 );
4917}
4918#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */
4919
4920/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004921 * Write application data (public-facing wrapper)
4922 */
4923int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
4924{
4925 int ret;
4926
4927 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4928
4929#if defined(POLARSSL_SSL_RENEGOTIATION)
4930 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4931 {
4932 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4933 return( ret );
4934 }
4935#endif
4936
4937 if( ssl->state != SSL_HANDSHAKE_OVER )
4938 {
4939 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4940 {
4941 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4942 return( ret );
4943 }
4944 }
4945
4946#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4947 ret = ssl_write_split( ssl, buf, len );
4948#else
4949 ret = ssl_write_real( ssl, buf, len );
4950#endif
4951
4952 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4953
4954 return( ret );
4955}
4956
4957/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004958 * Notify the peer that the connection is being closed
4959 */
4960int ssl_close_notify( ssl_context *ssl )
4961{
4962 int ret;
4963
4964 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4965
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004966 if( ssl->out_left != 0 )
4967 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004968
4969 if( ssl->state == SSL_HANDSHAKE_OVER )
4970 {
Paul Bakker48916f92012-09-16 19:57:18 +00004971 if( ( ret = ssl_send_alert_message( ssl,
4972 SSL_ALERT_LEVEL_WARNING,
4973 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004974 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004975 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004976 return( ret );
4977 }
4978 }
4979
4980 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4981
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004982 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004983}
4984
Paul Bakker48916f92012-09-16 19:57:18 +00004985void ssl_transform_free( ssl_transform *transform )
4986{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004987 if( transform == NULL )
4988 return;
4989
Paul Bakker48916f92012-09-16 19:57:18 +00004990#if defined(POLARSSL_ZLIB_SUPPORT)
4991 deflateEnd( &transform->ctx_deflate );
4992 inflateEnd( &transform->ctx_inflate );
4993#endif
4994
Paul Bakker84bbeb52014-07-01 14:53:22 +02004995 cipher_free( &transform->cipher_ctx_enc );
4996 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004997
Paul Bakker84bbeb52014-07-01 14:53:22 +02004998 md_free( &transform->md_ctx_enc );
4999 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02005000
Paul Bakker34617722014-06-13 17:20:13 +02005001 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005002}
5003
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005004#if defined(POLARSSL_X509_CRT_PARSE_C)
5005static void ssl_key_cert_free( ssl_key_cert *key_cert )
5006{
5007 ssl_key_cert *cur = key_cert, *next;
5008
5009 while( cur != NULL )
5010 {
5011 next = cur->next;
5012
5013 if( cur->key_own_alloc )
5014 {
5015 pk_free( cur->key );
5016 polarssl_free( cur->key );
5017 }
5018 polarssl_free( cur );
5019
5020 cur = next;
5021 }
5022}
5023#endif /* POLARSSL_X509_CRT_PARSE_C */
5024
Paul Bakker48916f92012-09-16 19:57:18 +00005025void ssl_handshake_free( ssl_handshake_params *handshake )
5026{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005027 if( handshake == NULL )
5028 return;
5029
Paul Bakker48916f92012-09-16 19:57:18 +00005030#if defined(POLARSSL_DHM_C)
5031 dhm_free( &handshake->dhm_ctx );
5032#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005033#if defined(POLARSSL_ECDH_C)
5034 ecdh_free( &handshake->ecdh_ctx );
5035#endif
5036
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005037#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02005038 /* explicit void pointer cast for buggy MS compiler */
5039 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005040#endif
5041
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02005042#if defined(POLARSSL_X509_CRT_PARSE_C) && \
5043 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
5044 /*
5045 * Free only the linked list wrapper, not the keys themselves
5046 * since the belong to the SNI callback
5047 */
5048 if( handshake->sni_key_cert != NULL )
5049 {
5050 ssl_key_cert *cur = handshake->sni_key_cert, *next;
5051
5052 while( cur != NULL )
5053 {
5054 next = cur->next;
5055 polarssl_free( cur );
5056 cur = next;
5057 }
5058 }
Paul Bakker9af723c2014-05-01 13:03:14 +02005059#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005060
Paul Bakker34617722014-06-13 17:20:13 +02005061 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005062}
5063
5064void ssl_session_free( ssl_session *session )
5065{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005066 if( session == NULL )
5067 return;
5068
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005069#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00005070 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00005071 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005072 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02005073 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00005074 }
Paul Bakkered27a042013-04-18 22:46:23 +02005075#endif
Paul Bakker0a597072012-09-25 21:55:46 +00005076
Paul Bakkera503a632013-08-14 13:48:06 +02005077#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005078 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02005079#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005080
Paul Bakker34617722014-06-13 17:20:13 +02005081 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005082}
5083
Paul Bakker5121ce52009-01-03 21:22:43 +00005084/*
5085 * Free an SSL context
5086 */
5087void ssl_free( ssl_context *ssl )
5088{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005089 if( ssl == NULL )
5090 return;
5091
Paul Bakker5121ce52009-01-03 21:22:43 +00005092 SSL_DEBUG_MSG( 2, ( "=> free" ) );
5093
Paul Bakker5121ce52009-01-03 21:22:43 +00005094 if( ssl->out_ctr != NULL )
5095 {
Paul Bakker34617722014-06-13 17:20:13 +02005096 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005097 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005098 }
5099
5100 if( ssl->in_ctr != NULL )
5101 {
Paul Bakker34617722014-06-13 17:20:13 +02005102 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005103 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005104 }
5105
Paul Bakker16770332013-10-11 09:59:44 +02005106#if defined(POLARSSL_ZLIB_SUPPORT)
5107 if( ssl->compress_buf != NULL )
5108 {
Paul Bakker34617722014-06-13 17:20:13 +02005109 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02005110 polarssl_free( ssl->compress_buf );
5111 }
5112#endif
5113
Paul Bakker40e46942009-01-03 21:51:57 +00005114#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00005115 mpi_free( &ssl->dhm_P );
5116 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00005117#endif
5118
Paul Bakker48916f92012-09-16 19:57:18 +00005119 if( ssl->transform )
5120 {
5121 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005122 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005123 }
5124
5125 if( ssl->handshake )
5126 {
5127 ssl_handshake_free( ssl->handshake );
5128 ssl_transform_free( ssl->transform_negotiate );
5129 ssl_session_free( ssl->session_negotiate );
5130
Paul Bakker6e339b52013-07-03 13:37:05 +02005131 polarssl_free( ssl->handshake );
5132 polarssl_free( ssl->transform_negotiate );
5133 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00005134 }
5135
Paul Bakkerc0463502013-02-14 11:19:38 +01005136 if( ssl->session )
5137 {
5138 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005139 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005140 }
5141
Paul Bakkera503a632013-08-14 13:48:06 +02005142#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005143 if( ssl->ticket_keys )
5144 {
5145 ssl_ticket_keys_free( ssl->ticket_keys );
5146 polarssl_free( ssl->ticket_keys );
5147 }
Paul Bakkera503a632013-08-14 13:48:06 +02005148#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005149
Paul Bakker0be444a2013-08-27 21:55:01 +02005150#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02005151 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005152 {
Paul Bakker34617722014-06-13 17:20:13 +02005153 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02005154 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00005155 ssl->hostname_len = 0;
5156 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005157#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005158
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005159#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005160 if( ssl->psk != NULL )
5161 {
Paul Bakker34617722014-06-13 17:20:13 +02005162 polarssl_zeroize( ssl->psk, ssl->psk_len );
5163 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005164 polarssl_free( ssl->psk );
5165 polarssl_free( ssl->psk_identity );
5166 ssl->psk_len = 0;
5167 ssl->psk_identity_len = 0;
5168 }
5169#endif
5170
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005171#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005172 ssl_key_cert_free( ssl->key_cert );
5173#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005174
Paul Bakker05ef8352012-05-08 09:17:57 +00005175#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
5176 if( ssl_hw_record_finish != NULL )
5177 {
5178 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
5179 ssl_hw_record_finish( ssl );
5180 }
5181#endif
5182
Paul Bakker5121ce52009-01-03 21:22:43 +00005183 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00005184
Paul Bakker86f04f42013-02-14 11:20:09 +01005185 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02005186 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005187}
5188
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005189#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005190/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005191 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005192 */
5193unsigned char ssl_sig_from_pk( pk_context *pk )
5194{
5195#if defined(POLARSSL_RSA_C)
5196 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
5197 return( SSL_SIG_RSA );
5198#endif
5199#if defined(POLARSSL_ECDSA_C)
5200 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
5201 return( SSL_SIG_ECDSA );
5202#endif
5203 return( SSL_SIG_ANON );
5204}
5205
Hanno Beckerc2b9d982017-05-10 16:37:21 +01005206unsigned char ssl_sig_from_pk_alg( pk_type_t type )
5207{
5208 switch( type ) {
5209 case POLARSSL_PK_RSA:
5210 return( SSL_SIG_RSA );
5211 case POLARSSL_PK_ECDSA:
5212 case POLARSSL_PK_ECKEY:
5213 return( SSL_SIG_ECDSA );
5214 default:
5215 return( SSL_SIG_ANON );
5216 }
5217}
5218
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005219pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
5220{
5221 switch( sig )
5222 {
5223#if defined(POLARSSL_RSA_C)
5224 case SSL_SIG_RSA:
5225 return( POLARSSL_PK_RSA );
5226#endif
5227#if defined(POLARSSL_ECDSA_C)
5228 case SSL_SIG_ECDSA:
5229 return( POLARSSL_PK_ECDSA );
5230#endif
5231 default:
5232 return( POLARSSL_PK_NONE );
5233 }
5234}
Paul Bakker9af723c2014-05-01 13:03:14 +02005235#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005236
Hanno Beckerc2b9d982017-05-10 16:37:21 +01005237#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
5238 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
5239
5240/* Find an entry in a signature-hash set matching a given hash algorithm. */
5241md_type_t ssl_sig_hash_set_find( ssl_sig_hash_set_t *set,
5242 pk_type_t sig_alg )
5243{
5244 switch( sig_alg )
5245 {
5246 case POLARSSL_PK_RSA:
5247 return( set->rsa );
5248 case POLARSSL_PK_ECDSA:
5249 return( set->ecdsa );
5250 default:
5251 return( POLARSSL_MD_NONE );
5252 }
5253}
5254
5255/* Add a signature-hash-pair to a signature-hash set */
5256void ssl_sig_hash_set_add( ssl_sig_hash_set_t *set,
5257 pk_type_t sig_alg,
5258 md_type_t md_alg )
5259{
5260 switch( sig_alg )
5261 {
5262 case POLARSSL_PK_RSA:
5263 if( set->rsa == POLARSSL_MD_NONE )
5264 set->rsa = md_alg;
5265 break;
5266
5267 case POLARSSL_PK_ECDSA:
5268 if( set->ecdsa == POLARSSL_MD_NONE )
5269 set->ecdsa = md_alg;
5270 break;
5271
5272 default:
5273 break;
5274 }
5275}
5276
5277/* Allow exactly one hash algorithm for each signature. */
5278void ssl_sig_hash_set_const_hash( ssl_sig_hash_set_t *set,
5279 md_type_t md_alg )
5280{
5281 set->rsa = md_alg;
5282 set->ecdsa = md_alg;
5283}
5284
5285#endif /* POLARSSL_SSL_PROTO_TLS1_2) &&
5286 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
5287
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005288/*
5289 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
5290 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005291md_type_t ssl_md_alg_from_hash( unsigned char hash )
5292{
5293 switch( hash )
5294 {
5295#if defined(POLARSSL_MD5_C)
5296 case SSL_HASH_MD5:
5297 return( POLARSSL_MD_MD5 );
5298#endif
5299#if defined(POLARSSL_SHA1_C)
5300 case SSL_HASH_SHA1:
5301 return( POLARSSL_MD_SHA1 );
5302#endif
5303#if defined(POLARSSL_SHA256_C)
5304 case SSL_HASH_SHA224:
5305 return( POLARSSL_MD_SHA224 );
5306 case SSL_HASH_SHA256:
5307 return( POLARSSL_MD_SHA256 );
5308#endif
5309#if defined(POLARSSL_SHA512_C)
5310 case SSL_HASH_SHA384:
5311 return( POLARSSL_MD_SHA384 );
5312 case SSL_HASH_SHA512:
5313 return( POLARSSL_MD_SHA512 );
5314#endif
5315 default:
5316 return( POLARSSL_MD_NONE );
5317 }
5318}
5319
Hanno Beckerc2b9d982017-05-10 16:37:21 +01005320/*
5321 * Convert from POLARSSL_MD_XXX to SSL_HASH_XXX
5322 */
5323unsigned char ssl_hash_from_md_alg( md_type_t md )
5324{
5325 switch( md )
5326 {
5327#if defined(POLARSSL_MD5_C)
5328 case POLARSSL_MD_MD5:
5329 return( SSL_HASH_MD5 );
5330#endif
5331#if defined(POLARSSL_SHA1_C)
5332 case POLARSSL_MD_SHA1:
5333 return( SSL_HASH_SHA1 );
5334#endif
5335#if defined(POLARSSL_SHA256_C)
5336 case POLARSSL_MD_SHA224:
5337 return( SSL_HASH_SHA224 );
5338 case POLARSSL_MD_SHA256:
5339 return( SSL_HASH_SHA256 );
5340#endif
5341#if defined(POLARSSL_SHA512_C)
5342 case POLARSSL_MD_SHA384:
5343 return( SSL_HASH_SHA384 );
5344 case POLARSSL_MD_SHA512:
5345 return( SSL_HASH_SHA512 );
5346#endif
5347 default:
5348 return( SSL_HASH_NONE );
5349 }
5350}
5351
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005352#if defined(POLARSSL_SSL_SET_CURVES)
5353/*
5354 * Check is a curve proposed by the peer is in our list.
5355 * Return 1 if we're willing to use it, 0 otherwise.
5356 */
5357int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
5358{
5359 const ecp_group_id *gid;
5360
5361 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5362 if( *gid == grp_id )
5363 return( 1 );
5364
5365 return( 0 );
5366}
Paul Bakker9af723c2014-05-01 13:03:14 +02005367#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005368
Hanno Beckerc2b9d982017-05-10 16:37:21 +01005369#if defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
5370/*
5371 * Check if a hash proposed by the peer is in our list.
5372 * Return 0 if we're willing to use it, -1 otherwise.
5373 */
5374int ssl_check_sig_hash( md_type_t md )
5375{
5376 const int *cur;
5377
5378 for( cur = md_list(); *cur != POLARSSL_MD_NONE; cur++ )
5379 {
5380#if !defined(POLARSSL_SSL_ENABLE_MD5_SIGNATURES)
5381 /* Skip MD5 */
5382 if( *cur == POLARSSL_MD_MD5 )
5383 continue;
5384#endif
5385 if( *cur == (int) md )
5386 return( 0 );
5387 }
5388
5389 return( -1 );
5390}
5391#endif /* POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
5392
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005393#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005394int ssl_check_cert_usage( const x509_crt *cert,
5395 const ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005396 int cert_endpoint,
5397 int *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005398{
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005399 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005400#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5401 int usage = 0;
5402#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005403#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5404 const char *ext_oid;
5405 size_t ext_len;
5406#endif
5407
5408#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5409 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5410 ((void) cert);
5411 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005412 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005413#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005414
5415#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5416 if( cert_endpoint == SSL_IS_SERVER )
5417 {
5418 /* Server part of the key exchange */
5419 switch( ciphersuite->key_exchange )
5420 {
5421 case POLARSSL_KEY_EXCHANGE_RSA:
5422 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5423 usage = KU_KEY_ENCIPHERMENT;
5424 break;
5425
5426 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5427 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5428 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5429 usage = KU_DIGITAL_SIGNATURE;
5430 break;
5431
5432 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5433 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5434 usage = KU_KEY_AGREEMENT;
5435 break;
5436
5437 /* Don't use default: we want warnings when adding new values */
5438 case POLARSSL_KEY_EXCHANGE_NONE:
5439 case POLARSSL_KEY_EXCHANGE_PSK:
5440 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5441 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5442 usage = 0;
5443 }
5444 }
5445 else
5446 {
5447 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5448 usage = KU_DIGITAL_SIGNATURE;
5449 }
5450
5451 if( x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005452 {
5453 *flags |= BADCERT_KEY_USAGE;
5454 ret = -1;
5455 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005456#else
5457 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005458#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5459
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005460#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5461 if( cert_endpoint == SSL_IS_SERVER )
5462 {
5463 ext_oid = OID_SERVER_AUTH;
5464 ext_len = OID_SIZE( OID_SERVER_AUTH );
5465 }
5466 else
5467 {
5468 ext_oid = OID_CLIENT_AUTH;
5469 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5470 }
5471
5472 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005473 {
5474 *flags |= BADCERT_EXT_KEY_USAGE;
5475 ret = -1;
5476 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005477#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5478
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005479 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005480}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005481#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005482
5483#endif /* POLARSSL_SSL_TLS_C */