blob: aa478e2f32279ad685528b8a2895dff6465e65ed [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 */
Manuel Pégourié-Gonnard921eb592017-12-19 10:03:46 +01001053#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard3ea75b32017-12-18 18:04:59 +01001054static void ssl_mac( md_context_t *md_ctx,
1055 const unsigned char *secret,
1056 const unsigned char *buf, size_t len,
1057 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnard921eb592017-12-19 10:03:46 +01001058 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001059{
1060 unsigned char header[11];
1061 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001062 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001063 int md_size = md_get_size( md_ctx->md_info );
1064 int md_type = md_get_type( md_ctx->md_info );
1065
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001066 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001067 if( md_type == POLARSSL_MD_MD5 )
1068 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001069 else
Paul Bakker68884e32013-01-07 18:20:04 +01001070 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001071
1072 memcpy( header, ctr, 8 );
1073 header[ 8] = (unsigned char) type;
1074 header[ 9] = (unsigned char)( len >> 8 );
1075 header[10] = (unsigned char)( len );
1076
Paul Bakker68884e32013-01-07 18:20:04 +01001077 memset( padding, 0x36, padlen );
1078 md_starts( md_ctx );
1079 md_update( md_ctx, secret, md_size );
1080 md_update( md_ctx, padding, padlen );
1081 md_update( md_ctx, header, 11 );
1082 md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard3ea75b32017-12-18 18:04:59 +01001083 md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001084
Paul Bakker68884e32013-01-07 18:20:04 +01001085 memset( padding, 0x5C, padlen );
1086 md_starts( md_ctx );
1087 md_update( md_ctx, secret, md_size );
1088 md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard3ea75b32017-12-18 18:04:59 +01001089 md_update( md_ctx, out, md_size );
1090 md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001091}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001092#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001093
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001094#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1095 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1096 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1097#define POLARSSL_SOME_MODES_USE_MAC
1098#endif
1099
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001100/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001101 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001102 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001103static int ssl_encrypt_buf( ssl_context *ssl )
1104{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001105 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001106 cipher_mode_t mode;
1107 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001108
1109 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1110
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001111 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1112 {
1113 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1114 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1115 }
1116
1117 mode = cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
1118
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001119 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1120 ssl->out_msg, ssl->out_msglen );
1121
Paul Bakker5121ce52009-01-03 21:22:43 +00001122 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001123 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001124 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001125#if defined(POLARSSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001126 if( mode == POLARSSL_MODE_STREAM ||
1127 ( mode == POLARSSL_MODE_CBC
1128#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1129 && ssl->session_out->encrypt_then_mac == SSL_ETM_DISABLED
1130#endif
1131 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001132 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001133#if defined(POLARSSL_SSL_PROTO_SSL3)
1134 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1135 {
Manuel Pégourié-Gonnard921eb592017-12-19 10:03:46 +01001136 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard3ea75b32017-12-18 18:04:59 +01001137
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001138 ssl_mac( &ssl->transform_out->md_ctx_enc,
1139 ssl->transform_out->mac_enc,
1140 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard3ea75b32017-12-18 18:04:59 +01001141 ssl->out_ctr, ssl->out_msgtype,
1142 mac );
1143
1144 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001145 }
1146 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001147#endif
1148#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001149 defined(POLARSSL_SSL_PROTO_TLS1_2)
1150 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1151 {
Hanno Becker251bab52017-11-20 10:30:08 +00001152 unsigned char mac[SSL_MAC_ADD];
1153
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001154 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1155 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1156 ssl->out_msg, ssl->out_msglen );
Hanno Becker251bab52017-11-20 10:30:08 +00001157 md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001158 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker251bab52017-11-20 10:30:08 +00001159
1160 memcpy( ssl->out_msg + ssl->out_msglen, mac,
1161 ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001162 }
1163 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001164#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001165 {
1166 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001167 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001168 }
1169
Hanno Becker0a139f92017-11-21 17:41:59 +00001170 SSL_DEBUG_BUF( 4, "expected mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001171 ssl->out_msg + ssl->out_msglen,
1172 ssl->transform_out->maclen );
1173
1174 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001175 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001176 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001177#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001178
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001179 /*
1180 * Encrypt
1181 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001182#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001183 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001185 int ret;
1186 size_t olen = 0;
1187
Paul Bakker5121ce52009-01-03 21:22:43 +00001188 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1189 "including %d bytes of padding",
1190 ssl->out_msglen, 0 ) );
1191
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001192 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001193 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001194 ssl->transform_out->ivlen,
1195 ssl->out_msg, ssl->out_msglen,
1196 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001197 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001198 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001199 return( ret );
1200 }
1201
1202 if( ssl->out_msglen != olen )
1203 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001204 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001205 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001206 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001207 }
Paul Bakker68884e32013-01-07 18:20:04 +01001208 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001209#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001210#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1211 if( mode == POLARSSL_MODE_GCM ||
1212 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001213 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001214 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001215 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001216 unsigned char *enc_msg;
1217 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001218 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1219 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001220
Paul Bakkerca4ab492012-04-18 14:23:57 +00001221 memcpy( add_data, ssl->out_ctr, 8 );
1222 add_data[8] = ssl->out_msgtype;
1223 add_data[9] = ssl->major_ver;
1224 add_data[10] = ssl->minor_ver;
1225 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1226 add_data[12] = ssl->out_msglen & 0xFF;
1227
1228 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1229 add_data, 13 );
1230
Paul Bakker68884e32013-01-07 18:20:04 +01001231 /*
1232 * Generate IV
1233 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001234 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1235 {
1236 /* Reminder if we ever add an AEAD mode with a different size */
1237 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1238 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1239 }
1240
1241 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1242 ssl->out_ctr, 8 );
1243 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001244
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001245 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001246 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001247
Paul Bakker68884e32013-01-07 18:20:04 +01001248 /*
1249 * Fix pointer positions and message length with added IV
1250 */
1251 enc_msg = ssl->out_msg;
1252 enc_msglen = ssl->out_msglen;
1253 ssl->out_msglen += ssl->transform_out->ivlen -
1254 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001255
Paul Bakker68884e32013-01-07 18:20:04 +01001256 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1257 "including %d bytes of padding",
1258 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001259
Paul Bakker68884e32013-01-07 18:20:04 +01001260 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001261 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001262 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001263 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1264 ssl->transform_out->iv_enc,
1265 ssl->transform_out->ivlen,
1266 add_data, 13,
1267 enc_msg, enc_msglen,
1268 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001269 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001270 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001271 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001272 return( ret );
1273 }
1274
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001275 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001276 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001277 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001278 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001279 }
1280
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001281 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001282 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001283
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001284 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001285 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001286 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001287#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001288#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1289 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001290 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001291 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001292 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001293 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001294 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001295
Paul Bakker48916f92012-09-16 19:57:18 +00001296 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1297 ssl->transform_out->ivlen;
1298 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001299 padlen = 0;
1300
1301 for( i = 0; i <= padlen; i++ )
1302 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1303
1304 ssl->out_msglen += padlen + 1;
1305
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001306 enc_msglen = ssl->out_msglen;
1307 enc_msg = ssl->out_msg;
1308
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001309#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001310 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001311 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1312 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001313 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001314 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001315 {
1316 /*
1317 * Generate IV
1318 */
Manuel Pégourié-Gonnarda67fd792015-08-27 12:02:40 +02001319 ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001320 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001321 if( ret != 0 )
1322 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001323
Paul Bakker92be97b2013-01-02 17:30:03 +01001324 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001325 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001326
1327 /*
1328 * Fix pointer positions and message length with added IV
1329 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001330 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001331 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001332 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001333 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001334#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001335
Paul Bakker5121ce52009-01-03 21:22:43 +00001336 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001337 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001338 ssl->out_msglen, ssl->transform_out->ivlen,
1339 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001340
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001341 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001342 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001343 ssl->transform_out->ivlen,
1344 enc_msg, enc_msglen,
1345 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001346 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001347 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001348 return( ret );
1349 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001350
Paul Bakkercca5b812013-08-31 17:40:26 +02001351 if( enc_msglen != olen )
1352 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001353 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001354 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001355 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001356
1357#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001358 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1359 {
1360 /*
1361 * Save IV in SSL3 and TLS1
1362 */
1363 memcpy( ssl->transform_out->iv_enc,
1364 ssl->transform_out->cipher_ctx_enc.iv,
1365 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001366 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001367#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001368
1369#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001370 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001371 {
1372 /*
1373 * MAC(MAC_write_key, seq_num +
1374 * TLSCipherText.type +
1375 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001376 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001377 * IV + // except for TLS 1.0
1378 * ENC(content + padding + padding_length));
1379 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001380 unsigned char pseudo_hdr[13];
1381
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001382 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1383
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001384 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1385 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001386 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1387 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1388
1389 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001390
1391 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1392 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1393 ssl->out_iv, ssl->out_msglen );
1394 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1395 ssl->out_iv + ssl->out_msglen );
1396 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1397
1398 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001399 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001400 }
1401#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001402 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001403 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001404#endif /* POLARSSL_CIPHER_MODE_CBC &&
1405 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001406 {
1407 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001408 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001409 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001410
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001411 /* Make extra sure authentication was performed, exactly once */
1412 if( auth_done != 1 )
1413 {
1414 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1415 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1416 }
1417
Paul Bakkerca4ab492012-04-18 14:23:57 +00001418 for( i = 8; i > 0; i-- )
1419 if( ++ssl->out_ctr[i - 1] != 0 )
1420 break;
1421
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001422 /* The loops goes to its end iff the counter is wrapping */
1423 if( i == 0 )
1424 {
1425 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1426 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1427 }
1428
Paul Bakker5121ce52009-01-03 21:22:43 +00001429 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1430
1431 return( 0 );
1432}
1433
1434static int ssl_decrypt_buf( ssl_context *ssl )
1435{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001436 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001437 cipher_mode_t mode;
1438 int auth_done = 0;
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001439#if defined(POLARSSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001440 size_t padlen = 0, correct = 1;
1441#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001442
1443 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1444
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001445 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1446 {
1447 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1448 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1449 }
1450
1451 mode = cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
1452
Paul Bakker48916f92012-09-16 19:57:18 +00001453 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001454 {
1455 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001456 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001457 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001458 }
1459
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001460#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001461 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001462 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001463 int ret;
1464 size_t olen = 0;
1465
Paul Bakker68884e32013-01-07 18:20:04 +01001466 padlen = 0;
1467
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001468 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001469 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001470 ssl->transform_in->ivlen,
1471 ssl->in_msg, ssl->in_msglen,
1472 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001473 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001474 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001475 return( ret );
1476 }
1477
1478 if( ssl->in_msglen != olen )
1479 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001480 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001481 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001482 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001483 }
Paul Bakker68884e32013-01-07 18:20:04 +01001484 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001485#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001486#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1487 if( mode == POLARSSL_MODE_GCM ||
1488 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001489 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001490 int ret;
1491 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001492 unsigned char *dec_msg;
1493 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001494 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001495 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1496 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Simon Ba697bf52016-11-10 13:19:42 +00001497 size_t explicit_iv_len = ssl->transform_in->ivlen -
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001498 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001499
Manuel Pégourié-Gonnard06d75192015-02-11 14:54:11 +00001500 if( ssl->in_msglen < (size_t) explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001501 {
1502 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1503 "+ taglen (%d)", ssl->in_msglen,
1504 explicit_iv_len, taglen ) );
1505 return( POLARSSL_ERR_SSL_INVALID_MAC );
1506 }
1507 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1508
Paul Bakker68884e32013-01-07 18:20:04 +01001509 dec_msg = ssl->in_msg;
1510 dec_msg_result = ssl->in_msg;
1511 ssl->in_msglen = dec_msglen;
1512
1513 memcpy( add_data, ssl->in_ctr, 8 );
1514 add_data[8] = ssl->in_msgtype;
1515 add_data[9] = ssl->major_ver;
1516 add_data[10] = ssl->minor_ver;
1517 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1518 add_data[12] = ssl->in_msglen & 0xFF;
1519
1520 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1521 add_data, 13 );
1522
1523 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1524 ssl->in_iv,
1525 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1526
1527 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1528 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001529 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001530
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001531 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001532 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001533 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001534 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1535 ssl->transform_in->iv_dec,
1536 ssl->transform_in->ivlen,
1537 add_data, 13,
1538 dec_msg, dec_msglen,
1539 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001540 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001541 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001542 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1543
1544 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1545 return( POLARSSL_ERR_SSL_INVALID_MAC );
1546
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001547 return( ret );
1548 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001549 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001550
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001551 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001552 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001553 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001554 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001555 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001556 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001557 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001558#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001559#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1560 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001561 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001562 {
Paul Bakker45829992013-01-03 14:52:21 +01001563 /*
1564 * Decrypt and check the padding
1565 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001566 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001567 unsigned char *dec_msg;
1568 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001569 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001570 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001571 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001572
Paul Bakker5121ce52009-01-03 21:22:43 +00001573 /*
Paul Bakker45829992013-01-03 14:52:21 +01001574 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001575 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001576#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001577 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1578 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001579#endif
Paul Bakker45829992013-01-03 14:52:21 +01001580
1581 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1582 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1583 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001584 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1585 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1586 ssl->transform_in->ivlen,
1587 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001588 return( POLARSSL_ERR_SSL_INVALID_MAC );
1589 }
1590
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001591 dec_msglen = ssl->in_msglen;
1592 dec_msg = ssl->in_msg;
1593 dec_msg_result = ssl->in_msg;
1594
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001595 /*
1596 * Authenticate before decrypt if enabled
1597 */
1598#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001599 if( ssl->session_in->encrypt_then_mac == SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001600 {
Hanno Becker251bab52017-11-20 10:30:08 +00001601 unsigned char mac_expect[SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001602 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001603
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001604 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1605
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001606 dec_msglen -= ssl->transform_in->maclen;
1607 ssl->in_msglen -= ssl->transform_in->maclen;
1608
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001609 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1610 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1611 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1612 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1613
1614 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1615
1616 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001617 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1618 ssl->in_iv, ssl->in_msglen );
Hanno Becker251bab52017-11-20 10:30:08 +00001619 md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001620 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1621
1622 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1623 ssl->transform_in->maclen );
Hanno Becker0a139f92017-11-21 17:41:59 +00001624 SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001625 ssl->transform_in->maclen );
1626
Hanno Becker251bab52017-11-20 10:30:08 +00001627 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001628 ssl->transform_in->maclen ) != 0 )
1629 {
1630 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1631
1632 return( POLARSSL_ERR_SSL_INVALID_MAC );
1633 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001634 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001635 }
1636#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1637
1638 /*
1639 * Check length sanity
1640 */
1641 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1642 {
1643 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1644 ssl->in_msglen, ssl->transform_in->ivlen ) );
1645 return( POLARSSL_ERR_SSL_INVALID_MAC );
1646 }
1647
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001648#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001649 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001650 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001651 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001652 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001653 {
Paul Bakker48916f92012-09-16 19:57:18 +00001654 dec_msglen -= ssl->transform_in->ivlen;
1655 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001656
Paul Bakker48916f92012-09-16 19:57:18 +00001657 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001658 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001659 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001660#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001661
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001662 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001663 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001664 ssl->transform_in->ivlen,
1665 dec_msg, dec_msglen,
1666 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001667 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001668 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001669 return( ret );
1670 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001671
Paul Bakkercca5b812013-08-31 17:40:26 +02001672 if( dec_msglen != olen )
1673 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001674 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001675 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001676 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001677
1678#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001679 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1680 {
1681 /*
1682 * Save IV in SSL3 and TLS1
1683 */
1684 memcpy( ssl->transform_in->iv_dec,
1685 ssl->transform_in->cipher_ctx_dec.iv,
1686 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001687 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001688#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001689
1690 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001691
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001692 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001693 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001694 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001695#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001696 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1697 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001698#endif
Paul Bakker45829992013-01-03 14:52:21 +01001699 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001700 correct = 0;
1701 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001702
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001703#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001704 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1705 {
Paul Bakker48916f92012-09-16 19:57:18 +00001706 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001707 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001708#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001709 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1710 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001711 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001712#endif
Paul Bakker45829992013-01-03 14:52:21 +01001713 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001714 }
1715 }
1716 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001717#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001718#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1719 defined(POLARSSL_SSL_PROTO_TLS1_2)
1720 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001721 {
1722 /*
Paul Bakker45829992013-01-03 14:52:21 +01001723 * TLSv1+: always check the padding up to the first failure
1724 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001725 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001726 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001727 size_t padding_idx = ssl->in_msglen - padlen - 1;
1728
Paul Bakker956c9e02013-12-19 14:42:28 +01001729 /*
1730 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001731 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001732 *
Paul Bakker61885c72014-04-25 12:59:03 +02001733 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1734 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001735 *
1736 * In both cases we reset padding_idx to a safe value (0) to
1737 * prevent out-of-buffer reads.
1738 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001739 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001740 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1741 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001742
1743 padding_idx *= correct;
1744
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001745 for( i = 1; i <= 256; i++ )
1746 {
1747 real_count &= ( i <= padlen );
1748 pad_count += real_count *
1749 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1750 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001751
1752 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001753
Paul Bakkerd66f0702013-01-31 16:57:45 +01001754#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001755 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001756 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001757#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001758 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001759 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001760 else
1761#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1762 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001763 {
1764 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001765 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001766 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001767
1768 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001769 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001770 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001771#endif /* POLARSSL_CIPHER_MODE_CBC &&
1772 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001773 {
1774 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001775 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001776 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001777
1778 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1779 ssl->in_msg, ssl->in_msglen );
1780
1781 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001782 * Authenticate if not done yet.
1783 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001784 */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001785#if defined(POLARSSL_SOME_MODES_USE_MAC)
1786 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001787 {
Hanno Becker251bab52017-11-20 10:30:08 +00001788 unsigned char mac_expect[SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01001789
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001790 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001791
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001792 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1793 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001794
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001795#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001796 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1797 {
1798 ssl_mac( &ssl->transform_in->md_ctx_dec,
1799 ssl->transform_in->mac_dec,
1800 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard3ea75b32017-12-18 18:04:59 +01001801 ssl->in_ctr, ssl->in_msgtype,
1802 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001803 }
1804 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001805#endif /* POLARSSL_SSL_PROTO_SSL3 */
1806#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001807 defined(POLARSSL_SSL_PROTO_TLS1_2)
1808 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1809 {
1810 /*
1811 * Process MAC and always update for padlen afterwards to make
1812 * total time independent of padlen
1813 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001814 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001815 *
1816 * Known timing attacks:
1817 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1818 *
1819 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1820 * correctly. (We round down instead of up, so -56 is the correct
1821 * value for our calculations instead of -55)
1822 */
1823 size_t j, extra_run = 0;
1824 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1825 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001826
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001827 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001828
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001829 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1830 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1831 ssl->in_msglen );
Hanno Becker251bab52017-11-20 10:30:08 +00001832 md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
1833
Manuel Pégourié-Gonnard7d1e95c2015-04-29 01:35:48 +02001834 /* Call md_process at least once due to cache attacks */
1835 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001836 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001837
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001838 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1839 }
1840 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001841#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001842 POLARSSL_SSL_PROTO_TLS1_2 */
1843 {
1844 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001845 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001846 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001847
Hanno Becker251bab52017-11-20 10:30:08 +00001848 SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
1849 SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001850 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001851
Hanno Becker251bab52017-11-20 10:30:08 +00001852 if( safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001853 ssl->transform_in->maclen ) != 0 )
1854 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001855#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001856 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001857#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001858 correct = 0;
1859 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001860 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001861
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001862 /*
1863 * Finally check the correct flag
1864 */
1865 if( correct == 0 )
1866 return( POLARSSL_ERR_SSL_INVALID_MAC );
1867 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001868#endif /* POLARSSL_SOME_MODES_USE_MAC */
1869
1870 /* Make extra sure authentication was performed, exactly once */
1871 if( auth_done != 1 )
1872 {
1873 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1874 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1875 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001876
1877 if( ssl->in_msglen == 0 )
1878 {
1879 ssl->nb_zero++;
1880
1881 /*
1882 * Three or more empty messages may be a DoS attack
1883 * (excessive CPU consumption).
1884 */
1885 if( ssl->nb_zero > 3 )
1886 {
1887 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1888 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001889 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001890 }
1891 }
1892 else
1893 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001894
Paul Bakker23986e52011-04-24 08:57:21 +00001895 for( i = 8; i > 0; i-- )
1896 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001897 break;
1898
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001899 /* The loops goes to its end iff the counter is wrapping */
1900 if( i == 0 )
1901 {
1902 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1903 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1904 }
1905
Paul Bakker5121ce52009-01-03 21:22:43 +00001906 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1907
1908 return( 0 );
1909}
1910
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001911#undef MAC_NONE
1912#undef MAC_PLAINTEXT
1913#undef MAC_CIPHERTEXT
1914
Paul Bakker2770fbd2012-07-03 13:30:23 +00001915#if defined(POLARSSL_ZLIB_SUPPORT)
1916/*
1917 * Compression/decompression functions
1918 */
1919static int ssl_compress_buf( ssl_context *ssl )
1920{
1921 int ret;
1922 unsigned char *msg_post = ssl->out_msg;
1923 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001924 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001925
1926 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1927
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001928 if( len_pre == 0 )
1929 return( 0 );
1930
Paul Bakker2770fbd2012-07-03 13:30:23 +00001931 memcpy( msg_pre, ssl->out_msg, len_pre );
1932
1933 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1934 ssl->out_msglen ) );
1935
1936 SSL_DEBUG_BUF( 4, "before compression: output payload",
1937 ssl->out_msg, ssl->out_msglen );
1938
Paul Bakker48916f92012-09-16 19:57:18 +00001939 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1940 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1941 ssl->transform_out->ctx_deflate.next_out = msg_post;
1942 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001943
Paul Bakker48916f92012-09-16 19:57:18 +00001944 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001945 if( ret != Z_OK )
1946 {
1947 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1948 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1949 }
1950
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001951 ssl->out_msglen = SSL_BUFFER_LEN -
1952 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001953
Paul Bakker2770fbd2012-07-03 13:30:23 +00001954 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1955 ssl->out_msglen ) );
1956
1957 SSL_DEBUG_BUF( 4, "after compression: output payload",
1958 ssl->out_msg, ssl->out_msglen );
1959
1960 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1961
1962 return( 0 );
1963}
1964
1965static int ssl_decompress_buf( ssl_context *ssl )
1966{
1967 int ret;
1968 unsigned char *msg_post = ssl->in_msg;
1969 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001970 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001971
1972 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1973
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001974 if( len_pre == 0 )
1975 return( 0 );
1976
Paul Bakker2770fbd2012-07-03 13:30:23 +00001977 memcpy( msg_pre, ssl->in_msg, len_pre );
1978
1979 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1980 ssl->in_msglen ) );
1981
1982 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1983 ssl->in_msg, ssl->in_msglen );
1984
Paul Bakker48916f92012-09-16 19:57:18 +00001985 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1986 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1987 ssl->transform_in->ctx_inflate.next_out = msg_post;
1988 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001989
Paul Bakker48916f92012-09-16 19:57:18 +00001990 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001991 if( ret != Z_OK )
1992 {
1993 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1994 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1995 }
1996
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001997 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1998 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001999
Paul Bakker2770fbd2012-07-03 13:30:23 +00002000 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
2001 ssl->in_msglen ) );
2002
2003 SSL_DEBUG_BUF( 4, "after decompression: input payload",
2004 ssl->in_msg, ssl->in_msglen );
2005
2006 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
2007
2008 return( 0 );
2009}
2010#endif /* POLARSSL_ZLIB_SUPPORT */
2011
Paul Bakker5121ce52009-01-03 21:22:43 +00002012/*
2013 * Fill the input message buffer
2014 */
Paul Bakker23986e52011-04-24 08:57:21 +00002015int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002016{
Paul Bakker23986e52011-04-24 08:57:21 +00002017 int ret;
2018 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002019
2020 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
2021
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002022 if( nb_want > SSL_BUFFER_LEN - 8 )
2023 {
2024 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2025 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2026 }
2027
Paul Bakker5121ce52009-01-03 21:22:43 +00002028 while( ssl->in_left < nb_want )
2029 {
2030 len = nb_want - ssl->in_left;
2031 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
2032
2033 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2034 ssl->in_left, nb_want ) );
2035 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2036
Paul Bakker831a7552011-05-18 13:32:51 +00002037 if( ret == 0 )
2038 return( POLARSSL_ERR_SSL_CONN_EOF );
2039
Paul Bakker5121ce52009-01-03 21:22:43 +00002040 if( ret < 0 )
2041 return( ret );
2042
2043 ssl->in_left += ret;
2044 }
2045
2046 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2047
2048 return( 0 );
2049}
2050
2051/*
2052 * Flush any data not yet written
2053 */
2054int ssl_flush_output( ssl_context *ssl )
2055{
2056 int ret;
2057 unsigned char *buf;
2058
2059 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2060
2061 while( ssl->out_left > 0 )
2062 {
2063 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2064 5 + ssl->out_msglen, ssl->out_left ) );
2065
Paul Bakker5bd42292012-12-19 14:40:42 +01002066 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00002067 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002068
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2070
2071 if( ret <= 0 )
2072 return( ret );
2073
2074 ssl->out_left -= ret;
2075 }
2076
2077 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2078
2079 return( 0 );
2080}
2081
2082/*
2083 * Record layer functions
2084 */
2085int ssl_write_record( ssl_context *ssl )
2086{
Paul Bakker05ef8352012-05-08 09:17:57 +00002087 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002088 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002089
2090 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2091
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2093 {
2094 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2095 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2096 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2097
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002098 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2099 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002100 }
2101
Paul Bakker2770fbd2012-07-03 13:30:23 +00002102#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002103 if( ssl->transform_out != NULL &&
2104 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002105 {
2106 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2107 {
2108 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2109 return( ret );
2110 }
2111
2112 len = ssl->out_msglen;
2113 }
2114#endif /*POLARSSL_ZLIB_SUPPORT */
2115
Paul Bakker05ef8352012-05-08 09:17:57 +00002116#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002117 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002118 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002119 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002120
Paul Bakker05ef8352012-05-08 09:17:57 +00002121 ret = ssl_hw_record_write( ssl );
2122 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2123 {
2124 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002125 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002126 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002127
2128 if( ret == 0 )
2129 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002130 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002131#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002132 if( !done )
2133 {
2134 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2135 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2136 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002137 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2138 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002139
Paul Bakker48916f92012-09-16 19:57:18 +00002140 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002141 {
2142 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2143 {
2144 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2145 return( ret );
2146 }
2147
2148 len = ssl->out_msglen;
2149 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2150 ssl->out_hdr[4] = (unsigned char)( len );
2151 }
2152
2153 ssl->out_left = 5 + ssl->out_msglen;
2154
2155 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2156 "version = [%d:%d], msglen = %d",
2157 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2158 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2159
2160 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002161 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002162 }
2163
Paul Bakker5121ce52009-01-03 21:22:43 +00002164 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2165 {
2166 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2167 return( ret );
2168 }
2169
2170 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2171
2172 return( 0 );
2173}
2174
2175int ssl_read_record( ssl_context *ssl )
2176{
Paul Bakker05ef8352012-05-08 09:17:57 +00002177 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002178
2179 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2180
Hanno Becker10699cc2017-06-08 15:41:02 +01002181 if( ssl->keep_current_message == 1 )
2182 {
2183 SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
2184 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2185 ssl->keep_current_message = 0;
2186
2187 return( 0 );
2188 }
2189
Hanno Beckerd37839e2017-06-08 15:56:50 +01002190 if( ssl->in_hslen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002191 {
Hanno Becker1bf86b72017-06-08 15:58:02 +01002192 if( ssl->in_offt != NULL )
2193 {
2194 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2195 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2196 }
2197
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 /*
2199 * Get next Handshake message in the current record
2200 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002201
Hanno Beckerd37839e2017-06-08 15:56:50 +01002202 if( ssl->in_hslen < ssl->in_msglen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002203 {
Hanno Beckerd37839e2017-06-08 15:56:50 +01002204 ssl->in_msglen -= ssl->in_hslen;
2205 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
2206 ssl->in_msglen );
2207
2208 ssl->in_hslen = 4;
2209 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2210
2211 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2212 " %d, type = %d, hslen = %d",
2213 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2214
2215 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2216 {
2217 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
2218 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2219 }
2220
2221 if( ssl->in_msglen < ssl->in_hslen )
2222 {
2223 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
2224 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2225 }
2226
2227 if( ssl->state != SSL_HANDSHAKE_OVER )
2228 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
2229
2230 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002231 }
2232
Hanno Beckerd37839e2017-06-08 15:56:50 +01002233 ssl->in_msglen = 0;
2234 ssl->in_hslen = 0;
2235 }
2236 else if( ssl->in_offt != NULL )
2237 {
2238 return( 0 );
2239 }
2240 else
2241 {
2242 ssl->in_msglen = 0;
2243 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002244
Hanno Beckerd37839e2017-06-08 15:56:50 +01002245 /*
2246 * Fetch and decode new record if current one is fully consumed.
2247 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002248
Hanno Beckerd37839e2017-06-08 15:56:50 +01002249 if( ssl->in_msglen > 0 )
2250 {
2251 /* There's something left to be processed in the current record. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002252 return( 0 );
2253 }
2254
Hanno Beckerd37839e2017-06-08 15:56:50 +01002255 /* Need to fetch a new record */
Paul Bakker5121ce52009-01-03 21:22:43 +00002256
Manuel Pégourié-Gonnard0aaefce2015-10-27 13:42:11 +01002257read_record_header:
Paul Bakker5121ce52009-01-03 21:22:43 +00002258 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2259 {
2260 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2261 return( ret );
2262 }
2263
2264 ssl->in_msgtype = ssl->in_hdr[0];
2265 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2266
2267 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2268 "version = [%d:%d], msglen = %d",
2269 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2270 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2271
2272 if( ssl->in_hdr[1] != ssl->major_ver )
2273 {
2274 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002275 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002276 }
2277
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002278 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002279 {
2280 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002281 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002282 }
2283
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002284 /* Sanity check (outer boundaries) */
2285 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2286 {
2287 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2288 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2289 }
2290
Paul Bakker5121ce52009-01-03 21:22:43 +00002291 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002292 * Make sure the message length is acceptable for the current transform
2293 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002294 */
Paul Bakker48916f92012-09-16 19:57:18 +00002295 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002296 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002297 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002298 {
2299 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002300 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002301 }
2302 }
2303 else
2304 {
Paul Bakker48916f92012-09-16 19:57:18 +00002305 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002306 {
2307 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002308 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002309 }
2310
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002311#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002312 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002313 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002314 {
2315 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002316 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002317 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002318#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002319
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002320#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2321 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002322 /*
2323 * TLS encrypted messages can have up to 256 bytes of padding
2324 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002325 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002326 ssl->in_msglen > ssl->transform_in->minlen +
2327 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002328 {
2329 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002330 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002331 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002332#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002333 }
2334
2335 /*
2336 * Read and optionally decrypt the message contents
2337 */
2338 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2339 {
2340 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2341 return( ret );
2342 }
2343
2344 SSL_DEBUG_BUF( 4, "input record from network",
2345 ssl->in_hdr, 5 + ssl->in_msglen );
2346
Paul Bakker05ef8352012-05-08 09:17:57 +00002347#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002348 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002349 {
2350 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2351
2352 ret = ssl_hw_record_read( ssl );
2353 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2354 {
2355 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002356 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002357 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002358
2359 if( ret == 0 )
2360 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002361 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002362#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002363 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002364 {
2365 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2366 {
Paul Bakker40865c82013-01-31 17:13:13 +01002367#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2368 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2369 {
2370 ssl_send_alert_message( ssl,
2371 SSL_ALERT_LEVEL_FATAL,
2372 SSL_ALERT_MSG_BAD_RECORD_MAC );
2373 }
2374#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002375 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2376 return( ret );
2377 }
2378
2379 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2380 ssl->in_msg, ssl->in_msglen );
2381
2382 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2383 {
2384 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002385 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002386 }
2387 }
2388
Paul Bakker2770fbd2012-07-03 13:30:23 +00002389#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002390 if( ssl->transform_in != NULL &&
2391 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002392 {
2393 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2394 {
2395 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2396 return( ret );
2397 }
2398
2399 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2400 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2401 }
2402#endif /* POLARSSL_ZLIB_SUPPORT */
2403
Paul Bakker0a925182012-04-16 06:46:41 +00002404 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2405 ssl->in_msgtype != SSL_MSG_ALERT &&
2406 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2407 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2408 {
2409 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2410
Paul Bakker48916f92012-09-16 19:57:18 +00002411 if( ( ret = ssl_send_alert_message( ssl,
2412 SSL_ALERT_LEVEL_FATAL,
2413 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002414 {
2415 return( ret );
2416 }
2417
2418 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2419 }
2420
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2422 {
2423 ssl->in_hslen = 4;
2424 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2425
2426 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2427 " %d, type = %d, hslen = %d",
2428 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2429
2430 /*
2431 * Additional checks to validate the handshake header
2432 */
2433 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2434 {
2435 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002436 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002437 }
2438
2439 if( ssl->in_msglen < ssl->in_hslen )
2440 {
2441 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002442 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002443 }
2444
Paul Bakker48916f92012-09-16 19:57:18 +00002445 if( ssl->state != SSL_HANDSHAKE_OVER )
2446 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 }
2448
2449 if( ssl->in_msgtype == SSL_MSG_ALERT )
2450 {
2451 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2452 ssl->in_msg[0], ssl->in_msg[1] ) );
2453
2454 /*
Manuel Pégourié-Gonnard0aaefce2015-10-27 13:42:11 +01002455 * Ignore non-fatal alerts, except close_notify and no_renego
Paul Bakker5121ce52009-01-03 21:22:43 +00002456 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002457 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002459 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2460 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002461 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002462 }
2463
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002464 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2465 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002466 {
2467 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002468 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002469 }
Manuel Pégourié-Gonnard0aaefce2015-10-27 13:42:11 +01002470
2471 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2472 ssl->in_msg[1] == SSL_ALERT_MSG_NO_RENEGOTIATION )
2473 {
2474 SSL_DEBUG_MSG( 2, ( "is a no_renegotiation" ) );
2475 /* Will be handled when trying to parse ServerHello */
2476 ssl->in_left = 0;
2477 return( 0 );
2478 }
2479
2480 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
2481 ssl->endpoint == SSL_IS_SERVER &&
2482 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2483 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
2484 {
2485 SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
2486 /* Will be handled in ssl_parse_certificate() */
2487 ssl->in_left = 0;
2488 return( 0 );
2489 }
2490
2491 /* Silently discard: fetch new message */
2492 goto read_record_header;
Paul Bakker5121ce52009-01-03 21:22:43 +00002493 }
2494
2495 ssl->in_left = 0;
2496
2497 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2498
2499 return( 0 );
2500}
2501
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002502int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2503{
2504 int ret;
2505
2506 if( ( ret = ssl_send_alert_message( ssl,
2507 SSL_ALERT_LEVEL_FATAL,
2508 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2509 {
2510 return( ret );
2511 }
2512
2513 return( 0 );
2514}
2515
Paul Bakker0a925182012-04-16 06:46:41 +00002516int ssl_send_alert_message( ssl_context *ssl,
2517 unsigned char level,
2518 unsigned char message )
2519{
2520 int ret;
2521
2522 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2523
2524 ssl->out_msgtype = SSL_MSG_ALERT;
2525 ssl->out_msglen = 2;
2526 ssl->out_msg[0] = level;
2527 ssl->out_msg[1] = message;
2528
2529 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2530 {
2531 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2532 return( ret );
2533 }
2534
2535 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2536
2537 return( 0 );
2538}
2539
Paul Bakker5121ce52009-01-03 21:22:43 +00002540/*
2541 * Handshake functions
2542 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002543#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2544 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2545 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2546 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2547 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2548 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2549 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002550int ssl_write_certificate( ssl_context *ssl )
2551{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002552 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002553
2554 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2555
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002556 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002557 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2558 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002559 {
2560 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2561 ssl->state++;
2562 return( 0 );
2563 }
2564
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002565 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2566 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002567}
2568
2569int ssl_parse_certificate( ssl_context *ssl )
2570{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002571 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2572
2573 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2574
2575 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002576 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2577 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002578 {
2579 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2580 ssl->state++;
2581 return( 0 );
2582 }
2583
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002584 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2585 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002586}
2587#else
2588int ssl_write_certificate( ssl_context *ssl )
2589{
2590 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2591 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002592 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002593 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2594
2595 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2596
2597 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002598 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2599 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002600 {
2601 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2602 ssl->state++;
2603 return( 0 );
2604 }
2605
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002606#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002607 if( ssl->endpoint == SSL_IS_CLIENT )
2608 {
2609 if( ssl->client_auth == 0 )
2610 {
2611 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2612 ssl->state++;
2613 return( 0 );
2614 }
2615
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002616#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002617 /*
2618 * If using SSLv3 and got no cert, send an Alert message
2619 * (otherwise an empty Certificate message will be sent).
2620 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002621 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002622 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2623 {
2624 ssl->out_msglen = 2;
2625 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002626 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2627 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002628
2629 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2630 goto write_msg;
2631 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002632#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002633 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002634#endif /* POLARSSL_SSL_CLI_C */
2635#if defined(POLARSSL_SSL_SRV_C)
2636 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00002637 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002638 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002639 {
2640 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002641 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002642 }
2643 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002644#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002645
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002646 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002647
2648 /*
2649 * 0 . 0 handshake type
2650 * 1 . 3 handshake length
2651 * 4 . 6 length of all certs
2652 * 7 . 9 length of cert. 1
2653 * 10 . n-1 peer certificate
2654 * n . n+2 length of cert. 2
2655 * n+3 . ... upper level cert, etc.
2656 */
2657 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002658 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
Paul Bakker29087132010-03-21 21:03:34 +00002660 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002661 {
2662 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002663 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002664 {
2665 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2666 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002667 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002668 }
2669
2670 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2671 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2672 ssl->out_msg[i + 2] = (unsigned char)( n );
2673
2674 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2675 i += n; crt = crt->next;
2676 }
2677
2678 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2679 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2680 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2681
2682 ssl->out_msglen = i;
2683 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2684 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2685
Simon Butcher149950d2016-10-15 22:08:08 +01002686#if defined(POLARSSL_SSL_PROTO_SSL3) && defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002687write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002688#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002689
2690 ssl->state++;
2691
2692 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2693 {
2694 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2695 return( ret );
2696 }
2697
2698 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2699
Paul Bakkered27a042013-04-18 22:46:23 +02002700 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002701}
2702
2703int ssl_parse_certificate( ssl_context *ssl )
2704{
Paul Bakkered27a042013-04-18 22:46:23 +02002705 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002706 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002707 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
2709 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2710
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002711 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002712 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2713 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002714 {
2715 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2716 ssl->state++;
2717 return( 0 );
2718 }
2719
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002720#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002721 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002722 ( ssl->authmode == SSL_VERIFY_NONE ||
2723 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002724 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002725 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002726 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2727 ssl->state++;
2728 return( 0 );
2729 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002730#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002731
2732 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2733 {
2734 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2735 return( ret );
2736 }
2737
2738 ssl->state++;
2739
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002740#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002741#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002742 /*
2743 * Check if the client sent an empty certificate
2744 */
2745 if( ssl->endpoint == SSL_IS_SERVER &&
2746 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2747 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002748 if( ssl->in_msglen == 2 &&
2749 ssl->in_msgtype == SSL_MSG_ALERT &&
2750 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2751 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002752 {
2753 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2754
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002755 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002756 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2757 return( 0 );
2758 else
Paul Bakker40e46942009-01-03 21:51:57 +00002759 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002760 }
2761 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002762#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002763
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002764#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2765 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002766 if( ssl->endpoint == SSL_IS_SERVER &&
2767 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2768 {
2769 if( ssl->in_hslen == 7 &&
2770 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2771 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2772 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2773 {
2774 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2775
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002776 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002777 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002778 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002779 else
2780 return( 0 );
2781 }
2782 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002783#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2784 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002785#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002786
2787 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2788 {
2789 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002790 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002791 }
2792
2793 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2794 {
2795 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002796 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002797 }
2798
2799 /*
2800 * Same message structure as in ssl_write_certificate()
2801 */
2802 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2803
2804 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2805 {
2806 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002807 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002808 }
2809
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002810 /* In case we tried to reuse a session but it failed */
2811 if( ssl->session_negotiate->peer_cert != NULL )
2812 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002813 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002814 polarssl_free( ssl->session_negotiate->peer_cert );
2815 }
2816
Mansour Moufidc531b4a2015-02-15 17:35:38 -05002817 if( ( ssl->session_negotiate->peer_cert = polarssl_malloc(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002818 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002819 {
2820 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002821 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002822 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002823 }
2824
Paul Bakkerb6b09562013-09-18 14:17:41 +02002825 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002826
2827 i = 7;
2828
2829 while( i < ssl->in_hslen )
2830 {
2831 if( ssl->in_msg[i] != 0 )
2832 {
2833 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002834 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002835 }
2836
2837 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2838 | (unsigned int) ssl->in_msg[i + 2];
2839 i += 3;
2840
2841 if( n < 128 || i + n > ssl->in_hslen )
2842 {
2843 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002844 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002845 }
2846
Paul Bakkerddf26b42013-09-18 13:46:23 +02002847 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2848 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002849 if( ret != 0 )
2850 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002851 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002852 return( ret );
2853 }
2854
2855 i += n;
2856 }
2857
Paul Bakker48916f92012-09-16 19:57:18 +00002858 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002859
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002860 /*
2861 * On client, make sure the server cert doesn't change during renego to
2862 * avoid "triple handshake" attack: https://secure-resumption.com/
2863 */
Paul Bakkerf6080b82015-01-13 16:18:23 +01002864#if defined(POLARSSL_SSL_RENEGOTIATION) && defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002865 if( ssl->endpoint == SSL_IS_CLIENT &&
2866 ssl->renegotiation == SSL_RENEGOTIATION )
2867 {
2868 if( ssl->session->peer_cert == NULL )
2869 {
2870 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2871 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2872 }
2873
2874 if( ssl->session->peer_cert->raw.len !=
2875 ssl->session_negotiate->peer_cert->raw.len ||
2876 memcmp( ssl->session->peer_cert->raw.p,
2877 ssl->session_negotiate->peer_cert->raw.p,
2878 ssl->session->peer_cert->raw.len ) != 0 )
2879 {
2880 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2881 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2882 }
2883 }
Paul Bakkerf6080b82015-01-13 16:18:23 +01002884#endif /* POLARSSL_SSL_RENEGOTIATION && POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002885
Paul Bakker5121ce52009-01-03 21:22:43 +00002886 if( ssl->authmode != SSL_VERIFY_NONE )
2887 {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002888 /*
2889 * Main check: verify certificate
2890 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002891 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2892 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2893 &ssl->session_negotiate->verify_result,
2894 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002895
2896 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002897 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002898 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002899 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002900
2901 /*
2902 * Secondary checks: always done, but change 'ret' only if it was 0
2903 */
2904
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002905#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002906 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002907 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002908
2909 /* If certificate uses an EC key, make sure the curve is OK */
2910 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2911 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2912 {
Hanno Becker888c2fd2017-05-11 11:12:40 +01002913 ssl->session_negotiate->verify_result |= BADCERT_BAD_KEY;
2914
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002915 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002916 if( ret == 0 )
2917 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002918 }
2919 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002920#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002921
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002922 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2923 ciphersuite_info,
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02002924 ! ssl->endpoint,
2925 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002926 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002927 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002928 if( ret == 0 )
2929 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2930 }
2931
Hanno Becker888c2fd2017-05-11 11:12:40 +01002932 /* x509_crt_verify_with_profile is supposed to report a
2933 * verification failure through POLARSSL_ERR_X509_CERT_VERIFY_FAILED,
2934 * with details encoded in the verification flags. All other kinds
2935 * of error codes, including those from the user provided f_vrfy
2936 * functions, are treated as fatal and lead to a failure of
2937 * ssl_parse_certificate even if verification was optional. */
2938 if( ssl->authmode == SSL_VERIFY_OPTIONAL &&
2939 ( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED ||
2940 ret == POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE ) )
2941 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002942 ret = 0;
Hanno Becker888c2fd2017-05-11 11:12:40 +01002943 }
2944
2945 if( ssl->ca_chain == NULL && ssl->authmode == SSL_VERIFY_REQUIRED )
2946 {
2947 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
2948 ret = POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED;
2949 }
2950
2951#if defined(POLARSSL_DEBUG_C)
2952 if( ssl->session_negotiate->verify_result != 0 )
2953 {
2954 SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
2955 ssl->session_negotiate->verify_result ) );
2956 }
2957 else
2958 {
2959 SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
2960 }
2961#endif /* POLARSSL_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002962 }
2963
2964 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2965
2966 return( ret );
2967}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002968#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2969 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2970 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2971 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2972 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2973 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2974 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002975
2976int ssl_write_change_cipher_spec( ssl_context *ssl )
2977{
2978 int ret;
2979
2980 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2981
2982 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2983 ssl->out_msglen = 1;
2984 ssl->out_msg[0] = 1;
2985
Paul Bakker5121ce52009-01-03 21:22:43 +00002986 ssl->state++;
2987
2988 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2989 {
2990 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2991 return( ret );
2992 }
2993
2994 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2995
2996 return( 0 );
2997}
2998
2999int ssl_parse_change_cipher_spec( ssl_context *ssl )
3000{
3001 int ret;
3002
3003 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
3004
Paul Bakker5121ce52009-01-03 21:22:43 +00003005 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3006 {
3007 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3008 return( ret );
3009 }
3010
3011 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
3012 {
3013 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003014 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003015 }
3016
3017 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
3018 {
3019 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003020 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00003021 }
3022
3023 ssl->state++;
3024
3025 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
3026
3027 return( 0 );
3028}
3029
Paul Bakker41c83d32013-03-20 14:39:14 +01003030void ssl_optimize_checksum( ssl_context *ssl,
3031 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00003032{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003033 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003034
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003035#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3036 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00003037 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00003038 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00003039 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003040#endif
3041#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3042#if defined(POLARSSL_SHA512_C)
3043 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
3044 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
3045 else
3046#endif
3047#if defined(POLARSSL_SHA256_C)
3048 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00003049 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003050 else
3051#endif
3052#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003053 {
3054 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003055 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003056 }
Paul Bakker380da532012-04-18 16:10:25 +00003057}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003058
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003059static void ssl_update_checksum_start( ssl_context *ssl,
3060 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003061{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003062#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3063 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003064 md5_update( &ssl->handshake->fin_md5 , buf, len );
3065 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003066#endif
3067#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3068#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003069 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003070#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003071#if defined(POLARSSL_SHA512_C)
3072 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003073#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003074#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003075}
3076
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003077#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3078 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003079static void ssl_update_checksum_md5sha1( ssl_context *ssl,
3080 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003081{
Paul Bakker48916f92012-09-16 19:57:18 +00003082 md5_update( &ssl->handshake->fin_md5 , buf, len );
3083 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003084}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003085#endif
Paul Bakker380da532012-04-18 16:10:25 +00003086
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003087#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3088#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003089static void ssl_update_checksum_sha256( ssl_context *ssl,
3090 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003091{
Paul Bakker9e36f042013-06-30 14:34:05 +02003092 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003093}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003094#endif
Paul Bakker380da532012-04-18 16:10:25 +00003095
Paul Bakker9e36f042013-06-30 14:34:05 +02003096#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003097static void ssl_update_checksum_sha384( ssl_context *ssl,
3098 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003099{
Paul Bakker9e36f042013-06-30 14:34:05 +02003100 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003101}
Paul Bakker769075d2012-11-24 11:26:46 +01003102#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003103#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003104
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003105#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003106static void ssl_calc_finished_ssl(
3107 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003108{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003109 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003110 md5_context md5;
3111 sha1_context sha1;
3112
Paul Bakker5121ce52009-01-03 21:22:43 +00003113 unsigned char padbuf[48];
3114 unsigned char md5sum[16];
3115 unsigned char sha1sum[20];
3116
Paul Bakker48916f92012-09-16 19:57:18 +00003117 ssl_session *session = ssl->session_negotiate;
3118 if( !session )
3119 session = ssl->session;
3120
Paul Bakker1ef83d62012-04-11 12:09:53 +00003121 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3122
Paul Bakker48916f92012-09-16 19:57:18 +00003123 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3124 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003125
3126 /*
3127 * SSLv3:
3128 * hash =
3129 * MD5( master + pad2 +
3130 * MD5( handshake + sender + master + pad1 ) )
3131 * + SHA1( master + pad2 +
3132 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003133 */
3134
Paul Bakker90995b52013-06-24 19:20:35 +02003135#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003136 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003137 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003138#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003139
Paul Bakker90995b52013-06-24 19:20:35 +02003140#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003141 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003142 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003143#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003144
Paul Bakker3c2122f2013-06-24 19:03:14 +02003145 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3146 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003147
Paul Bakker1ef83d62012-04-11 12:09:53 +00003148 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003149
Paul Bakker3c2122f2013-06-24 19:03:14 +02003150 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003151 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003152 md5_update( &md5, padbuf, 48 );
3153 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003154
Paul Bakker3c2122f2013-06-24 19:03:14 +02003155 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003156 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003157 sha1_update( &sha1, padbuf, 40 );
3158 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003159
Paul Bakker1ef83d62012-04-11 12:09:53 +00003160 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003161
Paul Bakker1ef83d62012-04-11 12:09:53 +00003162 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003163 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003164 md5_update( &md5, padbuf, 48 );
3165 md5_update( &md5, md5sum, 16 );
3166 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003167
Paul Bakker1ef83d62012-04-11 12:09:53 +00003168 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003169 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003170 sha1_update( &sha1, padbuf , 40 );
3171 sha1_update( &sha1, sha1sum, 20 );
3172 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003173
Paul Bakker1ef83d62012-04-11 12:09:53 +00003174 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003175
Paul Bakker5b4af392014-06-26 12:09:34 +02003176 md5_free( &md5 );
3177 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003178
Paul Bakker34617722014-06-13 17:20:13 +02003179 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3180 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3181 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003182
3183 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3184}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003185#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003186
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003187#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003188static void ssl_calc_finished_tls(
3189 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003190{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003191 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003192 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003193 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003194 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003195 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003196
Paul Bakker48916f92012-09-16 19:57:18 +00003197 ssl_session *session = ssl->session_negotiate;
3198 if( !session )
3199 session = ssl->session;
3200
Paul Bakker1ef83d62012-04-11 12:09:53 +00003201 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003202
Paul Bakker48916f92012-09-16 19:57:18 +00003203 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3204 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003205
Paul Bakker1ef83d62012-04-11 12:09:53 +00003206 /*
3207 * TLSv1:
3208 * hash = PRF( master, finished_label,
3209 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3210 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003211
Paul Bakker90995b52013-06-24 19:20:35 +02003212#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003213 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3214 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003215#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003216
Paul Bakker90995b52013-06-24 19:20:35 +02003217#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003218 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3219 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003220#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003221
3222 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003223 ? "client finished"
3224 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003225
3226 md5_finish( &md5, padbuf );
3227 sha1_finish( &sha1, padbuf + 16 );
3228
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003229 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003230 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003231
3232 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3233
Paul Bakker5b4af392014-06-26 12:09:34 +02003234 md5_free( &md5 );
3235 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003236
Paul Bakker34617722014-06-13 17:20:13 +02003237 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003238
3239 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3240}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003241#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003242
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003243#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3244#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003245static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003246 ssl_context *ssl, unsigned char *buf, int from )
3247{
3248 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003249 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003250 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003251 unsigned char padbuf[32];
3252
Paul Bakker48916f92012-09-16 19:57:18 +00003253 ssl_session *session = ssl->session_negotiate;
3254 if( !session )
3255 session = ssl->session;
3256
Paul Bakker380da532012-04-18 16:10:25 +00003257 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003258
Paul Bakker9e36f042013-06-30 14:34:05 +02003259 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003260
3261 /*
3262 * TLSv1.2:
3263 * hash = PRF( master, finished_label,
3264 * Hash( handshake ) )[0.11]
3265 */
3266
Paul Bakker9e36f042013-06-30 14:34:05 +02003267#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003268 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003269 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003270#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003271
3272 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003273 ? "client finished"
3274 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003275
Paul Bakker9e36f042013-06-30 14:34:05 +02003276 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003277
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003278 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003279 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003280
3281 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3282
Paul Bakker5b4af392014-06-26 12:09:34 +02003283 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003284
Paul Bakker34617722014-06-13 17:20:13 +02003285 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003286
3287 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3288}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003289#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003290
Paul Bakker9e36f042013-06-30 14:34:05 +02003291#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003292static void ssl_calc_finished_tls_sha384(
3293 ssl_context *ssl, unsigned char *buf, int from )
3294{
3295 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003296 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003297 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003298 unsigned char padbuf[48];
3299
Paul Bakker48916f92012-09-16 19:57:18 +00003300 ssl_session *session = ssl->session_negotiate;
3301 if( !session )
3302 session = ssl->session;
3303
Paul Bakker380da532012-04-18 16:10:25 +00003304 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003305
Paul Bakker9e36f042013-06-30 14:34:05 +02003306 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003307
3308 /*
3309 * TLSv1.2:
3310 * hash = PRF( master, finished_label,
3311 * Hash( handshake ) )[0.11]
3312 */
3313
Paul Bakker9e36f042013-06-30 14:34:05 +02003314#if !defined(POLARSSL_SHA512_ALT)
3315 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3316 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003317#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003318
3319 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003320 ? "client finished"
3321 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003322
Paul Bakker9e36f042013-06-30 14:34:05 +02003323 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003324
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003325 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003326 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003327
3328 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3329
Paul Bakker5b4af392014-06-26 12:09:34 +02003330 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003331
Paul Bakker34617722014-06-13 17:20:13 +02003332 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003333
3334 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3335}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003336#endif /* POLARSSL_SHA512_C */
3337#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003338
Paul Bakker48916f92012-09-16 19:57:18 +00003339void ssl_handshake_wrapup( ssl_context *ssl )
3340{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003341 int resume = ssl->handshake->resume;
3342
Paul Bakker48916f92012-09-16 19:57:18 +00003343 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3344
3345 /*
3346 * Free our handshake params
3347 */
3348 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003349 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003350 ssl->handshake = NULL;
3351
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003352#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003353 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003354 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003355 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003356 ssl->renego_records_seen = 0;
3357 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003358#endif
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003359
Paul Bakker48916f92012-09-16 19:57:18 +00003360 /*
3361 * Switch in our now active transform context
3362 */
3363 if( ssl->transform )
3364 {
3365 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003366 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003367 }
3368 ssl->transform = ssl->transform_negotiate;
3369 ssl->transform_negotiate = NULL;
3370
Paul Bakker0a597072012-09-25 21:55:46 +00003371 if( ssl->session )
3372 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003373#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3374 /* RFC 7366 3.1: keep the EtM state */
3375 ssl->session_negotiate->encrypt_then_mac =
3376 ssl->session->encrypt_then_mac;
3377#endif
3378
Paul Bakker0a597072012-09-25 21:55:46 +00003379 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003380 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003381 }
3382 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003383 ssl->session_negotiate = NULL;
3384
Paul Bakker0a597072012-09-25 21:55:46 +00003385 /*
3386 * Add cache entry
3387 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003388 if( ssl->f_set_cache != NULL &&
3389 ssl->session->length != 0 &&
3390 resume == 0 )
3391 {
Paul Bakker0a597072012-09-25 21:55:46 +00003392 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3393 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003394 }
Paul Bakker0a597072012-09-25 21:55:46 +00003395
Paul Bakker48916f92012-09-16 19:57:18 +00003396 ssl->state++;
3397
3398 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3399}
3400
Paul Bakker1ef83d62012-04-11 12:09:53 +00003401int ssl_write_finished( ssl_context *ssl )
3402{
3403 int ret, hash_len;
3404
3405 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3406
Paul Bakker92be97b2013-01-02 17:30:03 +01003407 /*
3408 * Set the out_msg pointer to the correct location based on IV length
3409 */
3410 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3411 {
3412 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3413 ssl->transform_negotiate->fixed_ivlen;
3414 }
3415 else
3416 ssl->out_msg = ssl->out_iv;
3417
Paul Bakker48916f92012-09-16 19:57:18 +00003418 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003419
3420 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003421 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3422
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003423#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003424 ssl->verify_data_len = hash_len;
3425 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003426#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003427
Paul Bakker5121ce52009-01-03 21:22:43 +00003428 ssl->out_msglen = 4 + hash_len;
3429 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3430 ssl->out_msg[0] = SSL_HS_FINISHED;
3431
3432 /*
3433 * In case of session resuming, invert the client and server
3434 * ChangeCipherSpec messages order.
3435 */
Paul Bakker0a597072012-09-25 21:55:46 +00003436 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003437 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003438#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003439 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003440 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003441#endif
3442#if defined(POLARSSL_SSL_SRV_C)
3443 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003444 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003445#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003446 }
3447 else
3448 ssl->state++;
3449
Paul Bakker48916f92012-09-16 19:57:18 +00003450 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003451 * Switch to our negotiated transform and session parameters for outbound
3452 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003453 */
3454 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3455 ssl->transform_out = ssl->transform_negotiate;
3456 ssl->session_out = ssl->session_negotiate;
3457 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003458
Paul Bakker07eb38b2012-12-19 14:42:06 +01003459#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003460 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003461 {
3462 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3463 {
3464 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3465 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3466 }
3467 }
3468#endif
3469
Paul Bakker5121ce52009-01-03 21:22:43 +00003470 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3471 {
3472 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3473 return( ret );
3474 }
3475
3476 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3477
3478 return( 0 );
3479}
3480
3481int ssl_parse_finished( ssl_context *ssl )
3482{
Paul Bakker23986e52011-04-24 08:57:21 +00003483 int ret;
3484 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003485 unsigned char buf[36];
3486
3487 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3488
Paul Bakker48916f92012-09-16 19:57:18 +00003489 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003490
Paul Bakker48916f92012-09-16 19:57:18 +00003491 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003492 * Switch to our negotiated transform and session parameters for inbound
3493 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003494 */
3495 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3496 ssl->transform_in = ssl->transform_negotiate;
3497 ssl->session_in = ssl->session_negotiate;
3498 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003499
Paul Bakker92be97b2013-01-02 17:30:03 +01003500 /*
3501 * Set the in_msg pointer to the correct location based on IV length
3502 */
3503 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3504 {
3505 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3506 ssl->transform_negotiate->fixed_ivlen;
3507 }
3508 else
3509 ssl->in_msg = ssl->in_iv;
3510
Paul Bakker07eb38b2012-12-19 14:42:06 +01003511#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003512 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003513 {
3514 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3515 {
3516 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3517 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3518 }
3519 }
3520#endif
3521
Paul Bakker5121ce52009-01-03 21:22:43 +00003522 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3523 {
3524 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3525 return( ret );
3526 }
3527
3528 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3529 {
3530 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003531 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003532 }
3533
Paul Bakker1ef83d62012-04-11 12:09:53 +00003534 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003535 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3536
3537 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3538 ssl->in_hslen != 4 + hash_len )
3539 {
3540 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003541 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003542 }
3543
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003544 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003545 {
3546 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003547 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003548 }
3549
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003550#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003551 ssl->verify_data_len = hash_len;
3552 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003553#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003554
Paul Bakker0a597072012-09-25 21:55:46 +00003555 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003556 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003557#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003558 if( ssl->endpoint == SSL_IS_CLIENT )
3559 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003560#endif
3561#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003562 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003563 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003564#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003565 }
3566 else
3567 ssl->state++;
3568
3569 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3570
3571 return( 0 );
3572}
3573
Paul Bakker968afaa2014-07-09 11:09:24 +02003574static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003575{
3576 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3577
3578#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3579 defined(POLARSSL_SSL_PROTO_TLS1_1)
3580 md5_init( &handshake->fin_md5 );
3581 sha1_init( &handshake->fin_sha1 );
3582 md5_starts( &handshake->fin_md5 );
3583 sha1_starts( &handshake->fin_sha1 );
3584#endif
3585#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3586#if defined(POLARSSL_SHA256_C)
3587 sha256_init( &handshake->fin_sha256 );
3588 sha256_starts( &handshake->fin_sha256, 0 );
3589#endif
3590#if defined(POLARSSL_SHA512_C)
3591 sha512_init( &handshake->fin_sha512 );
3592 sha512_starts( &handshake->fin_sha512, 1 );
3593#endif
3594#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3595
3596 handshake->update_checksum = ssl_update_checksum_start;
Hanno Beckerc2b9d982017-05-10 16:37:21 +01003597
3598#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
3599 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
3600 ssl_sig_hash_set_init( &handshake->hash_algs );
3601#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003602
3603#if defined(POLARSSL_DHM_C)
3604 dhm_init( &handshake->dhm_ctx );
3605#endif
3606#if defined(POLARSSL_ECDH_C)
3607 ecdh_init( &handshake->ecdh_ctx );
3608#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003609}
3610
3611static void ssl_transform_init( ssl_transform *transform )
3612{
3613 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003614
3615 cipher_init( &transform->cipher_ctx_enc );
3616 cipher_init( &transform->cipher_ctx_dec );
3617
3618 md_init( &transform->md_ctx_enc );
3619 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003620}
3621
3622void ssl_session_init( ssl_session *session )
3623{
3624 memset( session, 0, sizeof(ssl_session) );
3625}
3626
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003627static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003628{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003629 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003630 if( ssl->transform_negotiate )
3631 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003632 if( ssl->session_negotiate )
3633 ssl_session_free( ssl->session_negotiate );
3634 if( ssl->handshake )
3635 ssl_handshake_free( ssl->handshake );
3636
3637 /*
3638 * Either the pointers are now NULL or cleared properly and can be freed.
3639 * Now allocate missing structures.
3640 */
3641 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003642 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003643 ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003644 }
Paul Bakker48916f92012-09-16 19:57:18 +00003645
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003646 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003647 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003648 ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003649 }
Paul Bakker48916f92012-09-16 19:57:18 +00003650
Paul Bakker82788fb2014-10-20 13:59:19 +02003651 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003652 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003653 ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003654 }
Paul Bakker48916f92012-09-16 19:57:18 +00003655
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003656 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003657 if( ssl->handshake == NULL ||
3658 ssl->transform_negotiate == NULL ||
3659 ssl->session_negotiate == NULL )
3660 {
3661 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003662
3663 polarssl_free( ssl->handshake );
3664 polarssl_free( ssl->transform_negotiate );
3665 polarssl_free( ssl->session_negotiate );
3666
3667 ssl->handshake = NULL;
3668 ssl->transform_negotiate = NULL;
3669 ssl->session_negotiate = NULL;
3670
Paul Bakker48916f92012-09-16 19:57:18 +00003671 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3672 }
3673
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003674 /* Initialize structures */
3675 ssl_session_init( ssl->session_negotiate );
3676 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003677 ssl_handshake_params_init( ssl->handshake );
3678
3679#if defined(POLARSSL_X509_CRT_PARSE_C)
3680 ssl->handshake->key_cert = ssl->key_cert;
3681#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003682
Paul Bakker48916f92012-09-16 19:57:18 +00003683 return( 0 );
3684}
3685
Paul Bakker5121ce52009-01-03 21:22:43 +00003686/*
3687 * Initialize an SSL context
3688 */
3689int ssl_init( ssl_context *ssl )
3690{
Paul Bakker48916f92012-09-16 19:57:18 +00003691 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003692 int len = SSL_BUFFER_LEN;
3693
3694 memset( ssl, 0, sizeof( ssl_context ) );
3695
Paul Bakker62f2dee2012-09-28 07:31:51 +00003696 /*
3697 * Sane defaults
3698 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003699 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3700 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3701 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3702 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003703
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003704 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003705
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003706#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003707 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003708 memset( ssl->renego_period, 0xFF, 7 );
3709 ssl->renego_period[7] = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003710#endif
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003711
Paul Bakker62f2dee2012-09-28 07:31:51 +00003712#if defined(POLARSSL_DHM_C)
3713 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
Hanno Becker11f740a2017-10-13 16:56:15 +01003714 POLARSSL_DHM_RFC3526_MODP_2048_P) ) != 0 ||
Paul Bakker62f2dee2012-09-28 07:31:51 +00003715 ( ret = mpi_read_string( &ssl->dhm_G, 16,
Hanno Becker11f740a2017-10-13 16:56:15 +01003716 POLARSSL_DHM_RFC3526_MODP_2048_G) ) != 0 )
Paul Bakker62f2dee2012-09-28 07:31:51 +00003717 {
3718 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3719 return( ret );
3720 }
3721#endif
3722
3723 /*
3724 * Prepare base structures
3725 */
Manuel Pégourié-Gonnardf7db5e02015-02-18 10:32:41 +00003726 if( ( ssl->in_ctr = polarssl_malloc( len ) ) == NULL ||
3727 ( ssl->out_ctr = polarssl_malloc( len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003728 {
3729 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003730 polarssl_free( ssl->in_ctr );
3731 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003732 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003733 }
3734
3735 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3736 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3737
Manuel Pégourié-Gonnardf7db5e02015-02-18 10:32:41 +00003738 ssl->in_hdr = ssl->in_ctr + 8;
3739 ssl->in_iv = ssl->in_ctr + 13;
3740 ssl->in_msg = ssl->in_ctr + 13;
3741
3742 ssl->out_hdr = ssl->out_ctr + 8;
3743 ssl->out_iv = ssl->out_ctr + 13;
3744 ssl->out_msg = ssl->out_ctr + 13;
3745
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003746#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3747 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
3748#endif
3749
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003750#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3751 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3752#endif
3753
Paul Bakker606b4ba2013-08-14 16:52:14 +02003754#if defined(POLARSSL_SSL_SESSION_TICKETS)
3755 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3756#endif
3757
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003758#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003759 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003760#endif
3761
Paul Bakker48916f92012-09-16 19:57:18 +00003762 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3763 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003764
3765 return( 0 );
3766}
3767
3768/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003769 * Reset an initialized and used SSL context for re-use while retaining
3770 * all application-set variables, function pointers and data.
3771 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003772int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003773{
Paul Bakker48916f92012-09-16 19:57:18 +00003774 int ret;
3775
Paul Bakker7eb013f2011-10-06 12:37:39 +00003776 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003777
3778#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003779 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003780 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003781
3782 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard61860192014-11-04 13:05:42 +01003783 memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
3784 memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003785#endif
3786 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00003787
Paul Bakker7eb013f2011-10-06 12:37:39 +00003788 ssl->in_offt = NULL;
3789
Paul Bakker92be97b2013-01-02 17:30:03 +01003790 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003791 ssl->in_msgtype = 0;
3792 ssl->in_msglen = 0;
3793 ssl->in_left = 0;
3794
3795 ssl->in_hslen = 0;
3796 ssl->nb_zero = 0;
Hanno Becker10699cc2017-06-08 15:41:02 +01003797 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003798
Paul Bakker92be97b2013-01-02 17:30:03 +01003799 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003800 ssl->out_msgtype = 0;
3801 ssl->out_msglen = 0;
3802 ssl->out_left = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003803#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01003804 if( ssl->split_done != SSL_CBC_RECORD_SPLITTING_DISABLED )
3805 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003806#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00003807
Paul Bakker48916f92012-09-16 19:57:18 +00003808 ssl->transform_in = NULL;
3809 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003810
3811 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3812 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003813
3814#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003815 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003816 {
3817 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003818 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003819 {
3820 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3821 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3822 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003823 }
3824#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003825
Paul Bakker48916f92012-09-16 19:57:18 +00003826 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003827 {
Paul Bakker48916f92012-09-16 19:57:18 +00003828 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003829 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003830 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003831 }
Paul Bakker48916f92012-09-16 19:57:18 +00003832
Paul Bakkerc0463502013-02-14 11:19:38 +01003833 if( ssl->session )
3834 {
3835 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003836 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003837 ssl->session = NULL;
3838 }
3839
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003840#if defined(POLARSSL_SSL_ALPN)
3841 ssl->alpn_chosen = NULL;
3842#endif
3843
Paul Bakker48916f92012-09-16 19:57:18 +00003844 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3845 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003846
3847 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003848}
3849
Paul Bakkera503a632013-08-14 13:48:06 +02003850#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003851static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3852{
3853 aes_free( &tkeys->enc );
3854 aes_free( &tkeys->dec );
3855
3856 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3857}
3858
Paul Bakker7eb013f2011-10-06 12:37:39 +00003859/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003860 * Allocate and initialize ticket keys
3861 */
3862static int ssl_ticket_keys_init( ssl_context *ssl )
3863{
3864 int ret;
3865 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003866 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003867
3868 if( ssl->ticket_keys != NULL )
3869 return( 0 );
3870
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003871 tkeys = polarssl_malloc( sizeof(ssl_ticket_keys) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003872 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003873 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3874
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003875 aes_init( &tkeys->enc );
3876 aes_init( &tkeys->dec );
3877
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003878 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003879 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003880 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003881 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003882 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003883 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003884
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003885 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3886 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3887 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3888 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003889 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003890 polarssl_free( tkeys );
3891 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003892 }
3893
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003894 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003895 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003896 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003897 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003898 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003899 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003900
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003901 ssl->ticket_keys = tkeys;
3902
3903 return( 0 );
3904}
Paul Bakkera503a632013-08-14 13:48:06 +02003905#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003906
3907/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003908 * SSL set accessors
3909 */
3910void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3911{
3912 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003913
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003914#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
3915 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003916 if( endpoint == SSL_IS_CLIENT )
3917 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003918#endif
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01003919
3920#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
3921 if( endpoint == SSL_IS_SERVER )
3922 ssl->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
3923#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003924}
3925
3926void ssl_set_authmode( ssl_context *ssl, int authmode )
3927{
3928 ssl->authmode = authmode;
3929}
3930
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003931#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003932void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003933 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003934 void *p_vrfy )
3935{
3936 ssl->f_vrfy = f_vrfy;
3937 ssl->p_vrfy = p_vrfy;
3938}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003939#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003940
Paul Bakker5121ce52009-01-03 21:22:43 +00003941void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003942 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003943 void *p_rng )
3944{
3945 ssl->f_rng = f_rng;
3946 ssl->p_rng = p_rng;
3947}
3948
3949void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003950 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003951 void *p_dbg )
3952{
3953 ssl->f_dbg = f_dbg;
3954 ssl->p_dbg = p_dbg;
3955}
3956
3957void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003958 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003959 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003960{
3961 ssl->f_recv = f_recv;
3962 ssl->f_send = f_send;
3963 ssl->p_recv = p_recv;
3964 ssl->p_send = p_send;
3965}
3966
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003967#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00003968void ssl_set_session_cache( ssl_context *ssl,
3969 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3970 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003971{
Paul Bakker0a597072012-09-25 21:55:46 +00003972 ssl->f_get_cache = f_get_cache;
3973 ssl->p_get_cache = p_get_cache;
3974 ssl->f_set_cache = f_set_cache;
3975 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003976}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003977#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003978
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003979#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003980int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003981{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003982 int ret;
3983
3984 if( ssl == NULL ||
3985 session == NULL ||
3986 ssl->session_negotiate == NULL ||
3987 ssl->endpoint != SSL_IS_CLIENT )
3988 {
3989 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3990 }
3991
3992 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3993 return( ret );
3994
Paul Bakker0a597072012-09-25 21:55:46 +00003995 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003996
3997 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003998}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003999#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004000
Paul Bakkerb68cad62012-08-23 08:34:18 +00004001void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00004002{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004003 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
4004 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
4005 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
4006 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
4007}
4008
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004009void ssl_set_ciphersuites_for_version( ssl_context *ssl,
4010 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004011 int major, int minor )
4012{
4013 if( major != SSL_MAJOR_VERSION_3 )
4014 return;
4015
4016 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
4017 return;
4018
4019 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00004020}
4021
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004022#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004023/* Add a new (empty) key_cert entry an return a pointer to it */
4024static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
4025{
4026 ssl_key_cert *key_cert, *last;
4027
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004028 key_cert = polarssl_malloc( sizeof(ssl_key_cert) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004029 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004030 return( NULL );
4031
4032 memset( key_cert, 0, sizeof( ssl_key_cert ) );
4033
4034 /* Append the new key_cert to the (possibly empty) current list */
4035 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01004036 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004037 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01004038 if( ssl->handshake != NULL )
4039 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01004040 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004041 else
4042 {
4043 last = ssl->key_cert;
4044 while( last->next != NULL )
4045 last = last->next;
4046 last->next = key_cert;
4047 }
4048
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004049 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004050}
4051
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004052void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00004053 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00004054{
4055 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00004056 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00004057 ssl->peer_cn = peer_cn;
4058}
4059
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004060int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004061 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00004062{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004063 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
4064
4065 if( key_cert == NULL )
4066 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4067
4068 key_cert->cert = own_cert;
4069 key_cert->key = pk_key;
4070
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004071 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004072}
4073
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +01004074#if ! defined(POLARSSL_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004075#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004076int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004077 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00004078{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004079 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004080 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004081
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004082 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004083 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4084
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004085 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004086 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004087 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004088
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004089 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004090
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004091 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004092 if( ret != 0 )
4093 return( ret );
4094
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004095 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004096 return( ret );
4097
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004098 key_cert->cert = own_cert;
4099 key_cert->key_own_alloc = 1;
4100
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004101 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004102}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004103#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004104
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004105int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02004106 void *rsa_key,
4107 rsa_decrypt_func rsa_decrypt,
4108 rsa_sign_func rsa_sign,
4109 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00004110{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004111 int ret;
4112 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004113
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004114 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004115 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4116
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004117 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004118 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004119 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004120
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004121 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004122
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004123 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
4124 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
4125 return( ret );
4126
4127 key_cert->cert = own_cert;
4128 key_cert->key_own_alloc = 1;
4129
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004130 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00004131}
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +01004132#endif /* POLARSSL_DEPRECATED_REMOVED */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004133#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004134
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004135#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004136int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
4137 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004138{
Paul Bakker6db455e2013-09-18 17:29:31 +02004139 if( psk == NULL || psk_identity == NULL )
4140 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4141
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02004142 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01004143 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4144
Manuel Pégourié-Gonnard65125542015-08-27 16:37:35 +02004145 /* Identity len will be encoded on two bytes */
4146 if( ( psk_identity_len >> 16 ) != 0 ||
4147 psk_identity_len > SSL_MAX_CONTENT_LEN )
4148 {
4149 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4150 }
4151
Andres Amaya Garciaa0ae1db2017-07-12 10:51:22 +01004152 if( ssl->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02004153 {
Andres Amaya Garciaa0ae1db2017-07-12 10:51:22 +01004154 polarssl_zeroize( ssl->psk, ssl->psk_len );
4155
Paul Bakker6db455e2013-09-18 17:29:31 +02004156 polarssl_free( ssl->psk );
Manuel Pégourié-Gonnard9c521762015-10-27 10:54:53 +01004157 ssl->psk = NULL;
Andres Amaya Garciaa0ae1db2017-07-12 10:51:22 +01004158 ssl->psk_len = 0;
4159 }
4160 if( ssl->psk_identity != NULL )
4161 {
4162 polarssl_free( ssl->psk_identity );
Manuel Pégourié-Gonnard9c521762015-10-27 10:54:53 +01004163 ssl->psk_identity = NULL;
Andres Amaya Garciaa0ae1db2017-07-12 10:51:22 +01004164 ssl->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02004165 }
4166
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004167 if( ( ssl->psk = polarssl_malloc( psk_len ) ) == NULL ||
4168 ( ssl->psk_identity = polarssl_malloc( psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05004169 {
4170 polarssl_free( ssl->psk );
Manuel Pégourié-Gonnard5aff0292015-09-28 18:09:45 +02004171 polarssl_free( ssl->psk_identity );
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004172 ssl->psk = NULL;
Manuel Pégourié-Gonnard5aff0292015-09-28 18:09:45 +02004173 ssl->psk_identity = NULL;
Mansour Moufidf81088b2015-02-17 13:10:21 -05004174 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4175 }
Paul Bakker6db455e2013-09-18 17:29:31 +02004176
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004177 ssl->psk_len = psk_len;
4178 ssl->psk_identity_len = psk_identity_len;
4179
Paul Bakker6db455e2013-09-18 17:29:31 +02004180 memcpy( ssl->psk, psk, ssl->psk_len );
4181 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004182
4183 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004184}
4185
4186void ssl_set_psk_cb( ssl_context *ssl,
4187 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4188 size_t),
4189 void *p_psk )
4190{
4191 ssl->f_psk = f_psk;
4192 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004193}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004194#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004195
Paul Bakker48916f92012-09-16 19:57:18 +00004196#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004197int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004198{
4199 int ret;
4200
Paul Bakker48916f92012-09-16 19:57:18 +00004201 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004202 {
4203 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4204 return( ret );
4205 }
4206
Paul Bakker48916f92012-09-16 19:57:18 +00004207 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004208 {
4209 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4210 return( ret );
4211 }
4212
4213 return( 0 );
4214}
4215
Paul Bakker1b57b062011-01-06 15:48:19 +00004216int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4217{
4218 int ret;
4219
Paul Bakker66d5d072014-06-17 16:39:18 +02004220 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004221 {
4222 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4223 return( ret );
4224 }
4225
Paul Bakker66d5d072014-06-17 16:39:18 +02004226 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004227 {
4228 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4229 return( ret );
4230 }
4231
4232 return( 0 );
4233}
Paul Bakker48916f92012-09-16 19:57:18 +00004234#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004235
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004236#if defined(POLARSSL_SSL_SET_CURVES)
4237/*
4238 * Set the allowed elliptic curves
4239 */
4240void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4241{
4242 ssl->curve_list = curve_list;
4243}
4244#endif
4245
Paul Bakker0be444a2013-08-27 21:55:01 +02004246#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004247int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004248{
Hanno Beckerb9ac47c2017-05-05 13:07:33 +01004249 size_t hostname_len = 0;
4250
4251 /* Check if new hostname is valid before
4252 * making any change to current one */
4253
4254 if( hostname != NULL )
4255 {
4256 hostname_len = strlen( hostname );
4257
4258 if( hostname_len > SSL_MAX_HOST_NAME_LEN )
4259 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4260 }
4261
4262 /* Now it's clear that we will overwrite the old hostname,
4263 * so we can free it safely */
4264
4265 if( ssl->hostname != NULL )
4266 {
4267 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
4268 polarssl_free( ssl->hostname );
4269 }
4270
4271 /* Passing NULL as hostname shall clear the old one */
4272
Paul Bakker5121ce52009-01-03 21:22:43 +00004273 if( hostname == NULL )
Hanno Beckerb9ac47c2017-05-05 13:07:33 +01004274 {
4275 ssl->hostname = NULL;
4276 ssl->hostname_len = 0;
4277 }
4278 else
4279 {
4280 ssl->hostname = polarssl_malloc( hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004281
Hanno Beckerb9ac47c2017-05-05 13:07:33 +01004282 if( ssl->hostname == NULL )
4283 {
4284 ssl->hostname_len = 0;
4285 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4286 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004287
Hanno Beckerb9ac47c2017-05-05 13:07:33 +01004288 memcpy( ssl->hostname, (const unsigned char*) hostname,
4289 hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004290
Hanno Beckerb9ac47c2017-05-05 13:07:33 +01004291 ssl->hostname[hostname_len] = '\0';
4292 ssl->hostname_len = hostname_len;
4293 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004294
4295 return( 0 );
4296}
4297
Paul Bakker5701cdc2012-09-27 21:49:42 +00004298void ssl_set_sni( ssl_context *ssl,
4299 int (*f_sni)(void *, ssl_context *,
4300 const unsigned char *, size_t),
4301 void *p_sni )
4302{
4303 ssl->f_sni = f_sni;
4304 ssl->p_sni = p_sni;
4305}
Paul Bakker0be444a2013-08-27 21:55:01 +02004306#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004307
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004308#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004309int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004310{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004311 size_t cur_len, tot_len;
4312 const char **p;
4313
4314 /*
4315 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4316 * truncated". Check lengths now rather than later.
4317 */
4318 tot_len = 0;
4319 for( p = protos; *p != NULL; p++ )
4320 {
4321 cur_len = strlen( *p );
4322 tot_len += cur_len;
4323
4324 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4325 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4326 }
4327
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004328 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004329
4330 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004331}
4332
4333const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4334{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004335 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004336}
4337#endif /* POLARSSL_SSL_ALPN */
4338
Paul Bakker490ecc82011-10-06 13:04:09 +00004339void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4340{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004341 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4342 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4343 {
4344 ssl->max_major_ver = major;
4345 ssl->max_minor_ver = minor;
4346 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004347}
4348
Paul Bakker1d29fb52012-09-28 13:28:45 +00004349void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4350{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004351 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4352 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4353 {
4354 ssl->min_major_ver = major;
4355 ssl->min_minor_ver = minor;
4356 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004357}
4358
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004359#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4360void ssl_set_fallback( ssl_context *ssl, char fallback )
4361{
4362 ssl->fallback = fallback;
4363}
4364#endif
4365
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004366#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4367void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
4368{
4369 ssl->encrypt_then_mac = etm;
4370}
4371#endif
4372
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004373#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4374void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4375{
4376 ssl->extended_ms = ems;
4377}
4378#endif
4379
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004380void ssl_set_arc4_support( ssl_context *ssl, char arc4 )
4381{
4382 ssl->arc4_disabled = arc4;
4383}
4384
Paul Bakker05decb22013-08-15 13:33:48 +02004385#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004386int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4387{
Paul Bakker77e257e2013-12-16 15:29:52 +01004388 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004389 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004390 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004391 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004392 }
4393
4394 ssl->mfl_code = mfl_code;
4395
4396 return( 0 );
4397}
Paul Bakker05decb22013-08-15 13:33:48 +02004398#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004399
Paul Bakker1f2bc622013-08-15 13:45:55 +02004400#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004401int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004402{
Paul Bakker8c1ede62013-07-19 14:14:37 +02004403 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004404
4405 return( 0 );
4406}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004407#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004408
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004409#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4410void ssl_set_cbc_record_splitting( ssl_context *ssl, char split )
4411{
4412 ssl->split_done = split;
4413}
4414#endif
4415
Paul Bakker48916f92012-09-16 19:57:18 +00004416void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4417{
4418 ssl->allow_legacy_renegotiation = allow_legacy;
4419}
4420
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004421#if defined(POLARSSL_SSL_RENEGOTIATION)
4422void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4423{
4424 ssl->disable_renegotiation = renegotiation;
4425}
4426
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004427void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4428{
4429 ssl->renego_max_records = max_records;
4430}
4431
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004432void ssl_set_renegotiation_period( ssl_context *ssl,
4433 const unsigned char period[8] )
4434{
4435 memcpy( ssl->renego_period, period, 8 );
4436}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004437#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004438
Paul Bakkera503a632013-08-14 13:48:06 +02004439#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004440int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4441{
4442 ssl->session_tickets = use_tickets;
4443
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004444#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004445 if( ssl->endpoint == SSL_IS_CLIENT )
4446 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004447#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004448
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01004449 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
4450 return( 0 );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004451
4452 if( ssl->f_rng == NULL )
4453 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4454
4455 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004456}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004457
4458void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4459{
4460 ssl->ticket_lifetime = lifetime;
4461}
Paul Bakkera503a632013-08-14 13:48:06 +02004462#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004463
Paul Bakker5121ce52009-01-03 21:22:43 +00004464/*
4465 * SSL get accessors
4466 */
4467size_t ssl_get_bytes_avail( const ssl_context *ssl )
4468{
4469 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4470}
4471
Paul Bakkerff60ee62010-03-16 21:09:09 +00004472int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004473{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00004474 if( ssl->session != NULL )
4475 return( ssl->session->verify_result );
4476
4477 if( ssl->session_negotiate != NULL )
4478 return( ssl->session_negotiate->verify_result );
4479
4480 return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004481}
4482
Paul Bakkere3166ce2011-01-27 17:40:50 +00004483const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004484{
Paul Bakker926c8e42013-03-06 10:23:34 +01004485 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004486 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004487
Paul Bakkere3166ce2011-01-27 17:40:50 +00004488 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004489}
4490
Paul Bakker43ca69c2011-01-15 17:35:19 +00004491const char *ssl_get_version( const ssl_context *ssl )
4492{
4493 switch( ssl->minor_ver )
4494 {
4495 case SSL_MINOR_VERSION_0:
4496 return( "SSLv3.0" );
4497
4498 case SSL_MINOR_VERSION_1:
4499 return( "TLSv1.0" );
4500
4501 case SSL_MINOR_VERSION_2:
4502 return( "TLSv1.1" );
4503
Paul Bakker1ef83d62012-04-11 12:09:53 +00004504 case SSL_MINOR_VERSION_3:
4505 return( "TLSv1.2" );
4506
Paul Bakker43ca69c2011-01-15 17:35:19 +00004507 default:
4508 break;
4509 }
4510 return( "unknown" );
4511}
4512
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004513#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004514const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004515{
4516 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004517 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004518
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004519 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004520}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004521#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004522
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004523#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004524int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4525{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004526 if( ssl == NULL ||
4527 dst == NULL ||
4528 ssl->session == NULL ||
4529 ssl->endpoint != SSL_IS_CLIENT )
4530 {
4531 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4532 }
4533
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004534 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004535}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004536#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004537
Paul Bakker5121ce52009-01-03 21:22:43 +00004538/*
Paul Bakker1961b702013-01-25 14:49:24 +01004539 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004540 */
Paul Bakker1961b702013-01-25 14:49:24 +01004541int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004542{
Paul Bakker40e46942009-01-03 21:51:57 +00004543 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004544
Paul Bakker40e46942009-01-03 21:51:57 +00004545#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004546 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004547 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004548#endif
Paul Bakker40e46942009-01-03 21:51:57 +00004549#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004550 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004551 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004552#endif
4553
Paul Bakker1961b702013-01-25 14:49:24 +01004554 return( ret );
4555}
4556
4557/*
4558 * Perform the SSL handshake
4559 */
4560int ssl_handshake( ssl_context *ssl )
4561{
4562 int ret = 0;
4563
4564 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4565
4566 while( ssl->state != SSL_HANDSHAKE_OVER )
4567 {
4568 ret = ssl_handshake_step( ssl );
4569
4570 if( ret != 0 )
4571 break;
4572 }
4573
Paul Bakker5121ce52009-01-03 21:22:43 +00004574 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4575
4576 return( ret );
4577}
4578
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004579#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004580#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004581/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004582 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004583 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004584static int ssl_write_hello_request( ssl_context *ssl )
4585{
4586 int ret;
4587
4588 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4589
4590 ssl->out_msglen = 4;
4591 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4592 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4593
4594 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4595 {
4596 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4597 return( ret );
4598 }
4599
4600 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4601
4602 return( 0 );
4603}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004604#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004605
4606/*
4607 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004608 * - any side: calling ssl_renegotiate(),
4609 * - client: receiving a HelloRequest during ssl_read(),
4610 * - server: receiving any handshake message on server during ssl_read() after
4611 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004612 * If the handshake doesn't complete due to waiting for I/O, it will continue
4613 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004614 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004615static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004616{
4617 int ret;
4618
4619 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4620
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004621 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4622 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004623
4624 ssl->state = SSL_HELLO_REQUEST;
4625 ssl->renegotiation = SSL_RENEGOTIATION;
4626
Paul Bakker48916f92012-09-16 19:57:18 +00004627 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4628 {
4629 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4630 return( ret );
4631 }
4632
4633 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4634
4635 return( 0 );
4636}
4637
4638/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004639 * Renegotiate current connection on client,
4640 * or request renegotiation on server
4641 */
4642int ssl_renegotiate( ssl_context *ssl )
4643{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004644 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004645
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004646#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004647 /* On server, just send the request */
4648 if( ssl->endpoint == SSL_IS_SERVER )
4649 {
4650 if( ssl->state != SSL_HANDSHAKE_OVER )
4651 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4652
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004653 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4654
4655 /* Did we already try/start sending HelloRequest? */
4656 if( ssl->out_left != 0 )
4657 return( ssl_flush_output( ssl ) );
4658
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004659 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004660 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004661#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004662
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004663#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004664 /*
4665 * On client, either start the renegotiation process or,
4666 * if already in progress, continue the handshake
4667 */
4668 if( ssl->renegotiation != SSL_RENEGOTIATION )
4669 {
4670 if( ssl->state != SSL_HANDSHAKE_OVER )
4671 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4672
4673 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4674 {
4675 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4676 return( ret );
4677 }
4678 }
4679 else
4680 {
4681 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4682 {
4683 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4684 return( ret );
4685 }
4686 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004687#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004688
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004689 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004690}
4691
4692/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004693 * Check record counters and renegotiate if they're above the limit.
4694 */
4695static int ssl_check_ctr_renegotiate( ssl_context *ssl )
4696{
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004697 if( ssl->state != SSL_HANDSHAKE_OVER ||
4698 ssl->renegotiation == SSL_RENEGOTIATION_PENDING ||
4699 ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED )
4700 {
4701 return( 0 );
4702 }
4703
4704 // TODO: adapt for DTLS
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004705 if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 &&
4706 memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004707 {
4708 return( 0 );
4709 }
4710
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004711 SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004712 return( ssl_renegotiate( ssl ) );
4713}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004714#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004715
4716/*
4717 * Receive application data decrypted from the SSL layer
4718 */
Paul Bakker23986e52011-04-24 08:57:21 +00004719int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004720{
Hanno Beckerd37839e2017-06-08 15:56:50 +01004721 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00004722 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004723
4724 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4725
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004726#if defined(POLARSSL_SSL_RENEGOTIATION)
Hanno Beckerd37839e2017-06-08 15:56:50 +01004727 ret = ssl_check_ctr_renegotiate( ssl );
4728 if( ret != POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
4729 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004730 {
4731 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4732 return( ret );
4733 }
4734#endif
4735
Paul Bakker5121ce52009-01-03 21:22:43 +00004736 if( ssl->state != SSL_HANDSHAKE_OVER )
4737 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004738 ret = ssl_handshake( ssl );
Hanno Beckerd37839e2017-06-08 15:56:50 +01004739 if( ret != POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
4740 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004741 {
4742 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4743 return( ret );
4744 }
4745 }
4746
4747 if( ssl->in_offt == NULL )
4748 {
Hanno Beckerd37839e2017-06-08 15:56:50 +01004749 if( ( ret = ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004750 {
Hanno Beckerd37839e2017-06-08 15:56:50 +01004751 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4752 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004753
Hanno Beckerd37839e2017-06-08 15:56:50 +01004754 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4755 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004756 }
4757
4758 if( ssl->in_msglen == 0 &&
4759 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4760 {
4761 /*
4762 * OpenSSL sends empty messages to randomize the IV
4763 */
4764 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4765 {
Paul Bakker831a7552011-05-18 13:32:51 +00004766 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4767 return( 0 );
4768
Paul Bakker5121ce52009-01-03 21:22:43 +00004769 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4770 return( ret );
4771 }
4772 }
4773
Paul Bakker48916f92012-09-16 19:57:18 +00004774 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4775 {
4776 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4777
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004778#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004779 if( ssl->endpoint == SSL_IS_CLIENT &&
4780 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4781 ssl->in_hslen != 4 ) )
4782 {
4783 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4784 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4785 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004786#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004787
Hanno Beckerbfd09912017-10-25 09:34:48 +01004788#if defined(POLARSSL_SSL_RENEGOTIATION)
Hanno Becker268191a2017-10-25 09:33:22 +01004789 if( ! ( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4790 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
4791 ssl->allow_legacy_renegotiation ==
4792 SSL_LEGACY_NO_RENEGOTIATION ) ) )
4793 {
4794 ret = ssl_start_renegotiation( ssl );
4795 if( ret != POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
4796 ret != 0 )
4797 {
4798 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4799 return( ret );
4800 }
4801 }
4802 else
Hanno Beckerbfd09912017-10-25 09:34:48 +01004803#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00004804 {
4805 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4806
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004807#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004808 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004809 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004810 /*
4811 * SSLv3 does not have a "no_renegotiation" alert
4812 */
4813 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4814 return( ret );
4815 }
4816 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004817#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004818#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4819 defined(POLARSSL_SSL_PROTO_TLS1_2)
4820 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004821 {
4822 if( ( ret = ssl_send_alert_message( ssl,
4823 SSL_ALERT_LEVEL_WARNING,
4824 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4825 {
4826 return( ret );
4827 }
Paul Bakker48916f92012-09-16 19:57:18 +00004828 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004829 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004830#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4831 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004832 {
4833 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004834 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004835 }
Paul Bakker48916f92012-09-16 19:57:18 +00004836 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004837
Hanno Beckerd37839e2017-06-08 15:56:50 +01004838 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004839 }
Hanno Beckerbfd09912017-10-25 09:34:48 +01004840#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004841 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4842 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004843 ssl->renego_records_seen++;
4844
4845 if( ssl->renego_max_records >= 0 &&
4846 ssl->renego_records_seen > ssl->renego_max_records )
4847 {
4848 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4849 "but not honored by client" ) );
4850 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4851 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004852 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004853#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004854
4855 /* Fatal and closure alerts handled by ssl_read_record() */
4856 if( ssl->in_msgtype == SSL_MSG_ALERT )
4857 {
4858 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4859 return( POLARSSL_ERR_NET_WANT_READ );
4860 }
4861
4862 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004863 {
4864 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004865 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004866 }
4867
4868 ssl->in_offt = ssl->in_msg;
4869 }
4870
4871 n = ( len < ssl->in_msglen )
4872 ? len : ssl->in_msglen;
4873
4874 memcpy( buf, ssl->in_offt, n );
4875 ssl->in_msglen -= n;
4876
4877 if( ssl->in_msglen == 0 )
Hanno Becker0401a3d2017-06-09 10:52:45 +01004878 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004879 /* all bytes consumed */
4880 ssl->in_offt = NULL;
Hanno Becker0401a3d2017-06-09 10:52:45 +01004881 ssl->keep_current_message = 0;
4882 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004883 else
4884 /* more data available */
4885 ssl->in_offt += n;
4886
4887 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4888
Paul Bakker23986e52011-04-24 08:57:21 +00004889 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004890}
4891
4892/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004893 * Send application data to be encrypted by the SSL layer,
4894 * taking care of max fragment length and buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +00004895 */
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004896static int ssl_write_real( ssl_context *ssl,
4897 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004898{
Paul Bakker23986e52011-04-24 08:57:21 +00004899 int ret;
4900 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004901 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004902
Paul Bakker05decb22013-08-15 13:33:48 +02004903#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004904 /*
4905 * Assume mfl_code is correct since it was checked when set
4906 */
4907 max_len = mfl_code_to_length[ssl->mfl_code];
4908
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004909 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004910 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004911 */
4912 if( ssl->session_out != NULL &&
4913 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4914 {
4915 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4916 }
Paul Bakker05decb22013-08-15 13:33:48 +02004917#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004918
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004919 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004920
Paul Bakker5121ce52009-01-03 21:22:43 +00004921 if( ssl->out_left != 0 )
4922 {
4923 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4924 {
4925 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4926 return( ret );
4927 }
4928 }
Paul Bakker887bd502011-06-08 13:10:54 +00004929 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004930 {
Paul Bakker887bd502011-06-08 13:10:54 +00004931 ssl->out_msglen = n;
4932 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4933 memcpy( ssl->out_msg, buf, n );
4934
4935 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4936 {
4937 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4938 return( ret );
4939 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004940 }
4941
Paul Bakker23986e52011-04-24 08:57:21 +00004942 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004943}
4944
4945/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004946 * Write application data, doing 1/n-1 splitting if necessary.
4947 *
4948 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004949 * then the caller will call us again with the same arguments, so
4950 * remember wether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004951 */
4952#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004953static int ssl_write_split( ssl_context *ssl,
4954 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004955{
4956 int ret;
4957
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004958 if( ssl->split_done == SSL_CBC_RECORD_SPLITTING_DISABLED ||
4959 len <= 1 ||
4960 ssl->minor_ver > SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004961 cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
4962 != POLARSSL_MODE_CBC )
4963 {
4964 return( ssl_write_real( ssl, buf, len ) );
4965 }
4966
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004967 if( ssl->split_done == 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004968 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004969 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004970 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004971 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004972 }
4973
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004974 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
4975 return( ret );
4976 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004977
4978 return( ret + 1 );
4979}
4980#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */
4981
4982/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004983 * Write application data (public-facing wrapper)
4984 */
4985int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
4986{
4987 int ret;
4988
4989 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4990
4991#if defined(POLARSSL_SSL_RENEGOTIATION)
4992 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4993 {
4994 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4995 return( ret );
4996 }
4997#endif
4998
4999 if( ssl->state != SSL_HANDSHAKE_OVER )
5000 {
5001 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5002 {
5003 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5004 return( ret );
5005 }
5006 }
5007
5008#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
5009 ret = ssl_write_split( ssl, buf, len );
5010#else
5011 ret = ssl_write_real( ssl, buf, len );
5012#endif
5013
5014 SSL_DEBUG_MSG( 2, ( "<= write" ) );
5015
5016 return( ret );
5017}
5018
5019/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005020 * Notify the peer that the connection is being closed
5021 */
5022int ssl_close_notify( ssl_context *ssl )
5023{
5024 int ret;
5025
5026 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
5027
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005028 if( ssl->out_left != 0 )
5029 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005030
5031 if( ssl->state == SSL_HANDSHAKE_OVER )
5032 {
Paul Bakker48916f92012-09-16 19:57:18 +00005033 if( ( ret = ssl_send_alert_message( ssl,
5034 SSL_ALERT_LEVEL_WARNING,
5035 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005036 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005037 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005038 return( ret );
5039 }
5040 }
5041
5042 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
5043
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005044 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005045}
5046
Paul Bakker48916f92012-09-16 19:57:18 +00005047void ssl_transform_free( ssl_transform *transform )
5048{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005049 if( transform == NULL )
5050 return;
5051
Paul Bakker48916f92012-09-16 19:57:18 +00005052#if defined(POLARSSL_ZLIB_SUPPORT)
5053 deflateEnd( &transform->ctx_deflate );
5054 inflateEnd( &transform->ctx_inflate );
5055#endif
5056
Paul Bakker84bbeb52014-07-01 14:53:22 +02005057 cipher_free( &transform->cipher_ctx_enc );
5058 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005059
Paul Bakker84bbeb52014-07-01 14:53:22 +02005060 md_free( &transform->md_ctx_enc );
5061 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02005062
Paul Bakker34617722014-06-13 17:20:13 +02005063 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005064}
5065
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005066#if defined(POLARSSL_X509_CRT_PARSE_C)
5067static void ssl_key_cert_free( ssl_key_cert *key_cert )
5068{
5069 ssl_key_cert *cur = key_cert, *next;
5070
5071 while( cur != NULL )
5072 {
5073 next = cur->next;
5074
5075 if( cur->key_own_alloc )
5076 {
5077 pk_free( cur->key );
5078 polarssl_free( cur->key );
5079 }
5080 polarssl_free( cur );
5081
5082 cur = next;
5083 }
5084}
5085#endif /* POLARSSL_X509_CRT_PARSE_C */
5086
Paul Bakker48916f92012-09-16 19:57:18 +00005087void ssl_handshake_free( ssl_handshake_params *handshake )
5088{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005089 if( handshake == NULL )
5090 return;
5091
Paul Bakker48916f92012-09-16 19:57:18 +00005092#if defined(POLARSSL_DHM_C)
5093 dhm_free( &handshake->dhm_ctx );
5094#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005095#if defined(POLARSSL_ECDH_C)
5096 ecdh_free( &handshake->ecdh_ctx );
5097#endif
5098
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005099#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02005100 /* explicit void pointer cast for buggy MS compiler */
5101 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005102#endif
5103
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02005104#if defined(POLARSSL_X509_CRT_PARSE_C) && \
5105 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
5106 /*
5107 * Free only the linked list wrapper, not the keys themselves
5108 * since the belong to the SNI callback
5109 */
5110 if( handshake->sni_key_cert != NULL )
5111 {
5112 ssl_key_cert *cur = handshake->sni_key_cert, *next;
5113
5114 while( cur != NULL )
5115 {
5116 next = cur->next;
5117 polarssl_free( cur );
5118 cur = next;
5119 }
5120 }
Paul Bakker9af723c2014-05-01 13:03:14 +02005121#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005122
Paul Bakker34617722014-06-13 17:20:13 +02005123 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005124}
5125
5126void ssl_session_free( ssl_session *session )
5127{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005128 if( session == NULL )
5129 return;
5130
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005131#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00005132 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00005133 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005134 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02005135 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00005136 }
Paul Bakkered27a042013-04-18 22:46:23 +02005137#endif
Paul Bakker0a597072012-09-25 21:55:46 +00005138
Paul Bakkera503a632013-08-14 13:48:06 +02005139#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005140 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02005141#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005142
Paul Bakker34617722014-06-13 17:20:13 +02005143 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005144}
5145
Paul Bakker5121ce52009-01-03 21:22:43 +00005146/*
5147 * Free an SSL context
5148 */
5149void ssl_free( ssl_context *ssl )
5150{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005151 if( ssl == NULL )
5152 return;
5153
Paul Bakker5121ce52009-01-03 21:22:43 +00005154 SSL_DEBUG_MSG( 2, ( "=> free" ) );
5155
Paul Bakker5121ce52009-01-03 21:22:43 +00005156 if( ssl->out_ctr != NULL )
5157 {
Paul Bakker34617722014-06-13 17:20:13 +02005158 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005159 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005160 }
5161
5162 if( ssl->in_ctr != NULL )
5163 {
Paul Bakker34617722014-06-13 17:20:13 +02005164 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005165 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005166 }
5167
Paul Bakker16770332013-10-11 09:59:44 +02005168#if defined(POLARSSL_ZLIB_SUPPORT)
5169 if( ssl->compress_buf != NULL )
5170 {
Paul Bakker34617722014-06-13 17:20:13 +02005171 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02005172 polarssl_free( ssl->compress_buf );
5173 }
5174#endif
5175
Paul Bakker40e46942009-01-03 21:51:57 +00005176#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00005177 mpi_free( &ssl->dhm_P );
5178 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00005179#endif
5180
Paul Bakker48916f92012-09-16 19:57:18 +00005181 if( ssl->transform )
5182 {
5183 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005184 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005185 }
5186
5187 if( ssl->handshake )
5188 {
5189 ssl_handshake_free( ssl->handshake );
5190 ssl_transform_free( ssl->transform_negotiate );
5191 ssl_session_free( ssl->session_negotiate );
5192
Paul Bakker6e339b52013-07-03 13:37:05 +02005193 polarssl_free( ssl->handshake );
5194 polarssl_free( ssl->transform_negotiate );
5195 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00005196 }
5197
Paul Bakkerc0463502013-02-14 11:19:38 +01005198 if( ssl->session )
5199 {
5200 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005201 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005202 }
5203
Paul Bakkera503a632013-08-14 13:48:06 +02005204#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005205 if( ssl->ticket_keys )
5206 {
5207 ssl_ticket_keys_free( ssl->ticket_keys );
5208 polarssl_free( ssl->ticket_keys );
5209 }
Paul Bakkera503a632013-08-14 13:48:06 +02005210#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005211
Paul Bakker0be444a2013-08-27 21:55:01 +02005212#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02005213 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005214 {
Paul Bakker34617722014-06-13 17:20:13 +02005215 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02005216 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00005217 ssl->hostname_len = 0;
5218 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005219#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005220
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005221#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005222 if( ssl->psk != NULL )
5223 {
Paul Bakker34617722014-06-13 17:20:13 +02005224 polarssl_zeroize( ssl->psk, ssl->psk_len );
5225 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005226 polarssl_free( ssl->psk );
5227 polarssl_free( ssl->psk_identity );
5228 ssl->psk_len = 0;
5229 ssl->psk_identity_len = 0;
5230 }
5231#endif
5232
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005233#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005234 ssl_key_cert_free( ssl->key_cert );
5235#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005236
Paul Bakker05ef8352012-05-08 09:17:57 +00005237#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
5238 if( ssl_hw_record_finish != NULL )
5239 {
5240 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
5241 ssl_hw_record_finish( ssl );
5242 }
5243#endif
5244
Paul Bakker5121ce52009-01-03 21:22:43 +00005245 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00005246
Paul Bakker86f04f42013-02-14 11:20:09 +01005247 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02005248 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005249}
5250
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005251#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005252/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005253 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005254 */
5255unsigned char ssl_sig_from_pk( pk_context *pk )
5256{
5257#if defined(POLARSSL_RSA_C)
5258 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
5259 return( SSL_SIG_RSA );
5260#endif
5261#if defined(POLARSSL_ECDSA_C)
5262 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
5263 return( SSL_SIG_ECDSA );
5264#endif
5265 return( SSL_SIG_ANON );
5266}
5267
Hanno Beckerc2b9d982017-05-10 16:37:21 +01005268unsigned char ssl_sig_from_pk_alg( pk_type_t type )
5269{
5270 switch( type ) {
5271 case POLARSSL_PK_RSA:
5272 return( SSL_SIG_RSA );
5273 case POLARSSL_PK_ECDSA:
5274 case POLARSSL_PK_ECKEY:
5275 return( SSL_SIG_ECDSA );
5276 default:
5277 return( SSL_SIG_ANON );
5278 }
5279}
5280
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005281pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
5282{
5283 switch( sig )
5284 {
5285#if defined(POLARSSL_RSA_C)
5286 case SSL_SIG_RSA:
5287 return( POLARSSL_PK_RSA );
5288#endif
5289#if defined(POLARSSL_ECDSA_C)
5290 case SSL_SIG_ECDSA:
5291 return( POLARSSL_PK_ECDSA );
5292#endif
5293 default:
5294 return( POLARSSL_PK_NONE );
5295 }
5296}
Paul Bakker9af723c2014-05-01 13:03:14 +02005297#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005298
Hanno Beckerc2b9d982017-05-10 16:37:21 +01005299#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
5300 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
5301
5302/* Find an entry in a signature-hash set matching a given hash algorithm. */
5303md_type_t ssl_sig_hash_set_find( ssl_sig_hash_set_t *set,
5304 pk_type_t sig_alg )
5305{
5306 switch( sig_alg )
5307 {
5308 case POLARSSL_PK_RSA:
5309 return( set->rsa );
5310 case POLARSSL_PK_ECDSA:
5311 return( set->ecdsa );
5312 default:
5313 return( POLARSSL_MD_NONE );
5314 }
5315}
5316
5317/* Add a signature-hash-pair to a signature-hash set */
5318void ssl_sig_hash_set_add( ssl_sig_hash_set_t *set,
5319 pk_type_t sig_alg,
5320 md_type_t md_alg )
5321{
5322 switch( sig_alg )
5323 {
5324 case POLARSSL_PK_RSA:
5325 if( set->rsa == POLARSSL_MD_NONE )
5326 set->rsa = md_alg;
5327 break;
5328
5329 case POLARSSL_PK_ECDSA:
5330 if( set->ecdsa == POLARSSL_MD_NONE )
5331 set->ecdsa = md_alg;
5332 break;
5333
5334 default:
5335 break;
5336 }
5337}
5338
5339/* Allow exactly one hash algorithm for each signature. */
5340void ssl_sig_hash_set_const_hash( ssl_sig_hash_set_t *set,
5341 md_type_t md_alg )
5342{
5343 set->rsa = md_alg;
5344 set->ecdsa = md_alg;
5345}
5346
5347#endif /* POLARSSL_SSL_PROTO_TLS1_2) &&
5348 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
5349
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005350/*
5351 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
5352 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005353md_type_t ssl_md_alg_from_hash( unsigned char hash )
5354{
5355 switch( hash )
5356 {
5357#if defined(POLARSSL_MD5_C)
5358 case SSL_HASH_MD5:
5359 return( POLARSSL_MD_MD5 );
5360#endif
5361#if defined(POLARSSL_SHA1_C)
5362 case SSL_HASH_SHA1:
5363 return( POLARSSL_MD_SHA1 );
5364#endif
5365#if defined(POLARSSL_SHA256_C)
5366 case SSL_HASH_SHA224:
5367 return( POLARSSL_MD_SHA224 );
5368 case SSL_HASH_SHA256:
5369 return( POLARSSL_MD_SHA256 );
5370#endif
5371#if defined(POLARSSL_SHA512_C)
5372 case SSL_HASH_SHA384:
5373 return( POLARSSL_MD_SHA384 );
5374 case SSL_HASH_SHA512:
5375 return( POLARSSL_MD_SHA512 );
5376#endif
5377 default:
5378 return( POLARSSL_MD_NONE );
5379 }
5380}
5381
Hanno Beckerc2b9d982017-05-10 16:37:21 +01005382/*
5383 * Convert from POLARSSL_MD_XXX to SSL_HASH_XXX
5384 */
5385unsigned char ssl_hash_from_md_alg( md_type_t md )
5386{
5387 switch( md )
5388 {
5389#if defined(POLARSSL_MD5_C)
5390 case POLARSSL_MD_MD5:
5391 return( SSL_HASH_MD5 );
5392#endif
5393#if defined(POLARSSL_SHA1_C)
5394 case POLARSSL_MD_SHA1:
5395 return( SSL_HASH_SHA1 );
5396#endif
5397#if defined(POLARSSL_SHA256_C)
5398 case POLARSSL_MD_SHA224:
5399 return( SSL_HASH_SHA224 );
5400 case POLARSSL_MD_SHA256:
5401 return( SSL_HASH_SHA256 );
5402#endif
5403#if defined(POLARSSL_SHA512_C)
5404 case POLARSSL_MD_SHA384:
5405 return( SSL_HASH_SHA384 );
5406 case POLARSSL_MD_SHA512:
5407 return( SSL_HASH_SHA512 );
5408#endif
5409 default:
5410 return( SSL_HASH_NONE );
5411 }
5412}
5413
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005414#if defined(POLARSSL_SSL_SET_CURVES)
5415/*
5416 * Check is a curve proposed by the peer is in our list.
5417 * Return 1 if we're willing to use it, 0 otherwise.
5418 */
5419int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
5420{
5421 const ecp_group_id *gid;
5422
5423 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5424 if( *gid == grp_id )
5425 return( 1 );
5426
5427 return( 0 );
5428}
Paul Bakker9af723c2014-05-01 13:03:14 +02005429#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005430
Hanno Beckerc2b9d982017-05-10 16:37:21 +01005431#if defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
5432/*
5433 * Check if a hash proposed by the peer is in our list.
5434 * Return 0 if we're willing to use it, -1 otherwise.
5435 */
5436int ssl_check_sig_hash( md_type_t md )
5437{
5438 const int *cur;
5439
5440 for( cur = md_list(); *cur != POLARSSL_MD_NONE; cur++ )
5441 {
5442#if !defined(POLARSSL_SSL_ENABLE_MD5_SIGNATURES)
5443 /* Skip MD5 */
5444 if( *cur == POLARSSL_MD_MD5 )
5445 continue;
5446#endif
5447 if( *cur == (int) md )
5448 return( 0 );
5449 }
5450
5451 return( -1 );
5452}
5453#endif /* POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
5454
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005455#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005456int ssl_check_cert_usage( const x509_crt *cert,
5457 const ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005458 int cert_endpoint,
5459 int *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005460{
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005461 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005462#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5463 int usage = 0;
5464#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005465#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5466 const char *ext_oid;
5467 size_t ext_len;
5468#endif
5469
5470#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5471 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5472 ((void) cert);
5473 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005474 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005475#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005476
5477#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5478 if( cert_endpoint == SSL_IS_SERVER )
5479 {
5480 /* Server part of the key exchange */
5481 switch( ciphersuite->key_exchange )
5482 {
5483 case POLARSSL_KEY_EXCHANGE_RSA:
5484 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5485 usage = KU_KEY_ENCIPHERMENT;
5486 break;
5487
5488 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5489 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5490 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5491 usage = KU_DIGITAL_SIGNATURE;
5492 break;
5493
5494 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5495 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5496 usage = KU_KEY_AGREEMENT;
5497 break;
5498
5499 /* Don't use default: we want warnings when adding new values */
5500 case POLARSSL_KEY_EXCHANGE_NONE:
5501 case POLARSSL_KEY_EXCHANGE_PSK:
5502 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5503 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5504 usage = 0;
5505 }
5506 }
5507 else
5508 {
5509 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5510 usage = KU_DIGITAL_SIGNATURE;
5511 }
5512
5513 if( x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005514 {
5515 *flags |= BADCERT_KEY_USAGE;
5516 ret = -1;
5517 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005518#else
5519 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005520#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5521
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005522#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5523 if( cert_endpoint == SSL_IS_SERVER )
5524 {
5525 ext_oid = OID_SERVER_AUTH;
5526 ext_len = OID_SIZE( OID_SERVER_AUTH );
5527 }
5528 else
5529 {
5530 ext_oid = OID_CLIENT_AUTH;
5531 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5532 }
5533
5534 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005535 {
5536 *flags |= BADCERT_EXT_KEY_USAGE;
5537 ret = -1;
5538 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005539#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5540
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005541 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005542}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005543#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005544
5545#endif /* POLARSSL_SSL_TLS_C */