blob: 1018270f97467d4d5068635b6ef9b499812d951c [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * The SSL 3.0 specification was drafted by Netscape in 1996,
24 * and became an IETF standard in 1999.
25 *
26 * http://wp.netscape.com/eng/ssl3/
27 * http://www.ietf.org/rfc/rfc2246.txt
28 * http://www.ietf.org/rfc/rfc4346.txt
29 */
30
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#else
34#include POLARSSL_CONFIG_FILE
35#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker40e46942009-01-03 21:51:57 +000037#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker0be444a2013-08-27 21:55:01 +020039#include "polarssl/debug.h"
40#include "polarssl/ssl.h"
41
Rich Evans00ab4702015-02-06 13:43:58 +000042#include <string.h>
43
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020044#if defined(POLARSSL_X509_CRT_PARSE_C) && \
45 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
46#include "polarssl/oid.h"
47#endif
48
Paul Bakker7dc4c442014-02-01 22:50:26 +010049#if defined(POLARSSL_PLATFORM_C)
50#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020051#else
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <stdlib.h>
Paul Bakker6e339b52013-07-03 13:37:05 +020053#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker6edcd412013-10-29 15:22:54 +010057#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
58 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000059#define strcasecmp _stricmp
60#endif
61
Paul Bakker34617722014-06-13 17:20:13 +020062/* Implementation that should never be optimized out by the compiler */
63static void polarssl_zeroize( void *v, size_t n ) {
64 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
65}
66
Paul Bakker05decb22013-08-15 13:33:48 +020067#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020068/*
69 * Convert max_fragment_length codes to length.
70 * RFC 6066 says:
71 * enum{
72 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
73 * } MaxFragmentLength;
74 * and we add 0 -> extension unused
75 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020076static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020077{
78 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
79 512, /* SSL_MAX_FRAG_LEN_512 */
80 1024, /* SSL_MAX_FRAG_LEN_1024 */
81 2048, /* SSL_MAX_FRAG_LEN_2048 */
82 4096, /* SSL_MAX_FRAG_LEN_4096 */
83};
Paul Bakker05decb22013-08-15 13:33:48 +020084#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020085
Simon Butcher149950d2016-10-15 22:08:08 +010086#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020087static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
88{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020089 ssl_session_free( dst );
90 memcpy( dst, src, sizeof( ssl_session ) );
91
Paul Bakker7c6b2c32013-09-16 13:49:26 +020092#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020093 if( src->peer_cert != NULL )
94 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020095 int ret;
96
Mansour Moufidc531b4a2015-02-15 17:35:38 -050097 dst->peer_cert = polarssl_malloc( sizeof(x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020098 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020099 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
100
Paul Bakkerb6b09562013-09-18 14:17:41 +0200101 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200102
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200103 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
104 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200105 {
106 polarssl_free( dst->peer_cert );
107 dst->peer_cert = NULL;
108 return( ret );
109 }
110 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200111#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200112
Paul Bakkera503a632013-08-14 13:48:06 +0200113#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200114 if( src->ticket != NULL )
115 {
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500116 dst->ticket = polarssl_malloc( src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200117 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200118 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
119
120 memcpy( dst->ticket, src->ticket, src->ticket_len );
121 }
Paul Bakkera503a632013-08-14 13:48:06 +0200122#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200123
124 return( 0 );
125}
Simon Butcher149950d2016-10-15 22:08:08 +0100126#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200127
Paul Bakker05ef8352012-05-08 09:17:57 +0000128#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200129int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200130 const unsigned char *key_enc, const unsigned char *key_dec,
131 size_t keylen,
132 const unsigned char *iv_enc, const unsigned char *iv_dec,
133 size_t ivlen,
134 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200135 size_t maclen ) = NULL;
136int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
137int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
138int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
139int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
140int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200141#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000142
Paul Bakker5121ce52009-01-03 21:22:43 +0000143/*
144 * Key material generation
145 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200146#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200147static int ssl3_prf( const unsigned char *secret, size_t slen,
148 const char *label,
149 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000150 unsigned char *dstbuf, size_t dlen )
151{
152 size_t i;
153 md5_context md5;
154 sha1_context sha1;
155 unsigned char padding[16];
156 unsigned char sha1sum[20];
157 ((void)label);
158
Paul Bakker5b4af392014-06-26 12:09:34 +0200159 md5_init( &md5 );
160 sha1_init( &sha1 );
161
Paul Bakker5f70b252012-09-13 14:23:06 +0000162 /*
163 * SSLv3:
164 * block =
165 * MD5( secret + SHA1( 'A' + secret + random ) ) +
166 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
167 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
168 * ...
169 */
170 for( i = 0; i < dlen / 16; i++ )
171 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200172 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000173
174 sha1_starts( &sha1 );
175 sha1_update( &sha1, padding, 1 + i );
176 sha1_update( &sha1, secret, slen );
177 sha1_update( &sha1, random, rlen );
178 sha1_finish( &sha1, sha1sum );
179
180 md5_starts( &md5 );
181 md5_update( &md5, secret, slen );
182 md5_update( &md5, sha1sum, 20 );
183 md5_finish( &md5, dstbuf + i * 16 );
184 }
185
Paul Bakker5b4af392014-06-26 12:09:34 +0200186 md5_free( &md5 );
187 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000188
Paul Bakker34617722014-06-13 17:20:13 +0200189 polarssl_zeroize( padding, sizeof( padding ) );
190 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000191
192 return( 0 );
193}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200194#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000195
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200196#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200197static int tls1_prf( const unsigned char *secret, size_t slen,
198 const char *label,
199 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000200 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000201{
Paul Bakker23986e52011-04-24 08:57:21 +0000202 size_t nb, hs;
203 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200204 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 unsigned char tmp[128];
206 unsigned char h_i[20];
207
208 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000209 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
211 hs = ( slen + 1 ) / 2;
212 S1 = secret;
213 S2 = secret + slen - hs;
214
215 nb = strlen( label );
216 memcpy( tmp + 20, label, nb );
217 memcpy( tmp + 20 + nb, random, rlen );
218 nb += rlen;
219
220 /*
221 * First compute P_md5(secret,label+random)[0..dlen]
222 */
223 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
224
225 for( i = 0; i < dlen; i += 16 )
226 {
227 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
228 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
229
230 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
231
232 for( j = 0; j < k; j++ )
233 dstbuf[i + j] = h_i[j];
234 }
235
236 /*
237 * XOR out with P_sha1(secret,label+random)[0..dlen]
238 */
239 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
240
241 for( i = 0; i < dlen; i += 20 )
242 {
243 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
244 sha1_hmac( S2, hs, tmp, 20, tmp );
245
246 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
247
248 for( j = 0; j < k; j++ )
249 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
250 }
251
Paul Bakker34617722014-06-13 17:20:13 +0200252 polarssl_zeroize( tmp, sizeof( tmp ) );
253 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
255 return( 0 );
256}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200257#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000258
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200259#if defined(POLARSSL_SSL_PROTO_TLS1_2)
260#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200261static int tls_prf_sha256( const unsigned char *secret, size_t slen,
262 const char *label,
263 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000264 unsigned char *dstbuf, size_t dlen )
265{
266 size_t nb;
267 size_t i, j, k;
268 unsigned char tmp[128];
269 unsigned char h_i[32];
270
271 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
272 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
273
274 nb = strlen( label );
275 memcpy( tmp + 32, label, nb );
276 memcpy( tmp + 32 + nb, random, rlen );
277 nb += rlen;
278
279 /*
280 * Compute P_<hash>(secret, label + random)[0..dlen]
281 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200282 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000283
284 for( i = 0; i < dlen; i += 32 )
285 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200286 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
287 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000288
289 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
290
291 for( j = 0; j < k; j++ )
292 dstbuf[i + j] = h_i[j];
293 }
294
Paul Bakker34617722014-06-13 17:20:13 +0200295 polarssl_zeroize( tmp, sizeof( tmp ) );
296 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000297
298 return( 0 );
299}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200300#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000301
Paul Bakker9e36f042013-06-30 14:34:05 +0200302#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200303static int tls_prf_sha384( const unsigned char *secret, size_t slen,
304 const char *label,
305 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000306 unsigned char *dstbuf, size_t dlen )
307{
308 size_t nb;
309 size_t i, j, k;
310 unsigned char tmp[128];
311 unsigned char h_i[48];
312
313 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
314 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
315
316 nb = strlen( label );
317 memcpy( tmp + 48, label, nb );
318 memcpy( tmp + 48 + nb, random, rlen );
319 nb += rlen;
320
321 /*
322 * Compute P_<hash>(secret, label + random)[0..dlen]
323 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200324 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000325
326 for( i = 0; i < dlen; i += 48 )
327 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200328 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
329 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000330
331 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
332
333 for( j = 0; j < k; j++ )
334 dstbuf[i + j] = h_i[j];
335 }
336
Paul Bakker34617722014-06-13 17:20:13 +0200337 polarssl_zeroize( tmp, sizeof( tmp ) );
338 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000339
340 return( 0 );
341}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200342#endif /* POLARSSL_SHA512_C */
343#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000344
Paul Bakker66d5d072014-06-17 16:39:18 +0200345static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200346
347#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
348 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200349static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200350#endif
Paul Bakker380da532012-04-18 16:10:25 +0000351
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200353static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
354static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200355#endif
356
357#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200358static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
359static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200360#endif
361
362#if defined(POLARSSL_SSL_PROTO_TLS1_2)
363#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200364static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
365static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
366static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200367#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100368
Paul Bakker9e36f042013-06-30 14:34:05 +0200369#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200370static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
371static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
372static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100373#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200374#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000375
Paul Bakker5121ce52009-01-03 21:22:43 +0000376int ssl_derive_keys( ssl_context *ssl )
377{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200378 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 unsigned char keyblk[256];
381 unsigned char *key1;
382 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100383 unsigned char *mac_enc;
384 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200385 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100386 const cipher_info_t *cipher_info;
387 const md_info_t *md_info;
388
Paul Bakker48916f92012-09-16 19:57:18 +0000389 ssl_session *session = ssl->session_negotiate;
390 ssl_transform *transform = ssl->transform_negotiate;
391 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000392
393 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
394
Paul Bakker68884e32013-01-07 18:20:04 +0100395 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
396 if( cipher_info == NULL )
397 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200398 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100399 transform->ciphersuite_info->cipher ) );
400 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
401 }
402
403 md_info = md_info_from_type( transform->ciphersuite_info->mac );
404 if( md_info == NULL )
405 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200406 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100407 transform->ciphersuite_info->mac ) );
408 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
409 }
410
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000412 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000413 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200414#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000415 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000416 {
Paul Bakker48916f92012-09-16 19:57:18 +0000417 handshake->tls_prf = ssl3_prf;
418 handshake->calc_verify = ssl_calc_verify_ssl;
419 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000420 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200421 else
422#endif
423#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
424 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000425 {
Paul Bakker48916f92012-09-16 19:57:18 +0000426 handshake->tls_prf = tls1_prf;
427 handshake->calc_verify = ssl_calc_verify_tls;
428 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000429 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200430 else
431#endif
432#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200433#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200434 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
435 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000436 {
Paul Bakker48916f92012-09-16 19:57:18 +0000437 handshake->tls_prf = tls_prf_sha384;
438 handshake->calc_verify = ssl_calc_verify_tls_sha384;
439 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000440 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000441 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200442#endif
443#if defined(POLARSSL_SHA256_C)
444 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000445 {
Paul Bakker48916f92012-09-16 19:57:18 +0000446 handshake->tls_prf = tls_prf_sha256;
447 handshake->calc_verify = ssl_calc_verify_tls_sha256;
448 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000449 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200450 else
451#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200452#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200453 {
454 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200455 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200456 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000457
458 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 * SSLv3:
460 * master =
461 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
462 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
463 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200464 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200465 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 * master = PRF( premaster, "master secret", randbytes )[0..47]
467 */
Paul Bakker0a597072012-09-25 21:55:46 +0000468 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 {
Paul Bakker48916f92012-09-16 19:57:18 +0000470 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
471 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200473#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
474 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200475 {
476 unsigned char session_hash[48];
477 size_t hash_len;
478
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200479 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200480
481 ssl->handshake->calc_verify( ssl, session_hash );
482
483#if defined(POLARSSL_SSL_PROTO_TLS1_2)
484 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
485 {
486#if defined(POLARSSL_SHA512_C)
487 if( ssl->transform_negotiate->ciphersuite_info->mac ==
488 POLARSSL_MD_SHA384 )
489 {
490 hash_len = 48;
491 }
492 else
493#endif
494 hash_len = 32;
495 }
496 else
497#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
498 hash_len = 36;
499
500 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
501
502 handshake->tls_prf( handshake->premaster, handshake->pmslen,
503 "extended master secret",
504 session_hash, hash_len, session->master, 48 );
505
506 }
507 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200508#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000509 handshake->tls_prf( handshake->premaster, handshake->pmslen,
510 "master secret",
511 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200513
Paul Bakker34617722014-06-13 17:20:13 +0200514 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 }
516 else
517 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
518
519 /*
520 * Swap the client and server random values.
521 */
Paul Bakker48916f92012-09-16 19:57:18 +0000522 memcpy( tmp, handshake->randbytes, 64 );
523 memcpy( handshake->randbytes, tmp + 32, 32 );
524 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200525 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527 /*
528 * SSLv3:
529 * key block =
530 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
531 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
532 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
533 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
534 * ...
535 *
536 * TLSv1:
537 * key block = PRF( master, "key expansion", randbytes )
538 */
Paul Bakker48916f92012-09-16 19:57:18 +0000539 handshake->tls_prf( session->master, 48, "key expansion",
540 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000541
Paul Bakker48916f92012-09-16 19:57:18 +0000542 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
543 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
544 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
545 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
547
Paul Bakker34617722014-06-13 17:20:13 +0200548 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
550 /*
551 * Determine the appropriate key, IV and MAC length.
552 */
Paul Bakker68884e32013-01-07 18:20:04 +0100553
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200554 transform->keylen = cipher_info->key_length / 8;
555
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200556 if( cipher_info->mode == POLARSSL_MODE_GCM ||
557 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200559 transform->maclen = 0;
560
Paul Bakker68884e32013-01-07 18:20:04 +0100561 transform->ivlen = 12;
562 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200563
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200564 /* Minimum length is expicit IV + tag */
565 transform->minlen = transform->ivlen - transform->fixed_ivlen
566 + ( transform->ciphersuite_info->flags &
567 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100568 }
569 else
570 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200571 /* Initialize HMAC contexts */
572 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
573 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100574 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200575 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
576 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100577 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200579 /* Get MAC length */
580 transform->maclen = md_get_size( md_info );
581
582#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
583 /*
584 * If HMAC is to be truncated, we shall keep the leftmost bytes,
585 * (rfc 6066 page 13 or rfc 2104 section 4),
586 * so we only need to adjust the length here.
587 */
588 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
589 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
590#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
591
592 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100593 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200595 /* Minimum length */
596 if( cipher_info->mode == POLARSSL_MODE_STREAM )
597 transform->minlen = transform->maclen;
598 else
Paul Bakker68884e32013-01-07 18:20:04 +0100599 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200600 /*
601 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100602 * 1. if EtM is in use: one block plus MAC
603 * otherwise: * first multiple of blocklen greater than maclen
604 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200605 */
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100606#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
607 if( session->encrypt_then_mac == SSL_ETM_ENABLED )
608 {
609 transform->minlen = transform->maclen
610 + cipher_info->block_size;
611 }
612 else
613#endif
614 {
615 transform->minlen = transform->maclen
616 + cipher_info->block_size
617 - transform->maclen % cipher_info->block_size;
618 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200619
620#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
621 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
622 ssl->minor_ver == SSL_MINOR_VERSION_1 )
623 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100624 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200625#endif
626#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
627 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
628 ssl->minor_ver == SSL_MINOR_VERSION_3 )
629 {
630 transform->minlen += transform->ivlen;
631 }
632 else
633#endif
634 {
635 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
636 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
637 }
Paul Bakker68884e32013-01-07 18:20:04 +0100638 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 }
640
641 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000642 transform->keylen, transform->minlen, transform->ivlen,
643 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000644
645 /*
646 * Finally setup the cipher contexts, IVs and MAC secrets.
647 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100648#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000649 if( ssl->endpoint == SSL_IS_CLIENT )
650 {
Paul Bakker48916f92012-09-16 19:57:18 +0000651 key1 = keyblk + transform->maclen * 2;
652 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000653
Paul Bakker68884e32013-01-07 18:20:04 +0100654 mac_enc = keyblk;
655 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000656
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000657 /*
658 * This is not used in TLS v1.1.
659 */
Paul Bakker48916f92012-09-16 19:57:18 +0000660 iv_copy_len = ( transform->fixed_ivlen ) ?
661 transform->fixed_ivlen : transform->ivlen;
662 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
663 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000664 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000665 }
666 else
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100667#endif /* POLARSSL_SSL_CLI_C */
668#if defined(POLARSSL_SSL_SRV_C)
669 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000670 {
Paul Bakker48916f92012-09-16 19:57:18 +0000671 key1 = keyblk + transform->maclen * 2 + transform->keylen;
672 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000673
Paul Bakker68884e32013-01-07 18:20:04 +0100674 mac_enc = keyblk + transform->maclen;
675 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000676
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000677 /*
678 * This is not used in TLS v1.1.
679 */
Paul Bakker48916f92012-09-16 19:57:18 +0000680 iv_copy_len = ( transform->fixed_ivlen ) ?
681 transform->fixed_ivlen : transform->ivlen;
682 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
683 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000684 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100686 else
687#endif /* POLARSSL_SSL_SRV_C */
688 {
689 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
690 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
691 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000692
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200693#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100694 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
695 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100696 if( transform->maclen > sizeof transform->mac_enc )
697 {
698 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200699 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100700 }
701
Paul Bakker68884e32013-01-07 18:20:04 +0100702 memcpy( transform->mac_enc, mac_enc, transform->maclen );
703 memcpy( transform->mac_dec, mac_dec, transform->maclen );
704 }
705 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200706#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200707#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
708 defined(POLARSSL_SSL_PROTO_TLS1_2)
709 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100710 {
711 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
712 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
713 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200714 else
715#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200716 {
717 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200718 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200719 }
Paul Bakker68884e32013-01-07 18:20:04 +0100720
Paul Bakker05ef8352012-05-08 09:17:57 +0000721#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200722 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000723 {
724 int ret = 0;
725
726 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
727
Paul Bakker07eb38b2012-12-19 14:42:06 +0100728 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
729 transform->iv_enc, transform->iv_dec,
730 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100731 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100732 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000733 {
734 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200735 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000736 }
737 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200738#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000739
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200740 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
741 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000742 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200743 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
744 return( ret );
745 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200746
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200747 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
748 cipher_info ) ) != 0 )
749 {
750 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
751 return( ret );
752 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200753
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200754 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
755 cipher_info->key_length,
756 POLARSSL_ENCRYPT ) ) != 0 )
757 {
758 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
759 return( ret );
760 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200761
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200762 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
763 cipher_info->key_length,
764 POLARSSL_DECRYPT ) ) != 0 )
765 {
766 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
767 return( ret );
768 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200769
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200770#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200771 if( cipher_info->mode == POLARSSL_MODE_CBC )
772 {
773 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
774 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200775 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200776 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
777 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200778 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200779
780 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
781 POLARSSL_PADDING_NONE ) ) != 0 )
782 {
783 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
784 return( ret );
785 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000786 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200787#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000788
Paul Bakker34617722014-06-13 17:20:13 +0200789 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000790
Paul Bakker2770fbd2012-07-03 13:30:23 +0000791#if defined(POLARSSL_ZLIB_SUPPORT)
792 // Initialize compression
793 //
Paul Bakker48916f92012-09-16 19:57:18 +0000794 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000795 {
Paul Bakker16770332013-10-11 09:59:44 +0200796 if( ssl->compress_buf == NULL )
797 {
798 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
799 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
800 if( ssl->compress_buf == NULL )
801 {
802 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
803 SSL_BUFFER_LEN ) );
804 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
805 }
806 }
807
Paul Bakker2770fbd2012-07-03 13:30:23 +0000808 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
809
Paul Bakker48916f92012-09-16 19:57:18 +0000810 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
811 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000812
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200813 if( deflateInit( &transform->ctx_deflate,
814 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000815 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000816 {
817 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
818 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
819 }
820 }
821#endif /* POLARSSL_ZLIB_SUPPORT */
822
Paul Bakker5121ce52009-01-03 21:22:43 +0000823 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
824
825 return( 0 );
826}
827
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200828#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000829void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000830{
831 md5_context md5;
832 sha1_context sha1;
833 unsigned char pad_1[48];
834 unsigned char pad_2[48];
835
Paul Bakker380da532012-04-18 16:10:25 +0000836 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000837
Paul Bakker48916f92012-09-16 19:57:18 +0000838 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
839 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000840
Paul Bakker380da532012-04-18 16:10:25 +0000841 memset( pad_1, 0x36, 48 );
842 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000843
Paul Bakker48916f92012-09-16 19:57:18 +0000844 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000845 md5_update( &md5, pad_1, 48 );
846 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000847
Paul Bakker380da532012-04-18 16:10:25 +0000848 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000849 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000850 md5_update( &md5, pad_2, 48 );
851 md5_update( &md5, hash, 16 );
852 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000853
Paul Bakker48916f92012-09-16 19:57:18 +0000854 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000855 sha1_update( &sha1, pad_1, 40 );
856 sha1_finish( &sha1, hash + 16 );
857
858 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000859 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000860 sha1_update( &sha1, pad_2, 40 );
861 sha1_update( &sha1, hash + 16, 20 );
862 sha1_finish( &sha1, hash + 16 );
863
864 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
865 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
866
Paul Bakker5b4af392014-06-26 12:09:34 +0200867 md5_free( &md5 );
868 sha1_free( &sha1 );
869
Paul Bakker380da532012-04-18 16:10:25 +0000870 return;
871}
Paul Bakker9af723c2014-05-01 13:03:14 +0200872#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000873
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200874#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000875void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
876{
877 md5_context md5;
878 sha1_context sha1;
879
880 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
881
Paul Bakker48916f92012-09-16 19:57:18 +0000882 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
883 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000884
Paul Bakker48916f92012-09-16 19:57:18 +0000885 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000886 sha1_finish( &sha1, hash + 16 );
887
888 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
889 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
890
Paul Bakker5b4af392014-06-26 12:09:34 +0200891 md5_free( &md5 );
892 sha1_free( &sha1 );
893
Paul Bakker380da532012-04-18 16:10:25 +0000894 return;
895}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200896#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000897
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200898#if defined(POLARSSL_SSL_PROTO_TLS1_2)
899#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000900void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
901{
Paul Bakker9e36f042013-06-30 14:34:05 +0200902 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000903
904 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
905
Paul Bakker9e36f042013-06-30 14:34:05 +0200906 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
907 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000908
909 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
910 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
911
Paul Bakker5b4af392014-06-26 12:09:34 +0200912 sha256_free( &sha256 );
913
Paul Bakker380da532012-04-18 16:10:25 +0000914 return;
915}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200916#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000917
Paul Bakker9e36f042013-06-30 14:34:05 +0200918#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000919void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
920{
Paul Bakker9e36f042013-06-30 14:34:05 +0200921 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000922
923 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
924
Paul Bakker9e36f042013-06-30 14:34:05 +0200925 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
926 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000927
Paul Bakkerca4ab492012-04-18 14:23:57 +0000928 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
930
Paul Bakker5b4af392014-06-26 12:09:34 +0200931 sha512_free( &sha512 );
932
Paul Bakker5121ce52009-01-03 21:22:43 +0000933 return;
934}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200935#endif /* POLARSSL_SHA512_C */
936#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000937
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200938#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200939int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
940{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200941 unsigned char *p = ssl->handshake->premaster;
942 unsigned char *end = p + sizeof( ssl->handshake->premaster );
943
944 /*
945 * PMS = struct {
946 * opaque other_secret<0..2^16-1>;
947 * opaque psk<0..2^16-1>;
948 * };
949 * with "other_secret" depending on the particular key exchange
950 */
951#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
952 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
953 {
Manuel Pégourié-Gonnard5ca36402015-10-21 12:35:29 +0200954 if( end - p < 2 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200955 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
956
957 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
958 *(p++) = (unsigned char)( ssl->psk_len );
Manuel Pégourié-Gonnard5ca36402015-10-21 12:35:29 +0200959
960 if( end < p || (size_t)( end - p ) < ssl->psk_len )
961 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
962
963 memset( p, 0, ssl->psk_len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200964 p += ssl->psk_len;
965 }
966 else
967#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200968#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
969 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
970 {
971 /*
972 * other_secret already set by the ClientKeyExchange message,
973 * and is 48 bytes long
974 */
975 *p++ = 0;
976 *p++ = 48;
977 p += 48;
978 }
979 else
980#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200981#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
982 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
983 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200984 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200985 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200986
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200987 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200988 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200989 p + 2, &len,
990 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200991 {
992 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
993 return( ret );
994 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200995 *(p++) = (unsigned char)( len >> 8 );
996 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200997 p += len;
998
999 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1000 }
1001 else
1002#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1003#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1004 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1005 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001006 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001007 size_t zlen;
1008
1009 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001010 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001011 ssl->f_rng, ssl->p_rng ) ) != 0 )
1012 {
1013 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1014 return( ret );
1015 }
1016
1017 *(p++) = (unsigned char)( zlen >> 8 );
1018 *(p++) = (unsigned char)( zlen );
1019 p += zlen;
1020
1021 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1022 }
1023 else
1024#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1025 {
1026 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001027 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001028 }
1029
1030 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnard5ca36402015-10-21 12:35:29 +02001031 if( end - p < 2 )
1032 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001033
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001034 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1035 *(p++) = (unsigned char)( ssl->psk_len );
Manuel Pégourié-Gonnard5ca36402015-10-21 12:35:29 +02001036
1037 if( end < p || (size_t)( end - p ) < ssl->psk_len )
1038 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1039
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001040 memcpy( p, ssl->psk, ssl->psk_len );
1041 p += ssl->psk_len;
1042
1043 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1044
1045 return( 0 );
1046}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001047#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001048
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001049#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001050/*
1051 * SSLv3.0 MAC functions
1052 */
Manuel Pégourié-Gonnard3ea75b32017-12-18 18:04:59 +01001053static void ssl_mac( md_context_t *md_ctx,
1054 const unsigned char *secret,
1055 const unsigned char *buf, size_t len,
1056 const unsigned char *ctr, int type,
1057 unsigned char out[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001058{
1059 unsigned char header[11];
1060 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001061 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001062 int md_size = md_get_size( md_ctx->md_info );
1063 int md_type = md_get_type( md_ctx->md_info );
1064
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001065 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001066 if( md_type == POLARSSL_MD_MD5 )
1067 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001068 else
Paul Bakker68884e32013-01-07 18:20:04 +01001069 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001070
1071 memcpy( header, ctr, 8 );
1072 header[ 8] = (unsigned char) type;
1073 header[ 9] = (unsigned char)( len >> 8 );
1074 header[10] = (unsigned char)( len );
1075
Paul Bakker68884e32013-01-07 18:20:04 +01001076 memset( padding, 0x36, padlen );
1077 md_starts( md_ctx );
1078 md_update( md_ctx, secret, md_size );
1079 md_update( md_ctx, padding, padlen );
1080 md_update( md_ctx, header, 11 );
1081 md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard3ea75b32017-12-18 18:04:59 +01001082 md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001083
Paul Bakker68884e32013-01-07 18:20:04 +01001084 memset( padding, 0x5C, padlen );
1085 md_starts( md_ctx );
1086 md_update( md_ctx, secret, md_size );
1087 md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard3ea75b32017-12-18 18:04:59 +01001088 md_update( md_ctx, out, md_size );
1089 md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001090}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001091#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001092
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001093#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1094 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1095 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1096#define POLARSSL_SOME_MODES_USE_MAC
1097#endif
1098
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001099/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001100 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001101 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001102static int ssl_encrypt_buf( ssl_context *ssl )
1103{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001104 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001105 cipher_mode_t mode;
1106 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001107
1108 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1109
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001110 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1111 {
1112 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1113 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1114 }
1115
1116 mode = cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
1117
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001118 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1119 ssl->out_msg, ssl->out_msglen );
1120
Paul Bakker5121ce52009-01-03 21:22:43 +00001121 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001122 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001123 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001124#if defined(POLARSSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001125 if( mode == POLARSSL_MODE_STREAM ||
1126 ( mode == POLARSSL_MODE_CBC
1127#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1128 && ssl->session_out->encrypt_then_mac == SSL_ETM_DISABLED
1129#endif
1130 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001131 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001132#if defined(POLARSSL_SSL_PROTO_SSL3)
1133 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1134 {
Manuel Pégourié-Gonnard3ea75b32017-12-18 18:04:59 +01001135 unsigned char mac[20]; /* SHA-1 at most */
1136
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001137 ssl_mac( &ssl->transform_out->md_ctx_enc,
1138 ssl->transform_out->mac_enc,
1139 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard3ea75b32017-12-18 18:04:59 +01001140 ssl->out_ctr, ssl->out_msgtype,
1141 mac );
1142
1143 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001144 }
1145 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001146#endif
1147#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001148 defined(POLARSSL_SSL_PROTO_TLS1_2)
1149 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1150 {
Hanno Becker251bab52017-11-20 10:30:08 +00001151 unsigned char mac[SSL_MAC_ADD];
1152
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001153 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1154 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1155 ssl->out_msg, ssl->out_msglen );
Hanno Becker251bab52017-11-20 10:30:08 +00001156 md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001157 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker251bab52017-11-20 10:30:08 +00001158
1159 memcpy( ssl->out_msg + ssl->out_msglen, mac,
1160 ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001161 }
1162 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001163#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001164 {
1165 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001166 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001167 }
1168
Hanno Becker0a139f92017-11-21 17:41:59 +00001169 SSL_DEBUG_BUF( 4, "expected mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001170 ssl->out_msg + ssl->out_msglen,
1171 ssl->transform_out->maclen );
1172
1173 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001174 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001175 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001176#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001177
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001178 /*
1179 * Encrypt
1180 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001181#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001182 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001183 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001184 int ret;
1185 size_t olen = 0;
1186
Paul Bakker5121ce52009-01-03 21:22:43 +00001187 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1188 "including %d bytes of padding",
1189 ssl->out_msglen, 0 ) );
1190
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001191 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001192 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001193 ssl->transform_out->ivlen,
1194 ssl->out_msg, ssl->out_msglen,
1195 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001196 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001197 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001198 return( ret );
1199 }
1200
1201 if( ssl->out_msglen != olen )
1202 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001203 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001204 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001205 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 }
Paul Bakker68884e32013-01-07 18:20:04 +01001207 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001208#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001209#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1210 if( mode == POLARSSL_MODE_GCM ||
1211 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001212 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001213 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001214 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001215 unsigned char *enc_msg;
1216 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001217 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1218 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001219
Paul Bakkerca4ab492012-04-18 14:23:57 +00001220 memcpy( add_data, ssl->out_ctr, 8 );
1221 add_data[8] = ssl->out_msgtype;
1222 add_data[9] = ssl->major_ver;
1223 add_data[10] = ssl->minor_ver;
1224 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1225 add_data[12] = ssl->out_msglen & 0xFF;
1226
1227 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1228 add_data, 13 );
1229
Paul Bakker68884e32013-01-07 18:20:04 +01001230 /*
1231 * Generate IV
1232 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001233 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1234 {
1235 /* Reminder if we ever add an AEAD mode with a different size */
1236 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1237 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1238 }
1239
1240 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1241 ssl->out_ctr, 8 );
1242 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001243
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001244 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001245 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001246
Paul Bakker68884e32013-01-07 18:20:04 +01001247 /*
1248 * Fix pointer positions and message length with added IV
1249 */
1250 enc_msg = ssl->out_msg;
1251 enc_msglen = ssl->out_msglen;
1252 ssl->out_msglen += ssl->transform_out->ivlen -
1253 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001254
Paul Bakker68884e32013-01-07 18:20:04 +01001255 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1256 "including %d bytes of padding",
1257 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001258
Paul Bakker68884e32013-01-07 18:20:04 +01001259 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001260 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001261 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001262 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1263 ssl->transform_out->iv_enc,
1264 ssl->transform_out->ivlen,
1265 add_data, 13,
1266 enc_msg, enc_msglen,
1267 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001268 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001269 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001270 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001271 return( ret );
1272 }
1273
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001274 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001275 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001276 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001277 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001278 }
1279
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001280 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001281 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001282
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001283 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001284 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001285 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001286#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001287#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1288 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001289 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001290 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001291 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001292 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001293 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001294
Paul Bakker48916f92012-09-16 19:57:18 +00001295 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1296 ssl->transform_out->ivlen;
1297 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001298 padlen = 0;
1299
1300 for( i = 0; i <= padlen; i++ )
1301 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1302
1303 ssl->out_msglen += padlen + 1;
1304
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001305 enc_msglen = ssl->out_msglen;
1306 enc_msg = ssl->out_msg;
1307
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001308#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001309 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001310 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1311 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001312 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001313 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001314 {
1315 /*
1316 * Generate IV
1317 */
Manuel Pégourié-Gonnarda67fd792015-08-27 12:02:40 +02001318 ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001319 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001320 if( ret != 0 )
1321 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001322
Paul Bakker92be97b2013-01-02 17:30:03 +01001323 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001324 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001325
1326 /*
1327 * Fix pointer positions and message length with added IV
1328 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001329 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001330 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001331 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001332 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001333#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001334
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001336 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001337 ssl->out_msglen, ssl->transform_out->ivlen,
1338 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001339
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001340 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001341 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001342 ssl->transform_out->ivlen,
1343 enc_msg, enc_msglen,
1344 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001345 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001346 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001347 return( ret );
1348 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001349
Paul Bakkercca5b812013-08-31 17:40:26 +02001350 if( enc_msglen != olen )
1351 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001352 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001353 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001354 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001355
1356#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001357 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1358 {
1359 /*
1360 * Save IV in SSL3 and TLS1
1361 */
1362 memcpy( ssl->transform_out->iv_enc,
1363 ssl->transform_out->cipher_ctx_enc.iv,
1364 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001365 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001366#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001367
1368#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001369 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001370 {
1371 /*
1372 * MAC(MAC_write_key, seq_num +
1373 * TLSCipherText.type +
1374 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001375 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001376 * IV + // except for TLS 1.0
1377 * ENC(content + padding + padding_length));
1378 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001379 unsigned char pseudo_hdr[13];
1380
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001381 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1382
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001383 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1384 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001385 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1386 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1387
1388 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001389
1390 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1391 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1392 ssl->out_iv, ssl->out_msglen );
1393 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1394 ssl->out_iv + ssl->out_msglen );
1395 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1396
1397 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001398 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001399 }
1400#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001401 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001402 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001403#endif /* POLARSSL_CIPHER_MODE_CBC &&
1404 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001405 {
1406 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001407 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001408 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001409
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001410 /* Make extra sure authentication was performed, exactly once */
1411 if( auth_done != 1 )
1412 {
1413 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1414 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1415 }
1416
Paul Bakkerca4ab492012-04-18 14:23:57 +00001417 for( i = 8; i > 0; i-- )
1418 if( ++ssl->out_ctr[i - 1] != 0 )
1419 break;
1420
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001421 /* The loops goes to its end iff the counter is wrapping */
1422 if( i == 0 )
1423 {
1424 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1425 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1426 }
1427
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1429
1430 return( 0 );
1431}
1432
1433static int ssl_decrypt_buf( ssl_context *ssl )
1434{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001435 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001436 cipher_mode_t mode;
1437 int auth_done = 0;
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001438#if defined(POLARSSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001439 size_t padlen = 0, correct = 1;
1440#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001441
1442 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1443
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001444 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1445 {
1446 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1447 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1448 }
1449
1450 mode = cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
1451
Paul Bakker48916f92012-09-16 19:57:18 +00001452 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001453 {
1454 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001455 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001456 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001457 }
1458
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001459#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001460 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001461 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001462 int ret;
1463 size_t olen = 0;
1464
Paul Bakker68884e32013-01-07 18:20:04 +01001465 padlen = 0;
1466
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001467 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001468 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001469 ssl->transform_in->ivlen,
1470 ssl->in_msg, ssl->in_msglen,
1471 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001472 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001473 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001474 return( ret );
1475 }
1476
1477 if( ssl->in_msglen != olen )
1478 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001479 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001480 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001481 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001482 }
Paul Bakker68884e32013-01-07 18:20:04 +01001483 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001484#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001485#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1486 if( mode == POLARSSL_MODE_GCM ||
1487 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001488 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001489 int ret;
1490 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001491 unsigned char *dec_msg;
1492 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001493 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001494 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1495 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Simon Ba697bf52016-11-10 13:19:42 +00001496 size_t explicit_iv_len = ssl->transform_in->ivlen -
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001497 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001498
Manuel Pégourié-Gonnard06d75192015-02-11 14:54:11 +00001499 if( ssl->in_msglen < (size_t) explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001500 {
1501 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1502 "+ taglen (%d)", ssl->in_msglen,
1503 explicit_iv_len, taglen ) );
1504 return( POLARSSL_ERR_SSL_INVALID_MAC );
1505 }
1506 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1507
Paul Bakker68884e32013-01-07 18:20:04 +01001508 dec_msg = ssl->in_msg;
1509 dec_msg_result = ssl->in_msg;
1510 ssl->in_msglen = dec_msglen;
1511
1512 memcpy( add_data, ssl->in_ctr, 8 );
1513 add_data[8] = ssl->in_msgtype;
1514 add_data[9] = ssl->major_ver;
1515 add_data[10] = ssl->minor_ver;
1516 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1517 add_data[12] = ssl->in_msglen & 0xFF;
1518
1519 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1520 add_data, 13 );
1521
1522 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1523 ssl->in_iv,
1524 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1525
1526 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1527 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001528 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001529
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001530 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001531 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001532 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001533 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1534 ssl->transform_in->iv_dec,
1535 ssl->transform_in->ivlen,
1536 add_data, 13,
1537 dec_msg, dec_msglen,
1538 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001539 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001540 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001541 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1542
1543 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1544 return( POLARSSL_ERR_SSL_INVALID_MAC );
1545
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001546 return( ret );
1547 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001548 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001549
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001550 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001551 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001552 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001553 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001554 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001555 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001556 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001557#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001558#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1559 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001560 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001561 {
Paul Bakker45829992013-01-03 14:52:21 +01001562 /*
1563 * Decrypt and check the padding
1564 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001565 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001566 unsigned char *dec_msg;
1567 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001568 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001569 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001570 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001571
Paul Bakker5121ce52009-01-03 21:22:43 +00001572 /*
Paul Bakker45829992013-01-03 14:52:21 +01001573 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001574 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001575#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001576 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1577 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001578#endif
Paul Bakker45829992013-01-03 14:52:21 +01001579
1580 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1581 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1582 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001583 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1584 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1585 ssl->transform_in->ivlen,
1586 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001587 return( POLARSSL_ERR_SSL_INVALID_MAC );
1588 }
1589
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001590 dec_msglen = ssl->in_msglen;
1591 dec_msg = ssl->in_msg;
1592 dec_msg_result = ssl->in_msg;
1593
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001594 /*
1595 * Authenticate before decrypt if enabled
1596 */
1597#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001598 if( ssl->session_in->encrypt_then_mac == SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001599 {
Hanno Becker251bab52017-11-20 10:30:08 +00001600 unsigned char mac_expect[SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001601 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001602
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001603 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1604
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001605 dec_msglen -= ssl->transform_in->maclen;
1606 ssl->in_msglen -= ssl->transform_in->maclen;
1607
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001608 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1609 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1610 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1611 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1612
1613 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1614
1615 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001616 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1617 ssl->in_iv, ssl->in_msglen );
Hanno Becker251bab52017-11-20 10:30:08 +00001618 md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001619 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1620
1621 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1622 ssl->transform_in->maclen );
Hanno Becker0a139f92017-11-21 17:41:59 +00001623 SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001624 ssl->transform_in->maclen );
1625
Hanno Becker251bab52017-11-20 10:30:08 +00001626 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001627 ssl->transform_in->maclen ) != 0 )
1628 {
1629 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1630
1631 return( POLARSSL_ERR_SSL_INVALID_MAC );
1632 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001633 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001634 }
1635#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1636
1637 /*
1638 * Check length sanity
1639 */
1640 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1641 {
1642 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1643 ssl->in_msglen, ssl->transform_in->ivlen ) );
1644 return( POLARSSL_ERR_SSL_INVALID_MAC );
1645 }
1646
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001647#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001648 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001649 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001650 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001651 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001652 {
Paul Bakker48916f92012-09-16 19:57:18 +00001653 dec_msglen -= ssl->transform_in->ivlen;
1654 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001655
Paul Bakker48916f92012-09-16 19:57:18 +00001656 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001657 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001658 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001659#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001660
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001661 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001662 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001663 ssl->transform_in->ivlen,
1664 dec_msg, dec_msglen,
1665 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001666 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001667 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001668 return( ret );
1669 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001670
Paul Bakkercca5b812013-08-31 17:40:26 +02001671 if( dec_msglen != olen )
1672 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001673 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001674 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001675 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001676
1677#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001678 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1679 {
1680 /*
1681 * Save IV in SSL3 and TLS1
1682 */
1683 memcpy( ssl->transform_in->iv_dec,
1684 ssl->transform_in->cipher_ctx_dec.iv,
1685 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001686 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001687#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001688
1689 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001690
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001691 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001692 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001693 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001694#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001695 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1696 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001697#endif
Paul Bakker45829992013-01-03 14:52:21 +01001698 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001699 correct = 0;
1700 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001701
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001702#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001703 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1704 {
Paul Bakker48916f92012-09-16 19:57:18 +00001705 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001706 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001707#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001708 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1709 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001710 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001711#endif
Paul Bakker45829992013-01-03 14:52:21 +01001712 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001713 }
1714 }
1715 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001716#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001717#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1718 defined(POLARSSL_SSL_PROTO_TLS1_2)
1719 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001720 {
1721 /*
Paul Bakker45829992013-01-03 14:52:21 +01001722 * TLSv1+: always check the padding up to the first failure
1723 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001724 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001725 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001726 size_t padding_idx = ssl->in_msglen - padlen - 1;
1727
Paul Bakker956c9e02013-12-19 14:42:28 +01001728 /*
1729 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001730 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001731 *
Paul Bakker61885c72014-04-25 12:59:03 +02001732 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1733 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001734 *
1735 * In both cases we reset padding_idx to a safe value (0) to
1736 * prevent out-of-buffer reads.
1737 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001738 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001739 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1740 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001741
1742 padding_idx *= correct;
1743
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001744 for( i = 1; i <= 256; i++ )
1745 {
1746 real_count &= ( i <= padlen );
1747 pad_count += real_count *
1748 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1749 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001750
1751 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001752
Paul Bakkerd66f0702013-01-31 16:57:45 +01001753#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001754 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001755 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001756#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001757 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001758 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001759 else
1760#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1761 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001762 {
1763 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001764 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001765 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001766
1767 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001768 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001769 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001770#endif /* POLARSSL_CIPHER_MODE_CBC &&
1771 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001772 {
1773 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001774 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001775 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001776
1777 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1778 ssl->in_msg, ssl->in_msglen );
1779
1780 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001781 * Authenticate if not done yet.
1782 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001783 */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001784#if defined(POLARSSL_SOME_MODES_USE_MAC)
1785 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001786 {
Hanno Becker251bab52017-11-20 10:30:08 +00001787 unsigned char mac_expect[SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01001788
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001789 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001790
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001791 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1792 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001793
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001794#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001795 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1796 {
1797 ssl_mac( &ssl->transform_in->md_ctx_dec,
1798 ssl->transform_in->mac_dec,
1799 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard3ea75b32017-12-18 18:04:59 +01001800 ssl->in_ctr, ssl->in_msgtype,
1801 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001802 }
1803 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001804#endif /* POLARSSL_SSL_PROTO_SSL3 */
1805#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001806 defined(POLARSSL_SSL_PROTO_TLS1_2)
1807 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1808 {
1809 /*
1810 * Process MAC and always update for padlen afterwards to make
1811 * total time independent of padlen
1812 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001813 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001814 *
1815 * Known timing attacks:
1816 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1817 *
1818 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1819 * correctly. (We round down instead of up, so -56 is the correct
1820 * value for our calculations instead of -55)
1821 */
1822 size_t j, extra_run = 0;
1823 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1824 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001825
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001826 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001827
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001828 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1829 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1830 ssl->in_msglen );
Hanno Becker251bab52017-11-20 10:30:08 +00001831 md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
1832
Manuel Pégourié-Gonnard7d1e95c2015-04-29 01:35:48 +02001833 /* Call md_process at least once due to cache attacks */
1834 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001835 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001836
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001837 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1838 }
1839 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001840#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001841 POLARSSL_SSL_PROTO_TLS1_2 */
1842 {
1843 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001844 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001845 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001846
Hanno Becker251bab52017-11-20 10:30:08 +00001847 SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
1848 SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001849 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001850
Hanno Becker251bab52017-11-20 10:30:08 +00001851 if( safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001852 ssl->transform_in->maclen ) != 0 )
1853 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001854#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001855 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001856#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001857 correct = 0;
1858 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001859 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001860
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001861 /*
1862 * Finally check the correct flag
1863 */
1864 if( correct == 0 )
1865 return( POLARSSL_ERR_SSL_INVALID_MAC );
1866 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001867#endif /* POLARSSL_SOME_MODES_USE_MAC */
1868
1869 /* Make extra sure authentication was performed, exactly once */
1870 if( auth_done != 1 )
1871 {
1872 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1873 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1874 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001875
1876 if( ssl->in_msglen == 0 )
1877 {
1878 ssl->nb_zero++;
1879
1880 /*
1881 * Three or more empty messages may be a DoS attack
1882 * (excessive CPU consumption).
1883 */
1884 if( ssl->nb_zero > 3 )
1885 {
1886 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1887 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001888 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001889 }
1890 }
1891 else
1892 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001893
Paul Bakker23986e52011-04-24 08:57:21 +00001894 for( i = 8; i > 0; i-- )
1895 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001896 break;
1897
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001898 /* The loops goes to its end iff the counter is wrapping */
1899 if( i == 0 )
1900 {
1901 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1902 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1903 }
1904
Paul Bakker5121ce52009-01-03 21:22:43 +00001905 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1906
1907 return( 0 );
1908}
1909
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001910#undef MAC_NONE
1911#undef MAC_PLAINTEXT
1912#undef MAC_CIPHERTEXT
1913
Paul Bakker2770fbd2012-07-03 13:30:23 +00001914#if defined(POLARSSL_ZLIB_SUPPORT)
1915/*
1916 * Compression/decompression functions
1917 */
1918static int ssl_compress_buf( ssl_context *ssl )
1919{
1920 int ret;
1921 unsigned char *msg_post = ssl->out_msg;
1922 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001923 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001924
1925 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1926
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001927 if( len_pre == 0 )
1928 return( 0 );
1929
Paul Bakker2770fbd2012-07-03 13:30:23 +00001930 memcpy( msg_pre, ssl->out_msg, len_pre );
1931
1932 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1933 ssl->out_msglen ) );
1934
1935 SSL_DEBUG_BUF( 4, "before compression: output payload",
1936 ssl->out_msg, ssl->out_msglen );
1937
Paul Bakker48916f92012-09-16 19:57:18 +00001938 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1939 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1940 ssl->transform_out->ctx_deflate.next_out = msg_post;
1941 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001942
Paul Bakker48916f92012-09-16 19:57:18 +00001943 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001944 if( ret != Z_OK )
1945 {
1946 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1947 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1948 }
1949
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001950 ssl->out_msglen = SSL_BUFFER_LEN -
1951 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001952
Paul Bakker2770fbd2012-07-03 13:30:23 +00001953 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1954 ssl->out_msglen ) );
1955
1956 SSL_DEBUG_BUF( 4, "after compression: output payload",
1957 ssl->out_msg, ssl->out_msglen );
1958
1959 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1960
1961 return( 0 );
1962}
1963
1964static int ssl_decompress_buf( ssl_context *ssl )
1965{
1966 int ret;
1967 unsigned char *msg_post = ssl->in_msg;
1968 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001969 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001970
1971 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1972
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001973 if( len_pre == 0 )
1974 return( 0 );
1975
Paul Bakker2770fbd2012-07-03 13:30:23 +00001976 memcpy( msg_pre, ssl->in_msg, len_pre );
1977
1978 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1979 ssl->in_msglen ) );
1980
1981 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1982 ssl->in_msg, ssl->in_msglen );
1983
Paul Bakker48916f92012-09-16 19:57:18 +00001984 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1985 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1986 ssl->transform_in->ctx_inflate.next_out = msg_post;
1987 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001988
Paul Bakker48916f92012-09-16 19:57:18 +00001989 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001990 if( ret != Z_OK )
1991 {
1992 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1993 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1994 }
1995
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001996 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1997 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001998
Paul Bakker2770fbd2012-07-03 13:30:23 +00001999 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
2000 ssl->in_msglen ) );
2001
2002 SSL_DEBUG_BUF( 4, "after decompression: input payload",
2003 ssl->in_msg, ssl->in_msglen );
2004
2005 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
2006
2007 return( 0 );
2008}
2009#endif /* POLARSSL_ZLIB_SUPPORT */
2010
Paul Bakker5121ce52009-01-03 21:22:43 +00002011/*
2012 * Fill the input message buffer
2013 */
Paul Bakker23986e52011-04-24 08:57:21 +00002014int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002015{
Paul Bakker23986e52011-04-24 08:57:21 +00002016 int ret;
2017 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002018
2019 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
2020
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002021 if( nb_want > SSL_BUFFER_LEN - 8 )
2022 {
2023 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2024 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2025 }
2026
Paul Bakker5121ce52009-01-03 21:22:43 +00002027 while( ssl->in_left < nb_want )
2028 {
2029 len = nb_want - ssl->in_left;
2030 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
2031
2032 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2033 ssl->in_left, nb_want ) );
2034 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2035
Paul Bakker831a7552011-05-18 13:32:51 +00002036 if( ret == 0 )
2037 return( POLARSSL_ERR_SSL_CONN_EOF );
2038
Paul Bakker5121ce52009-01-03 21:22:43 +00002039 if( ret < 0 )
2040 return( ret );
2041
2042 ssl->in_left += ret;
2043 }
2044
2045 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2046
2047 return( 0 );
2048}
2049
2050/*
2051 * Flush any data not yet written
2052 */
2053int ssl_flush_output( ssl_context *ssl )
2054{
2055 int ret;
2056 unsigned char *buf;
2057
2058 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2059
2060 while( ssl->out_left > 0 )
2061 {
2062 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2063 5 + ssl->out_msglen, ssl->out_left ) );
2064
Paul Bakker5bd42292012-12-19 14:40:42 +01002065 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00002066 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002067
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2069
2070 if( ret <= 0 )
2071 return( ret );
2072
2073 ssl->out_left -= ret;
2074 }
2075
2076 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2077
2078 return( 0 );
2079}
2080
2081/*
2082 * Record layer functions
2083 */
2084int ssl_write_record( ssl_context *ssl )
2085{
Paul Bakker05ef8352012-05-08 09:17:57 +00002086 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002087 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002088
2089 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2090
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2092 {
2093 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2094 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2095 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2096
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002097 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2098 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002099 }
2100
Paul Bakker2770fbd2012-07-03 13:30:23 +00002101#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002102 if( ssl->transform_out != NULL &&
2103 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002104 {
2105 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2106 {
2107 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2108 return( ret );
2109 }
2110
2111 len = ssl->out_msglen;
2112 }
2113#endif /*POLARSSL_ZLIB_SUPPORT */
2114
Paul Bakker05ef8352012-05-08 09:17:57 +00002115#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002116 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002117 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002118 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002119
Paul Bakker05ef8352012-05-08 09:17:57 +00002120 ret = ssl_hw_record_write( ssl );
2121 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2122 {
2123 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002124 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002125 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002126
2127 if( ret == 0 )
2128 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002129 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002130#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002131 if( !done )
2132 {
2133 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2134 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2135 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2137 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002138
Paul Bakker48916f92012-09-16 19:57:18 +00002139 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002140 {
2141 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2142 {
2143 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2144 return( ret );
2145 }
2146
2147 len = ssl->out_msglen;
2148 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2149 ssl->out_hdr[4] = (unsigned char)( len );
2150 }
2151
2152 ssl->out_left = 5 + ssl->out_msglen;
2153
2154 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2155 "version = [%d:%d], msglen = %d",
2156 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2157 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2158
2159 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002160 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002161 }
2162
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2164 {
2165 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2166 return( ret );
2167 }
2168
2169 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2170
2171 return( 0 );
2172}
2173
2174int ssl_read_record( ssl_context *ssl )
2175{
Paul Bakker05ef8352012-05-08 09:17:57 +00002176 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002177
2178 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2179
Hanno Becker10699cc2017-06-08 15:41:02 +01002180 if( ssl->keep_current_message == 1 )
2181 {
2182 SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
2183 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2184 ssl->keep_current_message = 0;
2185
2186 return( 0 );
2187 }
2188
Hanno Beckerd37839e2017-06-08 15:56:50 +01002189 if( ssl->in_hslen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002190 {
Hanno Becker1bf86b72017-06-08 15:58:02 +01002191 if( ssl->in_offt != NULL )
2192 {
2193 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2194 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2195 }
2196
Paul Bakker5121ce52009-01-03 21:22:43 +00002197 /*
2198 * Get next Handshake message in the current record
2199 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002200
Hanno Beckerd37839e2017-06-08 15:56:50 +01002201 if( ssl->in_hslen < ssl->in_msglen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002202 {
Hanno Beckerd37839e2017-06-08 15:56:50 +01002203 ssl->in_msglen -= ssl->in_hslen;
2204 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
2205 ssl->in_msglen );
2206
2207 ssl->in_hslen = 4;
2208 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2209
2210 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2211 " %d, type = %d, hslen = %d",
2212 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2213
2214 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2215 {
2216 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
2217 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2218 }
2219
2220 if( ssl->in_msglen < ssl->in_hslen )
2221 {
2222 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
2223 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2224 }
2225
2226 if( ssl->state != SSL_HANDSHAKE_OVER )
2227 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
2228
2229 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002230 }
2231
Hanno Beckerd37839e2017-06-08 15:56:50 +01002232 ssl->in_msglen = 0;
2233 ssl->in_hslen = 0;
2234 }
2235 else if( ssl->in_offt != NULL )
2236 {
2237 return( 0 );
2238 }
2239 else
2240 {
2241 ssl->in_msglen = 0;
2242 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002243
Hanno Beckerd37839e2017-06-08 15:56:50 +01002244 /*
2245 * Fetch and decode new record if current one is fully consumed.
2246 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002247
Hanno Beckerd37839e2017-06-08 15:56:50 +01002248 if( ssl->in_msglen > 0 )
2249 {
2250 /* There's something left to be processed in the current record. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 return( 0 );
2252 }
2253
Hanno Beckerd37839e2017-06-08 15:56:50 +01002254 /* Need to fetch a new record */
Paul Bakker5121ce52009-01-03 21:22:43 +00002255
Manuel Pégourié-Gonnard0aaefce2015-10-27 13:42:11 +01002256read_record_header:
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2258 {
2259 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2260 return( ret );
2261 }
2262
2263 ssl->in_msgtype = ssl->in_hdr[0];
2264 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2265
2266 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2267 "version = [%d:%d], msglen = %d",
2268 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2269 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2270
2271 if( ssl->in_hdr[1] != ssl->major_ver )
2272 {
2273 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002274 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002275 }
2276
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002277 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002278 {
2279 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002280 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002281 }
2282
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002283 /* Sanity check (outer boundaries) */
2284 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2285 {
2286 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2287 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2288 }
2289
Paul Bakker5121ce52009-01-03 21:22:43 +00002290 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002291 * Make sure the message length is acceptable for the current transform
2292 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002293 */
Paul Bakker48916f92012-09-16 19:57:18 +00002294 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002295 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002296 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
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 }
2302 else
2303 {
Paul Bakker48916f92012-09-16 19:57:18 +00002304 if( ssl->in_msglen < ssl->transform_in->minlen )
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 }
2309
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002310#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002311 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002312 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002313 {
2314 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002315 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002316 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002317#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002318
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002319#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2320 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002321 /*
2322 * TLS encrypted messages can have up to 256 bytes of padding
2323 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002324 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002325 ssl->in_msglen > ssl->transform_in->minlen +
2326 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002327 {
2328 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002329 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002330 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002331#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002332 }
2333
2334 /*
2335 * Read and optionally decrypt the message contents
2336 */
2337 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2338 {
2339 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2340 return( ret );
2341 }
2342
2343 SSL_DEBUG_BUF( 4, "input record from network",
2344 ssl->in_hdr, 5 + ssl->in_msglen );
2345
Paul Bakker05ef8352012-05-08 09:17:57 +00002346#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002347 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002348 {
2349 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2350
2351 ret = ssl_hw_record_read( ssl );
2352 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2353 {
2354 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002355 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002356 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002357
2358 if( ret == 0 )
2359 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002360 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002361#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002362 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002363 {
2364 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2365 {
Paul Bakker40865c82013-01-31 17:13:13 +01002366#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2367 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2368 {
2369 ssl_send_alert_message( ssl,
2370 SSL_ALERT_LEVEL_FATAL,
2371 SSL_ALERT_MSG_BAD_RECORD_MAC );
2372 }
2373#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002374 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2375 return( ret );
2376 }
2377
2378 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2379 ssl->in_msg, ssl->in_msglen );
2380
2381 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2382 {
2383 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002384 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002385 }
2386 }
2387
Paul Bakker2770fbd2012-07-03 13:30:23 +00002388#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002389 if( ssl->transform_in != NULL &&
2390 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002391 {
2392 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2393 {
2394 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2395 return( ret );
2396 }
2397
2398 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2399 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2400 }
2401#endif /* POLARSSL_ZLIB_SUPPORT */
2402
Paul Bakker0a925182012-04-16 06:46:41 +00002403 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2404 ssl->in_msgtype != SSL_MSG_ALERT &&
2405 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2406 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2407 {
2408 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2409
Paul Bakker48916f92012-09-16 19:57:18 +00002410 if( ( ret = ssl_send_alert_message( ssl,
2411 SSL_ALERT_LEVEL_FATAL,
2412 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002413 {
2414 return( ret );
2415 }
2416
2417 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2418 }
2419
Paul Bakker5121ce52009-01-03 21:22:43 +00002420 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2421 {
2422 ssl->in_hslen = 4;
2423 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2424
2425 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2426 " %d, type = %d, hslen = %d",
2427 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2428
2429 /*
2430 * Additional checks to validate the handshake header
2431 */
2432 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2433 {
2434 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002435 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002436 }
2437
2438 if( ssl->in_msglen < ssl->in_hslen )
2439 {
2440 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002441 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002442 }
2443
Paul Bakker48916f92012-09-16 19:57:18 +00002444 if( ssl->state != SSL_HANDSHAKE_OVER )
2445 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002446 }
2447
2448 if( ssl->in_msgtype == SSL_MSG_ALERT )
2449 {
2450 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2451 ssl->in_msg[0], ssl->in_msg[1] ) );
2452
2453 /*
Manuel Pégourié-Gonnard0aaefce2015-10-27 13:42:11 +01002454 * Ignore non-fatal alerts, except close_notify and no_renego
Paul Bakker5121ce52009-01-03 21:22:43 +00002455 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002456 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002457 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002458 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2459 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002460 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 }
2462
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002463 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2464 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002465 {
2466 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002467 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002468 }
Manuel Pégourié-Gonnard0aaefce2015-10-27 13:42:11 +01002469
2470 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2471 ssl->in_msg[1] == SSL_ALERT_MSG_NO_RENEGOTIATION )
2472 {
2473 SSL_DEBUG_MSG( 2, ( "is a no_renegotiation" ) );
2474 /* Will be handled when trying to parse ServerHello */
2475 ssl->in_left = 0;
2476 return( 0 );
2477 }
2478
2479 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
2480 ssl->endpoint == SSL_IS_SERVER &&
2481 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2482 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
2483 {
2484 SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
2485 /* Will be handled in ssl_parse_certificate() */
2486 ssl->in_left = 0;
2487 return( 0 );
2488 }
2489
2490 /* Silently discard: fetch new message */
2491 goto read_record_header;
Paul Bakker5121ce52009-01-03 21:22:43 +00002492 }
2493
2494 ssl->in_left = 0;
2495
2496 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2497
2498 return( 0 );
2499}
2500
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002501int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2502{
2503 int ret;
2504
2505 if( ( ret = ssl_send_alert_message( ssl,
2506 SSL_ALERT_LEVEL_FATAL,
2507 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2508 {
2509 return( ret );
2510 }
2511
2512 return( 0 );
2513}
2514
Paul Bakker0a925182012-04-16 06:46:41 +00002515int ssl_send_alert_message( ssl_context *ssl,
2516 unsigned char level,
2517 unsigned char message )
2518{
2519 int ret;
2520
2521 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2522
2523 ssl->out_msgtype = SSL_MSG_ALERT;
2524 ssl->out_msglen = 2;
2525 ssl->out_msg[0] = level;
2526 ssl->out_msg[1] = message;
2527
2528 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2529 {
2530 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2531 return( ret );
2532 }
2533
2534 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2535
2536 return( 0 );
2537}
2538
Paul Bakker5121ce52009-01-03 21:22:43 +00002539/*
2540 * Handshake functions
2541 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002542#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2543 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2544 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2545 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2546 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2547 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2548 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002549int ssl_write_certificate( ssl_context *ssl )
2550{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002551 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002552
2553 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2554
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002555 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002556 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2557 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002558 {
2559 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2560 ssl->state++;
2561 return( 0 );
2562 }
2563
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002564 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2565 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002566}
2567
2568int ssl_parse_certificate( ssl_context *ssl )
2569{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002570 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2571
2572 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2573
2574 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002575 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2576 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002577 {
2578 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2579 ssl->state++;
2580 return( 0 );
2581 }
2582
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002583 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2584 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002585}
2586#else
2587int ssl_write_certificate( ssl_context *ssl )
2588{
2589 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2590 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002591 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002592 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2593
2594 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2595
2596 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002597 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2598 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002599 {
2600 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2601 ssl->state++;
2602 return( 0 );
2603 }
2604
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002605#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002606 if( ssl->endpoint == SSL_IS_CLIENT )
2607 {
2608 if( ssl->client_auth == 0 )
2609 {
2610 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2611 ssl->state++;
2612 return( 0 );
2613 }
2614
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002615#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002616 /*
2617 * If using SSLv3 and got no cert, send an Alert message
2618 * (otherwise an empty Certificate message will be sent).
2619 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002620 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002621 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2622 {
2623 ssl->out_msglen = 2;
2624 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002625 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2626 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002627
2628 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2629 goto write_msg;
2630 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002631#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002632 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002633#endif /* POLARSSL_SSL_CLI_C */
2634#if defined(POLARSSL_SSL_SRV_C)
2635 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00002636 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002637 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002638 {
2639 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002640 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002641 }
2642 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002643#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002644
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002645 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002646
2647 /*
2648 * 0 . 0 handshake type
2649 * 1 . 3 handshake length
2650 * 4 . 6 length of all certs
2651 * 7 . 9 length of cert. 1
2652 * 10 . n-1 peer certificate
2653 * n . n+2 length of cert. 2
2654 * n+3 . ... upper level cert, etc.
2655 */
2656 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002657 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002658
Paul Bakker29087132010-03-21 21:03:34 +00002659 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002660 {
2661 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002662 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002663 {
2664 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2665 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002666 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002667 }
2668
2669 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2670 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2671 ssl->out_msg[i + 2] = (unsigned char)( n );
2672
2673 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2674 i += n; crt = crt->next;
2675 }
2676
2677 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2678 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2679 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2680
2681 ssl->out_msglen = i;
2682 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2683 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2684
Simon Butcher149950d2016-10-15 22:08:08 +01002685#if defined(POLARSSL_SSL_PROTO_SSL3) && defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002686write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002687#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002688
2689 ssl->state++;
2690
2691 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2692 {
2693 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2694 return( ret );
2695 }
2696
2697 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2698
Paul Bakkered27a042013-04-18 22:46:23 +02002699 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002700}
2701
2702int ssl_parse_certificate( ssl_context *ssl )
2703{
Paul Bakkered27a042013-04-18 22:46:23 +02002704 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002705 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002706 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
2708 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2709
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002710 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002711 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2712 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002713 {
2714 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2715 ssl->state++;
2716 return( 0 );
2717 }
2718
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002719#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002720 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002721 ( ssl->authmode == SSL_VERIFY_NONE ||
2722 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002723 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002724 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002725 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2726 ssl->state++;
2727 return( 0 );
2728 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002729#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002730
2731 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2732 {
2733 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2734 return( ret );
2735 }
2736
2737 ssl->state++;
2738
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002739#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002740#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002741 /*
2742 * Check if the client sent an empty certificate
2743 */
2744 if( ssl->endpoint == SSL_IS_SERVER &&
2745 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2746 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002747 if( ssl->in_msglen == 2 &&
2748 ssl->in_msgtype == SSL_MSG_ALERT &&
2749 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2750 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002751 {
2752 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2753
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002754 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002755 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2756 return( 0 );
2757 else
Paul Bakker40e46942009-01-03 21:51:57 +00002758 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002759 }
2760 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002761#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002762
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002763#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2764 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002765 if( ssl->endpoint == SSL_IS_SERVER &&
2766 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2767 {
2768 if( ssl->in_hslen == 7 &&
2769 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2770 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2771 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2772 {
2773 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2774
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002775 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002776 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002777 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 else
2779 return( 0 );
2780 }
2781 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002782#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2783 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002784#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002785
2786 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2787 {
2788 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002789 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002790 }
2791
2792 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2793 {
2794 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002795 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002796 }
2797
2798 /*
2799 * Same message structure as in ssl_write_certificate()
2800 */
2801 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2802
2803 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2804 {
2805 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002806 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002807 }
2808
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002809 /* In case we tried to reuse a session but it failed */
2810 if( ssl->session_negotiate->peer_cert != NULL )
2811 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002812 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002813 polarssl_free( ssl->session_negotiate->peer_cert );
2814 }
2815
Mansour Moufidc531b4a2015-02-15 17:35:38 -05002816 if( ( ssl->session_negotiate->peer_cert = polarssl_malloc(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002817 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002818 {
2819 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002820 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002821 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002822 }
2823
Paul Bakkerb6b09562013-09-18 14:17:41 +02002824 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002825
2826 i = 7;
2827
2828 while( i < ssl->in_hslen )
2829 {
2830 if( ssl->in_msg[i] != 0 )
2831 {
2832 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002833 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002834 }
2835
2836 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2837 | (unsigned int) ssl->in_msg[i + 2];
2838 i += 3;
2839
2840 if( n < 128 || i + n > ssl->in_hslen )
2841 {
2842 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002843 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002844 }
2845
Paul Bakkerddf26b42013-09-18 13:46:23 +02002846 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2847 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002848 if( ret != 0 )
2849 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002850 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002851 return( ret );
2852 }
2853
2854 i += n;
2855 }
2856
Paul Bakker48916f92012-09-16 19:57:18 +00002857 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002858
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002859 /*
2860 * On client, make sure the server cert doesn't change during renego to
2861 * avoid "triple handshake" attack: https://secure-resumption.com/
2862 */
Paul Bakkerf6080b82015-01-13 16:18:23 +01002863#if defined(POLARSSL_SSL_RENEGOTIATION) && defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002864 if( ssl->endpoint == SSL_IS_CLIENT &&
2865 ssl->renegotiation == SSL_RENEGOTIATION )
2866 {
2867 if( ssl->session->peer_cert == NULL )
2868 {
2869 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2870 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2871 }
2872
2873 if( ssl->session->peer_cert->raw.len !=
2874 ssl->session_negotiate->peer_cert->raw.len ||
2875 memcmp( ssl->session->peer_cert->raw.p,
2876 ssl->session_negotiate->peer_cert->raw.p,
2877 ssl->session->peer_cert->raw.len ) != 0 )
2878 {
2879 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2880 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2881 }
2882 }
Paul Bakkerf6080b82015-01-13 16:18:23 +01002883#endif /* POLARSSL_SSL_RENEGOTIATION && POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002884
Paul Bakker5121ce52009-01-03 21:22:43 +00002885 if( ssl->authmode != SSL_VERIFY_NONE )
2886 {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002887 /*
2888 * Main check: verify certificate
2889 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002890 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2891 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2892 &ssl->session_negotiate->verify_result,
2893 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002894
2895 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002896 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002897 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002898 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002899
2900 /*
2901 * Secondary checks: always done, but change 'ret' only if it was 0
2902 */
2903
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002904#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002905 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002906 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002907
2908 /* If certificate uses an EC key, make sure the curve is OK */
2909 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2910 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2911 {
Hanno Becker888c2fd2017-05-11 11:12:40 +01002912 ssl->session_negotiate->verify_result |= BADCERT_BAD_KEY;
2913
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002914 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002915 if( ret == 0 )
2916 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002917 }
2918 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002919#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002920
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002921 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2922 ciphersuite_info,
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02002923 ! ssl->endpoint,
2924 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002925 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002926 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002927 if( ret == 0 )
2928 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2929 }
2930
Hanno Becker888c2fd2017-05-11 11:12:40 +01002931 /* x509_crt_verify_with_profile is supposed to report a
2932 * verification failure through POLARSSL_ERR_X509_CERT_VERIFY_FAILED,
2933 * with details encoded in the verification flags. All other kinds
2934 * of error codes, including those from the user provided f_vrfy
2935 * functions, are treated as fatal and lead to a failure of
2936 * ssl_parse_certificate even if verification was optional. */
2937 if( ssl->authmode == SSL_VERIFY_OPTIONAL &&
2938 ( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED ||
2939 ret == POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE ) )
2940 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002941 ret = 0;
Hanno Becker888c2fd2017-05-11 11:12:40 +01002942 }
2943
2944 if( ssl->ca_chain == NULL && ssl->authmode == SSL_VERIFY_REQUIRED )
2945 {
2946 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
2947 ret = POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED;
2948 }
2949
2950#if defined(POLARSSL_DEBUG_C)
2951 if( ssl->session_negotiate->verify_result != 0 )
2952 {
2953 SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
2954 ssl->session_negotiate->verify_result ) );
2955 }
2956 else
2957 {
2958 SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
2959 }
2960#endif /* POLARSSL_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002961 }
2962
2963 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2964
2965 return( ret );
2966}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002967#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2968 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2969 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2970 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2971 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2972 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2973 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002974
2975int ssl_write_change_cipher_spec( ssl_context *ssl )
2976{
2977 int ret;
2978
2979 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2980
2981 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2982 ssl->out_msglen = 1;
2983 ssl->out_msg[0] = 1;
2984
Paul Bakker5121ce52009-01-03 21:22:43 +00002985 ssl->state++;
2986
2987 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2988 {
2989 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2990 return( ret );
2991 }
2992
2993 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2994
2995 return( 0 );
2996}
2997
2998int ssl_parse_change_cipher_spec( ssl_context *ssl )
2999{
3000 int ret;
3001
3002 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
3003
Paul Bakker5121ce52009-01-03 21:22:43 +00003004 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3005 {
3006 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3007 return( ret );
3008 }
3009
3010 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
3011 {
3012 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003013 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003014 }
3015
3016 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
3017 {
3018 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003019 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00003020 }
3021
3022 ssl->state++;
3023
3024 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
3025
3026 return( 0 );
3027}
3028
Paul Bakker41c83d32013-03-20 14:39:14 +01003029void ssl_optimize_checksum( ssl_context *ssl,
3030 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00003031{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003032 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003033
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003034#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3035 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00003036 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00003037 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00003038 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003039#endif
3040#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3041#if defined(POLARSSL_SHA512_C)
3042 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
3043 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
3044 else
3045#endif
3046#if defined(POLARSSL_SHA256_C)
3047 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00003048 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003049 else
3050#endif
3051#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003052 {
3053 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003054 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003055 }
Paul Bakker380da532012-04-18 16:10:25 +00003056}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003057
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003058static void ssl_update_checksum_start( ssl_context *ssl,
3059 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003060{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003061#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3062 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003063 md5_update( &ssl->handshake->fin_md5 , buf, len );
3064 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003065#endif
3066#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3067#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003068 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003069#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003070#if defined(POLARSSL_SHA512_C)
3071 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003072#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003073#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003074}
3075
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003076#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3077 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003078static void ssl_update_checksum_md5sha1( ssl_context *ssl,
3079 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003080{
Paul Bakker48916f92012-09-16 19:57:18 +00003081 md5_update( &ssl->handshake->fin_md5 , buf, len );
3082 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003083}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003084#endif
Paul Bakker380da532012-04-18 16:10:25 +00003085
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003086#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3087#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003088static void ssl_update_checksum_sha256( 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 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003092}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003093#endif
Paul Bakker380da532012-04-18 16:10:25 +00003094
Paul Bakker9e36f042013-06-30 14:34:05 +02003095#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003096static void ssl_update_checksum_sha384( ssl_context *ssl,
3097 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003098{
Paul Bakker9e36f042013-06-30 14:34:05 +02003099 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003100}
Paul Bakker769075d2012-11-24 11:26:46 +01003101#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003102#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003103
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003104#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003105static void ssl_calc_finished_ssl(
3106 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003107{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003108 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003109 md5_context md5;
3110 sha1_context sha1;
3111
Paul Bakker5121ce52009-01-03 21:22:43 +00003112 unsigned char padbuf[48];
3113 unsigned char md5sum[16];
3114 unsigned char sha1sum[20];
3115
Paul Bakker48916f92012-09-16 19:57:18 +00003116 ssl_session *session = ssl->session_negotiate;
3117 if( !session )
3118 session = ssl->session;
3119
Paul Bakker1ef83d62012-04-11 12:09:53 +00003120 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3121
Paul Bakker48916f92012-09-16 19:57:18 +00003122 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3123 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003124
3125 /*
3126 * SSLv3:
3127 * hash =
3128 * MD5( master + pad2 +
3129 * MD5( handshake + sender + master + pad1 ) )
3130 * + SHA1( master + pad2 +
3131 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003132 */
3133
Paul Bakker90995b52013-06-24 19:20:35 +02003134#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003135 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003136 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003137#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003138
Paul Bakker90995b52013-06-24 19:20:35 +02003139#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003140 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003141 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003142#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003143
Paul Bakker3c2122f2013-06-24 19:03:14 +02003144 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3145 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003146
Paul Bakker1ef83d62012-04-11 12:09:53 +00003147 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003148
Paul Bakker3c2122f2013-06-24 19:03:14 +02003149 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003150 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003151 md5_update( &md5, padbuf, 48 );
3152 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003153
Paul Bakker3c2122f2013-06-24 19:03:14 +02003154 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003155 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003156 sha1_update( &sha1, padbuf, 40 );
3157 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003158
Paul Bakker1ef83d62012-04-11 12:09:53 +00003159 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003160
Paul Bakker1ef83d62012-04-11 12:09:53 +00003161 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003162 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003163 md5_update( &md5, padbuf, 48 );
3164 md5_update( &md5, md5sum, 16 );
3165 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003166
Paul Bakker1ef83d62012-04-11 12:09:53 +00003167 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003168 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003169 sha1_update( &sha1, padbuf , 40 );
3170 sha1_update( &sha1, sha1sum, 20 );
3171 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003172
Paul Bakker1ef83d62012-04-11 12:09:53 +00003173 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003174
Paul Bakker5b4af392014-06-26 12:09:34 +02003175 md5_free( &md5 );
3176 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003177
Paul Bakker34617722014-06-13 17:20:13 +02003178 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3179 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3180 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003181
3182 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3183}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003184#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003185
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003186#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003187static void ssl_calc_finished_tls(
3188 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003189{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003190 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003191 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003192 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003193 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003194 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003195
Paul Bakker48916f92012-09-16 19:57:18 +00003196 ssl_session *session = ssl->session_negotiate;
3197 if( !session )
3198 session = ssl->session;
3199
Paul Bakker1ef83d62012-04-11 12:09:53 +00003200 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003201
Paul Bakker48916f92012-09-16 19:57:18 +00003202 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3203 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003204
Paul Bakker1ef83d62012-04-11 12:09:53 +00003205 /*
3206 * TLSv1:
3207 * hash = PRF( master, finished_label,
3208 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3209 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003210
Paul Bakker90995b52013-06-24 19:20:35 +02003211#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003212 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3213 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003214#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003215
Paul Bakker90995b52013-06-24 19:20:35 +02003216#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003217 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3218 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003219#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003220
3221 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003222 ? "client finished"
3223 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003224
3225 md5_finish( &md5, padbuf );
3226 sha1_finish( &sha1, padbuf + 16 );
3227
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003228 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003229 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003230
3231 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3232
Paul Bakker5b4af392014-06-26 12:09:34 +02003233 md5_free( &md5 );
3234 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003235
Paul Bakker34617722014-06-13 17:20:13 +02003236 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003237
3238 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3239}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003240#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003241
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003242#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3243#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003244static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003245 ssl_context *ssl, unsigned char *buf, int from )
3246{
3247 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003248 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003249 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003250 unsigned char padbuf[32];
3251
Paul Bakker48916f92012-09-16 19:57:18 +00003252 ssl_session *session = ssl->session_negotiate;
3253 if( !session )
3254 session = ssl->session;
3255
Paul Bakker380da532012-04-18 16:10:25 +00003256 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003257
Paul Bakker9e36f042013-06-30 14:34:05 +02003258 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003259
3260 /*
3261 * TLSv1.2:
3262 * hash = PRF( master, finished_label,
3263 * Hash( handshake ) )[0.11]
3264 */
3265
Paul Bakker9e36f042013-06-30 14:34:05 +02003266#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003267 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003268 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003269#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003270
3271 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003272 ? "client finished"
3273 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003274
Paul Bakker9e36f042013-06-30 14:34:05 +02003275 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003276
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003277 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003278 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003279
3280 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3281
Paul Bakker5b4af392014-06-26 12:09:34 +02003282 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003283
Paul Bakker34617722014-06-13 17:20:13 +02003284 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003285
3286 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3287}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003288#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003289
Paul Bakker9e36f042013-06-30 14:34:05 +02003290#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003291static void ssl_calc_finished_tls_sha384(
3292 ssl_context *ssl, unsigned char *buf, int from )
3293{
3294 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003295 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003296 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003297 unsigned char padbuf[48];
3298
Paul Bakker48916f92012-09-16 19:57:18 +00003299 ssl_session *session = ssl->session_negotiate;
3300 if( !session )
3301 session = ssl->session;
3302
Paul Bakker380da532012-04-18 16:10:25 +00003303 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003304
Paul Bakker9e36f042013-06-30 14:34:05 +02003305 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003306
3307 /*
3308 * TLSv1.2:
3309 * hash = PRF( master, finished_label,
3310 * Hash( handshake ) )[0.11]
3311 */
3312
Paul Bakker9e36f042013-06-30 14:34:05 +02003313#if !defined(POLARSSL_SHA512_ALT)
3314 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3315 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003316#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003317
3318 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003319 ? "client finished"
3320 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003321
Paul Bakker9e36f042013-06-30 14:34:05 +02003322 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003323
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003324 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003325 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003326
3327 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3328
Paul Bakker5b4af392014-06-26 12:09:34 +02003329 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003330
Paul Bakker34617722014-06-13 17:20:13 +02003331 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003332
3333 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3334}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003335#endif /* POLARSSL_SHA512_C */
3336#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003337
Paul Bakker48916f92012-09-16 19:57:18 +00003338void ssl_handshake_wrapup( ssl_context *ssl )
3339{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003340 int resume = ssl->handshake->resume;
3341
Paul Bakker48916f92012-09-16 19:57:18 +00003342 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3343
3344 /*
3345 * Free our handshake params
3346 */
3347 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003348 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003349 ssl->handshake = NULL;
3350
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003351#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003352 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003353 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003354 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003355 ssl->renego_records_seen = 0;
3356 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003357#endif
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003358
Paul Bakker48916f92012-09-16 19:57:18 +00003359 /*
3360 * Switch in our now active transform context
3361 */
3362 if( ssl->transform )
3363 {
3364 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003365 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003366 }
3367 ssl->transform = ssl->transform_negotiate;
3368 ssl->transform_negotiate = NULL;
3369
Paul Bakker0a597072012-09-25 21:55:46 +00003370 if( ssl->session )
3371 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003372#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3373 /* RFC 7366 3.1: keep the EtM state */
3374 ssl->session_negotiate->encrypt_then_mac =
3375 ssl->session->encrypt_then_mac;
3376#endif
3377
Paul Bakker0a597072012-09-25 21:55:46 +00003378 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003379 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003380 }
3381 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003382 ssl->session_negotiate = NULL;
3383
Paul Bakker0a597072012-09-25 21:55:46 +00003384 /*
3385 * Add cache entry
3386 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003387 if( ssl->f_set_cache != NULL &&
3388 ssl->session->length != 0 &&
3389 resume == 0 )
3390 {
Paul Bakker0a597072012-09-25 21:55:46 +00003391 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3392 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003393 }
Paul Bakker0a597072012-09-25 21:55:46 +00003394
Paul Bakker48916f92012-09-16 19:57:18 +00003395 ssl->state++;
3396
3397 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3398}
3399
Paul Bakker1ef83d62012-04-11 12:09:53 +00003400int ssl_write_finished( ssl_context *ssl )
3401{
3402 int ret, hash_len;
3403
3404 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3405
Paul Bakker92be97b2013-01-02 17:30:03 +01003406 /*
3407 * Set the out_msg pointer to the correct location based on IV length
3408 */
3409 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3410 {
3411 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3412 ssl->transform_negotiate->fixed_ivlen;
3413 }
3414 else
3415 ssl->out_msg = ssl->out_iv;
3416
Paul Bakker48916f92012-09-16 19:57:18 +00003417 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003418
3419 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003420 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3421
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003422#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003423 ssl->verify_data_len = hash_len;
3424 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003425#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003426
Paul Bakker5121ce52009-01-03 21:22:43 +00003427 ssl->out_msglen = 4 + hash_len;
3428 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3429 ssl->out_msg[0] = SSL_HS_FINISHED;
3430
3431 /*
3432 * In case of session resuming, invert the client and server
3433 * ChangeCipherSpec messages order.
3434 */
Paul Bakker0a597072012-09-25 21:55:46 +00003435 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003436 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003437#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003438 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003439 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003440#endif
3441#if defined(POLARSSL_SSL_SRV_C)
3442 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003443 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003444#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003445 }
3446 else
3447 ssl->state++;
3448
Paul Bakker48916f92012-09-16 19:57:18 +00003449 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003450 * Switch to our negotiated transform and session parameters for outbound
3451 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003452 */
3453 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3454 ssl->transform_out = ssl->transform_negotiate;
3455 ssl->session_out = ssl->session_negotiate;
3456 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003457
Paul Bakker07eb38b2012-12-19 14:42:06 +01003458#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003459 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003460 {
3461 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3462 {
3463 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3464 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3465 }
3466 }
3467#endif
3468
Paul Bakker5121ce52009-01-03 21:22:43 +00003469 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3470 {
3471 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3472 return( ret );
3473 }
3474
3475 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3476
3477 return( 0 );
3478}
3479
3480int ssl_parse_finished( ssl_context *ssl )
3481{
Paul Bakker23986e52011-04-24 08:57:21 +00003482 int ret;
3483 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003484 unsigned char buf[36];
3485
3486 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3487
Paul Bakker48916f92012-09-16 19:57:18 +00003488 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003489
Paul Bakker48916f92012-09-16 19:57:18 +00003490 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003491 * Switch to our negotiated transform and session parameters for inbound
3492 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003493 */
3494 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3495 ssl->transform_in = ssl->transform_negotiate;
3496 ssl->session_in = ssl->session_negotiate;
3497 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003498
Paul Bakker92be97b2013-01-02 17:30:03 +01003499 /*
3500 * Set the in_msg pointer to the correct location based on IV length
3501 */
3502 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3503 {
3504 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3505 ssl->transform_negotiate->fixed_ivlen;
3506 }
3507 else
3508 ssl->in_msg = ssl->in_iv;
3509
Paul Bakker07eb38b2012-12-19 14:42:06 +01003510#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003511 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003512 {
3513 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3514 {
3515 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3516 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3517 }
3518 }
3519#endif
3520
Paul Bakker5121ce52009-01-03 21:22:43 +00003521 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3522 {
3523 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3524 return( ret );
3525 }
3526
3527 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3528 {
3529 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003530 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003531 }
3532
Paul Bakker1ef83d62012-04-11 12:09:53 +00003533 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003534 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3535
3536 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3537 ssl->in_hslen != 4 + hash_len )
3538 {
3539 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003540 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003541 }
3542
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003543 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003544 {
3545 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003546 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003547 }
3548
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003549#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003550 ssl->verify_data_len = hash_len;
3551 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003552#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003553
Paul Bakker0a597072012-09-25 21:55:46 +00003554 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003555 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003556#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003557 if( ssl->endpoint == SSL_IS_CLIENT )
3558 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003559#endif
3560#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003561 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003562 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003563#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003564 }
3565 else
3566 ssl->state++;
3567
3568 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3569
3570 return( 0 );
3571}
3572
Paul Bakker968afaa2014-07-09 11:09:24 +02003573static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003574{
3575 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3576
3577#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3578 defined(POLARSSL_SSL_PROTO_TLS1_1)
3579 md5_init( &handshake->fin_md5 );
3580 sha1_init( &handshake->fin_sha1 );
3581 md5_starts( &handshake->fin_md5 );
3582 sha1_starts( &handshake->fin_sha1 );
3583#endif
3584#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3585#if defined(POLARSSL_SHA256_C)
3586 sha256_init( &handshake->fin_sha256 );
3587 sha256_starts( &handshake->fin_sha256, 0 );
3588#endif
3589#if defined(POLARSSL_SHA512_C)
3590 sha512_init( &handshake->fin_sha512 );
3591 sha512_starts( &handshake->fin_sha512, 1 );
3592#endif
3593#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3594
3595 handshake->update_checksum = ssl_update_checksum_start;
Hanno Beckerc2b9d982017-05-10 16:37:21 +01003596
3597#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
3598 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
3599 ssl_sig_hash_set_init( &handshake->hash_algs );
3600#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003601
3602#if defined(POLARSSL_DHM_C)
3603 dhm_init( &handshake->dhm_ctx );
3604#endif
3605#if defined(POLARSSL_ECDH_C)
3606 ecdh_init( &handshake->ecdh_ctx );
3607#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003608}
3609
3610static void ssl_transform_init( ssl_transform *transform )
3611{
3612 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003613
3614 cipher_init( &transform->cipher_ctx_enc );
3615 cipher_init( &transform->cipher_ctx_dec );
3616
3617 md_init( &transform->md_ctx_enc );
3618 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003619}
3620
3621void ssl_session_init( ssl_session *session )
3622{
3623 memset( session, 0, sizeof(ssl_session) );
3624}
3625
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003626static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003627{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003628 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003629 if( ssl->transform_negotiate )
3630 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003631 if( ssl->session_negotiate )
3632 ssl_session_free( ssl->session_negotiate );
3633 if( ssl->handshake )
3634 ssl_handshake_free( ssl->handshake );
3635
3636 /*
3637 * Either the pointers are now NULL or cleared properly and can be freed.
3638 * Now allocate missing structures.
3639 */
3640 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003641 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003642 ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003643 }
Paul Bakker48916f92012-09-16 19:57:18 +00003644
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003645 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003646 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003647 ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003648 }
Paul Bakker48916f92012-09-16 19:57:18 +00003649
Paul Bakker82788fb2014-10-20 13:59:19 +02003650 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003651 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003652 ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003653 }
Paul Bakker48916f92012-09-16 19:57:18 +00003654
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003655 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003656 if( ssl->handshake == NULL ||
3657 ssl->transform_negotiate == NULL ||
3658 ssl->session_negotiate == NULL )
3659 {
3660 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003661
3662 polarssl_free( ssl->handshake );
3663 polarssl_free( ssl->transform_negotiate );
3664 polarssl_free( ssl->session_negotiate );
3665
3666 ssl->handshake = NULL;
3667 ssl->transform_negotiate = NULL;
3668 ssl->session_negotiate = NULL;
3669
Paul Bakker48916f92012-09-16 19:57:18 +00003670 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3671 }
3672
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003673 /* Initialize structures */
3674 ssl_session_init( ssl->session_negotiate );
3675 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003676 ssl_handshake_params_init( ssl->handshake );
3677
3678#if defined(POLARSSL_X509_CRT_PARSE_C)
3679 ssl->handshake->key_cert = ssl->key_cert;
3680#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003681
Paul Bakker48916f92012-09-16 19:57:18 +00003682 return( 0 );
3683}
3684
Paul Bakker5121ce52009-01-03 21:22:43 +00003685/*
3686 * Initialize an SSL context
3687 */
3688int ssl_init( ssl_context *ssl )
3689{
Paul Bakker48916f92012-09-16 19:57:18 +00003690 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003691 int len = SSL_BUFFER_LEN;
3692
3693 memset( ssl, 0, sizeof( ssl_context ) );
3694
Paul Bakker62f2dee2012-09-28 07:31:51 +00003695 /*
3696 * Sane defaults
3697 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003698 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3699 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3700 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3701 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003702
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003703 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003704
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003705#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003706 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003707 memset( ssl->renego_period, 0xFF, 7 );
3708 ssl->renego_period[7] = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003709#endif
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003710
Paul Bakker62f2dee2012-09-28 07:31:51 +00003711#if defined(POLARSSL_DHM_C)
3712 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
Manuel Pégourié-Gonnardf0f399d2015-07-03 17:45:57 +02003713 POLARSSL_DHM_RFC5114_MODP_2048_P) ) != 0 ||
Paul Bakker62f2dee2012-09-28 07:31:51 +00003714 ( ret = mpi_read_string( &ssl->dhm_G, 16,
Manuel Pégourié-Gonnardf0f399d2015-07-03 17:45:57 +02003715 POLARSSL_DHM_RFC5114_MODP_2048_G) ) != 0 )
Paul Bakker62f2dee2012-09-28 07:31:51 +00003716 {
3717 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3718 return( ret );
3719 }
3720#endif
3721
3722 /*
3723 * Prepare base structures
3724 */
Manuel Pégourié-Gonnardf7db5e02015-02-18 10:32:41 +00003725 if( ( ssl->in_ctr = polarssl_malloc( len ) ) == NULL ||
3726 ( ssl->out_ctr = polarssl_malloc( len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003727 {
3728 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003729 polarssl_free( ssl->in_ctr );
3730 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003731 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003732 }
3733
3734 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3735 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3736
Manuel Pégourié-Gonnardf7db5e02015-02-18 10:32:41 +00003737 ssl->in_hdr = ssl->in_ctr + 8;
3738 ssl->in_iv = ssl->in_ctr + 13;
3739 ssl->in_msg = ssl->in_ctr + 13;
3740
3741 ssl->out_hdr = ssl->out_ctr + 8;
3742 ssl->out_iv = ssl->out_ctr + 13;
3743 ssl->out_msg = ssl->out_ctr + 13;
3744
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003745#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3746 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
3747#endif
3748
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003749#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3750 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3751#endif
3752
Paul Bakker606b4ba2013-08-14 16:52:14 +02003753#if defined(POLARSSL_SSL_SESSION_TICKETS)
3754 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3755#endif
3756
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003757#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003758 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003759#endif
3760
Paul Bakker48916f92012-09-16 19:57:18 +00003761 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3762 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003763
3764 return( 0 );
3765}
3766
3767/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003768 * Reset an initialized and used SSL context for re-use while retaining
3769 * all application-set variables, function pointers and data.
3770 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003771int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003772{
Paul Bakker48916f92012-09-16 19:57:18 +00003773 int ret;
3774
Paul Bakker7eb013f2011-10-06 12:37:39 +00003775 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003776
3777#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003778 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003779 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003780
3781 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard61860192014-11-04 13:05:42 +01003782 memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
3783 memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003784#endif
3785 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00003786
Paul Bakker7eb013f2011-10-06 12:37:39 +00003787 ssl->in_offt = NULL;
3788
Paul Bakker92be97b2013-01-02 17:30:03 +01003789 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003790 ssl->in_msgtype = 0;
3791 ssl->in_msglen = 0;
3792 ssl->in_left = 0;
3793
3794 ssl->in_hslen = 0;
3795 ssl->nb_zero = 0;
Hanno Becker10699cc2017-06-08 15:41:02 +01003796 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003797
Paul Bakker92be97b2013-01-02 17:30:03 +01003798 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003799 ssl->out_msgtype = 0;
3800 ssl->out_msglen = 0;
3801 ssl->out_left = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003802#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01003803 if( ssl->split_done != SSL_CBC_RECORD_SPLITTING_DISABLED )
3804 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003805#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00003806
Paul Bakker48916f92012-09-16 19:57:18 +00003807 ssl->transform_in = NULL;
3808 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003809
3810 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3811 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003812
3813#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003814 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003815 {
3816 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003817 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003818 {
3819 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3820 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3821 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003822 }
3823#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003824
Paul Bakker48916f92012-09-16 19:57:18 +00003825 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003826 {
Paul Bakker48916f92012-09-16 19:57:18 +00003827 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003828 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003829 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003830 }
Paul Bakker48916f92012-09-16 19:57:18 +00003831
Paul Bakkerc0463502013-02-14 11:19:38 +01003832 if( ssl->session )
3833 {
3834 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003835 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003836 ssl->session = NULL;
3837 }
3838
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003839#if defined(POLARSSL_SSL_ALPN)
3840 ssl->alpn_chosen = NULL;
3841#endif
3842
Paul Bakker48916f92012-09-16 19:57:18 +00003843 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3844 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003845
3846 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003847}
3848
Paul Bakkera503a632013-08-14 13:48:06 +02003849#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003850static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3851{
3852 aes_free( &tkeys->enc );
3853 aes_free( &tkeys->dec );
3854
3855 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3856}
3857
Paul Bakker7eb013f2011-10-06 12:37:39 +00003858/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003859 * Allocate and initialize ticket keys
3860 */
3861static int ssl_ticket_keys_init( ssl_context *ssl )
3862{
3863 int ret;
3864 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003865 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003866
3867 if( ssl->ticket_keys != NULL )
3868 return( 0 );
3869
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003870 tkeys = polarssl_malloc( sizeof(ssl_ticket_keys) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003871 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003872 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3873
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003874 aes_init( &tkeys->enc );
3875 aes_init( &tkeys->dec );
3876
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003877 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003878 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003879 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003880 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003881 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003882 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003883
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003884 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3885 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3886 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3887 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003888 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003889 polarssl_free( tkeys );
3890 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003891 }
3892
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003893 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003894 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003895 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003896 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003897 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003898 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003899
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003900 ssl->ticket_keys = tkeys;
3901
3902 return( 0 );
3903}
Paul Bakkera503a632013-08-14 13:48:06 +02003904#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003905
3906/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003907 * SSL set accessors
3908 */
3909void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3910{
3911 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003912
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003913#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
3914 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003915 if( endpoint == SSL_IS_CLIENT )
3916 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003917#endif
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01003918
3919#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
3920 if( endpoint == SSL_IS_SERVER )
3921 ssl->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
3922#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003923}
3924
3925void ssl_set_authmode( ssl_context *ssl, int authmode )
3926{
3927 ssl->authmode = authmode;
3928}
3929
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003930#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003931void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003932 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003933 void *p_vrfy )
3934{
3935 ssl->f_vrfy = f_vrfy;
3936 ssl->p_vrfy = p_vrfy;
3937}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003938#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003939
Paul Bakker5121ce52009-01-03 21:22:43 +00003940void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003941 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003942 void *p_rng )
3943{
3944 ssl->f_rng = f_rng;
3945 ssl->p_rng = p_rng;
3946}
3947
3948void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003949 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003950 void *p_dbg )
3951{
3952 ssl->f_dbg = f_dbg;
3953 ssl->p_dbg = p_dbg;
3954}
3955
3956void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003957 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003958 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003959{
3960 ssl->f_recv = f_recv;
3961 ssl->f_send = f_send;
3962 ssl->p_recv = p_recv;
3963 ssl->p_send = p_send;
3964}
3965
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003966#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00003967void ssl_set_session_cache( ssl_context *ssl,
3968 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3969 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003970{
Paul Bakker0a597072012-09-25 21:55:46 +00003971 ssl->f_get_cache = f_get_cache;
3972 ssl->p_get_cache = p_get_cache;
3973 ssl->f_set_cache = f_set_cache;
3974 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003975}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003976#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003977
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003978#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003979int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003980{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003981 int ret;
3982
3983 if( ssl == NULL ||
3984 session == NULL ||
3985 ssl->session_negotiate == NULL ||
3986 ssl->endpoint != SSL_IS_CLIENT )
3987 {
3988 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3989 }
3990
3991 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3992 return( ret );
3993
Paul Bakker0a597072012-09-25 21:55:46 +00003994 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003995
3996 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003997}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003998#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003999
Paul Bakkerb68cad62012-08-23 08:34:18 +00004000void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00004001{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004002 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
4003 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
4004 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
4005 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
4006}
4007
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004008void ssl_set_ciphersuites_for_version( ssl_context *ssl,
4009 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004010 int major, int minor )
4011{
4012 if( major != SSL_MAJOR_VERSION_3 )
4013 return;
4014
4015 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
4016 return;
4017
4018 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00004019}
4020
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004021#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004022/* Add a new (empty) key_cert entry an return a pointer to it */
4023static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
4024{
4025 ssl_key_cert *key_cert, *last;
4026
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004027 key_cert = polarssl_malloc( sizeof(ssl_key_cert) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004028 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004029 return( NULL );
4030
4031 memset( key_cert, 0, sizeof( ssl_key_cert ) );
4032
4033 /* Append the new key_cert to the (possibly empty) current list */
4034 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01004035 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004036 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01004037 if( ssl->handshake != NULL )
4038 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01004039 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004040 else
4041 {
4042 last = ssl->key_cert;
4043 while( last->next != NULL )
4044 last = last->next;
4045 last->next = key_cert;
4046 }
4047
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004048 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004049}
4050
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004051void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00004052 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00004053{
4054 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00004055 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00004056 ssl->peer_cn = peer_cn;
4057}
4058
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004059int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004060 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00004061{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004062 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
4063
4064 if( key_cert == NULL )
4065 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4066
4067 key_cert->cert = own_cert;
4068 key_cert->key = pk_key;
4069
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004070 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004071}
4072
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +01004073#if ! defined(POLARSSL_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004074#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004075int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004076 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00004077{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004078 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004079 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004080
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004081 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004082 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4083
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004084 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004085 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004086 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004087
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004088 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004089
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004090 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004091 if( ret != 0 )
4092 return( ret );
4093
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004094 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004095 return( ret );
4096
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004097 key_cert->cert = own_cert;
4098 key_cert->key_own_alloc = 1;
4099
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004100 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004101}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004102#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004103
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004104int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02004105 void *rsa_key,
4106 rsa_decrypt_func rsa_decrypt,
4107 rsa_sign_func rsa_sign,
4108 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00004109{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004110 int ret;
4111 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004112
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004113 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004114 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4115
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004116 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004117 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004118 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004119
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004120 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004121
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004122 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
4123 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
4124 return( ret );
4125
4126 key_cert->cert = own_cert;
4127 key_cert->key_own_alloc = 1;
4128
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004129 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00004130}
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +01004131#endif /* POLARSSL_DEPRECATED_REMOVED */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004132#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004133
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004134#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004135int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
4136 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004137{
Paul Bakker6db455e2013-09-18 17:29:31 +02004138 if( psk == NULL || psk_identity == NULL )
4139 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4140
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02004141 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01004142 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4143
Manuel Pégourié-Gonnard65125542015-08-27 16:37:35 +02004144 /* Identity len will be encoded on two bytes */
4145 if( ( psk_identity_len >> 16 ) != 0 ||
4146 psk_identity_len > SSL_MAX_CONTENT_LEN )
4147 {
4148 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4149 }
4150
Andres Amaya Garciaa0ae1db2017-07-12 10:51:22 +01004151 if( ssl->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02004152 {
Andres Amaya Garciaa0ae1db2017-07-12 10:51:22 +01004153 polarssl_zeroize( ssl->psk, ssl->psk_len );
4154
Paul Bakker6db455e2013-09-18 17:29:31 +02004155 polarssl_free( ssl->psk );
Manuel Pégourié-Gonnard9c521762015-10-27 10:54:53 +01004156 ssl->psk = NULL;
Andres Amaya Garciaa0ae1db2017-07-12 10:51:22 +01004157 ssl->psk_len = 0;
4158 }
4159 if( ssl->psk_identity != NULL )
4160 {
4161 polarssl_free( ssl->psk_identity );
Manuel Pégourié-Gonnard9c521762015-10-27 10:54:53 +01004162 ssl->psk_identity = NULL;
Andres Amaya Garciaa0ae1db2017-07-12 10:51:22 +01004163 ssl->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02004164 }
4165
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004166 if( ( ssl->psk = polarssl_malloc( psk_len ) ) == NULL ||
4167 ( ssl->psk_identity = polarssl_malloc( psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05004168 {
4169 polarssl_free( ssl->psk );
Manuel Pégourié-Gonnard5aff0292015-09-28 18:09:45 +02004170 polarssl_free( ssl->psk_identity );
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004171 ssl->psk = NULL;
Manuel Pégourié-Gonnard5aff0292015-09-28 18:09:45 +02004172 ssl->psk_identity = NULL;
Mansour Moufidf81088b2015-02-17 13:10:21 -05004173 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4174 }
Paul Bakker6db455e2013-09-18 17:29:31 +02004175
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004176 ssl->psk_len = psk_len;
4177 ssl->psk_identity_len = psk_identity_len;
4178
Paul Bakker6db455e2013-09-18 17:29:31 +02004179 memcpy( ssl->psk, psk, ssl->psk_len );
4180 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004181
4182 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004183}
4184
4185void ssl_set_psk_cb( ssl_context *ssl,
4186 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4187 size_t),
4188 void *p_psk )
4189{
4190 ssl->f_psk = f_psk;
4191 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004192}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004193#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004194
Paul Bakker48916f92012-09-16 19:57:18 +00004195#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004196int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004197{
4198 int ret;
4199
Paul Bakker48916f92012-09-16 19:57:18 +00004200 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004201 {
4202 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4203 return( ret );
4204 }
4205
Paul Bakker48916f92012-09-16 19:57:18 +00004206 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004207 {
4208 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4209 return( ret );
4210 }
4211
4212 return( 0 );
4213}
4214
Paul Bakker1b57b062011-01-06 15:48:19 +00004215int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4216{
4217 int ret;
4218
Paul Bakker66d5d072014-06-17 16:39:18 +02004219 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004220 {
4221 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4222 return( ret );
4223 }
4224
Paul Bakker66d5d072014-06-17 16:39:18 +02004225 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004226 {
4227 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4228 return( ret );
4229 }
4230
4231 return( 0 );
4232}
Paul Bakker48916f92012-09-16 19:57:18 +00004233#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004234
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004235#if defined(POLARSSL_SSL_SET_CURVES)
4236/*
4237 * Set the allowed elliptic curves
4238 */
4239void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4240{
4241 ssl->curve_list = curve_list;
4242}
4243#endif
4244
Paul Bakker0be444a2013-08-27 21:55:01 +02004245#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004246int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004247{
Hanno Beckerb9ac47c2017-05-05 13:07:33 +01004248 size_t hostname_len = 0;
4249
4250 /* Check if new hostname is valid before
4251 * making any change to current one */
4252
4253 if( hostname != NULL )
4254 {
4255 hostname_len = strlen( hostname );
4256
4257 if( hostname_len > SSL_MAX_HOST_NAME_LEN )
4258 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4259 }
4260
4261 /* Now it's clear that we will overwrite the old hostname,
4262 * so we can free it safely */
4263
4264 if( ssl->hostname != NULL )
4265 {
4266 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
4267 polarssl_free( ssl->hostname );
4268 }
4269
4270 /* Passing NULL as hostname shall clear the old one */
4271
Paul Bakker5121ce52009-01-03 21:22:43 +00004272 if( hostname == NULL )
Hanno Beckerb9ac47c2017-05-05 13:07:33 +01004273 {
4274 ssl->hostname = NULL;
4275 ssl->hostname_len = 0;
4276 }
4277 else
4278 {
4279 ssl->hostname = polarssl_malloc( hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004280
Hanno Beckerb9ac47c2017-05-05 13:07:33 +01004281 if( ssl->hostname == NULL )
4282 {
4283 ssl->hostname_len = 0;
4284 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4285 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004286
Hanno Beckerb9ac47c2017-05-05 13:07:33 +01004287 memcpy( ssl->hostname, (const unsigned char*) hostname,
4288 hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004289
Hanno Beckerb9ac47c2017-05-05 13:07:33 +01004290 ssl->hostname[hostname_len] = '\0';
4291 ssl->hostname_len = hostname_len;
4292 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004293
4294 return( 0 );
4295}
4296
Paul Bakker5701cdc2012-09-27 21:49:42 +00004297void ssl_set_sni( ssl_context *ssl,
4298 int (*f_sni)(void *, ssl_context *,
4299 const unsigned char *, size_t),
4300 void *p_sni )
4301{
4302 ssl->f_sni = f_sni;
4303 ssl->p_sni = p_sni;
4304}
Paul Bakker0be444a2013-08-27 21:55:01 +02004305#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004306
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004307#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004308int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004309{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004310 size_t cur_len, tot_len;
4311 const char **p;
4312
4313 /*
4314 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4315 * truncated". Check lengths now rather than later.
4316 */
4317 tot_len = 0;
4318 for( p = protos; *p != NULL; p++ )
4319 {
4320 cur_len = strlen( *p );
4321 tot_len += cur_len;
4322
4323 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4324 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4325 }
4326
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004327 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004328
4329 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004330}
4331
4332const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4333{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004334 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004335}
4336#endif /* POLARSSL_SSL_ALPN */
4337
Paul Bakker490ecc82011-10-06 13:04:09 +00004338void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4339{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004340 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4341 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4342 {
4343 ssl->max_major_ver = major;
4344 ssl->max_minor_ver = minor;
4345 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004346}
4347
Paul Bakker1d29fb52012-09-28 13:28:45 +00004348void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4349{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004350 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4351 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4352 {
4353 ssl->min_major_ver = major;
4354 ssl->min_minor_ver = minor;
4355 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004356}
4357
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004358#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4359void ssl_set_fallback( ssl_context *ssl, char fallback )
4360{
4361 ssl->fallback = fallback;
4362}
4363#endif
4364
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004365#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4366void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
4367{
4368 ssl->encrypt_then_mac = etm;
4369}
4370#endif
4371
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004372#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4373void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4374{
4375 ssl->extended_ms = ems;
4376}
4377#endif
4378
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004379void ssl_set_arc4_support( ssl_context *ssl, char arc4 )
4380{
4381 ssl->arc4_disabled = arc4;
4382}
4383
Paul Bakker05decb22013-08-15 13:33:48 +02004384#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004385int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4386{
Paul Bakker77e257e2013-12-16 15:29:52 +01004387 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004388 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004389 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004390 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004391 }
4392
4393 ssl->mfl_code = mfl_code;
4394
4395 return( 0 );
4396}
Paul Bakker05decb22013-08-15 13:33:48 +02004397#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004398
Paul Bakker1f2bc622013-08-15 13:45:55 +02004399#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004400int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004401{
Paul Bakker8c1ede62013-07-19 14:14:37 +02004402 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004403
4404 return( 0 );
4405}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004406#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004407
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004408#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4409void ssl_set_cbc_record_splitting( ssl_context *ssl, char split )
4410{
4411 ssl->split_done = split;
4412}
4413#endif
4414
Paul Bakker48916f92012-09-16 19:57:18 +00004415void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4416{
4417 ssl->allow_legacy_renegotiation = allow_legacy;
4418}
4419
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004420#if defined(POLARSSL_SSL_RENEGOTIATION)
4421void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4422{
4423 ssl->disable_renegotiation = renegotiation;
4424}
4425
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004426void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4427{
4428 ssl->renego_max_records = max_records;
4429}
4430
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004431void ssl_set_renegotiation_period( ssl_context *ssl,
4432 const unsigned char period[8] )
4433{
4434 memcpy( ssl->renego_period, period, 8 );
4435}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004436#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004437
Paul Bakkera503a632013-08-14 13:48:06 +02004438#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004439int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4440{
4441 ssl->session_tickets = use_tickets;
4442
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004443#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004444 if( ssl->endpoint == SSL_IS_CLIENT )
4445 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004446#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004447
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01004448 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
4449 return( 0 );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004450
4451 if( ssl->f_rng == NULL )
4452 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4453
4454 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004455}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004456
4457void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4458{
4459 ssl->ticket_lifetime = lifetime;
4460}
Paul Bakkera503a632013-08-14 13:48:06 +02004461#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004462
Paul Bakker5121ce52009-01-03 21:22:43 +00004463/*
4464 * SSL get accessors
4465 */
4466size_t ssl_get_bytes_avail( const ssl_context *ssl )
4467{
4468 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4469}
4470
Paul Bakkerff60ee62010-03-16 21:09:09 +00004471int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004472{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00004473 if( ssl->session != NULL )
4474 return( ssl->session->verify_result );
4475
4476 if( ssl->session_negotiate != NULL )
4477 return( ssl->session_negotiate->verify_result );
4478
4479 return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004480}
4481
Paul Bakkere3166ce2011-01-27 17:40:50 +00004482const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004483{
Paul Bakker926c8e42013-03-06 10:23:34 +01004484 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004485 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004486
Paul Bakkere3166ce2011-01-27 17:40:50 +00004487 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004488}
4489
Paul Bakker43ca69c2011-01-15 17:35:19 +00004490const char *ssl_get_version( const ssl_context *ssl )
4491{
4492 switch( ssl->minor_ver )
4493 {
4494 case SSL_MINOR_VERSION_0:
4495 return( "SSLv3.0" );
4496
4497 case SSL_MINOR_VERSION_1:
4498 return( "TLSv1.0" );
4499
4500 case SSL_MINOR_VERSION_2:
4501 return( "TLSv1.1" );
4502
Paul Bakker1ef83d62012-04-11 12:09:53 +00004503 case SSL_MINOR_VERSION_3:
4504 return( "TLSv1.2" );
4505
Paul Bakker43ca69c2011-01-15 17:35:19 +00004506 default:
4507 break;
4508 }
4509 return( "unknown" );
4510}
4511
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004512#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004513const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004514{
4515 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004516 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004517
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004518 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004519}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004520#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004521
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004522#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004523int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4524{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004525 if( ssl == NULL ||
4526 dst == NULL ||
4527 ssl->session == NULL ||
4528 ssl->endpoint != SSL_IS_CLIENT )
4529 {
4530 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4531 }
4532
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004533 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004534}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004535#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004536
Paul Bakker5121ce52009-01-03 21:22:43 +00004537/*
Paul Bakker1961b702013-01-25 14:49:24 +01004538 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004539 */
Paul Bakker1961b702013-01-25 14:49:24 +01004540int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004541{
Paul Bakker40e46942009-01-03 21:51:57 +00004542 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004543
Paul Bakker40e46942009-01-03 21:51:57 +00004544#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004545 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004546 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004547#endif
Paul Bakker40e46942009-01-03 21:51:57 +00004548#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004549 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004550 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004551#endif
4552
Paul Bakker1961b702013-01-25 14:49:24 +01004553 return( ret );
4554}
4555
4556/*
4557 * Perform the SSL handshake
4558 */
4559int ssl_handshake( ssl_context *ssl )
4560{
4561 int ret = 0;
4562
4563 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4564
4565 while( ssl->state != SSL_HANDSHAKE_OVER )
4566 {
4567 ret = ssl_handshake_step( ssl );
4568
4569 if( ret != 0 )
4570 break;
4571 }
4572
Paul Bakker5121ce52009-01-03 21:22:43 +00004573 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4574
4575 return( ret );
4576}
4577
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004578#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004579#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004580/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004581 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004582 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004583static int ssl_write_hello_request( ssl_context *ssl )
4584{
4585 int ret;
4586
4587 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4588
4589 ssl->out_msglen = 4;
4590 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4591 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4592
4593 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4594 {
4595 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4596 return( ret );
4597 }
4598
4599 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4600
4601 return( 0 );
4602}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004603#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004604
4605/*
4606 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004607 * - any side: calling ssl_renegotiate(),
4608 * - client: receiving a HelloRequest during ssl_read(),
4609 * - server: receiving any handshake message on server during ssl_read() after
4610 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004611 * If the handshake doesn't complete due to waiting for I/O, it will continue
4612 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004613 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004614static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004615{
4616 int ret;
4617
4618 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4619
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004620 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4621 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004622
4623 ssl->state = SSL_HELLO_REQUEST;
4624 ssl->renegotiation = SSL_RENEGOTIATION;
4625
Paul Bakker48916f92012-09-16 19:57:18 +00004626 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4627 {
4628 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4629 return( ret );
4630 }
4631
4632 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4633
4634 return( 0 );
4635}
4636
4637/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004638 * Renegotiate current connection on client,
4639 * or request renegotiation on server
4640 */
4641int ssl_renegotiate( ssl_context *ssl )
4642{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004643 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004644
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004645#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004646 /* On server, just send the request */
4647 if( ssl->endpoint == SSL_IS_SERVER )
4648 {
4649 if( ssl->state != SSL_HANDSHAKE_OVER )
4650 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4651
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004652 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4653
4654 /* Did we already try/start sending HelloRequest? */
4655 if( ssl->out_left != 0 )
4656 return( ssl_flush_output( ssl ) );
4657
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004658 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004659 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004660#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004661
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004662#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004663 /*
4664 * On client, either start the renegotiation process or,
4665 * if already in progress, continue the handshake
4666 */
4667 if( ssl->renegotiation != SSL_RENEGOTIATION )
4668 {
4669 if( ssl->state != SSL_HANDSHAKE_OVER )
4670 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4671
4672 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4673 {
4674 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4675 return( ret );
4676 }
4677 }
4678 else
4679 {
4680 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4681 {
4682 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4683 return( ret );
4684 }
4685 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004686#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004687
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004688 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004689}
4690
4691/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004692 * Check record counters and renegotiate if they're above the limit.
4693 */
4694static int ssl_check_ctr_renegotiate( ssl_context *ssl )
4695{
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004696 if( ssl->state != SSL_HANDSHAKE_OVER ||
4697 ssl->renegotiation == SSL_RENEGOTIATION_PENDING ||
4698 ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED )
4699 {
4700 return( 0 );
4701 }
4702
4703 // TODO: adapt for DTLS
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004704 if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 &&
4705 memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004706 {
4707 return( 0 );
4708 }
4709
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004710 SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004711 return( ssl_renegotiate( ssl ) );
4712}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004713#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004714
4715/*
4716 * Receive application data decrypted from the SSL layer
4717 */
Paul Bakker23986e52011-04-24 08:57:21 +00004718int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004719{
Hanno Beckerd37839e2017-06-08 15:56:50 +01004720 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00004721 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004722
4723 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4724
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004725#if defined(POLARSSL_SSL_RENEGOTIATION)
Hanno Beckerd37839e2017-06-08 15:56:50 +01004726 ret = ssl_check_ctr_renegotiate( ssl );
4727 if( ret != POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
4728 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004729 {
4730 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4731 return( ret );
4732 }
4733#endif
4734
Paul Bakker5121ce52009-01-03 21:22:43 +00004735 if( ssl->state != SSL_HANDSHAKE_OVER )
4736 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004737 ret = ssl_handshake( ssl );
Hanno Beckerd37839e2017-06-08 15:56:50 +01004738 if( ret != POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
4739 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004740 {
4741 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4742 return( ret );
4743 }
4744 }
4745
4746 if( ssl->in_offt == NULL )
4747 {
Hanno Beckerd37839e2017-06-08 15:56:50 +01004748 if( ( ret = ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004749 {
Hanno Beckerd37839e2017-06-08 15:56:50 +01004750 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4751 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004752
Hanno Beckerd37839e2017-06-08 15:56:50 +01004753 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4754 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004755 }
4756
4757 if( ssl->in_msglen == 0 &&
4758 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4759 {
4760 /*
4761 * OpenSSL sends empty messages to randomize the IV
4762 */
4763 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4764 {
Paul Bakker831a7552011-05-18 13:32:51 +00004765 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4766 return( 0 );
4767
Paul Bakker5121ce52009-01-03 21:22:43 +00004768 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4769 return( ret );
4770 }
4771 }
4772
Paul Bakker48916f92012-09-16 19:57:18 +00004773 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4774 {
4775 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4776
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004777#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004778 if( ssl->endpoint == SSL_IS_CLIENT &&
4779 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4780 ssl->in_hslen != 4 ) )
4781 {
4782 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4783 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4784 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004785#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004786
Hanno Beckerbfd09912017-10-25 09:34:48 +01004787#if defined(POLARSSL_SSL_RENEGOTIATION)
Hanno Becker268191a2017-10-25 09:33:22 +01004788 if( ! ( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4789 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
4790 ssl->allow_legacy_renegotiation ==
4791 SSL_LEGACY_NO_RENEGOTIATION ) ) )
4792 {
4793 ret = ssl_start_renegotiation( ssl );
4794 if( ret != POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
4795 ret != 0 )
4796 {
4797 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4798 return( ret );
4799 }
4800 }
4801 else
Hanno Beckerbfd09912017-10-25 09:34:48 +01004802#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00004803 {
4804 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4805
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004806#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004807 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004808 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004809 /*
4810 * SSLv3 does not have a "no_renegotiation" alert
4811 */
4812 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4813 return( ret );
4814 }
4815 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004816#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004817#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4818 defined(POLARSSL_SSL_PROTO_TLS1_2)
4819 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004820 {
4821 if( ( ret = ssl_send_alert_message( ssl,
4822 SSL_ALERT_LEVEL_WARNING,
4823 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4824 {
4825 return( ret );
4826 }
Paul Bakker48916f92012-09-16 19:57:18 +00004827 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004828 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004829#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4830 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004831 {
4832 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004833 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004834 }
Paul Bakker48916f92012-09-16 19:57:18 +00004835 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004836
Hanno Beckerd37839e2017-06-08 15:56:50 +01004837 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004838 }
Hanno Beckerbfd09912017-10-25 09:34:48 +01004839#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004840 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4841 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004842 ssl->renego_records_seen++;
4843
4844 if( ssl->renego_max_records >= 0 &&
4845 ssl->renego_records_seen > ssl->renego_max_records )
4846 {
4847 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4848 "but not honored by client" ) );
4849 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4850 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004851 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004852#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004853
4854 /* Fatal and closure alerts handled by ssl_read_record() */
4855 if( ssl->in_msgtype == SSL_MSG_ALERT )
4856 {
4857 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4858 return( POLARSSL_ERR_NET_WANT_READ );
4859 }
4860
4861 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004862 {
4863 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004864 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004865 }
4866
4867 ssl->in_offt = ssl->in_msg;
4868 }
4869
4870 n = ( len < ssl->in_msglen )
4871 ? len : ssl->in_msglen;
4872
4873 memcpy( buf, ssl->in_offt, n );
4874 ssl->in_msglen -= n;
4875
4876 if( ssl->in_msglen == 0 )
Hanno Becker0401a3d2017-06-09 10:52:45 +01004877 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004878 /* all bytes consumed */
4879 ssl->in_offt = NULL;
Hanno Becker0401a3d2017-06-09 10:52:45 +01004880 ssl->keep_current_message = 0;
4881 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004882 else
4883 /* more data available */
4884 ssl->in_offt += n;
4885
4886 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4887
Paul Bakker23986e52011-04-24 08:57:21 +00004888 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004889}
4890
4891/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004892 * Send application data to be encrypted by the SSL layer,
4893 * taking care of max fragment length and buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +00004894 */
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004895static int ssl_write_real( ssl_context *ssl,
4896 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004897{
Paul Bakker23986e52011-04-24 08:57:21 +00004898 int ret;
4899 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004900 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004901
Paul Bakker05decb22013-08-15 13:33:48 +02004902#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004903 /*
4904 * Assume mfl_code is correct since it was checked when set
4905 */
4906 max_len = mfl_code_to_length[ssl->mfl_code];
4907
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004908 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004909 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004910 */
4911 if( ssl->session_out != NULL &&
4912 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4913 {
4914 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4915 }
Paul Bakker05decb22013-08-15 13:33:48 +02004916#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004917
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004918 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004919
Paul Bakker5121ce52009-01-03 21:22:43 +00004920 if( ssl->out_left != 0 )
4921 {
4922 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4923 {
4924 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4925 return( ret );
4926 }
4927 }
Paul Bakker887bd502011-06-08 13:10:54 +00004928 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004929 {
Paul Bakker887bd502011-06-08 13:10:54 +00004930 ssl->out_msglen = n;
4931 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4932 memcpy( ssl->out_msg, buf, n );
4933
4934 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4935 {
4936 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4937 return( ret );
4938 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004939 }
4940
Paul Bakker23986e52011-04-24 08:57:21 +00004941 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004942}
4943
4944/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004945 * Write application data, doing 1/n-1 splitting if necessary.
4946 *
4947 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004948 * then the caller will call us again with the same arguments, so
4949 * remember wether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004950 */
4951#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004952static int ssl_write_split( ssl_context *ssl,
4953 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004954{
4955 int ret;
4956
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004957 if( ssl->split_done == SSL_CBC_RECORD_SPLITTING_DISABLED ||
4958 len <= 1 ||
4959 ssl->minor_ver > SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004960 cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
4961 != POLARSSL_MODE_CBC )
4962 {
4963 return( ssl_write_real( ssl, buf, len ) );
4964 }
4965
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004966 if( ssl->split_done == 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004967 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004968 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004969 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004970 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004971 }
4972
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004973 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
4974 return( ret );
4975 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004976
4977 return( ret + 1 );
4978}
4979#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */
4980
4981/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004982 * Write application data (public-facing wrapper)
4983 */
4984int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
4985{
4986 int ret;
4987
4988 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4989
4990#if defined(POLARSSL_SSL_RENEGOTIATION)
4991 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4992 {
4993 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4994 return( ret );
4995 }
4996#endif
4997
4998 if( ssl->state != SSL_HANDSHAKE_OVER )
4999 {
5000 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5001 {
5002 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5003 return( ret );
5004 }
5005 }
5006
5007#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
5008 ret = ssl_write_split( ssl, buf, len );
5009#else
5010 ret = ssl_write_real( ssl, buf, len );
5011#endif
5012
5013 SSL_DEBUG_MSG( 2, ( "<= write" ) );
5014
5015 return( ret );
5016}
5017
5018/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005019 * Notify the peer that the connection is being closed
5020 */
5021int ssl_close_notify( ssl_context *ssl )
5022{
5023 int ret;
5024
5025 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
5026
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005027 if( ssl->out_left != 0 )
5028 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005029
5030 if( ssl->state == SSL_HANDSHAKE_OVER )
5031 {
Paul Bakker48916f92012-09-16 19:57:18 +00005032 if( ( ret = ssl_send_alert_message( ssl,
5033 SSL_ALERT_LEVEL_WARNING,
5034 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005035 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005036 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005037 return( ret );
5038 }
5039 }
5040
5041 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
5042
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005043 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005044}
5045
Paul Bakker48916f92012-09-16 19:57:18 +00005046void ssl_transform_free( ssl_transform *transform )
5047{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005048 if( transform == NULL )
5049 return;
5050
Paul Bakker48916f92012-09-16 19:57:18 +00005051#if defined(POLARSSL_ZLIB_SUPPORT)
5052 deflateEnd( &transform->ctx_deflate );
5053 inflateEnd( &transform->ctx_inflate );
5054#endif
5055
Paul Bakker84bbeb52014-07-01 14:53:22 +02005056 cipher_free( &transform->cipher_ctx_enc );
5057 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005058
Paul Bakker84bbeb52014-07-01 14:53:22 +02005059 md_free( &transform->md_ctx_enc );
5060 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02005061
Paul Bakker34617722014-06-13 17:20:13 +02005062 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005063}
5064
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005065#if defined(POLARSSL_X509_CRT_PARSE_C)
5066static void ssl_key_cert_free( ssl_key_cert *key_cert )
5067{
5068 ssl_key_cert *cur = key_cert, *next;
5069
5070 while( cur != NULL )
5071 {
5072 next = cur->next;
5073
5074 if( cur->key_own_alloc )
5075 {
5076 pk_free( cur->key );
5077 polarssl_free( cur->key );
5078 }
5079 polarssl_free( cur );
5080
5081 cur = next;
5082 }
5083}
5084#endif /* POLARSSL_X509_CRT_PARSE_C */
5085
Paul Bakker48916f92012-09-16 19:57:18 +00005086void ssl_handshake_free( ssl_handshake_params *handshake )
5087{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005088 if( handshake == NULL )
5089 return;
5090
Paul Bakker48916f92012-09-16 19:57:18 +00005091#if defined(POLARSSL_DHM_C)
5092 dhm_free( &handshake->dhm_ctx );
5093#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005094#if defined(POLARSSL_ECDH_C)
5095 ecdh_free( &handshake->ecdh_ctx );
5096#endif
5097
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005098#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02005099 /* explicit void pointer cast for buggy MS compiler */
5100 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005101#endif
5102
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02005103#if defined(POLARSSL_X509_CRT_PARSE_C) && \
5104 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
5105 /*
5106 * Free only the linked list wrapper, not the keys themselves
5107 * since the belong to the SNI callback
5108 */
5109 if( handshake->sni_key_cert != NULL )
5110 {
5111 ssl_key_cert *cur = handshake->sni_key_cert, *next;
5112
5113 while( cur != NULL )
5114 {
5115 next = cur->next;
5116 polarssl_free( cur );
5117 cur = next;
5118 }
5119 }
Paul Bakker9af723c2014-05-01 13:03:14 +02005120#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005121
Paul Bakker34617722014-06-13 17:20:13 +02005122 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005123}
5124
5125void ssl_session_free( ssl_session *session )
5126{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005127 if( session == NULL )
5128 return;
5129
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005130#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00005131 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00005132 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005133 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02005134 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00005135 }
Paul Bakkered27a042013-04-18 22:46:23 +02005136#endif
Paul Bakker0a597072012-09-25 21:55:46 +00005137
Paul Bakkera503a632013-08-14 13:48:06 +02005138#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005139 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02005140#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005141
Paul Bakker34617722014-06-13 17:20:13 +02005142 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005143}
5144
Paul Bakker5121ce52009-01-03 21:22:43 +00005145/*
5146 * Free an SSL context
5147 */
5148void ssl_free( ssl_context *ssl )
5149{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005150 if( ssl == NULL )
5151 return;
5152
Paul Bakker5121ce52009-01-03 21:22:43 +00005153 SSL_DEBUG_MSG( 2, ( "=> free" ) );
5154
Paul Bakker5121ce52009-01-03 21:22:43 +00005155 if( ssl->out_ctr != NULL )
5156 {
Paul Bakker34617722014-06-13 17:20:13 +02005157 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005158 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005159 }
5160
5161 if( ssl->in_ctr != NULL )
5162 {
Paul Bakker34617722014-06-13 17:20:13 +02005163 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005164 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005165 }
5166
Paul Bakker16770332013-10-11 09:59:44 +02005167#if defined(POLARSSL_ZLIB_SUPPORT)
5168 if( ssl->compress_buf != NULL )
5169 {
Paul Bakker34617722014-06-13 17:20:13 +02005170 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02005171 polarssl_free( ssl->compress_buf );
5172 }
5173#endif
5174
Paul Bakker40e46942009-01-03 21:51:57 +00005175#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00005176 mpi_free( &ssl->dhm_P );
5177 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00005178#endif
5179
Paul Bakker48916f92012-09-16 19:57:18 +00005180 if( ssl->transform )
5181 {
5182 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005183 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005184 }
5185
5186 if( ssl->handshake )
5187 {
5188 ssl_handshake_free( ssl->handshake );
5189 ssl_transform_free( ssl->transform_negotiate );
5190 ssl_session_free( ssl->session_negotiate );
5191
Paul Bakker6e339b52013-07-03 13:37:05 +02005192 polarssl_free( ssl->handshake );
5193 polarssl_free( ssl->transform_negotiate );
5194 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00005195 }
5196
Paul Bakkerc0463502013-02-14 11:19:38 +01005197 if( ssl->session )
5198 {
5199 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005200 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005201 }
5202
Paul Bakkera503a632013-08-14 13:48:06 +02005203#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005204 if( ssl->ticket_keys )
5205 {
5206 ssl_ticket_keys_free( ssl->ticket_keys );
5207 polarssl_free( ssl->ticket_keys );
5208 }
Paul Bakkera503a632013-08-14 13:48:06 +02005209#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005210
Paul Bakker0be444a2013-08-27 21:55:01 +02005211#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02005212 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005213 {
Paul Bakker34617722014-06-13 17:20:13 +02005214 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02005215 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00005216 ssl->hostname_len = 0;
5217 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005218#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005219
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005220#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005221 if( ssl->psk != NULL )
5222 {
Paul Bakker34617722014-06-13 17:20:13 +02005223 polarssl_zeroize( ssl->psk, ssl->psk_len );
5224 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005225 polarssl_free( ssl->psk );
5226 polarssl_free( ssl->psk_identity );
5227 ssl->psk_len = 0;
5228 ssl->psk_identity_len = 0;
5229 }
5230#endif
5231
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005232#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005233 ssl_key_cert_free( ssl->key_cert );
5234#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005235
Paul Bakker05ef8352012-05-08 09:17:57 +00005236#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
5237 if( ssl_hw_record_finish != NULL )
5238 {
5239 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
5240 ssl_hw_record_finish( ssl );
5241 }
5242#endif
5243
Paul Bakker5121ce52009-01-03 21:22:43 +00005244 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00005245
Paul Bakker86f04f42013-02-14 11:20:09 +01005246 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02005247 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005248}
5249
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005250#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005251/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005252 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005253 */
5254unsigned char ssl_sig_from_pk( pk_context *pk )
5255{
5256#if defined(POLARSSL_RSA_C)
5257 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
5258 return( SSL_SIG_RSA );
5259#endif
5260#if defined(POLARSSL_ECDSA_C)
5261 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
5262 return( SSL_SIG_ECDSA );
5263#endif
5264 return( SSL_SIG_ANON );
5265}
5266
Hanno Beckerc2b9d982017-05-10 16:37:21 +01005267unsigned char ssl_sig_from_pk_alg( pk_type_t type )
5268{
5269 switch( type ) {
5270 case POLARSSL_PK_RSA:
5271 return( SSL_SIG_RSA );
5272 case POLARSSL_PK_ECDSA:
5273 case POLARSSL_PK_ECKEY:
5274 return( SSL_SIG_ECDSA );
5275 default:
5276 return( SSL_SIG_ANON );
5277 }
5278}
5279
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005280pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
5281{
5282 switch( sig )
5283 {
5284#if defined(POLARSSL_RSA_C)
5285 case SSL_SIG_RSA:
5286 return( POLARSSL_PK_RSA );
5287#endif
5288#if defined(POLARSSL_ECDSA_C)
5289 case SSL_SIG_ECDSA:
5290 return( POLARSSL_PK_ECDSA );
5291#endif
5292 default:
5293 return( POLARSSL_PK_NONE );
5294 }
5295}
Paul Bakker9af723c2014-05-01 13:03:14 +02005296#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005297
Hanno Beckerc2b9d982017-05-10 16:37:21 +01005298#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
5299 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
5300
5301/* Find an entry in a signature-hash set matching a given hash algorithm. */
5302md_type_t ssl_sig_hash_set_find( ssl_sig_hash_set_t *set,
5303 pk_type_t sig_alg )
5304{
5305 switch( sig_alg )
5306 {
5307 case POLARSSL_PK_RSA:
5308 return( set->rsa );
5309 case POLARSSL_PK_ECDSA:
5310 return( set->ecdsa );
5311 default:
5312 return( POLARSSL_MD_NONE );
5313 }
5314}
5315
5316/* Add a signature-hash-pair to a signature-hash set */
5317void ssl_sig_hash_set_add( ssl_sig_hash_set_t *set,
5318 pk_type_t sig_alg,
5319 md_type_t md_alg )
5320{
5321 switch( sig_alg )
5322 {
5323 case POLARSSL_PK_RSA:
5324 if( set->rsa == POLARSSL_MD_NONE )
5325 set->rsa = md_alg;
5326 break;
5327
5328 case POLARSSL_PK_ECDSA:
5329 if( set->ecdsa == POLARSSL_MD_NONE )
5330 set->ecdsa = md_alg;
5331 break;
5332
5333 default:
5334 break;
5335 }
5336}
5337
5338/* Allow exactly one hash algorithm for each signature. */
5339void ssl_sig_hash_set_const_hash( ssl_sig_hash_set_t *set,
5340 md_type_t md_alg )
5341{
5342 set->rsa = md_alg;
5343 set->ecdsa = md_alg;
5344}
5345
5346#endif /* POLARSSL_SSL_PROTO_TLS1_2) &&
5347 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
5348
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005349/*
5350 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
5351 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005352md_type_t ssl_md_alg_from_hash( unsigned char hash )
5353{
5354 switch( hash )
5355 {
5356#if defined(POLARSSL_MD5_C)
5357 case SSL_HASH_MD5:
5358 return( POLARSSL_MD_MD5 );
5359#endif
5360#if defined(POLARSSL_SHA1_C)
5361 case SSL_HASH_SHA1:
5362 return( POLARSSL_MD_SHA1 );
5363#endif
5364#if defined(POLARSSL_SHA256_C)
5365 case SSL_HASH_SHA224:
5366 return( POLARSSL_MD_SHA224 );
5367 case SSL_HASH_SHA256:
5368 return( POLARSSL_MD_SHA256 );
5369#endif
5370#if defined(POLARSSL_SHA512_C)
5371 case SSL_HASH_SHA384:
5372 return( POLARSSL_MD_SHA384 );
5373 case SSL_HASH_SHA512:
5374 return( POLARSSL_MD_SHA512 );
5375#endif
5376 default:
5377 return( POLARSSL_MD_NONE );
5378 }
5379}
5380
Hanno Beckerc2b9d982017-05-10 16:37:21 +01005381/*
5382 * Convert from POLARSSL_MD_XXX to SSL_HASH_XXX
5383 */
5384unsigned char ssl_hash_from_md_alg( md_type_t md )
5385{
5386 switch( md )
5387 {
5388#if defined(POLARSSL_MD5_C)
5389 case POLARSSL_MD_MD5:
5390 return( SSL_HASH_MD5 );
5391#endif
5392#if defined(POLARSSL_SHA1_C)
5393 case POLARSSL_MD_SHA1:
5394 return( SSL_HASH_SHA1 );
5395#endif
5396#if defined(POLARSSL_SHA256_C)
5397 case POLARSSL_MD_SHA224:
5398 return( SSL_HASH_SHA224 );
5399 case POLARSSL_MD_SHA256:
5400 return( SSL_HASH_SHA256 );
5401#endif
5402#if defined(POLARSSL_SHA512_C)
5403 case POLARSSL_MD_SHA384:
5404 return( SSL_HASH_SHA384 );
5405 case POLARSSL_MD_SHA512:
5406 return( SSL_HASH_SHA512 );
5407#endif
5408 default:
5409 return( SSL_HASH_NONE );
5410 }
5411}
5412
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005413#if defined(POLARSSL_SSL_SET_CURVES)
5414/*
5415 * Check is a curve proposed by the peer is in our list.
5416 * Return 1 if we're willing to use it, 0 otherwise.
5417 */
5418int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
5419{
5420 const ecp_group_id *gid;
5421
5422 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5423 if( *gid == grp_id )
5424 return( 1 );
5425
5426 return( 0 );
5427}
Paul Bakker9af723c2014-05-01 13:03:14 +02005428#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005429
Hanno Beckerc2b9d982017-05-10 16:37:21 +01005430#if defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
5431/*
5432 * Check if a hash proposed by the peer is in our list.
5433 * Return 0 if we're willing to use it, -1 otherwise.
5434 */
5435int ssl_check_sig_hash( md_type_t md )
5436{
5437 const int *cur;
5438
5439 for( cur = md_list(); *cur != POLARSSL_MD_NONE; cur++ )
5440 {
5441#if !defined(POLARSSL_SSL_ENABLE_MD5_SIGNATURES)
5442 /* Skip MD5 */
5443 if( *cur == POLARSSL_MD_MD5 )
5444 continue;
5445#endif
5446 if( *cur == (int) md )
5447 return( 0 );
5448 }
5449
5450 return( -1 );
5451}
5452#endif /* POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
5453
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005454#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005455int ssl_check_cert_usage( const x509_crt *cert,
5456 const ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005457 int cert_endpoint,
5458 int *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005459{
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005460 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005461#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5462 int usage = 0;
5463#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005464#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5465 const char *ext_oid;
5466 size_t ext_len;
5467#endif
5468
5469#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5470 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5471 ((void) cert);
5472 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005473 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005474#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005475
5476#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5477 if( cert_endpoint == SSL_IS_SERVER )
5478 {
5479 /* Server part of the key exchange */
5480 switch( ciphersuite->key_exchange )
5481 {
5482 case POLARSSL_KEY_EXCHANGE_RSA:
5483 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5484 usage = KU_KEY_ENCIPHERMENT;
5485 break;
5486
5487 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5488 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5489 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5490 usage = KU_DIGITAL_SIGNATURE;
5491 break;
5492
5493 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5494 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5495 usage = KU_KEY_AGREEMENT;
5496 break;
5497
5498 /* Don't use default: we want warnings when adding new values */
5499 case POLARSSL_KEY_EXCHANGE_NONE:
5500 case POLARSSL_KEY_EXCHANGE_PSK:
5501 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5502 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5503 usage = 0;
5504 }
5505 }
5506 else
5507 {
5508 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5509 usage = KU_DIGITAL_SIGNATURE;
5510 }
5511
5512 if( x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005513 {
5514 *flags |= BADCERT_KEY_USAGE;
5515 ret = -1;
5516 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005517#else
5518 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005519#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5520
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005521#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5522 if( cert_endpoint == SSL_IS_SERVER )
5523 {
5524 ext_oid = OID_SERVER_AUTH;
5525 ext_len = OID_SIZE( OID_SERVER_AUTH );
5526 }
5527 else
5528 {
5529 ext_oid = OID_CLIENT_AUTH;
5530 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5531 }
5532
5533 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005534 {
5535 *flags |= BADCERT_EXT_KEY_USAGE;
5536 ret = -1;
5537 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005538#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5539
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005540 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005541}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005542#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005543
5544#endif /* POLARSSL_SSL_TLS_C */