blob: 5877e58df64bd305932f3f1fe26abe152e8d55ce [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * The SSL 3.0 specification was drafted by Netscape in 1996,
24 * and became an IETF standard in 1999.
25 *
26 * http://wp.netscape.com/eng/ssl3/
27 * http://www.ietf.org/rfc/rfc2246.txt
28 * http://www.ietf.org/rfc/rfc4346.txt
29 */
30
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#else
34#include POLARSSL_CONFIG_FILE
35#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker40e46942009-01-03 21:51:57 +000037#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker0be444a2013-08-27 21:55:01 +020039#include "polarssl/debug.h"
40#include "polarssl/ssl.h"
41
Rich Evans00ab4702015-02-06 13:43:58 +000042#include <string.h>
43
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020044#if defined(POLARSSL_X509_CRT_PARSE_C) && \
45 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
46#include "polarssl/oid.h"
47#endif
48
Paul Bakker7dc4c442014-02-01 22:50:26 +010049#if defined(POLARSSL_PLATFORM_C)
50#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020051#else
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <stdlib.h>
Paul Bakker6e339b52013-07-03 13:37:05 +020053#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker6edcd412013-10-29 15:22:54 +010057#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
58 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000059#define strcasecmp _stricmp
60#endif
61
Paul Bakker34617722014-06-13 17:20:13 +020062/* Implementation that should never be optimized out by the compiler */
63static void polarssl_zeroize( void *v, size_t n ) {
64 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
65}
66
Paul Bakker05decb22013-08-15 13:33:48 +020067#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020068/*
69 * Convert max_fragment_length codes to length.
70 * RFC 6066 says:
71 * enum{
72 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
73 * } MaxFragmentLength;
74 * and we add 0 -> extension unused
75 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020076static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020077{
78 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
79 512, /* SSL_MAX_FRAG_LEN_512 */
80 1024, /* SSL_MAX_FRAG_LEN_1024 */
81 2048, /* SSL_MAX_FRAG_LEN_2048 */
82 4096, /* SSL_MAX_FRAG_LEN_4096 */
83};
Paul Bakker05decb22013-08-15 13:33:48 +020084#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020085
Simon Butcher149950d2016-10-15 22:08:08 +010086#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020087static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
88{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020089 ssl_session_free( dst );
90 memcpy( dst, src, sizeof( ssl_session ) );
91
Paul Bakker7c6b2c32013-09-16 13:49:26 +020092#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020093 if( src->peer_cert != NULL )
94 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020095 int ret;
96
Mansour Moufidc531b4a2015-02-15 17:35:38 -050097 dst->peer_cert = polarssl_malloc( sizeof(x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020098 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020099 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
100
Paul Bakkerb6b09562013-09-18 14:17:41 +0200101 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200102
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200103 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
104 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200105 {
106 polarssl_free( dst->peer_cert );
107 dst->peer_cert = NULL;
108 return( ret );
109 }
110 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200111#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200112
Paul Bakkera503a632013-08-14 13:48:06 +0200113#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200114 if( src->ticket != NULL )
115 {
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500116 dst->ticket = polarssl_malloc( src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200117 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200118 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
119
120 memcpy( dst->ticket, src->ticket, src->ticket_len );
121 }
Paul Bakkera503a632013-08-14 13:48:06 +0200122#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200123
124 return( 0 );
125}
Simon Butcher149950d2016-10-15 22:08:08 +0100126#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200127
Paul Bakker05ef8352012-05-08 09:17:57 +0000128#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200129int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200130 const unsigned char *key_enc, const unsigned char *key_dec,
131 size_t keylen,
132 const unsigned char *iv_enc, const unsigned char *iv_dec,
133 size_t ivlen,
134 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200135 size_t maclen ) = NULL;
136int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
137int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
138int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
139int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
140int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200141#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000142
Paul Bakker5121ce52009-01-03 21:22:43 +0000143/*
144 * Key material generation
145 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200146#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200147static int ssl3_prf( const unsigned char *secret, size_t slen,
148 const char *label,
149 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000150 unsigned char *dstbuf, size_t dlen )
151{
152 size_t i;
153 md5_context md5;
154 sha1_context sha1;
155 unsigned char padding[16];
156 unsigned char sha1sum[20];
157 ((void)label);
158
Paul Bakker5b4af392014-06-26 12:09:34 +0200159 md5_init( &md5 );
160 sha1_init( &sha1 );
161
Paul Bakker5f70b252012-09-13 14:23:06 +0000162 /*
163 * SSLv3:
164 * block =
165 * MD5( secret + SHA1( 'A' + secret + random ) ) +
166 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
167 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
168 * ...
169 */
170 for( i = 0; i < dlen / 16; i++ )
171 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200172 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000173
174 sha1_starts( &sha1 );
175 sha1_update( &sha1, padding, 1 + i );
176 sha1_update( &sha1, secret, slen );
177 sha1_update( &sha1, random, rlen );
178 sha1_finish( &sha1, sha1sum );
179
180 md5_starts( &md5 );
181 md5_update( &md5, secret, slen );
182 md5_update( &md5, sha1sum, 20 );
183 md5_finish( &md5, dstbuf + i * 16 );
184 }
185
Paul Bakker5b4af392014-06-26 12:09:34 +0200186 md5_free( &md5 );
187 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000188
Paul Bakker34617722014-06-13 17:20:13 +0200189 polarssl_zeroize( padding, sizeof( padding ) );
190 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000191
192 return( 0 );
193}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200194#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000195
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200196#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200197static int tls1_prf( const unsigned char *secret, size_t slen,
198 const char *label,
199 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000200 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000201{
Paul Bakker23986e52011-04-24 08:57:21 +0000202 size_t nb, hs;
203 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200204 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 unsigned char tmp[128];
206 unsigned char h_i[20];
207
208 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000209 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
211 hs = ( slen + 1 ) / 2;
212 S1 = secret;
213 S2 = secret + slen - hs;
214
215 nb = strlen( label );
216 memcpy( tmp + 20, label, nb );
217 memcpy( tmp + 20 + nb, random, rlen );
218 nb += rlen;
219
220 /*
221 * First compute P_md5(secret,label+random)[0..dlen]
222 */
223 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
224
225 for( i = 0; i < dlen; i += 16 )
226 {
227 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
228 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
229
230 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
231
232 for( j = 0; j < k; j++ )
233 dstbuf[i + j] = h_i[j];
234 }
235
236 /*
237 * XOR out with P_sha1(secret,label+random)[0..dlen]
238 */
239 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
240
241 for( i = 0; i < dlen; i += 20 )
242 {
243 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
244 sha1_hmac( S2, hs, tmp, 20, tmp );
245
246 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
247
248 for( j = 0; j < k; j++ )
249 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
250 }
251
Paul Bakker34617722014-06-13 17:20:13 +0200252 polarssl_zeroize( tmp, sizeof( tmp ) );
253 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
255 return( 0 );
256}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200257#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000258
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200259#if defined(POLARSSL_SSL_PROTO_TLS1_2)
260#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200261static int tls_prf_sha256( const unsigned char *secret, size_t slen,
262 const char *label,
263 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000264 unsigned char *dstbuf, size_t dlen )
265{
266 size_t nb;
267 size_t i, j, k;
268 unsigned char tmp[128];
269 unsigned char h_i[32];
270
271 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
272 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
273
274 nb = strlen( label );
275 memcpy( tmp + 32, label, nb );
276 memcpy( tmp + 32 + nb, random, rlen );
277 nb += rlen;
278
279 /*
280 * Compute P_<hash>(secret, label + random)[0..dlen]
281 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200282 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000283
284 for( i = 0; i < dlen; i += 32 )
285 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200286 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
287 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000288
289 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
290
291 for( j = 0; j < k; j++ )
292 dstbuf[i + j] = h_i[j];
293 }
294
Paul Bakker34617722014-06-13 17:20:13 +0200295 polarssl_zeroize( tmp, sizeof( tmp ) );
296 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000297
298 return( 0 );
299}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200300#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000301
Paul Bakker9e36f042013-06-30 14:34:05 +0200302#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200303static int tls_prf_sha384( const unsigned char *secret, size_t slen,
304 const char *label,
305 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000306 unsigned char *dstbuf, size_t dlen )
307{
308 size_t nb;
309 size_t i, j, k;
310 unsigned char tmp[128];
311 unsigned char h_i[48];
312
313 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
314 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
315
316 nb = strlen( label );
317 memcpy( tmp + 48, label, nb );
318 memcpy( tmp + 48 + nb, random, rlen );
319 nb += rlen;
320
321 /*
322 * Compute P_<hash>(secret, label + random)[0..dlen]
323 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200324 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000325
326 for( i = 0; i < dlen; i += 48 )
327 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200328 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
329 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000330
331 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
332
333 for( j = 0; j < k; j++ )
334 dstbuf[i + j] = h_i[j];
335 }
336
Paul Bakker34617722014-06-13 17:20:13 +0200337 polarssl_zeroize( tmp, sizeof( tmp ) );
338 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000339
340 return( 0 );
341}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200342#endif /* POLARSSL_SHA512_C */
343#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000344
Paul Bakker66d5d072014-06-17 16:39:18 +0200345static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200346
347#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
348 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200349static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200350#endif
Paul Bakker380da532012-04-18 16:10:25 +0000351
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200353static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
354static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200355#endif
356
357#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200358static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
359static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200360#endif
361
362#if defined(POLARSSL_SSL_PROTO_TLS1_2)
363#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200364static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
365static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
366static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200367#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100368
Paul Bakker9e36f042013-06-30 14:34:05 +0200369#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200370static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
371static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
372static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100373#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200374#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000375
Paul Bakker5121ce52009-01-03 21:22:43 +0000376int ssl_derive_keys( ssl_context *ssl )
377{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200378 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 unsigned char keyblk[256];
381 unsigned char *key1;
382 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100383 unsigned char *mac_enc;
384 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200385 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100386 const cipher_info_t *cipher_info;
387 const md_info_t *md_info;
388
Paul Bakker48916f92012-09-16 19:57:18 +0000389 ssl_session *session = ssl->session_negotiate;
390 ssl_transform *transform = ssl->transform_negotiate;
391 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000392
393 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
394
Paul Bakker68884e32013-01-07 18:20:04 +0100395 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
396 if( cipher_info == NULL )
397 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200398 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100399 transform->ciphersuite_info->cipher ) );
400 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
401 }
402
403 md_info = md_info_from_type( transform->ciphersuite_info->mac );
404 if( md_info == NULL )
405 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200406 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100407 transform->ciphersuite_info->mac ) );
408 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
409 }
410
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000412 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000413 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200414#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000415 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000416 {
Paul Bakker48916f92012-09-16 19:57:18 +0000417 handshake->tls_prf = ssl3_prf;
418 handshake->calc_verify = ssl_calc_verify_ssl;
419 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000420 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200421 else
422#endif
423#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
424 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000425 {
Paul Bakker48916f92012-09-16 19:57:18 +0000426 handshake->tls_prf = tls1_prf;
427 handshake->calc_verify = ssl_calc_verify_tls;
428 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000429 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200430 else
431#endif
432#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200433#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200434 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
435 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000436 {
Paul Bakker48916f92012-09-16 19:57:18 +0000437 handshake->tls_prf = tls_prf_sha384;
438 handshake->calc_verify = ssl_calc_verify_tls_sha384;
439 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000440 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000441 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200442#endif
443#if defined(POLARSSL_SHA256_C)
444 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000445 {
Paul Bakker48916f92012-09-16 19:57:18 +0000446 handshake->tls_prf = tls_prf_sha256;
447 handshake->calc_verify = ssl_calc_verify_tls_sha256;
448 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000449 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200450 else
451#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200452#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200453 {
454 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200455 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200456 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000457
458 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 * SSLv3:
460 * master =
461 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
462 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
463 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200464 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200465 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 * master = PRF( premaster, "master secret", randbytes )[0..47]
467 */
Paul Bakker0a597072012-09-25 21:55:46 +0000468 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 {
Paul Bakker48916f92012-09-16 19:57:18 +0000470 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
471 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200473#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
474 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200475 {
476 unsigned char session_hash[48];
477 size_t hash_len;
478
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200479 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200480
481 ssl->handshake->calc_verify( ssl, session_hash );
482
483#if defined(POLARSSL_SSL_PROTO_TLS1_2)
484 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
485 {
486#if defined(POLARSSL_SHA512_C)
487 if( ssl->transform_negotiate->ciphersuite_info->mac ==
488 POLARSSL_MD_SHA384 )
489 {
490 hash_len = 48;
491 }
492 else
493#endif
494 hash_len = 32;
495 }
496 else
497#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
498 hash_len = 36;
499
500 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
501
502 handshake->tls_prf( handshake->premaster, handshake->pmslen,
503 "extended master secret",
504 session_hash, hash_len, session->master, 48 );
505
506 }
507 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200508#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000509 handshake->tls_prf( handshake->premaster, handshake->pmslen,
510 "master secret",
511 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200513
Paul Bakker34617722014-06-13 17:20:13 +0200514 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 }
516 else
517 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
518
519 /*
520 * Swap the client and server random values.
521 */
Paul Bakker48916f92012-09-16 19:57:18 +0000522 memcpy( tmp, handshake->randbytes, 64 );
523 memcpy( handshake->randbytes, tmp + 32, 32 );
524 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200525 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527 /*
528 * SSLv3:
529 * key block =
530 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
531 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
532 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
533 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
534 * ...
535 *
536 * TLSv1:
537 * key block = PRF( master, "key expansion", randbytes )
538 */
Paul Bakker48916f92012-09-16 19:57:18 +0000539 handshake->tls_prf( session->master, 48, "key expansion",
540 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000541
Paul Bakker48916f92012-09-16 19:57:18 +0000542 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
543 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
544 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
545 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
547
Paul Bakker34617722014-06-13 17:20:13 +0200548 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
550 /*
551 * Determine the appropriate key, IV and MAC length.
552 */
Paul Bakker68884e32013-01-07 18:20:04 +0100553
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200554 transform->keylen = cipher_info->key_length / 8;
555
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200556 if( cipher_info->mode == POLARSSL_MODE_GCM ||
557 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200559 transform->maclen = 0;
560
Paul Bakker68884e32013-01-07 18:20:04 +0100561 transform->ivlen = 12;
562 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200563
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200564 /* Minimum length is expicit IV + tag */
565 transform->minlen = transform->ivlen - transform->fixed_ivlen
566 + ( transform->ciphersuite_info->flags &
567 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100568 }
569 else
570 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200571 /* Initialize HMAC contexts */
572 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
573 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100574 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200575 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
576 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100577 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200579 /* Get MAC length */
580 transform->maclen = md_get_size( md_info );
581
582#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
583 /*
584 * If HMAC is to be truncated, we shall keep the leftmost bytes,
585 * (rfc 6066 page 13 or rfc 2104 section 4),
586 * so we only need to adjust the length here.
587 */
588 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
589 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
590#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
591
592 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100593 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200595 /* Minimum length */
596 if( cipher_info->mode == POLARSSL_MODE_STREAM )
597 transform->minlen = transform->maclen;
598 else
Paul Bakker68884e32013-01-07 18:20:04 +0100599 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200600 /*
601 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100602 * 1. if EtM is in use: one block plus MAC
603 * otherwise: * first multiple of blocklen greater than maclen
604 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200605 */
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100606#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
607 if( session->encrypt_then_mac == SSL_ETM_ENABLED )
608 {
609 transform->minlen = transform->maclen
610 + cipher_info->block_size;
611 }
612 else
613#endif
614 {
615 transform->minlen = transform->maclen
616 + cipher_info->block_size
617 - transform->maclen % cipher_info->block_size;
618 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200619
620#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
621 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
622 ssl->minor_ver == SSL_MINOR_VERSION_1 )
623 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100624 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200625#endif
626#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
627 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
628 ssl->minor_ver == SSL_MINOR_VERSION_3 )
629 {
630 transform->minlen += transform->ivlen;
631 }
632 else
633#endif
634 {
635 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
636 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
637 }
Paul Bakker68884e32013-01-07 18:20:04 +0100638 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 }
640
641 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000642 transform->keylen, transform->minlen, transform->ivlen,
643 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000644
645 /*
646 * Finally setup the cipher contexts, IVs and MAC secrets.
647 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100648#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000649 if( ssl->endpoint == SSL_IS_CLIENT )
650 {
Paul Bakker48916f92012-09-16 19:57:18 +0000651 key1 = keyblk + transform->maclen * 2;
652 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000653
Paul Bakker68884e32013-01-07 18:20:04 +0100654 mac_enc = keyblk;
655 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000656
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000657 /*
658 * This is not used in TLS v1.1.
659 */
Paul Bakker48916f92012-09-16 19:57:18 +0000660 iv_copy_len = ( transform->fixed_ivlen ) ?
661 transform->fixed_ivlen : transform->ivlen;
662 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
663 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000664 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000665 }
666 else
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100667#endif /* POLARSSL_SSL_CLI_C */
668#if defined(POLARSSL_SSL_SRV_C)
669 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000670 {
Paul Bakker48916f92012-09-16 19:57:18 +0000671 key1 = keyblk + transform->maclen * 2 + transform->keylen;
672 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000673
Paul Bakker68884e32013-01-07 18:20:04 +0100674 mac_enc = keyblk + transform->maclen;
675 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000676
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000677 /*
678 * This is not used in TLS v1.1.
679 */
Paul Bakker48916f92012-09-16 19:57:18 +0000680 iv_copy_len = ( transform->fixed_ivlen ) ?
681 transform->fixed_ivlen : transform->ivlen;
682 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
683 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000684 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100686 else
687#endif /* POLARSSL_SSL_SRV_C */
688 {
689 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
690 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
691 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000692
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200693#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100694 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
695 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100696 if( transform->maclen > sizeof transform->mac_enc )
697 {
698 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200699 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100700 }
701
Paul Bakker68884e32013-01-07 18:20:04 +0100702 memcpy( transform->mac_enc, mac_enc, transform->maclen );
703 memcpy( transform->mac_dec, mac_dec, transform->maclen );
704 }
705 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200706#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200707#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
708 defined(POLARSSL_SSL_PROTO_TLS1_2)
709 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100710 {
711 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
712 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
713 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200714 else
715#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200716 {
717 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200718 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200719 }
Paul Bakker68884e32013-01-07 18:20:04 +0100720
Paul Bakker05ef8352012-05-08 09:17:57 +0000721#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200722 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000723 {
724 int ret = 0;
725
726 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
727
Paul Bakker07eb38b2012-12-19 14:42:06 +0100728 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
729 transform->iv_enc, transform->iv_dec,
730 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100731 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100732 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000733 {
734 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200735 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000736 }
737 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200738#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000739
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200740 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
741 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000742 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200743 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
744 return( ret );
745 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200746
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200747 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
748 cipher_info ) ) != 0 )
749 {
750 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
751 return( ret );
752 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200753
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200754 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
755 cipher_info->key_length,
756 POLARSSL_ENCRYPT ) ) != 0 )
757 {
758 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
759 return( ret );
760 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200761
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200762 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
763 cipher_info->key_length,
764 POLARSSL_DECRYPT ) ) != 0 )
765 {
766 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
767 return( ret );
768 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200769
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200770#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200771 if( cipher_info->mode == POLARSSL_MODE_CBC )
772 {
773 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
774 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200775 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200776 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
777 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200778 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200779
780 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
781 POLARSSL_PADDING_NONE ) ) != 0 )
782 {
783 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
784 return( ret );
785 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000786 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200787#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000788
Paul Bakker34617722014-06-13 17:20:13 +0200789 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000790
Paul Bakker2770fbd2012-07-03 13:30:23 +0000791#if defined(POLARSSL_ZLIB_SUPPORT)
792 // Initialize compression
793 //
Paul Bakker48916f92012-09-16 19:57:18 +0000794 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000795 {
Paul Bakker16770332013-10-11 09:59:44 +0200796 if( ssl->compress_buf == NULL )
797 {
798 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
799 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
800 if( ssl->compress_buf == NULL )
801 {
802 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
803 SSL_BUFFER_LEN ) );
804 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
805 }
806 }
807
Paul Bakker2770fbd2012-07-03 13:30:23 +0000808 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
809
Paul Bakker48916f92012-09-16 19:57:18 +0000810 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
811 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000812
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200813 if( deflateInit( &transform->ctx_deflate,
814 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000815 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000816 {
817 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
818 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
819 }
820 }
821#endif /* POLARSSL_ZLIB_SUPPORT */
822
Paul Bakker5121ce52009-01-03 21:22:43 +0000823 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
824
825 return( 0 );
826}
827
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200828#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000829void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000830{
831 md5_context md5;
832 sha1_context sha1;
833 unsigned char pad_1[48];
834 unsigned char pad_2[48];
835
Paul Bakker380da532012-04-18 16:10:25 +0000836 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000837
Paul Bakker48916f92012-09-16 19:57:18 +0000838 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
839 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000840
Paul Bakker380da532012-04-18 16:10:25 +0000841 memset( pad_1, 0x36, 48 );
842 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000843
Paul Bakker48916f92012-09-16 19:57:18 +0000844 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000845 md5_update( &md5, pad_1, 48 );
846 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000847
Paul Bakker380da532012-04-18 16:10:25 +0000848 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000849 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000850 md5_update( &md5, pad_2, 48 );
851 md5_update( &md5, hash, 16 );
852 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000853
Paul Bakker48916f92012-09-16 19:57:18 +0000854 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000855 sha1_update( &sha1, pad_1, 40 );
856 sha1_finish( &sha1, hash + 16 );
857
858 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000859 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000860 sha1_update( &sha1, pad_2, 40 );
861 sha1_update( &sha1, hash + 16, 20 );
862 sha1_finish( &sha1, hash + 16 );
863
864 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
865 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
866
Paul Bakker5b4af392014-06-26 12:09:34 +0200867 md5_free( &md5 );
868 sha1_free( &sha1 );
869
Paul Bakker380da532012-04-18 16:10:25 +0000870 return;
871}
Paul Bakker9af723c2014-05-01 13:03:14 +0200872#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000873
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200874#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000875void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
876{
877 md5_context md5;
878 sha1_context sha1;
879
880 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
881
Paul Bakker48916f92012-09-16 19:57:18 +0000882 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
883 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000884
Paul Bakker48916f92012-09-16 19:57:18 +0000885 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000886 sha1_finish( &sha1, hash + 16 );
887
888 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
889 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
890
Paul Bakker5b4af392014-06-26 12:09:34 +0200891 md5_free( &md5 );
892 sha1_free( &sha1 );
893
Paul Bakker380da532012-04-18 16:10:25 +0000894 return;
895}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200896#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000897
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200898#if defined(POLARSSL_SSL_PROTO_TLS1_2)
899#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000900void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
901{
Paul Bakker9e36f042013-06-30 14:34:05 +0200902 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000903
904 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
905
Paul Bakker9e36f042013-06-30 14:34:05 +0200906 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
907 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000908
909 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
910 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
911
Paul Bakker5b4af392014-06-26 12:09:34 +0200912 sha256_free( &sha256 );
913
Paul Bakker380da532012-04-18 16:10:25 +0000914 return;
915}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200916#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000917
Paul Bakker9e36f042013-06-30 14:34:05 +0200918#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000919void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
920{
Paul Bakker9e36f042013-06-30 14:34:05 +0200921 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000922
923 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
924
Paul Bakker9e36f042013-06-30 14:34:05 +0200925 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
926 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000927
Paul Bakkerca4ab492012-04-18 14:23:57 +0000928 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
930
Paul Bakker5b4af392014-06-26 12:09:34 +0200931 sha512_free( &sha512 );
932
Paul Bakker5121ce52009-01-03 21:22:43 +0000933 return;
934}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200935#endif /* POLARSSL_SHA512_C */
936#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000937
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200938#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200939int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
940{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200941 unsigned char *p = ssl->handshake->premaster;
942 unsigned char *end = p + sizeof( ssl->handshake->premaster );
943
944 /*
945 * PMS = struct {
946 * opaque other_secret<0..2^16-1>;
947 * opaque psk<0..2^16-1>;
948 * };
949 * with "other_secret" depending on the particular key exchange
950 */
951#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
952 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
953 {
Manuel Pégourié-Gonnard5ca36402015-10-21 12:35:29 +0200954 if( end - p < 2 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200955 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
956
957 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
958 *(p++) = (unsigned char)( ssl->psk_len );
Manuel Pégourié-Gonnard5ca36402015-10-21 12:35:29 +0200959
960 if( end < p || (size_t)( end - p ) < ssl->psk_len )
961 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
962
963 memset( p, 0, ssl->psk_len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200964 p += ssl->psk_len;
965 }
966 else
967#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200968#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
969 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
970 {
971 /*
972 * other_secret already set by the ClientKeyExchange message,
973 * and is 48 bytes long
974 */
975 *p++ = 0;
976 *p++ = 48;
977 p += 48;
978 }
979 else
980#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200981#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
982 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
983 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200984 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200985 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200986
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200987 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200988 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200989 p + 2, &len,
990 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200991 {
992 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
993 return( ret );
994 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200995 *(p++) = (unsigned char)( len >> 8 );
996 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200997 p += len;
998
999 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1000 }
1001 else
1002#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1003#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1004 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1005 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001006 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001007 size_t zlen;
1008
1009 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001010 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001011 ssl->f_rng, ssl->p_rng ) ) != 0 )
1012 {
1013 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1014 return( ret );
1015 }
1016
1017 *(p++) = (unsigned char)( zlen >> 8 );
1018 *(p++) = (unsigned char)( zlen );
1019 p += zlen;
1020
1021 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1022 }
1023 else
1024#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1025 {
1026 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001027 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001028 }
1029
1030 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnard5ca36402015-10-21 12:35:29 +02001031 if( end - p < 2 )
1032 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001033
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001034 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1035 *(p++) = (unsigned char)( ssl->psk_len );
Manuel Pégourié-Gonnard5ca36402015-10-21 12:35:29 +02001036
1037 if( end < p || (size_t)( end - p ) < ssl->psk_len )
1038 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1039
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001040 memcpy( p, ssl->psk, ssl->psk_len );
1041 p += ssl->psk_len;
1042
1043 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1044
1045 return( 0 );
1046}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001047#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001048
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001049#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001050/*
1051 * SSLv3.0 MAC functions
1052 */
Paul Bakker68884e32013-01-07 18:20:04 +01001053static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1054 unsigned char *buf, size_t len,
1055 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001056{
1057 unsigned char header[11];
1058 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001059 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001060 int md_size = md_get_size( md_ctx->md_info );
1061 int md_type = md_get_type( md_ctx->md_info );
1062
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001063 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001064 if( md_type == POLARSSL_MD_MD5 )
1065 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001066 else
Paul Bakker68884e32013-01-07 18:20:04 +01001067 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001068
1069 memcpy( header, ctr, 8 );
1070 header[ 8] = (unsigned char) type;
1071 header[ 9] = (unsigned char)( len >> 8 );
1072 header[10] = (unsigned char)( len );
1073
Paul Bakker68884e32013-01-07 18:20:04 +01001074 memset( padding, 0x36, padlen );
1075 md_starts( md_ctx );
1076 md_update( md_ctx, secret, md_size );
1077 md_update( md_ctx, padding, padlen );
1078 md_update( md_ctx, header, 11 );
1079 md_update( md_ctx, buf, len );
1080 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001081
Paul Bakker68884e32013-01-07 18:20:04 +01001082 memset( padding, 0x5C, padlen );
1083 md_starts( md_ctx );
1084 md_update( md_ctx, secret, md_size );
1085 md_update( md_ctx, padding, padlen );
1086 md_update( md_ctx, buf + len, md_size );
1087 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001088}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001089#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001090
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001091#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1092 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1093 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1094#define POLARSSL_SOME_MODES_USE_MAC
1095#endif
1096
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001097/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001098 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001099 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001100static int ssl_encrypt_buf( ssl_context *ssl )
1101{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001102 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001103 cipher_mode_t mode;
1104 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001105
1106 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1107
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001108 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1109 {
1110 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1111 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1112 }
1113
1114 mode = cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
1115
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001116 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1117 ssl->out_msg, ssl->out_msglen );
1118
Paul Bakker5121ce52009-01-03 21:22:43 +00001119 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001120 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001121 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001122#if defined(POLARSSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001123 if( mode == POLARSSL_MODE_STREAM ||
1124 ( mode == POLARSSL_MODE_CBC
1125#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1126 && ssl->session_out->encrypt_then_mac == SSL_ETM_DISABLED
1127#endif
1128 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001129 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001130#if defined(POLARSSL_SSL_PROTO_SSL3)
1131 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1132 {
1133 ssl_mac( &ssl->transform_out->md_ctx_enc,
1134 ssl->transform_out->mac_enc,
1135 ssl->out_msg, ssl->out_msglen,
1136 ssl->out_ctr, ssl->out_msgtype );
1137 }
1138 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001139#endif
1140#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001141 defined(POLARSSL_SSL_PROTO_TLS1_2)
1142 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1143 {
Hanno Becker251bab52017-11-20 10:30:08 +00001144 unsigned char mac[SSL_MAC_ADD];
1145
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001146 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1147 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1148 ssl->out_msg, ssl->out_msglen );
Hanno Becker251bab52017-11-20 10:30:08 +00001149 md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001150 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker251bab52017-11-20 10:30:08 +00001151
1152 memcpy( ssl->out_msg + ssl->out_msglen, mac,
1153 ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001154 }
1155 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001156#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001157 {
1158 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001159 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001160 }
1161
Hanno Becker0a139f92017-11-21 17:41:59 +00001162 SSL_DEBUG_BUF( 4, "expected mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001163 ssl->out_msg + ssl->out_msglen,
1164 ssl->transform_out->maclen );
1165
1166 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001167 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001168 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001169#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001170
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001171 /*
1172 * Encrypt
1173 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001174#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001175 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001176 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001177 int ret;
1178 size_t olen = 0;
1179
Paul Bakker5121ce52009-01-03 21:22:43 +00001180 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1181 "including %d bytes of padding",
1182 ssl->out_msglen, 0 ) );
1183
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001184 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001185 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001186 ssl->transform_out->ivlen,
1187 ssl->out_msg, ssl->out_msglen,
1188 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001189 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001190 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001191 return( ret );
1192 }
1193
1194 if( ssl->out_msglen != olen )
1195 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001196 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001197 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001198 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001199 }
Paul Bakker68884e32013-01-07 18:20:04 +01001200 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001201#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001202#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1203 if( mode == POLARSSL_MODE_GCM ||
1204 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001205 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001206 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001207 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001208 unsigned char *enc_msg;
1209 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001210 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1211 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001212
Paul Bakkerca4ab492012-04-18 14:23:57 +00001213 memcpy( add_data, ssl->out_ctr, 8 );
1214 add_data[8] = ssl->out_msgtype;
1215 add_data[9] = ssl->major_ver;
1216 add_data[10] = ssl->minor_ver;
1217 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1218 add_data[12] = ssl->out_msglen & 0xFF;
1219
1220 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1221 add_data, 13 );
1222
Paul Bakker68884e32013-01-07 18:20:04 +01001223 /*
1224 * Generate IV
1225 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001226 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1227 {
1228 /* Reminder if we ever add an AEAD mode with a different size */
1229 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1230 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1231 }
1232
1233 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1234 ssl->out_ctr, 8 );
1235 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001236
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001237 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001238 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001239
Paul Bakker68884e32013-01-07 18:20:04 +01001240 /*
1241 * Fix pointer positions and message length with added IV
1242 */
1243 enc_msg = ssl->out_msg;
1244 enc_msglen = ssl->out_msglen;
1245 ssl->out_msglen += ssl->transform_out->ivlen -
1246 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001247
Paul Bakker68884e32013-01-07 18:20:04 +01001248 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1249 "including %d bytes of padding",
1250 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001251
Paul Bakker68884e32013-01-07 18:20:04 +01001252 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001253 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001254 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001255 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1256 ssl->transform_out->iv_enc,
1257 ssl->transform_out->ivlen,
1258 add_data, 13,
1259 enc_msg, enc_msglen,
1260 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001261 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001262 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001263 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001264 return( ret );
1265 }
1266
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001267 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001268 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001269 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001270 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001271 }
1272
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001273 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001274 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001275
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001276 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001277 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001278 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001279#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001280#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1281 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001282 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001283 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001284 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001285 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001286 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001287
Paul Bakker48916f92012-09-16 19:57:18 +00001288 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1289 ssl->transform_out->ivlen;
1290 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001291 padlen = 0;
1292
1293 for( i = 0; i <= padlen; i++ )
1294 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1295
1296 ssl->out_msglen += padlen + 1;
1297
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001298 enc_msglen = ssl->out_msglen;
1299 enc_msg = ssl->out_msg;
1300
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001301#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001302 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001303 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1304 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001305 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001306 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001307 {
1308 /*
1309 * Generate IV
1310 */
Manuel Pégourié-Gonnarda67fd792015-08-27 12:02:40 +02001311 ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001312 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001313 if( ret != 0 )
1314 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001315
Paul Bakker92be97b2013-01-02 17:30:03 +01001316 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001317 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001318
1319 /*
1320 * Fix pointer positions and message length with added IV
1321 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001322 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001323 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001324 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001325 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001326#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001327
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001329 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001330 ssl->out_msglen, ssl->transform_out->ivlen,
1331 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001332
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001333 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001334 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001335 ssl->transform_out->ivlen,
1336 enc_msg, enc_msglen,
1337 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001338 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001339 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001340 return( ret );
1341 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001342
Paul Bakkercca5b812013-08-31 17:40:26 +02001343 if( enc_msglen != olen )
1344 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001345 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001346 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001347 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001348
1349#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001350 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1351 {
1352 /*
1353 * Save IV in SSL3 and TLS1
1354 */
1355 memcpy( ssl->transform_out->iv_enc,
1356 ssl->transform_out->cipher_ctx_enc.iv,
1357 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001359#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001360
1361#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001362 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001363 {
1364 /*
1365 * MAC(MAC_write_key, seq_num +
1366 * TLSCipherText.type +
1367 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001368 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001369 * IV + // except for TLS 1.0
1370 * ENC(content + padding + padding_length));
1371 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001372 unsigned char pseudo_hdr[13];
1373
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001374 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1375
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001376 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1377 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001378 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1379 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1380
1381 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001382
1383 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1384 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1385 ssl->out_iv, ssl->out_msglen );
1386 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1387 ssl->out_iv + ssl->out_msglen );
1388 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1389
1390 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001391 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001392 }
1393#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001395 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001396#endif /* POLARSSL_CIPHER_MODE_CBC &&
1397 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001398 {
1399 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001400 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001401 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001402
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001403 /* Make extra sure authentication was performed, exactly once */
1404 if( auth_done != 1 )
1405 {
1406 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1407 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1408 }
1409
Paul Bakkerca4ab492012-04-18 14:23:57 +00001410 for( i = 8; i > 0; i-- )
1411 if( ++ssl->out_ctr[i - 1] != 0 )
1412 break;
1413
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001414 /* The loops goes to its end iff the counter is wrapping */
1415 if( i == 0 )
1416 {
1417 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1418 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1419 }
1420
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1422
1423 return( 0 );
1424}
1425
1426static int ssl_decrypt_buf( ssl_context *ssl )
1427{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001428 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001429 cipher_mode_t mode;
1430 int auth_done = 0;
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001431#if defined(POLARSSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001432 size_t padlen = 0, correct = 1;
1433#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001434
1435 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1436
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001437 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1438 {
1439 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1440 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1441 }
1442
1443 mode = cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
1444
Paul Bakker48916f92012-09-16 19:57:18 +00001445 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001446 {
1447 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001448 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001449 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001450 }
1451
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001452#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001453 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001454 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001455 int ret;
1456 size_t olen = 0;
1457
Paul Bakker68884e32013-01-07 18:20:04 +01001458 padlen = 0;
1459
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001460 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001461 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001462 ssl->transform_in->ivlen,
1463 ssl->in_msg, ssl->in_msglen,
1464 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001465 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001466 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001467 return( ret );
1468 }
1469
1470 if( ssl->in_msglen != olen )
1471 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001472 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001473 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001474 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001475 }
Paul Bakker68884e32013-01-07 18:20:04 +01001476 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001477#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001478#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1479 if( mode == POLARSSL_MODE_GCM ||
1480 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001481 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001482 int ret;
1483 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001484 unsigned char *dec_msg;
1485 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001486 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001487 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1488 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Simon Ba697bf52016-11-10 13:19:42 +00001489 size_t explicit_iv_len = ssl->transform_in->ivlen -
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001490 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001491
Manuel Pégourié-Gonnard06d75192015-02-11 14:54:11 +00001492 if( ssl->in_msglen < (size_t) explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001493 {
1494 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1495 "+ taglen (%d)", ssl->in_msglen,
1496 explicit_iv_len, taglen ) );
1497 return( POLARSSL_ERR_SSL_INVALID_MAC );
1498 }
1499 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1500
Paul Bakker68884e32013-01-07 18:20:04 +01001501 dec_msg = ssl->in_msg;
1502 dec_msg_result = ssl->in_msg;
1503 ssl->in_msglen = dec_msglen;
1504
1505 memcpy( add_data, ssl->in_ctr, 8 );
1506 add_data[8] = ssl->in_msgtype;
1507 add_data[9] = ssl->major_ver;
1508 add_data[10] = ssl->minor_ver;
1509 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1510 add_data[12] = ssl->in_msglen & 0xFF;
1511
1512 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1513 add_data, 13 );
1514
1515 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1516 ssl->in_iv,
1517 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1518
1519 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1520 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001521 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001522
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001523 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001524 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001525 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001526 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1527 ssl->transform_in->iv_dec,
1528 ssl->transform_in->ivlen,
1529 add_data, 13,
1530 dec_msg, dec_msglen,
1531 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001532 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001533 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001534 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1535
1536 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1537 return( POLARSSL_ERR_SSL_INVALID_MAC );
1538
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001539 return( ret );
1540 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001541 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001542
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001543 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001544 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001545 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001546 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001547 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001548 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001549 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001550#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001551#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1552 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001553 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001554 {
Paul Bakker45829992013-01-03 14:52:21 +01001555 /*
1556 * Decrypt and check the padding
1557 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001558 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001559 unsigned char *dec_msg;
1560 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001561 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001562 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001563 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001564
Paul Bakker5121ce52009-01-03 21:22:43 +00001565 /*
Paul Bakker45829992013-01-03 14:52:21 +01001566 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001567 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001568#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001569 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1570 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001571#endif
Paul Bakker45829992013-01-03 14:52:21 +01001572
1573 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1574 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1575 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001576 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1577 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1578 ssl->transform_in->ivlen,
1579 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001580 return( POLARSSL_ERR_SSL_INVALID_MAC );
1581 }
1582
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001583 dec_msglen = ssl->in_msglen;
1584 dec_msg = ssl->in_msg;
1585 dec_msg_result = ssl->in_msg;
1586
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001587 /*
1588 * Authenticate before decrypt if enabled
1589 */
1590#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001591 if( ssl->session_in->encrypt_then_mac == SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001592 {
Hanno Becker251bab52017-11-20 10:30:08 +00001593 unsigned char mac_expect[SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001594 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001595
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001596 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1597
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001598 dec_msglen -= ssl->transform_in->maclen;
1599 ssl->in_msglen -= ssl->transform_in->maclen;
1600
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001601 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1602 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1603 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1604 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1605
1606 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1607
1608 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001609 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1610 ssl->in_iv, ssl->in_msglen );
Hanno Becker251bab52017-11-20 10:30:08 +00001611 md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001612 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1613
1614 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1615 ssl->transform_in->maclen );
Hanno Becker0a139f92017-11-21 17:41:59 +00001616 SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001617 ssl->transform_in->maclen );
1618
Hanno Becker251bab52017-11-20 10:30:08 +00001619 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001620 ssl->transform_in->maclen ) != 0 )
1621 {
1622 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1623
1624 return( POLARSSL_ERR_SSL_INVALID_MAC );
1625 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001626 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001627 }
1628#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1629
1630 /*
1631 * Check length sanity
1632 */
1633 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1634 {
1635 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1636 ssl->in_msglen, ssl->transform_in->ivlen ) );
1637 return( POLARSSL_ERR_SSL_INVALID_MAC );
1638 }
1639
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001640#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001641 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001642 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001643 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001644 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001645 {
Paul Bakker48916f92012-09-16 19:57:18 +00001646 dec_msglen -= ssl->transform_in->ivlen;
1647 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001648
Paul Bakker48916f92012-09-16 19:57:18 +00001649 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001650 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001651 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001652#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001653
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001654 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001655 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001656 ssl->transform_in->ivlen,
1657 dec_msg, dec_msglen,
1658 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001659 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001660 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001661 return( ret );
1662 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001663
Paul Bakkercca5b812013-08-31 17:40:26 +02001664 if( dec_msglen != olen )
1665 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001666 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001667 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001668 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001669
1670#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001671 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1672 {
1673 /*
1674 * Save IV in SSL3 and TLS1
1675 */
1676 memcpy( ssl->transform_in->iv_dec,
1677 ssl->transform_in->cipher_ctx_dec.iv,
1678 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001679 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001680#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001681
1682 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001683
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001684 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001685 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001686 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001687#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001688 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1689 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001690#endif
Paul Bakker45829992013-01-03 14:52:21 +01001691 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001692 correct = 0;
1693 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001694
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001695#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001696 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1697 {
Paul Bakker48916f92012-09-16 19:57:18 +00001698 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001699 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001700#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001701 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1702 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001703 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001704#endif
Paul Bakker45829992013-01-03 14:52:21 +01001705 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001706 }
1707 }
1708 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001709#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001710#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1711 defined(POLARSSL_SSL_PROTO_TLS1_2)
1712 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001713 {
1714 /*
Paul Bakker45829992013-01-03 14:52:21 +01001715 * TLSv1+: always check the padding up to the first failure
1716 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001717 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001718 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001719 size_t padding_idx = ssl->in_msglen - padlen - 1;
1720
Paul Bakker956c9e02013-12-19 14:42:28 +01001721 /*
1722 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001723 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001724 *
Paul Bakker61885c72014-04-25 12:59:03 +02001725 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1726 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001727 *
1728 * In both cases we reset padding_idx to a safe value (0) to
1729 * prevent out-of-buffer reads.
1730 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001731 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001732 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1733 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001734
1735 padding_idx *= correct;
1736
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001737 for( i = 1; i <= 256; i++ )
1738 {
1739 real_count &= ( i <= padlen );
1740 pad_count += real_count *
1741 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1742 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001743
1744 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001745
Paul Bakkerd66f0702013-01-31 16:57:45 +01001746#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001747 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001748 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001749#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001750 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001751 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001752 else
1753#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1754 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001755 {
1756 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001757 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001758 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001759
1760 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001761 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001762 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001763#endif /* POLARSSL_CIPHER_MODE_CBC &&
1764 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001765 {
1766 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001767 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001768 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001769
1770 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1771 ssl->in_msg, ssl->in_msglen );
1772
1773 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001774 * Authenticate if not done yet.
1775 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001776 */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001777#if defined(POLARSSL_SOME_MODES_USE_MAC)
1778 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001779 {
Hanno Becker251bab52017-11-20 10:30:08 +00001780 unsigned char mac_expect[SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01001781
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001782 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001783
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001784 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1785 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001786
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001787#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001788 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1789 {
1790 ssl_mac( &ssl->transform_in->md_ctx_dec,
1791 ssl->transform_in->mac_dec,
1792 ssl->in_msg, ssl->in_msglen,
1793 ssl->in_ctr, ssl->in_msgtype );
1794 }
1795 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001796#endif /* POLARSSL_SSL_PROTO_SSL3 */
1797#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001798 defined(POLARSSL_SSL_PROTO_TLS1_2)
1799 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1800 {
1801 /*
1802 * Process MAC and always update for padlen afterwards to make
1803 * total time independent of padlen
1804 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001805 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001806 *
1807 * Known timing attacks:
1808 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1809 *
1810 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1811 * correctly. (We round down instead of up, so -56 is the correct
1812 * value for our calculations instead of -55)
1813 */
1814 size_t j, extra_run = 0;
1815 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1816 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001817
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001818 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001819
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001820 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1821 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1822 ssl->in_msglen );
Hanno Becker251bab52017-11-20 10:30:08 +00001823 md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
1824
Manuel Pégourié-Gonnard7d1e95c2015-04-29 01:35:48 +02001825 /* Call md_process at least once due to cache attacks */
1826 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001827 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001828
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001829 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1830 }
1831 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001832#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001833 POLARSSL_SSL_PROTO_TLS1_2 */
1834 {
1835 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001836 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001837 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001838
Hanno Becker251bab52017-11-20 10:30:08 +00001839 SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
1840 SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001841 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001842
Hanno Becker251bab52017-11-20 10:30:08 +00001843 if( safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001844 ssl->transform_in->maclen ) != 0 )
1845 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001846#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001847 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001848#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001849 correct = 0;
1850 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001851 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001852
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001853 /*
1854 * Finally check the correct flag
1855 */
1856 if( correct == 0 )
1857 return( POLARSSL_ERR_SSL_INVALID_MAC );
1858 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001859#endif /* POLARSSL_SOME_MODES_USE_MAC */
1860
1861 /* Make extra sure authentication was performed, exactly once */
1862 if( auth_done != 1 )
1863 {
1864 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1865 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1866 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001867
1868 if( ssl->in_msglen == 0 )
1869 {
1870 ssl->nb_zero++;
1871
1872 /*
1873 * Three or more empty messages may be a DoS attack
1874 * (excessive CPU consumption).
1875 */
1876 if( ssl->nb_zero > 3 )
1877 {
1878 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1879 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001880 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001881 }
1882 }
1883 else
1884 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001885
Paul Bakker23986e52011-04-24 08:57:21 +00001886 for( i = 8; i > 0; i-- )
1887 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001888 break;
1889
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001890 /* The loops goes to its end iff the counter is wrapping */
1891 if( i == 0 )
1892 {
1893 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1894 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1895 }
1896
Paul Bakker5121ce52009-01-03 21:22:43 +00001897 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1898
1899 return( 0 );
1900}
1901
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001902#undef MAC_NONE
1903#undef MAC_PLAINTEXT
1904#undef MAC_CIPHERTEXT
1905
Paul Bakker2770fbd2012-07-03 13:30:23 +00001906#if defined(POLARSSL_ZLIB_SUPPORT)
1907/*
1908 * Compression/decompression functions
1909 */
1910static int ssl_compress_buf( ssl_context *ssl )
1911{
1912 int ret;
1913 unsigned char *msg_post = ssl->out_msg;
1914 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001915 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001916
1917 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1918
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001919 if( len_pre == 0 )
1920 return( 0 );
1921
Paul Bakker2770fbd2012-07-03 13:30:23 +00001922 memcpy( msg_pre, ssl->out_msg, len_pre );
1923
1924 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1925 ssl->out_msglen ) );
1926
1927 SSL_DEBUG_BUF( 4, "before compression: output payload",
1928 ssl->out_msg, ssl->out_msglen );
1929
Paul Bakker48916f92012-09-16 19:57:18 +00001930 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1931 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1932 ssl->transform_out->ctx_deflate.next_out = msg_post;
1933 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001934
Paul Bakker48916f92012-09-16 19:57:18 +00001935 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001936 if( ret != Z_OK )
1937 {
1938 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1939 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1940 }
1941
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001942 ssl->out_msglen = SSL_BUFFER_LEN -
1943 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001944
Paul Bakker2770fbd2012-07-03 13:30:23 +00001945 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1946 ssl->out_msglen ) );
1947
1948 SSL_DEBUG_BUF( 4, "after compression: output payload",
1949 ssl->out_msg, ssl->out_msglen );
1950
1951 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1952
1953 return( 0 );
1954}
1955
1956static int ssl_decompress_buf( ssl_context *ssl )
1957{
1958 int ret;
1959 unsigned char *msg_post = ssl->in_msg;
1960 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001961 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001962
1963 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1964
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001965 if( len_pre == 0 )
1966 return( 0 );
1967
Paul Bakker2770fbd2012-07-03 13:30:23 +00001968 memcpy( msg_pre, ssl->in_msg, len_pre );
1969
1970 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1971 ssl->in_msglen ) );
1972
1973 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1974 ssl->in_msg, ssl->in_msglen );
1975
Paul Bakker48916f92012-09-16 19:57:18 +00001976 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1977 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1978 ssl->transform_in->ctx_inflate.next_out = msg_post;
1979 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001980
Paul Bakker48916f92012-09-16 19:57:18 +00001981 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001982 if( ret != Z_OK )
1983 {
1984 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1985 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1986 }
1987
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001988 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1989 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001990
Paul Bakker2770fbd2012-07-03 13:30:23 +00001991 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1992 ssl->in_msglen ) );
1993
1994 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1995 ssl->in_msg, ssl->in_msglen );
1996
1997 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1998
1999 return( 0 );
2000}
2001#endif /* POLARSSL_ZLIB_SUPPORT */
2002
Paul Bakker5121ce52009-01-03 21:22:43 +00002003/*
2004 * Fill the input message buffer
2005 */
Paul Bakker23986e52011-04-24 08:57:21 +00002006int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002007{
Paul Bakker23986e52011-04-24 08:57:21 +00002008 int ret;
2009 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002010
2011 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
2012
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002013 if( nb_want > SSL_BUFFER_LEN - 8 )
2014 {
2015 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2016 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2017 }
2018
Paul Bakker5121ce52009-01-03 21:22:43 +00002019 while( ssl->in_left < nb_want )
2020 {
2021 len = nb_want - ssl->in_left;
2022 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
2023
2024 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2025 ssl->in_left, nb_want ) );
2026 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2027
Paul Bakker831a7552011-05-18 13:32:51 +00002028 if( ret == 0 )
2029 return( POLARSSL_ERR_SSL_CONN_EOF );
2030
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 if( ret < 0 )
2032 return( ret );
2033
2034 ssl->in_left += ret;
2035 }
2036
2037 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2038
2039 return( 0 );
2040}
2041
2042/*
2043 * Flush any data not yet written
2044 */
2045int ssl_flush_output( ssl_context *ssl )
2046{
2047 int ret;
2048 unsigned char *buf;
2049
2050 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2051
2052 while( ssl->out_left > 0 )
2053 {
2054 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2055 5 + ssl->out_msglen, ssl->out_left ) );
2056
Paul Bakker5bd42292012-12-19 14:40:42 +01002057 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00002058 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002059
Paul Bakker5121ce52009-01-03 21:22:43 +00002060 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2061
2062 if( ret <= 0 )
2063 return( ret );
2064
2065 ssl->out_left -= ret;
2066 }
2067
2068 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2069
2070 return( 0 );
2071}
2072
2073/*
2074 * Record layer functions
2075 */
2076int ssl_write_record( ssl_context *ssl )
2077{
Paul Bakker05ef8352012-05-08 09:17:57 +00002078 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002079 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002080
2081 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2082
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2084 {
2085 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2086 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2087 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2088
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002089 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2090 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 }
2092
Paul Bakker2770fbd2012-07-03 13:30:23 +00002093#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002094 if( ssl->transform_out != NULL &&
2095 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002096 {
2097 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2098 {
2099 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2100 return( ret );
2101 }
2102
2103 len = ssl->out_msglen;
2104 }
2105#endif /*POLARSSL_ZLIB_SUPPORT */
2106
Paul Bakker05ef8352012-05-08 09:17:57 +00002107#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002108 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002109 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002110 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002111
Paul Bakker05ef8352012-05-08 09:17:57 +00002112 ret = ssl_hw_record_write( ssl );
2113 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2114 {
2115 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002116 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002117 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002118
2119 if( ret == 0 )
2120 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002121 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002122#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002123 if( !done )
2124 {
2125 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2126 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2127 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2129 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002130
Paul Bakker48916f92012-09-16 19:57:18 +00002131 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002132 {
2133 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2134 {
2135 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2136 return( ret );
2137 }
2138
2139 len = ssl->out_msglen;
2140 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2141 ssl->out_hdr[4] = (unsigned char)( len );
2142 }
2143
2144 ssl->out_left = 5 + ssl->out_msglen;
2145
2146 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2147 "version = [%d:%d], msglen = %d",
2148 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2149 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2150
2151 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002152 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002153 }
2154
Paul Bakker5121ce52009-01-03 21:22:43 +00002155 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2156 {
2157 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2158 return( ret );
2159 }
2160
2161 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2162
2163 return( 0 );
2164}
2165
2166int ssl_read_record( ssl_context *ssl )
2167{
Paul Bakker05ef8352012-05-08 09:17:57 +00002168 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002169
2170 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2171
Hanno Becker10699cc2017-06-08 15:41:02 +01002172 if( ssl->keep_current_message == 1 )
2173 {
2174 SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
2175 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2176 ssl->keep_current_message = 0;
2177
2178 return( 0 );
2179 }
2180
Hanno Beckerd37839e2017-06-08 15:56:50 +01002181 if( ssl->in_hslen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002182 {
Hanno Becker1bf86b72017-06-08 15:58:02 +01002183 if( ssl->in_offt != NULL )
2184 {
2185 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2186 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2187 }
2188
Paul Bakker5121ce52009-01-03 21:22:43 +00002189 /*
2190 * Get next Handshake message in the current record
2191 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002192
Hanno Beckerd37839e2017-06-08 15:56:50 +01002193 if( ssl->in_hslen < ssl->in_msglen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002194 {
Hanno Beckerd37839e2017-06-08 15:56:50 +01002195 ssl->in_msglen -= ssl->in_hslen;
2196 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
2197 ssl->in_msglen );
2198
2199 ssl->in_hslen = 4;
2200 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2201
2202 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2203 " %d, type = %d, hslen = %d",
2204 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2205
2206 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2207 {
2208 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
2209 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2210 }
2211
2212 if( ssl->in_msglen < ssl->in_hslen )
2213 {
2214 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
2215 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2216 }
2217
2218 if( ssl->state != SSL_HANDSHAKE_OVER )
2219 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
2220
2221 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002222 }
2223
Hanno Beckerd37839e2017-06-08 15:56:50 +01002224 ssl->in_msglen = 0;
2225 ssl->in_hslen = 0;
2226 }
2227 else if( ssl->in_offt != NULL )
2228 {
2229 return( 0 );
2230 }
2231 else
2232 {
2233 ssl->in_msglen = 0;
2234 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002235
Hanno Beckerd37839e2017-06-08 15:56:50 +01002236 /*
2237 * Fetch and decode new record if current one is fully consumed.
2238 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002239
Hanno Beckerd37839e2017-06-08 15:56:50 +01002240 if( ssl->in_msglen > 0 )
2241 {
2242 /* There's something left to be processed in the current record. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002243 return( 0 );
2244 }
2245
Hanno Beckerd37839e2017-06-08 15:56:50 +01002246 /* Need to fetch a new record */
Paul Bakker5121ce52009-01-03 21:22:43 +00002247
Manuel Pégourié-Gonnard0aaefce2015-10-27 13:42:11 +01002248read_record_header:
Paul Bakker5121ce52009-01-03 21:22:43 +00002249 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2250 {
2251 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2252 return( ret );
2253 }
2254
2255 ssl->in_msgtype = ssl->in_hdr[0];
2256 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2257
2258 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2259 "version = [%d:%d], msglen = %d",
2260 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2261 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2262
2263 if( ssl->in_hdr[1] != ssl->major_ver )
2264 {
2265 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002266 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002267 }
2268
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002269 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002270 {
2271 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002272 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002273 }
2274
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002275 /* Sanity check (outer boundaries) */
2276 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2277 {
2278 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2279 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2280 }
2281
Paul Bakker5121ce52009-01-03 21:22:43 +00002282 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002283 * Make sure the message length is acceptable for the current transform
2284 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002285 */
Paul Bakker48916f92012-09-16 19:57:18 +00002286 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002288 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002289 {
2290 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002291 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002292 }
2293 }
2294 else
2295 {
Paul Bakker48916f92012-09-16 19:57:18 +00002296 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002297 {
2298 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002299 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002300 }
2301
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002302#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002303 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002304 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002305 {
2306 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002307 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002308 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002309#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002310
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002311#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2312 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002313 /*
2314 * TLS encrypted messages can have up to 256 bytes of padding
2315 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002316 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002317 ssl->in_msglen > ssl->transform_in->minlen +
2318 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002319 {
2320 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002321 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002322 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002323#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002324 }
2325
2326 /*
2327 * Read and optionally decrypt the message contents
2328 */
2329 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2330 {
2331 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2332 return( ret );
2333 }
2334
2335 SSL_DEBUG_BUF( 4, "input record from network",
2336 ssl->in_hdr, 5 + ssl->in_msglen );
2337
Paul Bakker05ef8352012-05-08 09:17:57 +00002338#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002339 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002340 {
2341 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2342
2343 ret = ssl_hw_record_read( ssl );
2344 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2345 {
2346 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002347 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002348 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002349
2350 if( ret == 0 )
2351 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002352 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002353#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002354 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002355 {
2356 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2357 {
Paul Bakker40865c82013-01-31 17:13:13 +01002358#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2359 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2360 {
2361 ssl_send_alert_message( ssl,
2362 SSL_ALERT_LEVEL_FATAL,
2363 SSL_ALERT_MSG_BAD_RECORD_MAC );
2364 }
2365#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002366 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2367 return( ret );
2368 }
2369
2370 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2371 ssl->in_msg, ssl->in_msglen );
2372
2373 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2374 {
2375 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002376 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002377 }
2378 }
2379
Paul Bakker2770fbd2012-07-03 13:30:23 +00002380#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002381 if( ssl->transform_in != NULL &&
2382 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002383 {
2384 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2385 {
2386 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2387 return( ret );
2388 }
2389
2390 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2391 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2392 }
2393#endif /* POLARSSL_ZLIB_SUPPORT */
2394
Paul Bakker0a925182012-04-16 06:46:41 +00002395 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2396 ssl->in_msgtype != SSL_MSG_ALERT &&
2397 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2398 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2399 {
2400 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2401
Paul Bakker48916f92012-09-16 19:57:18 +00002402 if( ( ret = ssl_send_alert_message( ssl,
2403 SSL_ALERT_LEVEL_FATAL,
2404 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002405 {
2406 return( ret );
2407 }
2408
2409 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2410 }
2411
Paul Bakker5121ce52009-01-03 21:22:43 +00002412 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2413 {
2414 ssl->in_hslen = 4;
2415 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2416
2417 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2418 " %d, type = %d, hslen = %d",
2419 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2420
2421 /*
2422 * Additional checks to validate the handshake header
2423 */
2424 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2425 {
2426 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002427 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002428 }
2429
2430 if( ssl->in_msglen < ssl->in_hslen )
2431 {
2432 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002433 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002434 }
2435
Paul Bakker48916f92012-09-16 19:57:18 +00002436 if( ssl->state != SSL_HANDSHAKE_OVER )
2437 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002438 }
2439
2440 if( ssl->in_msgtype == SSL_MSG_ALERT )
2441 {
2442 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2443 ssl->in_msg[0], ssl->in_msg[1] ) );
2444
2445 /*
Manuel Pégourié-Gonnard0aaefce2015-10-27 13:42:11 +01002446 * Ignore non-fatal alerts, except close_notify and no_renego
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002448 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002449 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002450 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2451 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002452 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 }
2454
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002455 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2456 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002457 {
2458 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002459 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002460 }
Manuel Pégourié-Gonnard0aaefce2015-10-27 13:42:11 +01002461
2462 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2463 ssl->in_msg[1] == SSL_ALERT_MSG_NO_RENEGOTIATION )
2464 {
2465 SSL_DEBUG_MSG( 2, ( "is a no_renegotiation" ) );
2466 /* Will be handled when trying to parse ServerHello */
2467 ssl->in_left = 0;
2468 return( 0 );
2469 }
2470
2471 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
2472 ssl->endpoint == SSL_IS_SERVER &&
2473 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2474 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
2475 {
2476 SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
2477 /* Will be handled in ssl_parse_certificate() */
2478 ssl->in_left = 0;
2479 return( 0 );
2480 }
2481
2482 /* Silently discard: fetch new message */
2483 goto read_record_header;
Paul Bakker5121ce52009-01-03 21:22:43 +00002484 }
2485
2486 ssl->in_left = 0;
2487
2488 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2489
2490 return( 0 );
2491}
2492
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002493int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2494{
2495 int ret;
2496
2497 if( ( ret = ssl_send_alert_message( ssl,
2498 SSL_ALERT_LEVEL_FATAL,
2499 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2500 {
2501 return( ret );
2502 }
2503
2504 return( 0 );
2505}
2506
Paul Bakker0a925182012-04-16 06:46:41 +00002507int ssl_send_alert_message( ssl_context *ssl,
2508 unsigned char level,
2509 unsigned char message )
2510{
2511 int ret;
2512
2513 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2514
2515 ssl->out_msgtype = SSL_MSG_ALERT;
2516 ssl->out_msglen = 2;
2517 ssl->out_msg[0] = level;
2518 ssl->out_msg[1] = message;
2519
2520 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2521 {
2522 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2523 return( ret );
2524 }
2525
2526 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2527
2528 return( 0 );
2529}
2530
Paul Bakker5121ce52009-01-03 21:22:43 +00002531/*
2532 * Handshake functions
2533 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002534#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2535 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2536 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2537 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2538 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2539 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2540 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002541int ssl_write_certificate( ssl_context *ssl )
2542{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002543 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002544
2545 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2546
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002547 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002548 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2549 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002550 {
2551 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2552 ssl->state++;
2553 return( 0 );
2554 }
2555
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002556 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2557 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002558}
2559
2560int ssl_parse_certificate( ssl_context *ssl )
2561{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002562 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2563
2564 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2565
2566 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002567 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2568 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002569 {
2570 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2571 ssl->state++;
2572 return( 0 );
2573 }
2574
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002575 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2576 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002577}
2578#else
2579int ssl_write_certificate( ssl_context *ssl )
2580{
2581 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2582 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002583 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002584 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2585
2586 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2587
2588 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002589 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2590 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002591 {
2592 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2593 ssl->state++;
2594 return( 0 );
2595 }
2596
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002597#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 if( ssl->endpoint == SSL_IS_CLIENT )
2599 {
2600 if( ssl->client_auth == 0 )
2601 {
2602 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2603 ssl->state++;
2604 return( 0 );
2605 }
2606
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002607#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002608 /*
2609 * If using SSLv3 and got no cert, send an Alert message
2610 * (otherwise an empty Certificate message will be sent).
2611 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002612 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002613 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2614 {
2615 ssl->out_msglen = 2;
2616 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002617 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2618 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002619
2620 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2621 goto write_msg;
2622 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002623#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002624 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002625#endif /* POLARSSL_SSL_CLI_C */
2626#if defined(POLARSSL_SSL_SRV_C)
2627 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00002628 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002629 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002630 {
2631 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002632 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002633 }
2634 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002635#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002636
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002637 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002638
2639 /*
2640 * 0 . 0 handshake type
2641 * 1 . 3 handshake length
2642 * 4 . 6 length of all certs
2643 * 7 . 9 length of cert. 1
2644 * 10 . n-1 peer certificate
2645 * n . n+2 length of cert. 2
2646 * n+3 . ... upper level cert, etc.
2647 */
2648 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002649 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002650
Paul Bakker29087132010-03-21 21:03:34 +00002651 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002652 {
2653 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002654 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 {
2656 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2657 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002658 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002659 }
2660
2661 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2662 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2663 ssl->out_msg[i + 2] = (unsigned char)( n );
2664
2665 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2666 i += n; crt = crt->next;
2667 }
2668
2669 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2670 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2671 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2672
2673 ssl->out_msglen = i;
2674 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2675 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2676
Simon Butcher149950d2016-10-15 22:08:08 +01002677#if defined(POLARSSL_SSL_PROTO_SSL3) && defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002678write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002679#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002680
2681 ssl->state++;
2682
2683 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2684 {
2685 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2686 return( ret );
2687 }
2688
2689 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2690
Paul Bakkered27a042013-04-18 22:46:23 +02002691 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002692}
2693
2694int ssl_parse_certificate( ssl_context *ssl )
2695{
Paul Bakkered27a042013-04-18 22:46:23 +02002696 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002697 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002698 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
2700 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2701
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002702 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002703 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2704 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002705 {
2706 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2707 ssl->state++;
2708 return( 0 );
2709 }
2710
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002711#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002713 ( ssl->authmode == SSL_VERIFY_NONE ||
2714 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002715 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002716 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002717 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2718 ssl->state++;
2719 return( 0 );
2720 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002721#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002722
2723 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2724 {
2725 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2726 return( ret );
2727 }
2728
2729 ssl->state++;
2730
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002731#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002732#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 /*
2734 * Check if the client sent an empty certificate
2735 */
2736 if( ssl->endpoint == SSL_IS_SERVER &&
2737 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2738 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002739 if( ssl->in_msglen == 2 &&
2740 ssl->in_msgtype == SSL_MSG_ALERT &&
2741 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2742 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002743 {
2744 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2745
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002746 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002747 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2748 return( 0 );
2749 else
Paul Bakker40e46942009-01-03 21:51:57 +00002750 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002751 }
2752 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002753#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002754
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002755#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2756 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002757 if( ssl->endpoint == SSL_IS_SERVER &&
2758 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2759 {
2760 if( ssl->in_hslen == 7 &&
2761 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2762 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2763 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2764 {
2765 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2766
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002767 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002768 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002769 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002770 else
2771 return( 0 );
2772 }
2773 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002774#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2775 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002776#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002777
2778 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2779 {
2780 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002781 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002782 }
2783
2784 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2785 {
2786 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002787 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002788 }
2789
2790 /*
2791 * Same message structure as in ssl_write_certificate()
2792 */
2793 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2794
2795 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2796 {
2797 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002798 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002799 }
2800
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002801 /* In case we tried to reuse a session but it failed */
2802 if( ssl->session_negotiate->peer_cert != NULL )
2803 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002804 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002805 polarssl_free( ssl->session_negotiate->peer_cert );
2806 }
2807
Mansour Moufidc531b4a2015-02-15 17:35:38 -05002808 if( ( ssl->session_negotiate->peer_cert = polarssl_malloc(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002809 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002810 {
2811 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002812 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002813 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002814 }
2815
Paul Bakkerb6b09562013-09-18 14:17:41 +02002816 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002817
2818 i = 7;
2819
2820 while( i < ssl->in_hslen )
2821 {
2822 if( ssl->in_msg[i] != 0 )
2823 {
2824 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002825 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002826 }
2827
2828 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2829 | (unsigned int) ssl->in_msg[i + 2];
2830 i += 3;
2831
2832 if( n < 128 || i + n > ssl->in_hslen )
2833 {
2834 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002835 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002836 }
2837
Paul Bakkerddf26b42013-09-18 13:46:23 +02002838 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2839 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002840 if( ret != 0 )
2841 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002842 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002843 return( ret );
2844 }
2845
2846 i += n;
2847 }
2848
Paul Bakker48916f92012-09-16 19:57:18 +00002849 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002850
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002851 /*
2852 * On client, make sure the server cert doesn't change during renego to
2853 * avoid "triple handshake" attack: https://secure-resumption.com/
2854 */
Paul Bakkerf6080b82015-01-13 16:18:23 +01002855#if defined(POLARSSL_SSL_RENEGOTIATION) && defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002856 if( ssl->endpoint == SSL_IS_CLIENT &&
2857 ssl->renegotiation == SSL_RENEGOTIATION )
2858 {
2859 if( ssl->session->peer_cert == NULL )
2860 {
2861 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2862 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2863 }
2864
2865 if( ssl->session->peer_cert->raw.len !=
2866 ssl->session_negotiate->peer_cert->raw.len ||
2867 memcmp( ssl->session->peer_cert->raw.p,
2868 ssl->session_negotiate->peer_cert->raw.p,
2869 ssl->session->peer_cert->raw.len ) != 0 )
2870 {
2871 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2872 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2873 }
2874 }
Paul Bakkerf6080b82015-01-13 16:18:23 +01002875#endif /* POLARSSL_SSL_RENEGOTIATION && POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002876
Paul Bakker5121ce52009-01-03 21:22:43 +00002877 if( ssl->authmode != SSL_VERIFY_NONE )
2878 {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002879 /*
2880 * Main check: verify certificate
2881 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002882 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2883 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2884 &ssl->session_negotiate->verify_result,
2885 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002886
2887 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002888 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002889 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002890 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002891
2892 /*
2893 * Secondary checks: always done, but change 'ret' only if it was 0
2894 */
2895
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002896#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002897 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002898 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002899
2900 /* If certificate uses an EC key, make sure the curve is OK */
2901 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2902 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2903 {
Hanno Becker888c2fd2017-05-11 11:12:40 +01002904 ssl->session_negotiate->verify_result |= BADCERT_BAD_KEY;
2905
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002906 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002907 if( ret == 0 )
2908 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002909 }
2910 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002911#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002912
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002913 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2914 ciphersuite_info,
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02002915 ! ssl->endpoint,
2916 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002917 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002918 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002919 if( ret == 0 )
2920 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2921 }
2922
Hanno Becker888c2fd2017-05-11 11:12:40 +01002923 /* x509_crt_verify_with_profile is supposed to report a
2924 * verification failure through POLARSSL_ERR_X509_CERT_VERIFY_FAILED,
2925 * with details encoded in the verification flags. All other kinds
2926 * of error codes, including those from the user provided f_vrfy
2927 * functions, are treated as fatal and lead to a failure of
2928 * ssl_parse_certificate even if verification was optional. */
2929 if( ssl->authmode == SSL_VERIFY_OPTIONAL &&
2930 ( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED ||
2931 ret == POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE ) )
2932 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002933 ret = 0;
Hanno Becker888c2fd2017-05-11 11:12:40 +01002934 }
2935
2936 if( ssl->ca_chain == NULL && ssl->authmode == SSL_VERIFY_REQUIRED )
2937 {
2938 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
2939 ret = POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED;
2940 }
2941
2942#if defined(POLARSSL_DEBUG_C)
2943 if( ssl->session_negotiate->verify_result != 0 )
2944 {
2945 SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
2946 ssl->session_negotiate->verify_result ) );
2947 }
2948 else
2949 {
2950 SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
2951 }
2952#endif /* POLARSSL_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002953 }
2954
2955 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2956
2957 return( ret );
2958}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002959#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2960 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2961 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2962 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2963 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2964 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2965 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002966
2967int ssl_write_change_cipher_spec( ssl_context *ssl )
2968{
2969 int ret;
2970
2971 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2972
2973 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2974 ssl->out_msglen = 1;
2975 ssl->out_msg[0] = 1;
2976
Paul Bakker5121ce52009-01-03 21:22:43 +00002977 ssl->state++;
2978
2979 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2980 {
2981 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2982 return( ret );
2983 }
2984
2985 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2986
2987 return( 0 );
2988}
2989
2990int ssl_parse_change_cipher_spec( ssl_context *ssl )
2991{
2992 int ret;
2993
2994 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2995
Paul Bakker5121ce52009-01-03 21:22:43 +00002996 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2997 {
2998 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2999 return( ret );
3000 }
3001
3002 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
3003 {
3004 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003005 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003006 }
3007
3008 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
3009 {
3010 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003011 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00003012 }
3013
3014 ssl->state++;
3015
3016 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
3017
3018 return( 0 );
3019}
3020
Paul Bakker41c83d32013-03-20 14:39:14 +01003021void ssl_optimize_checksum( ssl_context *ssl,
3022 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00003023{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003024 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003025
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003026#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3027 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00003028 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00003029 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00003030 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003031#endif
3032#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3033#if defined(POLARSSL_SHA512_C)
3034 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
3035 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
3036 else
3037#endif
3038#if defined(POLARSSL_SHA256_C)
3039 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00003040 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003041 else
3042#endif
3043#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003044 {
3045 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003046 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003047 }
Paul Bakker380da532012-04-18 16:10:25 +00003048}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003049
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003050static void ssl_update_checksum_start( ssl_context *ssl,
3051 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003052{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003053#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3054 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003055 md5_update( &ssl->handshake->fin_md5 , buf, len );
3056 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003057#endif
3058#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3059#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003060 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003061#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003062#if defined(POLARSSL_SHA512_C)
3063 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003064#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003065#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003066}
3067
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003068#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3069 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003070static void ssl_update_checksum_md5sha1( ssl_context *ssl,
3071 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003072{
Paul Bakker48916f92012-09-16 19:57:18 +00003073 md5_update( &ssl->handshake->fin_md5 , buf, len );
3074 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003075}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003076#endif
Paul Bakker380da532012-04-18 16:10:25 +00003077
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003078#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3079#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003080static void ssl_update_checksum_sha256( ssl_context *ssl,
3081 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003082{
Paul Bakker9e36f042013-06-30 14:34:05 +02003083 sha256_update( &ssl->handshake->fin_sha256, 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 Bakker9e36f042013-06-30 14:34:05 +02003087#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003088static void ssl_update_checksum_sha384( ssl_context *ssl,
3089 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003090{
Paul Bakker9e36f042013-06-30 14:34:05 +02003091 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003092}
Paul Bakker769075d2012-11-24 11:26:46 +01003093#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003094#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003095
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003096#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003097static void ssl_calc_finished_ssl(
3098 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003099{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003100 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003101 md5_context md5;
3102 sha1_context sha1;
3103
Paul Bakker5121ce52009-01-03 21:22:43 +00003104 unsigned char padbuf[48];
3105 unsigned char md5sum[16];
3106 unsigned char sha1sum[20];
3107
Paul Bakker48916f92012-09-16 19:57:18 +00003108 ssl_session *session = ssl->session_negotiate;
3109 if( !session )
3110 session = ssl->session;
3111
Paul Bakker1ef83d62012-04-11 12:09:53 +00003112 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3113
Paul Bakker48916f92012-09-16 19:57:18 +00003114 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3115 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003116
3117 /*
3118 * SSLv3:
3119 * hash =
3120 * MD5( master + pad2 +
3121 * MD5( handshake + sender + master + pad1 ) )
3122 * + SHA1( master + pad2 +
3123 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003124 */
3125
Paul Bakker90995b52013-06-24 19:20:35 +02003126#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003127 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003128 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003129#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003130
Paul Bakker90995b52013-06-24 19:20:35 +02003131#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003132 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003133 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003134#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003135
Paul Bakker3c2122f2013-06-24 19:03:14 +02003136 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3137 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003138
Paul Bakker1ef83d62012-04-11 12:09:53 +00003139 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003140
Paul Bakker3c2122f2013-06-24 19:03:14 +02003141 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003142 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003143 md5_update( &md5, padbuf, 48 );
3144 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003145
Paul Bakker3c2122f2013-06-24 19:03:14 +02003146 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003147 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003148 sha1_update( &sha1, padbuf, 40 );
3149 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003150
Paul Bakker1ef83d62012-04-11 12:09:53 +00003151 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003152
Paul Bakker1ef83d62012-04-11 12:09:53 +00003153 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003154 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003155 md5_update( &md5, padbuf, 48 );
3156 md5_update( &md5, md5sum, 16 );
3157 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003158
Paul Bakker1ef83d62012-04-11 12:09:53 +00003159 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003160 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003161 sha1_update( &sha1, padbuf , 40 );
3162 sha1_update( &sha1, sha1sum, 20 );
3163 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003164
Paul Bakker1ef83d62012-04-11 12:09:53 +00003165 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003166
Paul Bakker5b4af392014-06-26 12:09:34 +02003167 md5_free( &md5 );
3168 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003169
Paul Bakker34617722014-06-13 17:20:13 +02003170 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3171 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3172 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003173
3174 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3175}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003176#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003177
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003178#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003179static void ssl_calc_finished_tls(
3180 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003181{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003182 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003183 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003184 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003185 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003186 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003187
Paul Bakker48916f92012-09-16 19:57:18 +00003188 ssl_session *session = ssl->session_negotiate;
3189 if( !session )
3190 session = ssl->session;
3191
Paul Bakker1ef83d62012-04-11 12:09:53 +00003192 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003193
Paul Bakker48916f92012-09-16 19:57:18 +00003194 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3195 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003196
Paul Bakker1ef83d62012-04-11 12:09:53 +00003197 /*
3198 * TLSv1:
3199 * hash = PRF( master, finished_label,
3200 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3201 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003202
Paul Bakker90995b52013-06-24 19:20:35 +02003203#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003204 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3205 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003206#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003207
Paul Bakker90995b52013-06-24 19:20:35 +02003208#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003209 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3210 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003211#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003212
3213 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003214 ? "client finished"
3215 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003216
3217 md5_finish( &md5, padbuf );
3218 sha1_finish( &sha1, padbuf + 16 );
3219
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003220 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003221 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003222
3223 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3224
Paul Bakker5b4af392014-06-26 12:09:34 +02003225 md5_free( &md5 );
3226 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003227
Paul Bakker34617722014-06-13 17:20:13 +02003228 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003229
3230 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3231}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003232#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003233
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003234#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3235#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003236static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003237 ssl_context *ssl, unsigned char *buf, int from )
3238{
3239 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003240 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003241 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003242 unsigned char padbuf[32];
3243
Paul Bakker48916f92012-09-16 19:57:18 +00003244 ssl_session *session = ssl->session_negotiate;
3245 if( !session )
3246 session = ssl->session;
3247
Paul Bakker380da532012-04-18 16:10:25 +00003248 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003249
Paul Bakker9e36f042013-06-30 14:34:05 +02003250 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003251
3252 /*
3253 * TLSv1.2:
3254 * hash = PRF( master, finished_label,
3255 * Hash( handshake ) )[0.11]
3256 */
3257
Paul Bakker9e36f042013-06-30 14:34:05 +02003258#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003259 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003260 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003261#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003262
3263 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003264 ? "client finished"
3265 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003266
Paul Bakker9e36f042013-06-30 14:34:05 +02003267 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003268
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003269 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003270 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003271
3272 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3273
Paul Bakker5b4af392014-06-26 12:09:34 +02003274 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003275
Paul Bakker34617722014-06-13 17:20:13 +02003276 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003277
3278 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3279}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003280#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003281
Paul Bakker9e36f042013-06-30 14:34:05 +02003282#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003283static void ssl_calc_finished_tls_sha384(
3284 ssl_context *ssl, unsigned char *buf, int from )
3285{
3286 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003287 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003288 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003289 unsigned char padbuf[48];
3290
Paul Bakker48916f92012-09-16 19:57:18 +00003291 ssl_session *session = ssl->session_negotiate;
3292 if( !session )
3293 session = ssl->session;
3294
Paul Bakker380da532012-04-18 16:10:25 +00003295 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003296
Paul Bakker9e36f042013-06-30 14:34:05 +02003297 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003298
3299 /*
3300 * TLSv1.2:
3301 * hash = PRF( master, finished_label,
3302 * Hash( handshake ) )[0.11]
3303 */
3304
Paul Bakker9e36f042013-06-30 14:34:05 +02003305#if !defined(POLARSSL_SHA512_ALT)
3306 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3307 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003308#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003309
3310 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003311 ? "client finished"
3312 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003313
Paul Bakker9e36f042013-06-30 14:34:05 +02003314 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003315
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003316 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003317 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003318
3319 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3320
Paul Bakker5b4af392014-06-26 12:09:34 +02003321 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003322
Paul Bakker34617722014-06-13 17:20:13 +02003323 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003324
3325 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3326}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003327#endif /* POLARSSL_SHA512_C */
3328#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003329
Paul Bakker48916f92012-09-16 19:57:18 +00003330void ssl_handshake_wrapup( ssl_context *ssl )
3331{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003332 int resume = ssl->handshake->resume;
3333
Paul Bakker48916f92012-09-16 19:57:18 +00003334 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3335
3336 /*
3337 * Free our handshake params
3338 */
3339 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003340 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003341 ssl->handshake = NULL;
3342
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003343#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003344 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003345 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003346 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003347 ssl->renego_records_seen = 0;
3348 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003349#endif
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003350
Paul Bakker48916f92012-09-16 19:57:18 +00003351 /*
3352 * Switch in our now active transform context
3353 */
3354 if( ssl->transform )
3355 {
3356 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003357 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003358 }
3359 ssl->transform = ssl->transform_negotiate;
3360 ssl->transform_negotiate = NULL;
3361
Paul Bakker0a597072012-09-25 21:55:46 +00003362 if( ssl->session )
3363 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003364#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3365 /* RFC 7366 3.1: keep the EtM state */
3366 ssl->session_negotiate->encrypt_then_mac =
3367 ssl->session->encrypt_then_mac;
3368#endif
3369
Paul Bakker0a597072012-09-25 21:55:46 +00003370 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003371 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003372 }
3373 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003374 ssl->session_negotiate = NULL;
3375
Paul Bakker0a597072012-09-25 21:55:46 +00003376 /*
3377 * Add cache entry
3378 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003379 if( ssl->f_set_cache != NULL &&
3380 ssl->session->length != 0 &&
3381 resume == 0 )
3382 {
Paul Bakker0a597072012-09-25 21:55:46 +00003383 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3384 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003385 }
Paul Bakker0a597072012-09-25 21:55:46 +00003386
Paul Bakker48916f92012-09-16 19:57:18 +00003387 ssl->state++;
3388
3389 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3390}
3391
Paul Bakker1ef83d62012-04-11 12:09:53 +00003392int ssl_write_finished( ssl_context *ssl )
3393{
3394 int ret, hash_len;
3395
3396 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3397
Paul Bakker92be97b2013-01-02 17:30:03 +01003398 /*
3399 * Set the out_msg pointer to the correct location based on IV length
3400 */
3401 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3402 {
3403 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3404 ssl->transform_negotiate->fixed_ivlen;
3405 }
3406 else
3407 ssl->out_msg = ssl->out_iv;
3408
Paul Bakker48916f92012-09-16 19:57:18 +00003409 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003410
3411 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003412 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3413
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003414#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003415 ssl->verify_data_len = hash_len;
3416 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003417#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003418
Paul Bakker5121ce52009-01-03 21:22:43 +00003419 ssl->out_msglen = 4 + hash_len;
3420 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3421 ssl->out_msg[0] = SSL_HS_FINISHED;
3422
3423 /*
3424 * In case of session resuming, invert the client and server
3425 * ChangeCipherSpec messages order.
3426 */
Paul Bakker0a597072012-09-25 21:55:46 +00003427 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003428 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003429#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003430 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003431 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003432#endif
3433#if defined(POLARSSL_SSL_SRV_C)
3434 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003435 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003436#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003437 }
3438 else
3439 ssl->state++;
3440
Paul Bakker48916f92012-09-16 19:57:18 +00003441 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003442 * Switch to our negotiated transform and session parameters for outbound
3443 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003444 */
3445 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3446 ssl->transform_out = ssl->transform_negotiate;
3447 ssl->session_out = ssl->session_negotiate;
3448 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003449
Paul Bakker07eb38b2012-12-19 14:42:06 +01003450#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003451 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003452 {
3453 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3454 {
3455 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3456 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3457 }
3458 }
3459#endif
3460
Paul Bakker5121ce52009-01-03 21:22:43 +00003461 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3462 {
3463 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3464 return( ret );
3465 }
3466
3467 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3468
3469 return( 0 );
3470}
3471
3472int ssl_parse_finished( ssl_context *ssl )
3473{
Paul Bakker23986e52011-04-24 08:57:21 +00003474 int ret;
3475 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003476 unsigned char buf[36];
3477
3478 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3479
Paul Bakker48916f92012-09-16 19:57:18 +00003480 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003481
Paul Bakker48916f92012-09-16 19:57:18 +00003482 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003483 * Switch to our negotiated transform and session parameters for inbound
3484 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003485 */
3486 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3487 ssl->transform_in = ssl->transform_negotiate;
3488 ssl->session_in = ssl->session_negotiate;
3489 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003490
Paul Bakker92be97b2013-01-02 17:30:03 +01003491 /*
3492 * Set the in_msg pointer to the correct location based on IV length
3493 */
3494 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3495 {
3496 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3497 ssl->transform_negotiate->fixed_ivlen;
3498 }
3499 else
3500 ssl->in_msg = ssl->in_iv;
3501
Paul Bakker07eb38b2012-12-19 14:42:06 +01003502#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003503 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003504 {
3505 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3506 {
3507 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3508 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3509 }
3510 }
3511#endif
3512
Paul Bakker5121ce52009-01-03 21:22:43 +00003513 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3514 {
3515 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3516 return( ret );
3517 }
3518
3519 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3520 {
3521 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003522 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003523 }
3524
Paul Bakker1ef83d62012-04-11 12:09:53 +00003525 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003526 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3527
3528 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3529 ssl->in_hslen != 4 + hash_len )
3530 {
3531 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003532 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003533 }
3534
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003535 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003536 {
3537 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003538 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003539 }
3540
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003541#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003542 ssl->verify_data_len = hash_len;
3543 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003544#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003545
Paul Bakker0a597072012-09-25 21:55:46 +00003546 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003547 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003548#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003549 if( ssl->endpoint == SSL_IS_CLIENT )
3550 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003551#endif
3552#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003553 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003554 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003555#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003556 }
3557 else
3558 ssl->state++;
3559
3560 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3561
3562 return( 0 );
3563}
3564
Paul Bakker968afaa2014-07-09 11:09:24 +02003565static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003566{
3567 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3568
3569#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3570 defined(POLARSSL_SSL_PROTO_TLS1_1)
3571 md5_init( &handshake->fin_md5 );
3572 sha1_init( &handshake->fin_sha1 );
3573 md5_starts( &handshake->fin_md5 );
3574 sha1_starts( &handshake->fin_sha1 );
3575#endif
3576#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3577#if defined(POLARSSL_SHA256_C)
3578 sha256_init( &handshake->fin_sha256 );
3579 sha256_starts( &handshake->fin_sha256, 0 );
3580#endif
3581#if defined(POLARSSL_SHA512_C)
3582 sha512_init( &handshake->fin_sha512 );
3583 sha512_starts( &handshake->fin_sha512, 1 );
3584#endif
3585#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3586
3587 handshake->update_checksum = ssl_update_checksum_start;
Hanno Beckerc2b9d982017-05-10 16:37:21 +01003588
3589#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
3590 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
3591 ssl_sig_hash_set_init( &handshake->hash_algs );
3592#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003593
3594#if defined(POLARSSL_DHM_C)
3595 dhm_init( &handshake->dhm_ctx );
3596#endif
3597#if defined(POLARSSL_ECDH_C)
3598 ecdh_init( &handshake->ecdh_ctx );
3599#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003600}
3601
3602static void ssl_transform_init( ssl_transform *transform )
3603{
3604 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003605
3606 cipher_init( &transform->cipher_ctx_enc );
3607 cipher_init( &transform->cipher_ctx_dec );
3608
3609 md_init( &transform->md_ctx_enc );
3610 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003611}
3612
3613void ssl_session_init( ssl_session *session )
3614{
3615 memset( session, 0, sizeof(ssl_session) );
3616}
3617
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003618static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003619{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003620 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003621 if( ssl->transform_negotiate )
3622 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003623 if( ssl->session_negotiate )
3624 ssl_session_free( ssl->session_negotiate );
3625 if( ssl->handshake )
3626 ssl_handshake_free( ssl->handshake );
3627
3628 /*
3629 * Either the pointers are now NULL or cleared properly and can be freed.
3630 * Now allocate missing structures.
3631 */
3632 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003633 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003634 ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003635 }
Paul Bakker48916f92012-09-16 19:57:18 +00003636
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003637 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003638 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003639 ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003640 }
Paul Bakker48916f92012-09-16 19:57:18 +00003641
Paul Bakker82788fb2014-10-20 13:59:19 +02003642 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003643 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003644 ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003645 }
Paul Bakker48916f92012-09-16 19:57:18 +00003646
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003647 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003648 if( ssl->handshake == NULL ||
3649 ssl->transform_negotiate == NULL ||
3650 ssl->session_negotiate == NULL )
3651 {
3652 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003653
3654 polarssl_free( ssl->handshake );
3655 polarssl_free( ssl->transform_negotiate );
3656 polarssl_free( ssl->session_negotiate );
3657
3658 ssl->handshake = NULL;
3659 ssl->transform_negotiate = NULL;
3660 ssl->session_negotiate = NULL;
3661
Paul Bakker48916f92012-09-16 19:57:18 +00003662 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3663 }
3664
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003665 /* Initialize structures */
3666 ssl_session_init( ssl->session_negotiate );
3667 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003668 ssl_handshake_params_init( ssl->handshake );
3669
3670#if defined(POLARSSL_X509_CRT_PARSE_C)
3671 ssl->handshake->key_cert = ssl->key_cert;
3672#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003673
Paul Bakker48916f92012-09-16 19:57:18 +00003674 return( 0 );
3675}
3676
Paul Bakker5121ce52009-01-03 21:22:43 +00003677/*
3678 * Initialize an SSL context
3679 */
3680int ssl_init( ssl_context *ssl )
3681{
Paul Bakker48916f92012-09-16 19:57:18 +00003682 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003683 int len = SSL_BUFFER_LEN;
3684
3685 memset( ssl, 0, sizeof( ssl_context ) );
3686
Paul Bakker62f2dee2012-09-28 07:31:51 +00003687 /*
3688 * Sane defaults
3689 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003690 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3691 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3692 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3693 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003694
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003695 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003696
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003697#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003698 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003699 memset( ssl->renego_period, 0xFF, 7 );
3700 ssl->renego_period[7] = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003701#endif
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003702
Paul Bakker62f2dee2012-09-28 07:31:51 +00003703#if defined(POLARSSL_DHM_C)
3704 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
Manuel Pégourié-Gonnardf0f399d2015-07-03 17:45:57 +02003705 POLARSSL_DHM_RFC5114_MODP_2048_P) ) != 0 ||
Paul Bakker62f2dee2012-09-28 07:31:51 +00003706 ( ret = mpi_read_string( &ssl->dhm_G, 16,
Manuel Pégourié-Gonnardf0f399d2015-07-03 17:45:57 +02003707 POLARSSL_DHM_RFC5114_MODP_2048_G) ) != 0 )
Paul Bakker62f2dee2012-09-28 07:31:51 +00003708 {
3709 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3710 return( ret );
3711 }
3712#endif
3713
3714 /*
3715 * Prepare base structures
3716 */
Manuel Pégourié-Gonnardf7db5e02015-02-18 10:32:41 +00003717 if( ( ssl->in_ctr = polarssl_malloc( len ) ) == NULL ||
3718 ( ssl->out_ctr = polarssl_malloc( len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003719 {
3720 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003721 polarssl_free( ssl->in_ctr );
3722 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003723 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003724 }
3725
3726 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3727 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3728
Manuel Pégourié-Gonnardf7db5e02015-02-18 10:32:41 +00003729 ssl->in_hdr = ssl->in_ctr + 8;
3730 ssl->in_iv = ssl->in_ctr + 13;
3731 ssl->in_msg = ssl->in_ctr + 13;
3732
3733 ssl->out_hdr = ssl->out_ctr + 8;
3734 ssl->out_iv = ssl->out_ctr + 13;
3735 ssl->out_msg = ssl->out_ctr + 13;
3736
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003737#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3738 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
3739#endif
3740
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003741#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3742 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3743#endif
3744
Paul Bakker606b4ba2013-08-14 16:52:14 +02003745#if defined(POLARSSL_SSL_SESSION_TICKETS)
3746 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3747#endif
3748
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003749#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003750 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003751#endif
3752
Paul Bakker48916f92012-09-16 19:57:18 +00003753 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3754 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003755
3756 return( 0 );
3757}
3758
3759/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003760 * Reset an initialized and used SSL context for re-use while retaining
3761 * all application-set variables, function pointers and data.
3762 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003763int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003764{
Paul Bakker48916f92012-09-16 19:57:18 +00003765 int ret;
3766
Paul Bakker7eb013f2011-10-06 12:37:39 +00003767 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003768
3769#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003770 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003771 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003772
3773 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard61860192014-11-04 13:05:42 +01003774 memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
3775 memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003776#endif
3777 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00003778
Paul Bakker7eb013f2011-10-06 12:37:39 +00003779 ssl->in_offt = NULL;
3780
Paul Bakker92be97b2013-01-02 17:30:03 +01003781 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003782 ssl->in_msgtype = 0;
3783 ssl->in_msglen = 0;
3784 ssl->in_left = 0;
3785
3786 ssl->in_hslen = 0;
3787 ssl->nb_zero = 0;
Hanno Becker10699cc2017-06-08 15:41:02 +01003788 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003789
Paul Bakker92be97b2013-01-02 17:30:03 +01003790 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003791 ssl->out_msgtype = 0;
3792 ssl->out_msglen = 0;
3793 ssl->out_left = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003794#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01003795 if( ssl->split_done != SSL_CBC_RECORD_SPLITTING_DISABLED )
3796 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003797#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00003798
Paul Bakker48916f92012-09-16 19:57:18 +00003799 ssl->transform_in = NULL;
3800 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003801
3802 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3803 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003804
3805#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003806 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003807 {
3808 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003809 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003810 {
3811 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3812 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3813 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003814 }
3815#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003816
Paul Bakker48916f92012-09-16 19:57:18 +00003817 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003818 {
Paul Bakker48916f92012-09-16 19:57:18 +00003819 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003820 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003821 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003822 }
Paul Bakker48916f92012-09-16 19:57:18 +00003823
Paul Bakkerc0463502013-02-14 11:19:38 +01003824 if( ssl->session )
3825 {
3826 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003827 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003828 ssl->session = NULL;
3829 }
3830
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003831#if defined(POLARSSL_SSL_ALPN)
3832 ssl->alpn_chosen = NULL;
3833#endif
3834
Paul Bakker48916f92012-09-16 19:57:18 +00003835 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3836 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003837
3838 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003839}
3840
Paul Bakkera503a632013-08-14 13:48:06 +02003841#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003842static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3843{
3844 aes_free( &tkeys->enc );
3845 aes_free( &tkeys->dec );
3846
3847 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3848}
3849
Paul Bakker7eb013f2011-10-06 12:37:39 +00003850/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003851 * Allocate and initialize ticket keys
3852 */
3853static int ssl_ticket_keys_init( ssl_context *ssl )
3854{
3855 int ret;
3856 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003857 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003858
3859 if( ssl->ticket_keys != NULL )
3860 return( 0 );
3861
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003862 tkeys = polarssl_malloc( sizeof(ssl_ticket_keys) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003863 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003864 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3865
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003866 aes_init( &tkeys->enc );
3867 aes_init( &tkeys->dec );
3868
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003869 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003870 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003871 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003872 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003873 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003874 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003875
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003876 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3877 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3878 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3879 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003880 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003881 polarssl_free( tkeys );
3882 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003883 }
3884
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003885 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003886 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003887 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003888 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003889 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003890 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003891
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003892 ssl->ticket_keys = tkeys;
3893
3894 return( 0 );
3895}
Paul Bakkera503a632013-08-14 13:48:06 +02003896#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003897
3898/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003899 * SSL set accessors
3900 */
3901void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3902{
3903 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003904
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003905#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
3906 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003907 if( endpoint == SSL_IS_CLIENT )
3908 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003909#endif
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01003910
3911#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
3912 if( endpoint == SSL_IS_SERVER )
3913 ssl->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
3914#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003915}
3916
3917void ssl_set_authmode( ssl_context *ssl, int authmode )
3918{
3919 ssl->authmode = authmode;
3920}
3921
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003922#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003923void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003924 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003925 void *p_vrfy )
3926{
3927 ssl->f_vrfy = f_vrfy;
3928 ssl->p_vrfy = p_vrfy;
3929}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003930#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003931
Paul Bakker5121ce52009-01-03 21:22:43 +00003932void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003933 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003934 void *p_rng )
3935{
3936 ssl->f_rng = f_rng;
3937 ssl->p_rng = p_rng;
3938}
3939
3940void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003941 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003942 void *p_dbg )
3943{
3944 ssl->f_dbg = f_dbg;
3945 ssl->p_dbg = p_dbg;
3946}
3947
3948void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003949 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003950 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003951{
3952 ssl->f_recv = f_recv;
3953 ssl->f_send = f_send;
3954 ssl->p_recv = p_recv;
3955 ssl->p_send = p_send;
3956}
3957
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003958#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00003959void ssl_set_session_cache( ssl_context *ssl,
3960 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3961 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003962{
Paul Bakker0a597072012-09-25 21:55:46 +00003963 ssl->f_get_cache = f_get_cache;
3964 ssl->p_get_cache = p_get_cache;
3965 ssl->f_set_cache = f_set_cache;
3966 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003967}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003968#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003969
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003970#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003971int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003972{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003973 int ret;
3974
3975 if( ssl == NULL ||
3976 session == NULL ||
3977 ssl->session_negotiate == NULL ||
3978 ssl->endpoint != SSL_IS_CLIENT )
3979 {
3980 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3981 }
3982
3983 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3984 return( ret );
3985
Paul Bakker0a597072012-09-25 21:55:46 +00003986 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003987
3988 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003989}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003990#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003991
Paul Bakkerb68cad62012-08-23 08:34:18 +00003992void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003993{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003994 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3995 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3996 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3997 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3998}
3999
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004000void ssl_set_ciphersuites_for_version( ssl_context *ssl,
4001 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004002 int major, int minor )
4003{
4004 if( major != SSL_MAJOR_VERSION_3 )
4005 return;
4006
4007 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
4008 return;
4009
4010 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00004011}
4012
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004013#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004014/* Add a new (empty) key_cert entry an return a pointer to it */
4015static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
4016{
4017 ssl_key_cert *key_cert, *last;
4018
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004019 key_cert = polarssl_malloc( sizeof(ssl_key_cert) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004020 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004021 return( NULL );
4022
4023 memset( key_cert, 0, sizeof( ssl_key_cert ) );
4024
4025 /* Append the new key_cert to the (possibly empty) current list */
4026 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01004027 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004028 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01004029 if( ssl->handshake != NULL )
4030 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01004031 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004032 else
4033 {
4034 last = ssl->key_cert;
4035 while( last->next != NULL )
4036 last = last->next;
4037 last->next = key_cert;
4038 }
4039
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004040 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004041}
4042
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004043void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00004044 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00004045{
4046 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00004047 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00004048 ssl->peer_cn = peer_cn;
4049}
4050
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004051int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004052 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00004053{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004054 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
4055
4056 if( key_cert == NULL )
4057 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4058
4059 key_cert->cert = own_cert;
4060 key_cert->key = pk_key;
4061
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004062 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004063}
4064
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +01004065#if ! defined(POLARSSL_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004066#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004067int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004068 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00004069{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004070 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004071 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004072
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004073 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004074 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4075
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004076 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004077 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004078 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004079
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004080 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004081
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004082 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004083 if( ret != 0 )
4084 return( ret );
4085
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004086 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004087 return( ret );
4088
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004089 key_cert->cert = own_cert;
4090 key_cert->key_own_alloc = 1;
4091
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004092 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004093}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004094#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004095
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004096int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02004097 void *rsa_key,
4098 rsa_decrypt_func rsa_decrypt,
4099 rsa_sign_func rsa_sign,
4100 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00004101{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004102 int ret;
4103 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004104
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004105 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004106 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4107
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004108 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004109 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004110 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004111
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004112 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004113
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004114 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
4115 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
4116 return( ret );
4117
4118 key_cert->cert = own_cert;
4119 key_cert->key_own_alloc = 1;
4120
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004121 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00004122}
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +01004123#endif /* POLARSSL_DEPRECATED_REMOVED */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004124#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004125
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004126#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004127int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
4128 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004129{
Paul Bakker6db455e2013-09-18 17:29:31 +02004130 if( psk == NULL || psk_identity == NULL )
4131 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4132
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02004133 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01004134 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4135
Manuel Pégourié-Gonnard65125542015-08-27 16:37:35 +02004136 /* Identity len will be encoded on two bytes */
4137 if( ( psk_identity_len >> 16 ) != 0 ||
4138 psk_identity_len > SSL_MAX_CONTENT_LEN )
4139 {
4140 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4141 }
4142
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004143 if( ssl->psk != NULL || ssl->psk_identity != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02004144 {
4145 polarssl_free( ssl->psk );
4146 polarssl_free( ssl->psk_identity );
Manuel Pégourié-Gonnard9c521762015-10-27 10:54:53 +01004147 ssl->psk = NULL;
4148 ssl->psk_identity = NULL;
Paul Bakker6db455e2013-09-18 17:29:31 +02004149 }
4150
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004151 if( ( ssl->psk = polarssl_malloc( psk_len ) ) == NULL ||
4152 ( ssl->psk_identity = polarssl_malloc( psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05004153 {
4154 polarssl_free( ssl->psk );
Manuel Pégourié-Gonnard5aff0292015-09-28 18:09:45 +02004155 polarssl_free( ssl->psk_identity );
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004156 ssl->psk = NULL;
Manuel Pégourié-Gonnard5aff0292015-09-28 18:09:45 +02004157 ssl->psk_identity = NULL;
Mansour Moufidf81088b2015-02-17 13:10:21 -05004158 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4159 }
Paul Bakker6db455e2013-09-18 17:29:31 +02004160
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004161 ssl->psk_len = psk_len;
4162 ssl->psk_identity_len = psk_identity_len;
4163
Paul Bakker6db455e2013-09-18 17:29:31 +02004164 memcpy( ssl->psk, psk, ssl->psk_len );
4165 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004166
4167 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004168}
4169
4170void ssl_set_psk_cb( ssl_context *ssl,
4171 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4172 size_t),
4173 void *p_psk )
4174{
4175 ssl->f_psk = f_psk;
4176 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004177}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004178#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004179
Paul Bakker48916f92012-09-16 19:57:18 +00004180#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004181int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004182{
4183 int ret;
4184
Paul Bakker48916f92012-09-16 19:57:18 +00004185 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004186 {
4187 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4188 return( ret );
4189 }
4190
Paul Bakker48916f92012-09-16 19:57:18 +00004191 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004192 {
4193 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4194 return( ret );
4195 }
4196
4197 return( 0 );
4198}
4199
Paul Bakker1b57b062011-01-06 15:48:19 +00004200int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4201{
4202 int ret;
4203
Paul Bakker66d5d072014-06-17 16:39:18 +02004204 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004205 {
4206 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4207 return( ret );
4208 }
4209
Paul Bakker66d5d072014-06-17 16:39:18 +02004210 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004211 {
4212 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4213 return( ret );
4214 }
4215
4216 return( 0 );
4217}
Paul Bakker48916f92012-09-16 19:57:18 +00004218#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004219
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004220#if defined(POLARSSL_SSL_SET_CURVES)
4221/*
4222 * Set the allowed elliptic curves
4223 */
4224void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4225{
4226 ssl->curve_list = curve_list;
4227}
4228#endif
4229
Paul Bakker0be444a2013-08-27 21:55:01 +02004230#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004231int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004232{
Hanno Beckerb9ac47c2017-05-05 13:07:33 +01004233 size_t hostname_len = 0;
4234
4235 /* Check if new hostname is valid before
4236 * making any change to current one */
4237
4238 if( hostname != NULL )
4239 {
4240 hostname_len = strlen( hostname );
4241
4242 if( hostname_len > SSL_MAX_HOST_NAME_LEN )
4243 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4244 }
4245
4246 /* Now it's clear that we will overwrite the old hostname,
4247 * so we can free it safely */
4248
4249 if( ssl->hostname != NULL )
4250 {
4251 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
4252 polarssl_free( ssl->hostname );
4253 }
4254
4255 /* Passing NULL as hostname shall clear the old one */
4256
Paul Bakker5121ce52009-01-03 21:22:43 +00004257 if( hostname == NULL )
Hanno Beckerb9ac47c2017-05-05 13:07:33 +01004258 {
4259 ssl->hostname = NULL;
4260 ssl->hostname_len = 0;
4261 }
4262 else
4263 {
4264 ssl->hostname = polarssl_malloc( hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004265
Hanno Beckerb9ac47c2017-05-05 13:07:33 +01004266 if( ssl->hostname == NULL )
4267 {
4268 ssl->hostname_len = 0;
4269 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4270 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004271
Hanno Beckerb9ac47c2017-05-05 13:07:33 +01004272 memcpy( ssl->hostname, (const unsigned char*) hostname,
4273 hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004274
Hanno Beckerb9ac47c2017-05-05 13:07:33 +01004275 ssl->hostname[hostname_len] = '\0';
4276 ssl->hostname_len = hostname_len;
4277 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004278
4279 return( 0 );
4280}
4281
Paul Bakker5701cdc2012-09-27 21:49:42 +00004282void ssl_set_sni( ssl_context *ssl,
4283 int (*f_sni)(void *, ssl_context *,
4284 const unsigned char *, size_t),
4285 void *p_sni )
4286{
4287 ssl->f_sni = f_sni;
4288 ssl->p_sni = p_sni;
4289}
Paul Bakker0be444a2013-08-27 21:55:01 +02004290#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004291
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004292#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004293int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004294{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004295 size_t cur_len, tot_len;
4296 const char **p;
4297
4298 /*
4299 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4300 * truncated". Check lengths now rather than later.
4301 */
4302 tot_len = 0;
4303 for( p = protos; *p != NULL; p++ )
4304 {
4305 cur_len = strlen( *p );
4306 tot_len += cur_len;
4307
4308 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4309 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4310 }
4311
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004312 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004313
4314 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004315}
4316
4317const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4318{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004319 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004320}
4321#endif /* POLARSSL_SSL_ALPN */
4322
Paul Bakker490ecc82011-10-06 13:04:09 +00004323void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4324{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004325 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4326 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4327 {
4328 ssl->max_major_ver = major;
4329 ssl->max_minor_ver = minor;
4330 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004331}
4332
Paul Bakker1d29fb52012-09-28 13:28:45 +00004333void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4334{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004335 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4336 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4337 {
4338 ssl->min_major_ver = major;
4339 ssl->min_minor_ver = minor;
4340 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004341}
4342
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004343#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4344void ssl_set_fallback( ssl_context *ssl, char fallback )
4345{
4346 ssl->fallback = fallback;
4347}
4348#endif
4349
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004350#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4351void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
4352{
4353 ssl->encrypt_then_mac = etm;
4354}
4355#endif
4356
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004357#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4358void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4359{
4360 ssl->extended_ms = ems;
4361}
4362#endif
4363
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004364void ssl_set_arc4_support( ssl_context *ssl, char arc4 )
4365{
4366 ssl->arc4_disabled = arc4;
4367}
4368
Paul Bakker05decb22013-08-15 13:33:48 +02004369#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004370int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4371{
Paul Bakker77e257e2013-12-16 15:29:52 +01004372 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004373 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004374 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004375 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004376 }
4377
4378 ssl->mfl_code = mfl_code;
4379
4380 return( 0 );
4381}
Paul Bakker05decb22013-08-15 13:33:48 +02004382#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004383
Paul Bakker1f2bc622013-08-15 13:45:55 +02004384#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004385int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004386{
Paul Bakker8c1ede62013-07-19 14:14:37 +02004387 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004388
4389 return( 0 );
4390}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004391#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004392
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004393#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4394void ssl_set_cbc_record_splitting( ssl_context *ssl, char split )
4395{
4396 ssl->split_done = split;
4397}
4398#endif
4399
Paul Bakker48916f92012-09-16 19:57:18 +00004400void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4401{
4402 ssl->allow_legacy_renegotiation = allow_legacy;
4403}
4404
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004405#if defined(POLARSSL_SSL_RENEGOTIATION)
4406void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4407{
4408 ssl->disable_renegotiation = renegotiation;
4409}
4410
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004411void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4412{
4413 ssl->renego_max_records = max_records;
4414}
4415
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004416void ssl_set_renegotiation_period( ssl_context *ssl,
4417 const unsigned char period[8] )
4418{
4419 memcpy( ssl->renego_period, period, 8 );
4420}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004421#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004422
Paul Bakkera503a632013-08-14 13:48:06 +02004423#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004424int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4425{
4426 ssl->session_tickets = use_tickets;
4427
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004428#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004429 if( ssl->endpoint == SSL_IS_CLIENT )
4430 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004431#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004432
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01004433 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
4434 return( 0 );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004435
4436 if( ssl->f_rng == NULL )
4437 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4438
4439 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004440}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004441
4442void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4443{
4444 ssl->ticket_lifetime = lifetime;
4445}
Paul Bakkera503a632013-08-14 13:48:06 +02004446#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004447
Paul Bakker5121ce52009-01-03 21:22:43 +00004448/*
4449 * SSL get accessors
4450 */
4451size_t ssl_get_bytes_avail( const ssl_context *ssl )
4452{
4453 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4454}
4455
Paul Bakkerff60ee62010-03-16 21:09:09 +00004456int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004457{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00004458 if( ssl->session != NULL )
4459 return( ssl->session->verify_result );
4460
4461 if( ssl->session_negotiate != NULL )
4462 return( ssl->session_negotiate->verify_result );
4463
4464 return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004465}
4466
Paul Bakkere3166ce2011-01-27 17:40:50 +00004467const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004468{
Paul Bakker926c8e42013-03-06 10:23:34 +01004469 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004470 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004471
Paul Bakkere3166ce2011-01-27 17:40:50 +00004472 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004473}
4474
Paul Bakker43ca69c2011-01-15 17:35:19 +00004475const char *ssl_get_version( const ssl_context *ssl )
4476{
4477 switch( ssl->minor_ver )
4478 {
4479 case SSL_MINOR_VERSION_0:
4480 return( "SSLv3.0" );
4481
4482 case SSL_MINOR_VERSION_1:
4483 return( "TLSv1.0" );
4484
4485 case SSL_MINOR_VERSION_2:
4486 return( "TLSv1.1" );
4487
Paul Bakker1ef83d62012-04-11 12:09:53 +00004488 case SSL_MINOR_VERSION_3:
4489 return( "TLSv1.2" );
4490
Paul Bakker43ca69c2011-01-15 17:35:19 +00004491 default:
4492 break;
4493 }
4494 return( "unknown" );
4495}
4496
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004497#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004498const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004499{
4500 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004501 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004502
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004503 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004504}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004505#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004506
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004507#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004508int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4509{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004510 if( ssl == NULL ||
4511 dst == NULL ||
4512 ssl->session == NULL ||
4513 ssl->endpoint != SSL_IS_CLIENT )
4514 {
4515 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4516 }
4517
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004518 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004519}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004520#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004521
Paul Bakker5121ce52009-01-03 21:22:43 +00004522/*
Paul Bakker1961b702013-01-25 14:49:24 +01004523 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004524 */
Paul Bakker1961b702013-01-25 14:49:24 +01004525int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004526{
Paul Bakker40e46942009-01-03 21:51:57 +00004527 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004528
Paul Bakker40e46942009-01-03 21:51:57 +00004529#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004530 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004531 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004532#endif
Paul Bakker40e46942009-01-03 21:51:57 +00004533#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004534 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004535 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004536#endif
4537
Paul Bakker1961b702013-01-25 14:49:24 +01004538 return( ret );
4539}
4540
4541/*
4542 * Perform the SSL handshake
4543 */
4544int ssl_handshake( ssl_context *ssl )
4545{
4546 int ret = 0;
4547
4548 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4549
4550 while( ssl->state != SSL_HANDSHAKE_OVER )
4551 {
4552 ret = ssl_handshake_step( ssl );
4553
4554 if( ret != 0 )
4555 break;
4556 }
4557
Paul Bakker5121ce52009-01-03 21:22:43 +00004558 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4559
4560 return( ret );
4561}
4562
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004563#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004564#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004565/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004566 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004567 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004568static int ssl_write_hello_request( ssl_context *ssl )
4569{
4570 int ret;
4571
4572 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4573
4574 ssl->out_msglen = 4;
4575 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4576 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4577
4578 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4579 {
4580 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4581 return( ret );
4582 }
4583
4584 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4585
4586 return( 0 );
4587}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004588#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004589
4590/*
4591 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004592 * - any side: calling ssl_renegotiate(),
4593 * - client: receiving a HelloRequest during ssl_read(),
4594 * - server: receiving any handshake message on server during ssl_read() after
4595 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004596 * If the handshake doesn't complete due to waiting for I/O, it will continue
4597 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004598 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004599static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004600{
4601 int ret;
4602
4603 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4604
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004605 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4606 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004607
4608 ssl->state = SSL_HELLO_REQUEST;
4609 ssl->renegotiation = SSL_RENEGOTIATION;
4610
Paul Bakker48916f92012-09-16 19:57:18 +00004611 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4612 {
4613 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4614 return( ret );
4615 }
4616
4617 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4618
4619 return( 0 );
4620}
4621
4622/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004623 * Renegotiate current connection on client,
4624 * or request renegotiation on server
4625 */
4626int ssl_renegotiate( ssl_context *ssl )
4627{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004628 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004629
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004630#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004631 /* On server, just send the request */
4632 if( ssl->endpoint == SSL_IS_SERVER )
4633 {
4634 if( ssl->state != SSL_HANDSHAKE_OVER )
4635 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4636
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004637 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4638
4639 /* Did we already try/start sending HelloRequest? */
4640 if( ssl->out_left != 0 )
4641 return( ssl_flush_output( ssl ) );
4642
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004643 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004644 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004645#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004646
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004647#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004648 /*
4649 * On client, either start the renegotiation process or,
4650 * if already in progress, continue the handshake
4651 */
4652 if( ssl->renegotiation != SSL_RENEGOTIATION )
4653 {
4654 if( ssl->state != SSL_HANDSHAKE_OVER )
4655 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4656
4657 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4658 {
4659 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4660 return( ret );
4661 }
4662 }
4663 else
4664 {
4665 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4666 {
4667 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4668 return( ret );
4669 }
4670 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004671#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004672
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004673 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004674}
4675
4676/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004677 * Check record counters and renegotiate if they're above the limit.
4678 */
4679static int ssl_check_ctr_renegotiate( ssl_context *ssl )
4680{
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004681 if( ssl->state != SSL_HANDSHAKE_OVER ||
4682 ssl->renegotiation == SSL_RENEGOTIATION_PENDING ||
4683 ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED )
4684 {
4685 return( 0 );
4686 }
4687
4688 // TODO: adapt for DTLS
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004689 if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 &&
4690 memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004691 {
4692 return( 0 );
4693 }
4694
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004695 SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004696 return( ssl_renegotiate( ssl ) );
4697}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004698#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004699
4700/*
4701 * Receive application data decrypted from the SSL layer
4702 */
Paul Bakker23986e52011-04-24 08:57:21 +00004703int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004704{
Hanno Beckerd37839e2017-06-08 15:56:50 +01004705 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00004706 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004707
4708 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4709
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004710#if defined(POLARSSL_SSL_RENEGOTIATION)
Hanno Beckerd37839e2017-06-08 15:56:50 +01004711 ret = ssl_check_ctr_renegotiate( ssl );
4712 if( ret != POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
4713 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004714 {
4715 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4716 return( ret );
4717 }
4718#endif
4719
Paul Bakker5121ce52009-01-03 21:22:43 +00004720 if( ssl->state != SSL_HANDSHAKE_OVER )
4721 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004722 ret = ssl_handshake( ssl );
Hanno Beckerd37839e2017-06-08 15:56:50 +01004723 if( ret != POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
4724 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004725 {
4726 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4727 return( ret );
4728 }
4729 }
4730
4731 if( ssl->in_offt == NULL )
4732 {
Hanno Beckerd37839e2017-06-08 15:56:50 +01004733 if( ( ret = ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004734 {
Hanno Beckerd37839e2017-06-08 15:56:50 +01004735 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4736 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004737
Hanno Beckerd37839e2017-06-08 15:56:50 +01004738 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4739 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004740 }
4741
4742 if( ssl->in_msglen == 0 &&
4743 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4744 {
4745 /*
4746 * OpenSSL sends empty messages to randomize the IV
4747 */
4748 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4749 {
Paul Bakker831a7552011-05-18 13:32:51 +00004750 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4751 return( 0 );
4752
Paul Bakker5121ce52009-01-03 21:22:43 +00004753 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4754 return( ret );
4755 }
4756 }
4757
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004758#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004759 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4760 {
4761 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4762
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004763#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004764 if( ssl->endpoint == SSL_IS_CLIENT &&
4765 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4766 ssl->in_hslen != 4 ) )
4767 {
4768 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4769 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4770 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004771#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004772
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004773 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4774 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004775 ssl->allow_legacy_renegotiation ==
4776 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004777 {
4778 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4779
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004780#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004781 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004782 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004783 /*
4784 * SSLv3 does not have a "no_renegotiation" alert
4785 */
4786 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4787 return( ret );
4788 }
4789 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004790#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004791#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4792 defined(POLARSSL_SSL_PROTO_TLS1_2)
4793 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004794 {
4795 if( ( ret = ssl_send_alert_message( ssl,
4796 SSL_ALERT_LEVEL_WARNING,
4797 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4798 {
4799 return( ret );
4800 }
Paul Bakker48916f92012-09-16 19:57:18 +00004801 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004802 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004803#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4804 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004805 {
4806 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004807 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004808 }
Paul Bakker48916f92012-09-16 19:57:18 +00004809 }
4810 else
4811 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004812 ret = ssl_start_renegotiation( ssl );
Hanno Beckerd37839e2017-06-08 15:56:50 +01004813 if( ret != POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
4814 ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004815 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004816 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004817 return( ret );
4818 }
Paul Bakker48916f92012-09-16 19:57:18 +00004819 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004820
Hanno Beckerd37839e2017-06-08 15:56:50 +01004821 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004822 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004823 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4824 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004825 ssl->renego_records_seen++;
4826
4827 if( ssl->renego_max_records >= 0 &&
4828 ssl->renego_records_seen > ssl->renego_max_records )
4829 {
4830 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4831 "but not honored by client" ) );
4832 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4833 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004834 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004835#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004836
4837 /* Fatal and closure alerts handled by ssl_read_record() */
4838 if( ssl->in_msgtype == SSL_MSG_ALERT )
4839 {
4840 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4841 return( POLARSSL_ERR_NET_WANT_READ );
4842 }
4843
4844 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004845 {
4846 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004847 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004848 }
4849
4850 ssl->in_offt = ssl->in_msg;
4851 }
4852
4853 n = ( len < ssl->in_msglen )
4854 ? len : ssl->in_msglen;
4855
4856 memcpy( buf, ssl->in_offt, n );
4857 ssl->in_msglen -= n;
4858
4859 if( ssl->in_msglen == 0 )
Hanno Becker0401a3d2017-06-09 10:52:45 +01004860 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004861 /* all bytes consumed */
4862 ssl->in_offt = NULL;
Hanno Becker0401a3d2017-06-09 10:52:45 +01004863 ssl->keep_current_message = 0;
4864 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004865 else
4866 /* more data available */
4867 ssl->in_offt += n;
4868
4869 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4870
Paul Bakker23986e52011-04-24 08:57:21 +00004871 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004872}
4873
4874/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004875 * Send application data to be encrypted by the SSL layer,
4876 * taking care of max fragment length and buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +00004877 */
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004878static int ssl_write_real( ssl_context *ssl,
4879 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004880{
Paul Bakker23986e52011-04-24 08:57:21 +00004881 int ret;
4882 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004883 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004884
Paul Bakker05decb22013-08-15 13:33:48 +02004885#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004886 /*
4887 * Assume mfl_code is correct since it was checked when set
4888 */
4889 max_len = mfl_code_to_length[ssl->mfl_code];
4890
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004891 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004892 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004893 */
4894 if( ssl->session_out != NULL &&
4895 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4896 {
4897 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4898 }
Paul Bakker05decb22013-08-15 13:33:48 +02004899#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004900
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004901 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004902
Paul Bakker5121ce52009-01-03 21:22:43 +00004903 if( ssl->out_left != 0 )
4904 {
4905 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4906 {
4907 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4908 return( ret );
4909 }
4910 }
Paul Bakker887bd502011-06-08 13:10:54 +00004911 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004912 {
Paul Bakker887bd502011-06-08 13:10:54 +00004913 ssl->out_msglen = n;
4914 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4915 memcpy( ssl->out_msg, buf, n );
4916
4917 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4918 {
4919 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4920 return( ret );
4921 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004922 }
4923
Paul Bakker23986e52011-04-24 08:57:21 +00004924 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004925}
4926
4927/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004928 * Write application data, doing 1/n-1 splitting if necessary.
4929 *
4930 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004931 * then the caller will call us again with the same arguments, so
4932 * remember wether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004933 */
4934#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004935static int ssl_write_split( ssl_context *ssl,
4936 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004937{
4938 int ret;
4939
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004940 if( ssl->split_done == SSL_CBC_RECORD_SPLITTING_DISABLED ||
4941 len <= 1 ||
4942 ssl->minor_ver > SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004943 cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
4944 != POLARSSL_MODE_CBC )
4945 {
4946 return( ssl_write_real( ssl, buf, len ) );
4947 }
4948
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004949 if( ssl->split_done == 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004950 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004951 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004952 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004953 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004954 }
4955
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004956 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
4957 return( ret );
4958 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004959
4960 return( ret + 1 );
4961}
4962#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */
4963
4964/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004965 * Write application data (public-facing wrapper)
4966 */
4967int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
4968{
4969 int ret;
4970
4971 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4972
4973#if defined(POLARSSL_SSL_RENEGOTIATION)
4974 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4975 {
4976 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4977 return( ret );
4978 }
4979#endif
4980
4981 if( ssl->state != SSL_HANDSHAKE_OVER )
4982 {
4983 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4984 {
4985 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4986 return( ret );
4987 }
4988 }
4989
4990#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4991 ret = ssl_write_split( ssl, buf, len );
4992#else
4993 ret = ssl_write_real( ssl, buf, len );
4994#endif
4995
4996 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4997
4998 return( ret );
4999}
5000
5001/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005002 * Notify the peer that the connection is being closed
5003 */
5004int ssl_close_notify( ssl_context *ssl )
5005{
5006 int ret;
5007
5008 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
5009
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005010 if( ssl->out_left != 0 )
5011 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005012
5013 if( ssl->state == SSL_HANDSHAKE_OVER )
5014 {
Paul Bakker48916f92012-09-16 19:57:18 +00005015 if( ( ret = ssl_send_alert_message( ssl,
5016 SSL_ALERT_LEVEL_WARNING,
5017 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005018 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005019 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005020 return( ret );
5021 }
5022 }
5023
5024 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
5025
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005026 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005027}
5028
Paul Bakker48916f92012-09-16 19:57:18 +00005029void ssl_transform_free( ssl_transform *transform )
5030{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005031 if( transform == NULL )
5032 return;
5033
Paul Bakker48916f92012-09-16 19:57:18 +00005034#if defined(POLARSSL_ZLIB_SUPPORT)
5035 deflateEnd( &transform->ctx_deflate );
5036 inflateEnd( &transform->ctx_inflate );
5037#endif
5038
Paul Bakker84bbeb52014-07-01 14:53:22 +02005039 cipher_free( &transform->cipher_ctx_enc );
5040 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005041
Paul Bakker84bbeb52014-07-01 14:53:22 +02005042 md_free( &transform->md_ctx_enc );
5043 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02005044
Paul Bakker34617722014-06-13 17:20:13 +02005045 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005046}
5047
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005048#if defined(POLARSSL_X509_CRT_PARSE_C)
5049static void ssl_key_cert_free( ssl_key_cert *key_cert )
5050{
5051 ssl_key_cert *cur = key_cert, *next;
5052
5053 while( cur != NULL )
5054 {
5055 next = cur->next;
5056
5057 if( cur->key_own_alloc )
5058 {
5059 pk_free( cur->key );
5060 polarssl_free( cur->key );
5061 }
5062 polarssl_free( cur );
5063
5064 cur = next;
5065 }
5066}
5067#endif /* POLARSSL_X509_CRT_PARSE_C */
5068
Paul Bakker48916f92012-09-16 19:57:18 +00005069void ssl_handshake_free( ssl_handshake_params *handshake )
5070{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005071 if( handshake == NULL )
5072 return;
5073
Paul Bakker48916f92012-09-16 19:57:18 +00005074#if defined(POLARSSL_DHM_C)
5075 dhm_free( &handshake->dhm_ctx );
5076#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005077#if defined(POLARSSL_ECDH_C)
5078 ecdh_free( &handshake->ecdh_ctx );
5079#endif
5080
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005081#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02005082 /* explicit void pointer cast for buggy MS compiler */
5083 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005084#endif
5085
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02005086#if defined(POLARSSL_X509_CRT_PARSE_C) && \
5087 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
5088 /*
5089 * Free only the linked list wrapper, not the keys themselves
5090 * since the belong to the SNI callback
5091 */
5092 if( handshake->sni_key_cert != NULL )
5093 {
5094 ssl_key_cert *cur = handshake->sni_key_cert, *next;
5095
5096 while( cur != NULL )
5097 {
5098 next = cur->next;
5099 polarssl_free( cur );
5100 cur = next;
5101 }
5102 }
Paul Bakker9af723c2014-05-01 13:03:14 +02005103#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005104
Paul Bakker34617722014-06-13 17:20:13 +02005105 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005106}
5107
5108void ssl_session_free( ssl_session *session )
5109{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005110 if( session == NULL )
5111 return;
5112
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005113#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00005114 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00005115 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005116 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02005117 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00005118 }
Paul Bakkered27a042013-04-18 22:46:23 +02005119#endif
Paul Bakker0a597072012-09-25 21:55:46 +00005120
Paul Bakkera503a632013-08-14 13:48:06 +02005121#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005122 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02005123#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005124
Paul Bakker34617722014-06-13 17:20:13 +02005125 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005126}
5127
Paul Bakker5121ce52009-01-03 21:22:43 +00005128/*
5129 * Free an SSL context
5130 */
5131void ssl_free( ssl_context *ssl )
5132{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005133 if( ssl == NULL )
5134 return;
5135
Paul Bakker5121ce52009-01-03 21:22:43 +00005136 SSL_DEBUG_MSG( 2, ( "=> free" ) );
5137
Paul Bakker5121ce52009-01-03 21:22:43 +00005138 if( ssl->out_ctr != NULL )
5139 {
Paul Bakker34617722014-06-13 17:20:13 +02005140 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005141 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005142 }
5143
5144 if( ssl->in_ctr != NULL )
5145 {
Paul Bakker34617722014-06-13 17:20:13 +02005146 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005147 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005148 }
5149
Paul Bakker16770332013-10-11 09:59:44 +02005150#if defined(POLARSSL_ZLIB_SUPPORT)
5151 if( ssl->compress_buf != NULL )
5152 {
Paul Bakker34617722014-06-13 17:20:13 +02005153 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02005154 polarssl_free( ssl->compress_buf );
5155 }
5156#endif
5157
Paul Bakker40e46942009-01-03 21:51:57 +00005158#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00005159 mpi_free( &ssl->dhm_P );
5160 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00005161#endif
5162
Paul Bakker48916f92012-09-16 19:57:18 +00005163 if( ssl->transform )
5164 {
5165 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005166 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005167 }
5168
5169 if( ssl->handshake )
5170 {
5171 ssl_handshake_free( ssl->handshake );
5172 ssl_transform_free( ssl->transform_negotiate );
5173 ssl_session_free( ssl->session_negotiate );
5174
Paul Bakker6e339b52013-07-03 13:37:05 +02005175 polarssl_free( ssl->handshake );
5176 polarssl_free( ssl->transform_negotiate );
5177 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00005178 }
5179
Paul Bakkerc0463502013-02-14 11:19:38 +01005180 if( ssl->session )
5181 {
5182 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005183 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005184 }
5185
Paul Bakkera503a632013-08-14 13:48:06 +02005186#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005187 if( ssl->ticket_keys )
5188 {
5189 ssl_ticket_keys_free( ssl->ticket_keys );
5190 polarssl_free( ssl->ticket_keys );
5191 }
Paul Bakkera503a632013-08-14 13:48:06 +02005192#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005193
Paul Bakker0be444a2013-08-27 21:55:01 +02005194#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02005195 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005196 {
Paul Bakker34617722014-06-13 17:20:13 +02005197 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02005198 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00005199 ssl->hostname_len = 0;
5200 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005201#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005202
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005203#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005204 if( ssl->psk != NULL )
5205 {
Paul Bakker34617722014-06-13 17:20:13 +02005206 polarssl_zeroize( ssl->psk, ssl->psk_len );
5207 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005208 polarssl_free( ssl->psk );
5209 polarssl_free( ssl->psk_identity );
5210 ssl->psk_len = 0;
5211 ssl->psk_identity_len = 0;
5212 }
5213#endif
5214
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005215#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005216 ssl_key_cert_free( ssl->key_cert );
5217#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005218
Paul Bakker05ef8352012-05-08 09:17:57 +00005219#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
5220 if( ssl_hw_record_finish != NULL )
5221 {
5222 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
5223 ssl_hw_record_finish( ssl );
5224 }
5225#endif
5226
Paul Bakker5121ce52009-01-03 21:22:43 +00005227 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00005228
Paul Bakker86f04f42013-02-14 11:20:09 +01005229 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02005230 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005231}
5232
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005233#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005234/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005235 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005236 */
5237unsigned char ssl_sig_from_pk( pk_context *pk )
5238{
5239#if defined(POLARSSL_RSA_C)
5240 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
5241 return( SSL_SIG_RSA );
5242#endif
5243#if defined(POLARSSL_ECDSA_C)
5244 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
5245 return( SSL_SIG_ECDSA );
5246#endif
5247 return( SSL_SIG_ANON );
5248}
5249
Hanno Beckerc2b9d982017-05-10 16:37:21 +01005250unsigned char ssl_sig_from_pk_alg( pk_type_t type )
5251{
5252 switch( type ) {
5253 case POLARSSL_PK_RSA:
5254 return( SSL_SIG_RSA );
5255 case POLARSSL_PK_ECDSA:
5256 case POLARSSL_PK_ECKEY:
5257 return( SSL_SIG_ECDSA );
5258 default:
5259 return( SSL_SIG_ANON );
5260 }
5261}
5262
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005263pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
5264{
5265 switch( sig )
5266 {
5267#if defined(POLARSSL_RSA_C)
5268 case SSL_SIG_RSA:
5269 return( POLARSSL_PK_RSA );
5270#endif
5271#if defined(POLARSSL_ECDSA_C)
5272 case SSL_SIG_ECDSA:
5273 return( POLARSSL_PK_ECDSA );
5274#endif
5275 default:
5276 return( POLARSSL_PK_NONE );
5277 }
5278}
Paul Bakker9af723c2014-05-01 13:03:14 +02005279#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005280
Hanno Beckerc2b9d982017-05-10 16:37:21 +01005281#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
5282 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
5283
5284/* Find an entry in a signature-hash set matching a given hash algorithm. */
5285md_type_t ssl_sig_hash_set_find( ssl_sig_hash_set_t *set,
5286 pk_type_t sig_alg )
5287{
5288 switch( sig_alg )
5289 {
5290 case POLARSSL_PK_RSA:
5291 return( set->rsa );
5292 case POLARSSL_PK_ECDSA:
5293 return( set->ecdsa );
5294 default:
5295 return( POLARSSL_MD_NONE );
5296 }
5297}
5298
5299/* Add a signature-hash-pair to a signature-hash set */
5300void ssl_sig_hash_set_add( ssl_sig_hash_set_t *set,
5301 pk_type_t sig_alg,
5302 md_type_t md_alg )
5303{
5304 switch( sig_alg )
5305 {
5306 case POLARSSL_PK_RSA:
5307 if( set->rsa == POLARSSL_MD_NONE )
5308 set->rsa = md_alg;
5309 break;
5310
5311 case POLARSSL_PK_ECDSA:
5312 if( set->ecdsa == POLARSSL_MD_NONE )
5313 set->ecdsa = md_alg;
5314 break;
5315
5316 default:
5317 break;
5318 }
5319}
5320
5321/* Allow exactly one hash algorithm for each signature. */
5322void ssl_sig_hash_set_const_hash( ssl_sig_hash_set_t *set,
5323 md_type_t md_alg )
5324{
5325 set->rsa = md_alg;
5326 set->ecdsa = md_alg;
5327}
5328
5329#endif /* POLARSSL_SSL_PROTO_TLS1_2) &&
5330 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
5331
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005332/*
5333 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
5334 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005335md_type_t ssl_md_alg_from_hash( unsigned char hash )
5336{
5337 switch( hash )
5338 {
5339#if defined(POLARSSL_MD5_C)
5340 case SSL_HASH_MD5:
5341 return( POLARSSL_MD_MD5 );
5342#endif
5343#if defined(POLARSSL_SHA1_C)
5344 case SSL_HASH_SHA1:
5345 return( POLARSSL_MD_SHA1 );
5346#endif
5347#if defined(POLARSSL_SHA256_C)
5348 case SSL_HASH_SHA224:
5349 return( POLARSSL_MD_SHA224 );
5350 case SSL_HASH_SHA256:
5351 return( POLARSSL_MD_SHA256 );
5352#endif
5353#if defined(POLARSSL_SHA512_C)
5354 case SSL_HASH_SHA384:
5355 return( POLARSSL_MD_SHA384 );
5356 case SSL_HASH_SHA512:
5357 return( POLARSSL_MD_SHA512 );
5358#endif
5359 default:
5360 return( POLARSSL_MD_NONE );
5361 }
5362}
5363
Hanno Beckerc2b9d982017-05-10 16:37:21 +01005364/*
5365 * Convert from POLARSSL_MD_XXX to SSL_HASH_XXX
5366 */
5367unsigned char ssl_hash_from_md_alg( md_type_t md )
5368{
5369 switch( md )
5370 {
5371#if defined(POLARSSL_MD5_C)
5372 case POLARSSL_MD_MD5:
5373 return( SSL_HASH_MD5 );
5374#endif
5375#if defined(POLARSSL_SHA1_C)
5376 case POLARSSL_MD_SHA1:
5377 return( SSL_HASH_SHA1 );
5378#endif
5379#if defined(POLARSSL_SHA256_C)
5380 case POLARSSL_MD_SHA224:
5381 return( SSL_HASH_SHA224 );
5382 case POLARSSL_MD_SHA256:
5383 return( SSL_HASH_SHA256 );
5384#endif
5385#if defined(POLARSSL_SHA512_C)
5386 case POLARSSL_MD_SHA384:
5387 return( SSL_HASH_SHA384 );
5388 case POLARSSL_MD_SHA512:
5389 return( SSL_HASH_SHA512 );
5390#endif
5391 default:
5392 return( SSL_HASH_NONE );
5393 }
5394}
5395
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005396#if defined(POLARSSL_SSL_SET_CURVES)
5397/*
5398 * Check is a curve proposed by the peer is in our list.
5399 * Return 1 if we're willing to use it, 0 otherwise.
5400 */
5401int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
5402{
5403 const ecp_group_id *gid;
5404
5405 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5406 if( *gid == grp_id )
5407 return( 1 );
5408
5409 return( 0 );
5410}
Paul Bakker9af723c2014-05-01 13:03:14 +02005411#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005412
Hanno Beckerc2b9d982017-05-10 16:37:21 +01005413#if defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
5414/*
5415 * Check if a hash proposed by the peer is in our list.
5416 * Return 0 if we're willing to use it, -1 otherwise.
5417 */
5418int ssl_check_sig_hash( md_type_t md )
5419{
5420 const int *cur;
5421
5422 for( cur = md_list(); *cur != POLARSSL_MD_NONE; cur++ )
5423 {
5424#if !defined(POLARSSL_SSL_ENABLE_MD5_SIGNATURES)
5425 /* Skip MD5 */
5426 if( *cur == POLARSSL_MD_MD5 )
5427 continue;
5428#endif
5429 if( *cur == (int) md )
5430 return( 0 );
5431 }
5432
5433 return( -1 );
5434}
5435#endif /* POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
5436
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005437#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005438int ssl_check_cert_usage( const x509_crt *cert,
5439 const ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005440 int cert_endpoint,
5441 int *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005442{
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005443 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005444#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5445 int usage = 0;
5446#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005447#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5448 const char *ext_oid;
5449 size_t ext_len;
5450#endif
5451
5452#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5453 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5454 ((void) cert);
5455 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005456 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005457#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005458
5459#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5460 if( cert_endpoint == SSL_IS_SERVER )
5461 {
5462 /* Server part of the key exchange */
5463 switch( ciphersuite->key_exchange )
5464 {
5465 case POLARSSL_KEY_EXCHANGE_RSA:
5466 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5467 usage = KU_KEY_ENCIPHERMENT;
5468 break;
5469
5470 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5471 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5472 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5473 usage = KU_DIGITAL_SIGNATURE;
5474 break;
5475
5476 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5477 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5478 usage = KU_KEY_AGREEMENT;
5479 break;
5480
5481 /* Don't use default: we want warnings when adding new values */
5482 case POLARSSL_KEY_EXCHANGE_NONE:
5483 case POLARSSL_KEY_EXCHANGE_PSK:
5484 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5485 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5486 usage = 0;
5487 }
5488 }
5489 else
5490 {
5491 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5492 usage = KU_DIGITAL_SIGNATURE;
5493 }
5494
5495 if( x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005496 {
5497 *flags |= BADCERT_KEY_USAGE;
5498 ret = -1;
5499 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005500#else
5501 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005502#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5503
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005504#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5505 if( cert_endpoint == SSL_IS_SERVER )
5506 {
5507 ext_oid = OID_SERVER_AUTH;
5508 ext_len = OID_SIZE( OID_SERVER_AUTH );
5509 }
5510 else
5511 {
5512 ext_oid = OID_CLIENT_AUTH;
5513 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5514 }
5515
5516 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005517 {
5518 *flags |= BADCERT_EXT_KEY_USAGE;
5519 ret = -1;
5520 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005521#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5522
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005523 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005524}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005525#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005526
5527#endif /* POLARSSL_SSL_TLS_C */