blob: 25f3a02a1316deedf818e549a58594cc856fbda3 [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 Bakkere0ccd0a2009-01-04 16:27:10 +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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/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
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/debug.h"
40#include "mbedtls/ssl.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020041
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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020047#endif
48
Paul Bakker7dc4c442014-02-01 22:50:26 +010049#if defined(POLARSSL_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/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
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010067/* Length of the "epoch" field in the record header */
68static inline size_t ssl_ep_len( const ssl_context *ssl )
69{
70#if defined(POLARSSL_SSL_PROTO_DTLS)
71 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
72 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010073#else
74 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010075#endif
76 return( 0 );
77}
78
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020079
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +020080#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081/*
82 * Start a timer.
83 * Passing millisecs = 0 cancels a running timer.
84 * The timer is already running iff time_limit != 0.
85 */
Manuel Pégourié-Gonnard46fb9422014-10-02 18:10:40 +020086static void ssl_set_timer( ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020087{
88 ssl->time_limit = millisecs;
89 get_timer( &ssl->time_info, 1 );
90}
91
92/*
93 * Return -1 is timer is expired, 0 if it isn't.
94 */
Manuel Pégourié-Gonnard46fb9422014-10-02 18:10:40 +020095static int ssl_check_timer( ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020096{
97 if( ssl->time_limit != 0 &&
98 get_timer( &ssl->time_info, 0 ) > ssl->time_limit )
99 {
100 return( -1 );
101 }
102
103 return( 0 );
104}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200105
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200106/*
107 * Double the retransmit timeout value, within the allowed range,
108 * returning -1 if the maximum value has already been reached.
109 */
110static int ssl_double_retransmit_timeout( ssl_context *ssl )
111{
112 uint32_t new_timeout;
113
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200114 if( ssl->handshake->retransmit_timeout >= ssl->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200115 return( -1 );
116
117 new_timeout = 2 * ssl->handshake->retransmit_timeout;
118
119 /* Avoid arithmetic overflow and range overflow */
120 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200121 new_timeout > ssl->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200122 {
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200123 new_timeout = ssl->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200124 }
125
126 ssl->handshake->retransmit_timeout = new_timeout;
127 SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
128 ssl->handshake->retransmit_timeout ) );
129
130 return( 0 );
131}
132
133static void ssl_reset_retransmit_timeout( ssl_context *ssl )
134{
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200135 ssl->handshake->retransmit_timeout = ssl->hs_timeout_min;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200136 SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
137 ssl->handshake->retransmit_timeout ) );
138}
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +0200139#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200140
Paul Bakker05decb22013-08-15 13:33:48 +0200141#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200142/*
143 * Convert max_fragment_length codes to length.
144 * RFC 6066 says:
145 * enum{
146 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
147 * } MaxFragmentLength;
148 * and we add 0 -> extension unused
149 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200150static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200151{
152 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
153 512, /* SSL_MAX_FRAG_LEN_512 */
154 1024, /* SSL_MAX_FRAG_LEN_1024 */
155 2048, /* SSL_MAX_FRAG_LEN_2048 */
156 4096, /* SSL_MAX_FRAG_LEN_4096 */
157};
Paul Bakker05decb22013-08-15 13:33:48 +0200158#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200159
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200160static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
161{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200162 ssl_session_free( dst );
163 memcpy( dst, src, sizeof( ssl_session ) );
164
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200165#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200166 if( src->peer_cert != NULL )
167 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200168 int ret;
169
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500170 dst->peer_cert = polarssl_malloc( sizeof(x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200171 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200172 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
173
Paul Bakkerb6b09562013-09-18 14:17:41 +0200174 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200175
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200176 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
177 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200178 {
179 polarssl_free( dst->peer_cert );
180 dst->peer_cert = NULL;
181 return( ret );
182 }
183 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200184#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200185
Paul Bakkera503a632013-08-14 13:48:06 +0200186#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200187 if( src->ticket != NULL )
188 {
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500189 dst->ticket = polarssl_malloc( src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200190 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200191 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
192
193 memcpy( dst->ticket, src->ticket, src->ticket_len );
194 }
Paul Bakkera503a632013-08-14 13:48:06 +0200195#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200196
197 return( 0 );
198}
199
Paul Bakker05ef8352012-05-08 09:17:57 +0000200#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200201int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200202 const unsigned char *key_enc, const unsigned char *key_dec,
203 size_t keylen,
204 const unsigned char *iv_enc, const unsigned char *iv_dec,
205 size_t ivlen,
206 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200207 size_t maclen ) = NULL;
208int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
209int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
210int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
211int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
212int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200213#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000214
Paul Bakker5121ce52009-01-03 21:22:43 +0000215/*
216 * Key material generation
217 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200218#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200219static int ssl3_prf( const unsigned char *secret, size_t slen,
220 const char *label,
221 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000222 unsigned char *dstbuf, size_t dlen )
223{
224 size_t i;
225 md5_context md5;
226 sha1_context sha1;
227 unsigned char padding[16];
228 unsigned char sha1sum[20];
229 ((void)label);
230
Paul Bakker5b4af392014-06-26 12:09:34 +0200231 md5_init( &md5 );
232 sha1_init( &sha1 );
233
Paul Bakker5f70b252012-09-13 14:23:06 +0000234 /*
235 * SSLv3:
236 * block =
237 * MD5( secret + SHA1( 'A' + secret + random ) ) +
238 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
239 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
240 * ...
241 */
242 for( i = 0; i < dlen / 16; i++ )
243 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200244 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000245
246 sha1_starts( &sha1 );
247 sha1_update( &sha1, padding, 1 + i );
248 sha1_update( &sha1, secret, slen );
249 sha1_update( &sha1, random, rlen );
250 sha1_finish( &sha1, sha1sum );
251
252 md5_starts( &md5 );
253 md5_update( &md5, secret, slen );
254 md5_update( &md5, sha1sum, 20 );
255 md5_finish( &md5, dstbuf + i * 16 );
256 }
257
Paul Bakker5b4af392014-06-26 12:09:34 +0200258 md5_free( &md5 );
259 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000260
Paul Bakker34617722014-06-13 17:20:13 +0200261 polarssl_zeroize( padding, sizeof( padding ) );
262 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000263
264 return( 0 );
265}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200266#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000267
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200268#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200269static int tls1_prf( const unsigned char *secret, size_t slen,
270 const char *label,
271 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000272 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000273{
Paul Bakker23986e52011-04-24 08:57:21 +0000274 size_t nb, hs;
275 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200276 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 unsigned char tmp[128];
278 unsigned char h_i[20];
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100279 const md_info_t *md_info;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100280 md_context_t md_ctx;
281 int ret;
282
283 md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000284
285 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000286 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000287
288 hs = ( slen + 1 ) / 2;
289 S1 = secret;
290 S2 = secret + slen - hs;
291
292 nb = strlen( label );
293 memcpy( tmp + 20, label, nb );
294 memcpy( tmp + 20 + nb, random, rlen );
295 nb += rlen;
296
297 /*
298 * First compute P_md5(secret,label+random)[0..dlen]
299 */
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100300 if( ( md_info = md_info_from_type( POLARSSL_MD_MD5 ) ) == NULL )
301 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
302
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100303 if( ( ret = md_setup( &md_ctx, md_info, 1 ) ) != 0 )
304 return( ret );
305
306 md_hmac_starts( &md_ctx, S1, hs );
307 md_hmac_update( &md_ctx, tmp + 20, nb );
308 md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000309
310 for( i = 0; i < dlen; i += 16 )
311 {
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100312 md_hmac_reset ( &md_ctx );
313 md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
314 md_hmac_finish( &md_ctx, h_i );
315
316 md_hmac_reset ( &md_ctx );
317 md_hmac_update( &md_ctx, 4 + tmp, 16 );
318 md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000319
320 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
321
322 for( j = 0; j < k; j++ )
323 dstbuf[i + j] = h_i[j];
324 }
325
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100326 md_free( &md_ctx );
327
Paul Bakker5121ce52009-01-03 21:22:43 +0000328 /*
329 * XOR out with P_sha1(secret,label+random)[0..dlen]
330 */
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100331 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA1 ) ) == NULL )
332 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
333
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100334 if( ( ret = md_setup( &md_ctx, md_info, 1 ) ) != 0 )
335 return( ret );
336
337 md_hmac_starts( &md_ctx, S2, hs );
338 md_hmac_update( &md_ctx, tmp + 20, nb );
339 md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000340
341 for( i = 0; i < dlen; i += 20 )
342 {
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100343 md_hmac_reset ( &md_ctx );
344 md_hmac_update( &md_ctx, tmp, 20 + nb );
345 md_hmac_finish( &md_ctx, h_i );
346
347 md_hmac_reset ( &md_ctx );
348 md_hmac_update( &md_ctx, tmp, 20 );
349 md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000350
351 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
352
353 for( j = 0; j < k; j++ )
354 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
355 }
356
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100357 md_free( &md_ctx );
358
Paul Bakker34617722014-06-13 17:20:13 +0200359 polarssl_zeroize( tmp, sizeof( tmp ) );
360 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000361
362 return( 0 );
363}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200364#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000365
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200366#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100367static int tls_prf_generic( md_type_t md_type,
368 const unsigned char *secret, size_t slen,
369 const char *label,
370 const unsigned char *random, size_t rlen,
371 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000372{
373 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100374 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000375 unsigned char tmp[128];
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100376 unsigned char h_i[POLARSSL_MD_MAX_SIZE];
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100377 const md_info_t *md_info;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100378 md_context_t md_ctx;
379 int ret;
380
381 md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000382
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100383 if( ( md_info = md_info_from_type( md_type ) ) == NULL )
384 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
385
386 md_len = md_get_size( md_info );
387
388 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000389 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
390
391 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100392 memcpy( tmp + md_len, label, nb );
393 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000394 nb += rlen;
395
396 /*
397 * Compute P_<hash>(secret, label + random)[0..dlen]
398 */
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100399 if ( ( ret = md_setup( &md_ctx, md_info, 1 ) ) != 0 )
400 return( ret );
401
402 md_hmac_starts( &md_ctx, secret, slen );
403 md_hmac_update( &md_ctx, tmp + md_len, nb );
404 md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100405
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100406 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000407 {
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100408 md_hmac_reset ( &md_ctx );
409 md_hmac_update( &md_ctx, tmp, md_len + nb );
410 md_hmac_finish( &md_ctx, h_i );
411
412 md_hmac_reset ( &md_ctx );
413 md_hmac_update( &md_ctx, tmp, md_len );
414 md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000415
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100416 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000417
418 for( j = 0; j < k; j++ )
419 dstbuf[i + j] = h_i[j];
420 }
421
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100422 md_free( &md_ctx );
423
Paul Bakker34617722014-06-13 17:20:13 +0200424 polarssl_zeroize( tmp, sizeof( tmp ) );
425 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000426
427 return( 0 );
428}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100429
430#if defined(POLARSSL_SHA256_C)
431static int tls_prf_sha256( const unsigned char *secret, size_t slen,
432 const char *label,
433 const unsigned char *random, size_t rlen,
434 unsigned char *dstbuf, size_t dlen )
435{
436 return( tls_prf_generic( POLARSSL_MD_SHA256, secret, slen,
437 label, random, rlen, dstbuf, dlen ) );
438}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200439#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000440
Paul Bakker9e36f042013-06-30 14:34:05 +0200441#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200442static int tls_prf_sha384( const unsigned char *secret, size_t slen,
443 const char *label,
444 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000445 unsigned char *dstbuf, size_t dlen )
446{
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100447 return( tls_prf_generic( POLARSSL_MD_SHA384, secret, slen,
448 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000449}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200450#endif /* POLARSSL_SHA512_C */
451#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000452
Paul Bakker66d5d072014-06-17 16:39:18 +0200453static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200454
455#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
456 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200457static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200458#endif
Paul Bakker380da532012-04-18 16:10:25 +0000459
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200460#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200461static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
462static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200463#endif
464
465#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200466static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
467static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200468#endif
469
470#if defined(POLARSSL_SSL_PROTO_TLS1_2)
471#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200472static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
473static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
474static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200475#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100476
Paul Bakker9e36f042013-06-30 14:34:05 +0200477#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200478static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
479static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
480static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100481#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200482#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000483
Paul Bakker5121ce52009-01-03 21:22:43 +0000484int ssl_derive_keys( ssl_context *ssl )
485{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200486 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000487 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000488 unsigned char keyblk[256];
489 unsigned char *key1;
490 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100491 unsigned char *mac_enc;
492 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200493 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100494 const cipher_info_t *cipher_info;
495 const md_info_t *md_info;
496
Paul Bakker48916f92012-09-16 19:57:18 +0000497 ssl_session *session = ssl->session_negotiate;
498 ssl_transform *transform = ssl->transform_negotiate;
499 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000500
501 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
502
Paul Bakker68884e32013-01-07 18:20:04 +0100503 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
504 if( cipher_info == NULL )
505 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200506 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100507 transform->ciphersuite_info->cipher ) );
508 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
509 }
510
511 md_info = md_info_from_type( transform->ciphersuite_info->mac );
512 if( md_info == NULL )
513 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200514 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100515 transform->ciphersuite_info->mac ) );
516 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
517 }
518
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000520 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000521 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200522#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000523 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000524 {
Paul Bakker48916f92012-09-16 19:57:18 +0000525 handshake->tls_prf = ssl3_prf;
526 handshake->calc_verify = ssl_calc_verify_ssl;
527 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000528 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200529 else
530#endif
531#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
532 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000533 {
Paul Bakker48916f92012-09-16 19:57:18 +0000534 handshake->tls_prf = tls1_prf;
535 handshake->calc_verify = ssl_calc_verify_tls;
536 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000537 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200538 else
539#endif
540#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200541#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200542 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
543 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000544 {
Paul Bakker48916f92012-09-16 19:57:18 +0000545 handshake->tls_prf = tls_prf_sha384;
546 handshake->calc_verify = ssl_calc_verify_tls_sha384;
547 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000548 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000549 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200550#endif
551#if defined(POLARSSL_SHA256_C)
552 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000553 {
Paul Bakker48916f92012-09-16 19:57:18 +0000554 handshake->tls_prf = tls_prf_sha256;
555 handshake->calc_verify = ssl_calc_verify_tls_sha256;
556 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000557 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200558 else
559#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200560#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200561 {
562 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200563 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200564 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000565
566 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 * SSLv3:
568 * master =
569 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
570 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
571 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200572 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200573 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 * master = PRF( premaster, "master secret", randbytes )[0..47]
575 */
Paul Bakker0a597072012-09-25 21:55:46 +0000576 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 {
Paul Bakker48916f92012-09-16 19:57:18 +0000578 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
579 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200581#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
582 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200583 {
584 unsigned char session_hash[48];
585 size_t hash_len;
586
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200587 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200588
589 ssl->handshake->calc_verify( ssl, session_hash );
590
591#if defined(POLARSSL_SSL_PROTO_TLS1_2)
592 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
593 {
594#if defined(POLARSSL_SHA512_C)
595 if( ssl->transform_negotiate->ciphersuite_info->mac ==
596 POLARSSL_MD_SHA384 )
597 {
598 hash_len = 48;
599 }
600 else
601#endif
602 hash_len = 32;
603 }
604 else
605#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
606 hash_len = 36;
607
608 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
609
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100610 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
611 "extended master secret",
612 session_hash, hash_len,
613 session->master, 48 );
614 if( ret != 0 )
615 {
616 SSL_DEBUG_RET( 1, "prf", ret );
617 return( ret );
618 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200619
620 }
621 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200622#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100623 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
624 "master secret",
625 handshake->randbytes, 64,
626 session->master, 48 );
627 if( ret != 0 )
628 {
629 SSL_DEBUG_RET( 1, "prf", ret );
630 return( ret );
631 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200632
Paul Bakker34617722014-06-13 17:20:13 +0200633 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000634 }
635 else
636 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
637
638 /*
639 * Swap the client and server random values.
640 */
Paul Bakker48916f92012-09-16 19:57:18 +0000641 memcpy( tmp, handshake->randbytes, 64 );
642 memcpy( handshake->randbytes, tmp + 32, 32 );
643 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200644 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000645
646 /*
647 * SSLv3:
648 * key block =
649 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
650 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
651 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
652 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
653 * ...
654 *
655 * TLSv1:
656 * key block = PRF( master, "key expansion", randbytes )
657 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100658 ret = handshake->tls_prf( session->master, 48, "key expansion",
659 handshake->randbytes, 64, keyblk, 256 );
660 if( ret != 0 )
661 {
662 SSL_DEBUG_RET( 1, "prf", ret );
663 return( ret );
664 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000665
Paul Bakker48916f92012-09-16 19:57:18 +0000666 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
667 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
668 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
669 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000670 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
671
Paul Bakker34617722014-06-13 17:20:13 +0200672 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000673
674 /*
675 * Determine the appropriate key, IV and MAC length.
676 */
Paul Bakker68884e32013-01-07 18:20:04 +0100677
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200678 transform->keylen = cipher_info->key_length / 8;
679
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200680 if( cipher_info->mode == POLARSSL_MODE_GCM ||
681 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000682 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200683 transform->maclen = 0;
684
Paul Bakker68884e32013-01-07 18:20:04 +0100685 transform->ivlen = 12;
686 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200687
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200688 /* Minimum length is expicit IV + tag */
689 transform->minlen = transform->ivlen - transform->fixed_ivlen
690 + ( transform->ciphersuite_info->flags &
691 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100692 }
693 else
694 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200695 int ret;
696
697 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnardabb67442015-03-25 16:29:51 +0100698 if( ( ret = md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
699 ( ret = md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100700 {
Manuel Pégourié-Gonnardabb67442015-03-25 16:29:51 +0100701 SSL_DEBUG_RET( 1, "md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200702 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100703 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000704
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200705 /* Get MAC length */
706 transform->maclen = md_get_size( md_info );
707
708#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
709 /*
710 * If HMAC is to be truncated, we shall keep the leftmost bytes,
711 * (rfc 6066 page 13 or rfc 2104 section 4),
712 * so we only need to adjust the length here.
713 */
714 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
715 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
716#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
717
718 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100719 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000720
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200721 /* Minimum length */
722 if( cipher_info->mode == POLARSSL_MODE_STREAM )
723 transform->minlen = transform->maclen;
724 else
Paul Bakker68884e32013-01-07 18:20:04 +0100725 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200726 /*
727 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100728 * 1. if EtM is in use: one block plus MAC
729 * otherwise: * first multiple of blocklen greater than maclen
730 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200731 */
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100732#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
733 if( session->encrypt_then_mac == SSL_ETM_ENABLED )
734 {
735 transform->minlen = transform->maclen
736 + cipher_info->block_size;
737 }
738 else
739#endif
740 {
741 transform->minlen = transform->maclen
742 + cipher_info->block_size
743 - transform->maclen % cipher_info->block_size;
744 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200745
746#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
747 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
748 ssl->minor_ver == SSL_MINOR_VERSION_1 )
749 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100750 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200751#endif
752#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
753 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
754 ssl->minor_ver == SSL_MINOR_VERSION_3 )
755 {
756 transform->minlen += transform->ivlen;
757 }
758 else
759#endif
760 {
761 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
762 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
763 }
Paul Bakker68884e32013-01-07 18:20:04 +0100764 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000765 }
766
767 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000768 transform->keylen, transform->minlen, transform->ivlen,
769 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000770
771 /*
772 * Finally setup the cipher contexts, IVs and MAC secrets.
773 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100774#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000775 if( ssl->endpoint == SSL_IS_CLIENT )
776 {
Paul Bakker48916f92012-09-16 19:57:18 +0000777 key1 = keyblk + transform->maclen * 2;
778 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000779
Paul Bakker68884e32013-01-07 18:20:04 +0100780 mac_enc = keyblk;
781 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000782
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000783 /*
784 * This is not used in TLS v1.1.
785 */
Paul Bakker48916f92012-09-16 19:57:18 +0000786 iv_copy_len = ( transform->fixed_ivlen ) ?
787 transform->fixed_ivlen : transform->ivlen;
788 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
789 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000790 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000791 }
792 else
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100793#endif /* POLARSSL_SSL_CLI_C */
794#if defined(POLARSSL_SSL_SRV_C)
795 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000796 {
Paul Bakker48916f92012-09-16 19:57:18 +0000797 key1 = keyblk + transform->maclen * 2 + transform->keylen;
798 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000799
Paul Bakker68884e32013-01-07 18:20:04 +0100800 mac_enc = keyblk + transform->maclen;
801 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000802
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000803 /*
804 * This is not used in TLS v1.1.
805 */
Paul Bakker48916f92012-09-16 19:57:18 +0000806 iv_copy_len = ( transform->fixed_ivlen ) ?
807 transform->fixed_ivlen : transform->ivlen;
808 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
809 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000810 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000811 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100812 else
813#endif /* POLARSSL_SSL_SRV_C */
814 {
815 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
816 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
817 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000818
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200819#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100820 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
821 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100822 if( transform->maclen > sizeof transform->mac_enc )
823 {
824 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200825 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100826 }
827
Paul Bakker68884e32013-01-07 18:20:04 +0100828 memcpy( transform->mac_enc, mac_enc, transform->maclen );
829 memcpy( transform->mac_dec, mac_dec, transform->maclen );
830 }
831 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200832#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200833#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
834 defined(POLARSSL_SSL_PROTO_TLS1_2)
835 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100836 {
837 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
838 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
839 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200840 else
841#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200842 {
843 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200844 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200845 }
Paul Bakker68884e32013-01-07 18:20:04 +0100846
Paul Bakker05ef8352012-05-08 09:17:57 +0000847#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200848 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000849 {
850 int ret = 0;
851
852 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
853
Paul Bakker07eb38b2012-12-19 14:42:06 +0100854 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
855 transform->iv_enc, transform->iv_dec,
856 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100857 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100858 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000859 {
860 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200861 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000862 }
863 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200864#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000865
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200866 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
867 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000868 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200869 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
870 return( ret );
871 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200872
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200873 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
874 cipher_info ) ) != 0 )
875 {
876 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
877 return( ret );
878 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200879
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200880 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
881 cipher_info->key_length,
882 POLARSSL_ENCRYPT ) ) != 0 )
883 {
884 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
885 return( ret );
886 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200887
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200888 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
889 cipher_info->key_length,
890 POLARSSL_DECRYPT ) ) != 0 )
891 {
892 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
893 return( ret );
894 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200895
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200896#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200897 if( cipher_info->mode == POLARSSL_MODE_CBC )
898 {
899 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
900 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200901 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200902 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
903 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200904 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200905
906 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
907 POLARSSL_PADDING_NONE ) ) != 0 )
908 {
909 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
910 return( ret );
911 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000912 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200913#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000914
Paul Bakker34617722014-06-13 17:20:13 +0200915 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000916
Paul Bakker2770fbd2012-07-03 13:30:23 +0000917#if defined(POLARSSL_ZLIB_SUPPORT)
918 // Initialize compression
919 //
Paul Bakker48916f92012-09-16 19:57:18 +0000920 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000921 {
Paul Bakker16770332013-10-11 09:59:44 +0200922 if( ssl->compress_buf == NULL )
923 {
924 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
925 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
926 if( ssl->compress_buf == NULL )
927 {
928 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
929 SSL_BUFFER_LEN ) );
930 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
931 }
932 }
933
Paul Bakker2770fbd2012-07-03 13:30:23 +0000934 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
935
Paul Bakker48916f92012-09-16 19:57:18 +0000936 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
937 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000938
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200939 if( deflateInit( &transform->ctx_deflate,
940 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000941 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000942 {
943 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
944 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
945 }
946 }
947#endif /* POLARSSL_ZLIB_SUPPORT */
948
Paul Bakker5121ce52009-01-03 21:22:43 +0000949 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
950
951 return( 0 );
952}
953
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200954#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000955void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000956{
957 md5_context md5;
958 sha1_context sha1;
959 unsigned char pad_1[48];
960 unsigned char pad_2[48];
961
Paul Bakker380da532012-04-18 16:10:25 +0000962 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000963
Paul Bakker48916f92012-09-16 19:57:18 +0000964 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
965 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000966
Paul Bakker380da532012-04-18 16:10:25 +0000967 memset( pad_1, 0x36, 48 );
968 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000969
Paul Bakker48916f92012-09-16 19:57:18 +0000970 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000971 md5_update( &md5, pad_1, 48 );
972 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000973
Paul Bakker380da532012-04-18 16:10:25 +0000974 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000975 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000976 md5_update( &md5, pad_2, 48 );
977 md5_update( &md5, hash, 16 );
978 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000979
Paul Bakker48916f92012-09-16 19:57:18 +0000980 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000981 sha1_update( &sha1, pad_1, 40 );
982 sha1_finish( &sha1, hash + 16 );
983
984 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000985 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000986 sha1_update( &sha1, pad_2, 40 );
987 sha1_update( &sha1, hash + 16, 20 );
988 sha1_finish( &sha1, hash + 16 );
989
990 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
991 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
992
Paul Bakker5b4af392014-06-26 12:09:34 +0200993 md5_free( &md5 );
994 sha1_free( &sha1 );
995
Paul Bakker380da532012-04-18 16:10:25 +0000996 return;
997}
Paul Bakker9af723c2014-05-01 13:03:14 +0200998#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000999
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001000#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00001001void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
1002{
1003 md5_context md5;
1004 sha1_context sha1;
1005
1006 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
1007
Paul Bakker48916f92012-09-16 19:57:18 +00001008 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
1009 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +00001010
Paul Bakker48916f92012-09-16 19:57:18 +00001011 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001012 sha1_finish( &sha1, hash + 16 );
1013
1014 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1015 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1016
Paul Bakker5b4af392014-06-26 12:09:34 +02001017 md5_free( &md5 );
1018 sha1_free( &sha1 );
1019
Paul Bakker380da532012-04-18 16:10:25 +00001020 return;
1021}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001022#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001023
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001024#if defined(POLARSSL_SSL_PROTO_TLS1_2)
1025#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +00001026void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
1027{
Paul Bakker9e36f042013-06-30 14:34:05 +02001028 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001029
1030 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1031
Paul Bakker9e36f042013-06-30 14:34:05 +02001032 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
1033 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001034
1035 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1036 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1037
Paul Bakker5b4af392014-06-26 12:09:34 +02001038 sha256_free( &sha256 );
1039
Paul Bakker380da532012-04-18 16:10:25 +00001040 return;
1041}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001042#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001043
Paul Bakker9e36f042013-06-30 14:34:05 +02001044#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +00001045void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
1046{
Paul Bakker9e36f042013-06-30 14:34:05 +02001047 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001048
1049 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1050
Paul Bakker9e36f042013-06-30 14:34:05 +02001051 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
1052 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001053
Paul Bakkerca4ab492012-04-18 14:23:57 +00001054 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001055 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1056
Paul Bakker5b4af392014-06-26 12:09:34 +02001057 sha512_free( &sha512 );
1058
Paul Bakker5121ce52009-01-03 21:22:43 +00001059 return;
1060}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001061#endif /* POLARSSL_SHA512_C */
1062#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001063
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001064#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001065int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
1066{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001067 unsigned char *p = ssl->handshake->premaster;
1068 unsigned char *end = p + sizeof( ssl->handshake->premaster );
1069
1070 /*
1071 * PMS = struct {
1072 * opaque other_secret<0..2^16-1>;
1073 * opaque psk<0..2^16-1>;
1074 * };
1075 * with "other_secret" depending on the particular key exchange
1076 */
1077#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
1078 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
1079 {
1080 if( end - p < 2 + (int) ssl->psk_len )
1081 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1082
1083 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1084 *(p++) = (unsigned char)( ssl->psk_len );
1085 p += ssl->psk_len;
1086 }
1087 else
1088#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001089#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1090 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1091 {
1092 /*
1093 * other_secret already set by the ClientKeyExchange message,
1094 * and is 48 bytes long
1095 */
1096 *p++ = 0;
1097 *p++ = 48;
1098 p += 48;
1099 }
1100 else
1101#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001102#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1103 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1104 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001105 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02001106 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001107
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001108 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001109 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001110 p + 2, &len,
1111 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001112 {
1113 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
1114 return( ret );
1115 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001116 *(p++) = (unsigned char)( len >> 8 );
1117 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001118 p += len;
1119
1120 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1121 }
1122 else
1123#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1124#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1125 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1126 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001127 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001128 size_t zlen;
1129
1130 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001131 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001132 ssl->f_rng, ssl->p_rng ) ) != 0 )
1133 {
1134 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1135 return( ret );
1136 }
1137
1138 *(p++) = (unsigned char)( zlen >> 8 );
1139 *(p++) = (unsigned char)( zlen );
1140 p += zlen;
1141
1142 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1143 }
1144 else
1145#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1146 {
1147 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001148 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001149 }
1150
1151 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001152 if( end - p < 2 + (int) ssl->psk_len )
1153 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1154
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001155 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1156 *(p++) = (unsigned char)( ssl->psk_len );
1157 memcpy( p, ssl->psk, ssl->psk_len );
1158 p += ssl->psk_len;
1159
1160 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1161
1162 return( 0 );
1163}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001164#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001165
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001166#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001167/*
1168 * SSLv3.0 MAC functions
1169 */
Paul Bakker68884e32013-01-07 18:20:04 +01001170static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1171 unsigned char *buf, size_t len,
1172 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001173{
1174 unsigned char header[11];
1175 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001176 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001177 int md_size = md_get_size( md_ctx->md_info );
1178 int md_type = md_get_type( md_ctx->md_info );
1179
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001180 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001181 if( md_type == POLARSSL_MD_MD5 )
1182 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001183 else
Paul Bakker68884e32013-01-07 18:20:04 +01001184 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001185
1186 memcpy( header, ctr, 8 );
1187 header[ 8] = (unsigned char) type;
1188 header[ 9] = (unsigned char)( len >> 8 );
1189 header[10] = (unsigned char)( len );
1190
Paul Bakker68884e32013-01-07 18:20:04 +01001191 memset( padding, 0x36, padlen );
1192 md_starts( md_ctx );
1193 md_update( md_ctx, secret, md_size );
1194 md_update( md_ctx, padding, padlen );
1195 md_update( md_ctx, header, 11 );
1196 md_update( md_ctx, buf, len );
1197 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001198
Paul Bakker68884e32013-01-07 18:20:04 +01001199 memset( padding, 0x5C, padlen );
1200 md_starts( md_ctx );
1201 md_update( md_ctx, secret, md_size );
1202 md_update( md_ctx, padding, padlen );
1203 md_update( md_ctx, buf + len, md_size );
1204 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001205}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001206#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001207
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001208#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1209 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1210 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1211#define POLARSSL_SOME_MODES_USE_MAC
1212#endif
1213
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001214/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001215 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001216 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001217static int ssl_encrypt_buf( ssl_context *ssl )
1218{
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001219 cipher_mode_t mode;
1220 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001221
1222 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1223
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001224 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1225 {
1226 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1227 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1228 }
1229
1230 mode = cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
1231
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001232 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1233 ssl->out_msg, ssl->out_msglen );
1234
Paul Bakker5121ce52009-01-03 21:22:43 +00001235 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001236 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001238#if defined(POLARSSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001239 if( mode == POLARSSL_MODE_STREAM ||
1240 ( mode == POLARSSL_MODE_CBC
1241#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1242 && ssl->session_out->encrypt_then_mac == SSL_ETM_DISABLED
1243#endif
1244 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001245 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001246#if defined(POLARSSL_SSL_PROTO_SSL3)
1247 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1248 {
1249 ssl_mac( &ssl->transform_out->md_ctx_enc,
1250 ssl->transform_out->mac_enc,
1251 ssl->out_msg, ssl->out_msglen,
1252 ssl->out_ctr, ssl->out_msgtype );
1253 }
1254 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001255#endif
1256#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001257 defined(POLARSSL_SSL_PROTO_TLS1_2)
1258 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1259 {
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001260 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1261 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1262 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001263 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1264 ssl->out_msg, ssl->out_msglen );
1265 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1266 ssl->out_msg + ssl->out_msglen );
1267 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1268 }
1269 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001270#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001271 {
1272 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001273 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001274 }
1275
1276 SSL_DEBUG_BUF( 4, "computed mac",
1277 ssl->out_msg + ssl->out_msglen,
1278 ssl->transform_out->maclen );
1279
1280 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001281 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001282 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001283#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001284
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001285 /*
1286 * Encrypt
1287 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001288#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001289 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001290 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001291 int ret;
1292 size_t olen = 0;
1293
Paul Bakker5121ce52009-01-03 21:22:43 +00001294 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1295 "including %d bytes of padding",
1296 ssl->out_msglen, 0 ) );
1297
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001298 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001299 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001300 ssl->transform_out->ivlen,
1301 ssl->out_msg, ssl->out_msglen,
1302 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001303 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001304 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001305 return( ret );
1306 }
1307
1308 if( ssl->out_msglen != olen )
1309 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001310 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001311 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001312 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 }
Paul Bakker68884e32013-01-07 18:20:04 +01001314 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001315#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001316#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1317 if( mode == POLARSSL_MODE_GCM ||
1318 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001319 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001320 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001321 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001322 unsigned char *enc_msg;
1323 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001324 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1325 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001326
Paul Bakkerca4ab492012-04-18 14:23:57 +00001327 memcpy( add_data, ssl->out_ctr, 8 );
1328 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001329 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1330 ssl->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001331 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1332 add_data[12] = ssl->out_msglen & 0xFF;
1333
1334 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1335 add_data, 13 );
1336
Paul Bakker68884e32013-01-07 18:20:04 +01001337 /*
1338 * Generate IV
1339 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001340#if defined(POLARSSL_SSL_AEAD_RANDOM_IV)
Paul Bakker68884e32013-01-07 18:20:04 +01001341 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001342 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1343 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001344 if( ret != 0 )
1345 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001346
Paul Bakker68884e32013-01-07 18:20:04 +01001347 memcpy( ssl->out_iv,
1348 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1349 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001350#else
1351 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1352 {
1353 /* Reminder if we ever add an AEAD mode with a different size */
1354 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1355 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1356 }
1357
1358 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1359 ssl->out_ctr, 8 );
1360 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1361#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00001362
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001363 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001364 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001365
Paul Bakker68884e32013-01-07 18:20:04 +01001366 /*
1367 * Fix pointer positions and message length with added IV
1368 */
1369 enc_msg = ssl->out_msg;
1370 enc_msglen = ssl->out_msglen;
1371 ssl->out_msglen += ssl->transform_out->ivlen -
1372 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001373
Paul Bakker68884e32013-01-07 18:20:04 +01001374 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1375 "including %d bytes of padding",
1376 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001377
Paul Bakker68884e32013-01-07 18:20:04 +01001378 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001379 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001380 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001381 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1382 ssl->transform_out->iv_enc,
1383 ssl->transform_out->ivlen,
1384 add_data, 13,
1385 enc_msg, enc_msglen,
1386 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001387 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001388 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001389 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001390 return( ret );
1391 }
1392
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001393 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001394 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001395 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001396 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001397 }
1398
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001399 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001400 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001401
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001402 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001403 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001405#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001406#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1407 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001408 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001410 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001411 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001412 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001413
Paul Bakker48916f92012-09-16 19:57:18 +00001414 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1415 ssl->transform_out->ivlen;
1416 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001417 padlen = 0;
1418
1419 for( i = 0; i <= padlen; i++ )
1420 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1421
1422 ssl->out_msglen += padlen + 1;
1423
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001424 enc_msglen = ssl->out_msglen;
1425 enc_msg = ssl->out_msg;
1426
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001427#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001428 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001429 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1430 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001431 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001432 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001433 {
1434 /*
1435 * Generate IV
1436 */
Paul Bakker48916f92012-09-16 19:57:18 +00001437 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1438 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001439 if( ret != 0 )
1440 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001441
Paul Bakker92be97b2013-01-02 17:30:03 +01001442 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001443 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001444
1445 /*
1446 * Fix pointer positions and message length with added IV
1447 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001448 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001449 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001450 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001451 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001452#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001453
Paul Bakker5121ce52009-01-03 21:22:43 +00001454 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001455 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001456 ssl->out_msglen, ssl->transform_out->ivlen,
1457 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001458
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001459 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001460 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001461 ssl->transform_out->ivlen,
1462 enc_msg, enc_msglen,
1463 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001464 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001465 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001466 return( ret );
1467 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001468
Paul Bakkercca5b812013-08-31 17:40:26 +02001469 if( enc_msglen != olen )
1470 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001471 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001472 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001473 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001474
1475#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001476 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1477 {
1478 /*
1479 * Save IV in SSL3 and TLS1
1480 */
1481 memcpy( ssl->transform_out->iv_enc,
1482 ssl->transform_out->cipher_ctx_enc.iv,
1483 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001484 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001485#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001486
1487#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001488 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001489 {
1490 /*
1491 * MAC(MAC_write_key, seq_num +
1492 * TLSCipherText.type +
1493 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001494 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001495 * IV + // except for TLS 1.0
1496 * ENC(content + padding + padding_length));
1497 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001498 unsigned char pseudo_hdr[13];
1499
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001500 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1501
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001502 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1503 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001504 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1505 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1506
1507 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001508
1509 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1510 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1511 ssl->out_iv, ssl->out_msglen );
1512 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1513 ssl->out_iv + ssl->out_msglen );
1514 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1515
1516 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001517 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001518 }
1519#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001520 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001521 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001522#endif /* POLARSSL_CIPHER_MODE_CBC &&
1523 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001524 {
1525 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001526 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001527 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001528
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001529 /* Make extra sure authentication was performed, exactly once */
1530 if( auth_done != 1 )
1531 {
1532 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1533 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1534 }
1535
Paul Bakker5121ce52009-01-03 21:22:43 +00001536 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1537
1538 return( 0 );
1539}
1540
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001541#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001542
Paul Bakker5121ce52009-01-03 21:22:43 +00001543static int ssl_decrypt_buf( ssl_context *ssl )
1544{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001545 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001546 cipher_mode_t mode;
1547 int auth_done = 0;
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001548#if defined(POLARSSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001549 size_t padlen = 0, correct = 1;
1550#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001551
1552 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1553
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001554 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1555 {
1556 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1557 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1558 }
1559
1560 mode = cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
1561
Paul Bakker48916f92012-09-16 19:57:18 +00001562 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001563 {
1564 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001565 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001566 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001567 }
1568
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001569#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001570 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001571 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001572 int ret;
1573 size_t olen = 0;
1574
Paul Bakker68884e32013-01-07 18:20:04 +01001575 padlen = 0;
1576
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001577 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001578 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001579 ssl->transform_in->ivlen,
1580 ssl->in_msg, ssl->in_msglen,
1581 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001582 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001583 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001584 return( ret );
1585 }
1586
1587 if( ssl->in_msglen != olen )
1588 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001589 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001590 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001591 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001592 }
Paul Bakker68884e32013-01-07 18:20:04 +01001593 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001594#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001595#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1596 if( mode == POLARSSL_MODE_GCM ||
1597 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001598 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001599 int ret;
1600 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001601 unsigned char *dec_msg;
1602 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001603 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001604 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1605 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001606 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1607 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001608
Manuel Pégourié-Gonnard06d75192015-02-11 14:54:11 +00001609 if( ssl->in_msglen < (size_t) explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001610 {
1611 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1612 "+ taglen (%d)", ssl->in_msglen,
1613 explicit_iv_len, taglen ) );
1614 return( POLARSSL_ERR_SSL_INVALID_MAC );
1615 }
1616 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1617
Paul Bakker68884e32013-01-07 18:20:04 +01001618 dec_msg = ssl->in_msg;
1619 dec_msg_result = ssl->in_msg;
1620 ssl->in_msglen = dec_msglen;
1621
1622 memcpy( add_data, ssl->in_ctr, 8 );
1623 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001624 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1625 ssl->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001626 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1627 add_data[12] = ssl->in_msglen & 0xFF;
1628
1629 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1630 add_data, 13 );
1631
1632 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1633 ssl->in_iv,
1634 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1635
1636 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1637 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001638 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001639
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001640 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001641 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001642 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001643 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1644 ssl->transform_in->iv_dec,
1645 ssl->transform_in->ivlen,
1646 add_data, 13,
1647 dec_msg, dec_msglen,
1648 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001649 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001650 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001651 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1652
1653 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1654 return( POLARSSL_ERR_SSL_INVALID_MAC );
1655
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001656 return( ret );
1657 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001658 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001659
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001660 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001661 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001662 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001663 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001664 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001665 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001666 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001667#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001668#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1669 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001670 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001671 {
Paul Bakker45829992013-01-03 14:52:21 +01001672 /*
1673 * Decrypt and check the padding
1674 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001675 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001676 unsigned char *dec_msg;
1677 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001678 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001679 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001680 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001681
Paul Bakker5121ce52009-01-03 21:22:43 +00001682 /*
Paul Bakker45829992013-01-03 14:52:21 +01001683 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001684 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001685#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001686 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1687 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001688#endif
Paul Bakker45829992013-01-03 14:52:21 +01001689
1690 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1691 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1692 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001693 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1694 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1695 ssl->transform_in->ivlen,
1696 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001697 return( POLARSSL_ERR_SSL_INVALID_MAC );
1698 }
1699
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001700 dec_msglen = ssl->in_msglen;
1701 dec_msg = ssl->in_msg;
1702 dec_msg_result = ssl->in_msg;
1703
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001704 /*
1705 * Authenticate before decrypt if enabled
1706 */
1707#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001708 if( ssl->session_in->encrypt_then_mac == SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001709 {
1710 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001711 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001712
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001713 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1714
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001715 dec_msglen -= ssl->transform_in->maclen;
1716 ssl->in_msglen -= ssl->transform_in->maclen;
1717
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001718 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1719 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1720 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1721 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1722
1723 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1724
1725 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001726 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1727 ssl->in_iv, ssl->in_msglen );
1728 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1729 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1730
1731 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1732 ssl->transform_in->maclen );
1733 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1734 ssl->transform_in->maclen );
1735
1736 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1737 ssl->transform_in->maclen ) != 0 )
1738 {
1739 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1740
1741 return( POLARSSL_ERR_SSL_INVALID_MAC );
1742 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001743 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001744 }
1745#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1746
1747 /*
1748 * Check length sanity
1749 */
1750 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1751 {
1752 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1753 ssl->in_msglen, ssl->transform_in->ivlen ) );
1754 return( POLARSSL_ERR_SSL_INVALID_MAC );
1755 }
1756
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001757#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001758 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001759 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001760 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001761 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001762 {
Paul Bakker48916f92012-09-16 19:57:18 +00001763 dec_msglen -= ssl->transform_in->ivlen;
1764 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001765
Paul Bakker48916f92012-09-16 19:57:18 +00001766 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001767 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001768 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001769#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001770
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001771 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001772 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001773 ssl->transform_in->ivlen,
1774 dec_msg, dec_msglen,
1775 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001776 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001777 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001778 return( ret );
1779 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001780
Paul Bakkercca5b812013-08-31 17:40:26 +02001781 if( dec_msglen != olen )
1782 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001783 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001784 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001785 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001786
1787#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001788 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1789 {
1790 /*
1791 * Save IV in SSL3 and TLS1
1792 */
1793 memcpy( ssl->transform_in->iv_dec,
1794 ssl->transform_in->cipher_ctx_dec.iv,
1795 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001796 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001797#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001798
1799 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001800
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001801 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001802 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001803 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001804#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001805 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1806 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001807#endif
Paul Bakker45829992013-01-03 14:52:21 +01001808 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001809 correct = 0;
1810 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001811
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001812#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001813 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1814 {
Paul Bakker48916f92012-09-16 19:57:18 +00001815 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001816 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001817#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001818 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1819 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001820 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001821#endif
Paul Bakker45829992013-01-03 14:52:21 +01001822 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001823 }
1824 }
1825 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001826#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001827#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1828 defined(POLARSSL_SSL_PROTO_TLS1_2)
1829 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001830 {
1831 /*
Paul Bakker45829992013-01-03 14:52:21 +01001832 * TLSv1+: always check the padding up to the first failure
1833 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001834 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001835 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001836 size_t padding_idx = ssl->in_msglen - padlen - 1;
1837
Paul Bakker956c9e02013-12-19 14:42:28 +01001838 /*
1839 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001840 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001841 *
Paul Bakker61885c72014-04-25 12:59:03 +02001842 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1843 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001844 *
1845 * In both cases we reset padding_idx to a safe value (0) to
1846 * prevent out-of-buffer reads.
1847 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001848 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001849 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1850 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001851
1852 padding_idx *= correct;
1853
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001854 for( i = 1; i <= 256; i++ )
1855 {
1856 real_count &= ( i <= padlen );
1857 pad_count += real_count *
1858 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1859 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001860
1861 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001862
Paul Bakkerd66f0702013-01-31 16:57:45 +01001863#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001864 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001865 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001866#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001867 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001868 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001869 else
1870#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1871 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001872 {
1873 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001874 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001875 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001876
1877 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001878 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001879 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001880#endif /* POLARSSL_CIPHER_MODE_CBC &&
1881 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001882 {
1883 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001884 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001885 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001886
1887 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1888 ssl->in_msg, ssl->in_msglen );
1889
1890 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001891 * Authenticate if not done yet.
1892 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001893 */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001894#if defined(POLARSSL_SOME_MODES_USE_MAC)
1895 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001896 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001897 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1898
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001899 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001900
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001901 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1902 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001903
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001904 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001905
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001906#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001907 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1908 {
1909 ssl_mac( &ssl->transform_in->md_ctx_dec,
1910 ssl->transform_in->mac_dec,
1911 ssl->in_msg, ssl->in_msglen,
1912 ssl->in_ctr, ssl->in_msgtype );
1913 }
1914 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001915#endif /* POLARSSL_SSL_PROTO_SSL3 */
1916#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001917 defined(POLARSSL_SSL_PROTO_TLS1_2)
1918 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1919 {
1920 /*
1921 * Process MAC and always update for padlen afterwards to make
1922 * total time independent of padlen
1923 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001924 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001925 *
1926 * Known timing attacks:
1927 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1928 *
1929 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1930 * correctly. (We round down instead of up, so -56 is the correct
1931 * value for our calculations instead of -55)
1932 */
1933 size_t j, extra_run = 0;
1934 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1935 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001936
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001937 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001938
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001939 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
1940 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
1941 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001942 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1943 ssl->in_msglen );
1944 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1945 ssl->in_msg + ssl->in_msglen );
1946 for( j = 0; j < extra_run; j++ )
1947 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001948
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001949 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1950 }
1951 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001952#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001953 POLARSSL_SSL_PROTO_TLS1_2 */
1954 {
1955 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001956 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001957 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001958
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001959 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1960 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1961 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001962
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001963 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001964 ssl->transform_in->maclen ) != 0 )
1965 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001966#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001967 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001968#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001969 correct = 0;
1970 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001971 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001972
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001973 /*
1974 * Finally check the correct flag
1975 */
1976 if( correct == 0 )
1977 return( POLARSSL_ERR_SSL_INVALID_MAC );
1978 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001979#endif /* POLARSSL_SOME_MODES_USE_MAC */
1980
1981 /* Make extra sure authentication was performed, exactly once */
1982 if( auth_done != 1 )
1983 {
1984 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1985 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1986 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001987
1988 if( ssl->in_msglen == 0 )
1989 {
1990 ssl->nb_zero++;
1991
1992 /*
1993 * Three or more empty messages may be a DoS attack
1994 * (excessive CPU consumption).
1995 */
1996 if( ssl->nb_zero > 3 )
1997 {
1998 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1999 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002000 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002001 }
2002 }
2003 else
2004 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002005
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002006#if defined(POLARSSL_SSL_PROTO_DTLS)
2007 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002008 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002009 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002010 }
2011 else
2012#endif
2013 {
2014 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2015 if( ++ssl->in_ctr[i - 1] != 0 )
2016 break;
2017
2018 /* The loop goes to its end iff the counter is wrapping */
2019 if( i == ssl_ep_len( ssl ) )
2020 {
2021 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2022 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
2023 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002024 }
2025
Paul Bakker5121ce52009-01-03 21:22:43 +00002026 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
2027
2028 return( 0 );
2029}
2030
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002031#undef MAC_NONE
2032#undef MAC_PLAINTEXT
2033#undef MAC_CIPHERTEXT
2034
Paul Bakker2770fbd2012-07-03 13:30:23 +00002035#if defined(POLARSSL_ZLIB_SUPPORT)
2036/*
2037 * Compression/decompression functions
2038 */
2039static int ssl_compress_buf( ssl_context *ssl )
2040{
2041 int ret;
2042 unsigned char *msg_post = ssl->out_msg;
2043 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002044 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002045
2046 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
2047
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002048 if( len_pre == 0 )
2049 return( 0 );
2050
Paul Bakker2770fbd2012-07-03 13:30:23 +00002051 memcpy( msg_pre, ssl->out_msg, len_pre );
2052
2053 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
2054 ssl->out_msglen ) );
2055
2056 SSL_DEBUG_BUF( 4, "before compression: output payload",
2057 ssl->out_msg, ssl->out_msglen );
2058
Paul Bakker48916f92012-09-16 19:57:18 +00002059 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2060 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2061 ssl->transform_out->ctx_deflate.next_out = msg_post;
2062 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002063
Paul Bakker48916f92012-09-16 19:57:18 +00002064 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002065 if( ret != Z_OK )
2066 {
2067 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2068 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
2069 }
2070
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002071 ssl->out_msglen = SSL_BUFFER_LEN -
2072 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002073
Paul Bakker2770fbd2012-07-03 13:30:23 +00002074 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
2075 ssl->out_msglen ) );
2076
2077 SSL_DEBUG_BUF( 4, "after compression: output payload",
2078 ssl->out_msg, ssl->out_msglen );
2079
2080 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
2081
2082 return( 0 );
2083}
2084
2085static int ssl_decompress_buf( ssl_context *ssl )
2086{
2087 int ret;
2088 unsigned char *msg_post = ssl->in_msg;
2089 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002090 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002091
2092 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
2093
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002094 if( len_pre == 0 )
2095 return( 0 );
2096
Paul Bakker2770fbd2012-07-03 13:30:23 +00002097 memcpy( msg_pre, ssl->in_msg, len_pre );
2098
2099 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
2100 ssl->in_msglen ) );
2101
2102 SSL_DEBUG_BUF( 4, "before decompression: input payload",
2103 ssl->in_msg, ssl->in_msglen );
2104
Paul Bakker48916f92012-09-16 19:57:18 +00002105 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2106 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2107 ssl->transform_in->ctx_inflate.next_out = msg_post;
2108 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002109
Paul Bakker48916f92012-09-16 19:57:18 +00002110 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002111 if( ret != Z_OK )
2112 {
2113 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2114 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
2115 }
2116
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002117 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
2118 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002119
Paul Bakker2770fbd2012-07-03 13:30:23 +00002120 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
2121 ssl->in_msglen ) );
2122
2123 SSL_DEBUG_BUF( 4, "after decompression: input payload",
2124 ssl->in_msg, ssl->in_msglen );
2125
2126 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
2127
2128 return( 0 );
2129}
2130#endif /* POLARSSL_ZLIB_SUPPORT */
2131
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002132#if defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002133static int ssl_write_hello_request( ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002134
2135#if defined(POLARSSL_SSL_PROTO_DTLS)
2136static int ssl_resend_hello_request( ssl_context *ssl )
2137{
2138 /* If renegotiation is not enforced, retransmit until we would reach max
2139 * timeout if we were using the usual handshake doubling scheme */
2140 if( ssl->renego_max_records < 0 )
2141 {
2142 uint32_t ratio = ssl->hs_timeout_max / ssl->hs_timeout_min + 1;
2143 unsigned char doublings = 1;
2144
2145 while( ratio != 0 )
2146 {
2147 ++doublings;
2148 ratio >>= 1;
2149 }
2150
2151 if( ++ssl->renego_records_seen > doublings )
2152 {
2153 SSL_DEBUG_MSG( 0, ( "no longer retransmitting hello request" ) );
2154 return( 0 );
2155 }
2156 }
2157
2158 return( ssl_write_hello_request( ssl ) );
2159}
2160#endif
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002161#endif /* POLARSSL_SSL_SRV_C && POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002162
Paul Bakker5121ce52009-01-03 21:22:43 +00002163/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002164 * Fill the input message buffer by appending data to it.
2165 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002166 *
2167 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2168 * available (from this read and/or a previous one). Otherwise, an error code
2169 * is returned (possibly EOF or WANT_READ).
2170 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002171 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2172 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2173 * since we always read a whole datagram at once.
2174 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002175 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002176 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002177 */
Paul Bakker23986e52011-04-24 08:57:21 +00002178int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002179{
Paul Bakker23986e52011-04-24 08:57:21 +00002180 int ret;
2181 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002182
2183 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
2184
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002185 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2186 {
2187 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2188 "or ssl_set_bio_timeout()" ) );
2189 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2190 }
2191
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002192 if( nb_want > SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002193 {
2194 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2195 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2196 }
2197
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002198#if defined(POLARSSL_SSL_PROTO_DTLS)
2199 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002200 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002201 uint32_t timeout;
2202
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002203 /*
2204 * The point is, we need to always read a full datagram at once, so we
2205 * sometimes read more then requested, and handle the additional data.
2206 * It could be the rest of the current record (while fetching the
2207 * header) and/or some other records in the same datagram.
2208 */
2209
2210 /*
2211 * Move to the next record in the already read datagram if applicable
2212 */
2213 if( ssl->next_record_offset != 0 )
2214 {
2215 if( ssl->in_left < ssl->next_record_offset )
2216 {
2217 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2218 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2219 }
2220
2221 ssl->in_left -= ssl->next_record_offset;
2222
2223 if( ssl->in_left != 0 )
2224 {
2225 SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
2226 ssl->next_record_offset ) );
2227 memmove( ssl->in_hdr,
2228 ssl->in_hdr + ssl->next_record_offset,
2229 ssl->in_left );
2230 }
2231
2232 ssl->next_record_offset = 0;
2233 }
2234
Paul Bakker5121ce52009-01-03 21:22:43 +00002235 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2236 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002237
2238 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002239 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002240 */
2241 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002242 {
2243 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002244 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002245 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002246
2247 /*
2248 * A record can't be split accross datagrams. If we need to read but
2249 * are not at the beginning of a new record, the caller did something
2250 * wrong.
2251 */
2252 if( ssl->in_left != 0 )
2253 {
2254 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2255 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2256 }
2257
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002258 SSL_DEBUG_MSG( 3, ( "current timer: %u", ssl->time_limit ) );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002259
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002260 /*
2261 * Don't even try to read if time's out already.
2262 * This avoids by-passing the timer when repeatedly receiving messages
2263 * that will end up being dropped.
2264 */
2265 if( ssl_check_timer( ssl ) != 0 )
2266 ret = POLARSSL_ERR_NET_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002267 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002268 {
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002269 len = SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
2270
2271 if( ssl->state != SSL_HANDSHAKE_OVER )
2272 timeout = ssl->handshake->retransmit_timeout;
2273 else
2274 timeout = ssl->read_timeout;
2275
2276 SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
2277
2278 if( ssl->f_recv_timeout != NULL && timeout != 0 )
2279 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2280 timeout );
2281 else
2282 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2283
2284 SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
2285
2286 if( ret == 0 )
2287 return( POLARSSL_ERR_SSL_CONN_EOF );
2288 }
2289
2290 if( ret == POLARSSL_ERR_NET_TIMEOUT )
2291 {
2292 SSL_DEBUG_MSG( 2, ( "timeout" ) );
2293 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002294
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002295 if( ssl->state != SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002296 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002297 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2298 {
2299 SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
2300 return( POLARSSL_ERR_NET_TIMEOUT );
2301 }
2302
2303 if( ( ret = ssl_resend( ssl ) ) != 0 )
2304 {
2305 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2306 return( ret );
2307 }
2308
2309 return( POLARSSL_ERR_NET_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002310 }
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002311#if defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002312 else if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00002313 ssl->renego_status == SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002314 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002315 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002316 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002317 SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002318 return( ret );
2319 }
2320
2321 return( POLARSSL_ERR_NET_WANT_READ );
2322 }
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002323#endif /* POLARSSL_SSL_SRV_C && POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002324 }
2325
Paul Bakker5121ce52009-01-03 21:22:43 +00002326 if( ret < 0 )
2327 return( ret );
2328
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002329 ssl->in_left = ret;
2330 }
2331 else
2332#endif
2333 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002334 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2335 ssl->in_left, nb_want ) );
2336
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002337 while( ssl->in_left < nb_want )
2338 {
2339 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002340 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr + ssl->in_left, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002341
2342 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2343 ssl->in_left, nb_want ) );
2344 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2345
2346 if( ret == 0 )
2347 return( POLARSSL_ERR_SSL_CONN_EOF );
2348
2349 if( ret < 0 )
2350 return( ret );
2351
2352 ssl->in_left += ret;
2353 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002354 }
2355
2356 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2357
2358 return( 0 );
2359}
2360
2361/*
2362 * Flush any data not yet written
2363 */
2364int ssl_flush_output( ssl_context *ssl )
2365{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002366 int ret;
2367 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002368
2369 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2370
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002371 if( ssl->f_send == NULL )
2372 {
2373 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2374 "or ssl_set_bio_timeout()" ) );
2375 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2376 }
2377
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002378 /* Avoid incrementing counter if data is flushed */
2379 if( ssl->out_left == 0 )
2380 {
2381 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2382 return( 0 );
2383 }
2384
Paul Bakker5121ce52009-01-03 21:22:43 +00002385 while( ssl->out_left > 0 )
2386 {
2387 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002388 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002389
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002390 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
2391 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002392 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002393
Paul Bakker5121ce52009-01-03 21:22:43 +00002394 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2395
2396 if( ret <= 0 )
2397 return( ret );
2398
2399 ssl->out_left -= ret;
2400 }
2401
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002402 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002403 if( ++ssl->out_ctr[i - 1] != 0 )
2404 break;
2405
2406 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002407 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002408 {
2409 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2410 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
2411 }
2412
Paul Bakker5121ce52009-01-03 21:22:43 +00002413 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2414
2415 return( 0 );
2416}
2417
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002418/*
2419 * Functions to handle the DTLS retransmission state machine
2420 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002421#if defined(POLARSSL_SSL_PROTO_DTLS)
2422/*
2423 * Append current handshake message to current outgoing flight
2424 */
2425static int ssl_flight_append( ssl_context *ssl )
2426{
2427 ssl_flight_item *msg;
2428
2429 /* Allocate space for current message */
2430 if( ( msg = polarssl_malloc( sizeof( ssl_flight_item ) ) ) == NULL )
2431 {
2432 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed",
2433 sizeof( ssl_flight_item ) ) );
2434 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2435 }
2436
2437 if( ( msg->p = polarssl_malloc( ssl->out_msglen ) ) == NULL )
2438 {
2439 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard6b875fc2014-10-17 14:02:33 +02002440 polarssl_free( msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002441 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2442 }
2443
2444 /* Copy current handshake message with headers */
2445 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2446 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002447 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002448 msg->next = NULL;
2449
2450 /* Append to the current flight */
2451 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002452 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002453 else
2454 {
2455 ssl_flight_item *cur = ssl->handshake->flight;
2456 while( cur->next != NULL )
2457 cur = cur->next;
2458 cur->next = msg;
2459 }
2460
2461 return( 0 );
2462}
2463
2464/*
2465 * Free the current flight of handshake messages
2466 */
2467static void ssl_flight_free( ssl_flight_item *flight )
2468{
2469 ssl_flight_item *cur = flight;
2470 ssl_flight_item *next;
2471
2472 while( cur != NULL )
2473 {
2474 next = cur->next;
2475
2476 polarssl_free( cur->p );
2477 polarssl_free( cur );
2478
2479 cur = next;
2480 }
2481}
2482
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002483#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2484static void ssl_dtls_replay_reset( ssl_context *ssl );
2485#endif
2486
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002487/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002488 * Swap transform_out and out_ctr with the alternative ones
2489 */
2490static void ssl_swap_epochs( ssl_context *ssl )
2491{
2492 ssl_transform *tmp_transform;
2493 unsigned char tmp_out_ctr[8];
2494
2495 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2496 {
2497 SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
2498 return;
2499 }
2500
2501 SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
2502
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002503 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002504 tmp_transform = ssl->transform_out;
2505 ssl->transform_out = ssl->handshake->alt_transform_out;
2506 ssl->handshake->alt_transform_out = tmp_transform;
2507
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002508 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002509 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2510 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2511 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002512
2513 /* Adjust to the newly activated transform */
2514 if( ssl->transform_out != NULL &&
2515 ssl->minor_ver >= SSL_MINOR_VERSION_2 )
2516 {
2517 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2518 ssl->transform_out->fixed_ivlen;
2519 }
2520 else
2521 ssl->out_msg = ssl->out_iv;
2522
2523#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2524 if( ssl_hw_record_activate != NULL )
2525 {
2526 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
2527 {
2528 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
2529 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
2530 }
2531 }
2532#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002533}
2534
2535/*
2536 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002537 *
2538 * Need to remember the current message in case flush_output returns
2539 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002540 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002541 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002542int ssl_resend( ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002543{
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002544 SSL_DEBUG_MSG( 2, ( "=> ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002545
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002546 if( ssl->handshake->retransmit_state != SSL_RETRANS_SENDING )
2547 {
2548 SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
2549
2550 ssl->handshake->cur_msg = ssl->handshake->flight;
2551 ssl_swap_epochs( ssl );
2552
2553 ssl->handshake->retransmit_state = SSL_RETRANS_SENDING;
2554 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002555
2556 while( ssl->handshake->cur_msg != NULL )
2557 {
2558 int ret;
2559 ssl_flight_item *cur = ssl->handshake->cur_msg;
2560
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002561 /* Swap epochs before sending Finished: we can't do it after
2562 * sending ChangeCipherSpec, in case write returns WANT_READ.
2563 * Must be done before copying, may change out_msg pointer */
2564 if( cur->type == SSL_MSG_HANDSHAKE &&
2565 cur->p[0] == SSL_HS_FINISHED )
2566 {
2567 ssl_swap_epochs( ssl );
2568 }
2569
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002570 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002571 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002572 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002573
2574 ssl->handshake->cur_msg = cur->next;
2575
2576 SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
2577
2578 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2579 {
2580 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2581 return( ret );
2582 }
2583 }
2584
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002585 if( ssl->state == SSL_HANDSHAKE_OVER )
2586 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2587 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002588 {
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002589 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002590 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2591 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002592
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002593 SSL_DEBUG_MSG( 2, ( "<= ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002594
2595 return( 0 );
2596}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002597
2598/*
2599 * To be called when the last message of an incoming flight is received.
2600 */
2601void ssl_recv_flight_completed( ssl_context *ssl )
2602{
2603 /* We won't need to resend that one any more */
2604 ssl_flight_free( ssl->handshake->flight );
2605 ssl->handshake->flight = NULL;
2606 ssl->handshake->cur_msg = NULL;
2607
2608 /* The next incoming flight will start with this msg_seq */
2609 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2610
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002611 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002612 ssl_set_timer( ssl, 0 );
2613
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002614 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2615 ssl->in_msg[0] == SSL_HS_FINISHED )
2616 {
2617 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2618 }
2619 else
2620 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
2621}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002622
2623/*
2624 * To be called when the last message of an outgoing flight is send.
2625 */
2626void ssl_send_flight_completed( ssl_context *ssl )
2627{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002628 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002629 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002630
2631 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2632 ssl->in_msg[0] == SSL_HS_FINISHED )
2633 {
2634 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2635 }
2636 else
2637 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
2638}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002639#endif /* POLARSSL_SSL_PROTO_DTLS */
2640
Paul Bakker5121ce52009-01-03 21:22:43 +00002641/*
2642 * Record layer functions
2643 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002644
2645/*
2646 * Write current record.
2647 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2648 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002649int ssl_write_record( ssl_context *ssl )
2650{
Paul Bakker05ef8352012-05-08 09:17:57 +00002651 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002652 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002653
2654 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2655
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002656#if defined(POLARSSL_SSL_PROTO_DTLS)
2657 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2658 ssl->handshake != NULL &&
2659 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2660 {
2661 ; /* Skip special handshake treatment when resending */
2662 }
2663 else
2664#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002665 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2666 {
2667 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2668 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2669 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2670
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002671 /*
2672 * DTLS has additional fields in the Handshake layer,
2673 * between the length field and the actual payload:
2674 * uint16 message_seq;
2675 * uint24 fragment_offset;
2676 * uint24 fragment_length;
2677 */
2678#if defined(POLARSSL_SSL_PROTO_DTLS)
2679 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2680 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002681 /* Make room for the additional DTLS fields */
2682 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002683 ssl->out_msglen += 8;
2684 len += 8;
2685
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002686 /* Write message_seq and update it, except for HelloRequest */
2687 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2688 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002689 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2690 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2691 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002692 }
2693 else
2694 {
2695 ssl->out_msg[4] = 0;
2696 ssl->out_msg[5] = 0;
2697 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002698
2699 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2700 memset( ssl->out_msg + 6, 0x00, 3 );
2701 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002702 }
2703#endif /* POLARSSL_SSL_PROTO_DTLS */
2704
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002705 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2706 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002707 }
2708
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002709 /* Save handshake and CCS messages for resending */
2710#if defined(POLARSSL_SSL_PROTO_DTLS)
2711 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2712 ssl->handshake != NULL &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02002713 ssl->handshake->retransmit_state != SSL_RETRANS_SENDING &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002714 ( ssl->out_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC ||
2715 ssl->out_msgtype == SSL_MSG_HANDSHAKE ) )
2716 {
2717 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2718 {
2719 SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
2720 return( ret );
2721 }
2722 }
2723#endif
2724
Paul Bakker2770fbd2012-07-03 13:30:23 +00002725#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002726 if( ssl->transform_out != NULL &&
2727 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002728 {
2729 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2730 {
2731 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2732 return( ret );
2733 }
2734
2735 len = ssl->out_msglen;
2736 }
2737#endif /*POLARSSL_ZLIB_SUPPORT */
2738
Paul Bakker05ef8352012-05-08 09:17:57 +00002739#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002740 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002741 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002742 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002743
Paul Bakker05ef8352012-05-08 09:17:57 +00002744 ret = ssl_hw_record_write( ssl );
2745 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2746 {
2747 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002748 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002749 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002750
2751 if( ret == 0 )
2752 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002753 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002754#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002755 if( !done )
2756 {
2757 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002758 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2759 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002760
2761 ssl->out_len[0] = (unsigned char)( len >> 8 );
2762 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002763
Paul Bakker48916f92012-09-16 19:57:18 +00002764 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002765 {
2766 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2767 {
2768 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2769 return( ret );
2770 }
2771
2772 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002773 ssl->out_len[0] = (unsigned char)( len >> 8 );
2774 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002775 }
2776
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002777 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002778
2779 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2780 "version = [%d:%d], msglen = %d",
2781 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002782 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002783
2784 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002785 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002786 }
2787
Paul Bakker5121ce52009-01-03 21:22:43 +00002788 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2789 {
2790 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2791 return( ret );
2792 }
2793
2794 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2795
2796 return( 0 );
2797}
2798
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002799#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002800/*
2801 * Mark bits in bitmask (used for DTLS HS reassembly)
2802 */
2803static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2804{
2805 unsigned int start_bits, end_bits;
2806
2807 start_bits = 8 - ( offset % 8 );
2808 if( start_bits != 8 )
2809 {
2810 size_t first_byte_idx = offset / 8;
2811
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002812 /* Special case */
2813 if( len <= start_bits )
2814 {
2815 for( ; len != 0; len-- )
2816 mask[first_byte_idx] |= 1 << ( start_bits - len );
2817
2818 /* Avoid potential issues with offset or len becoming invalid */
2819 return;
2820 }
2821
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002822 offset += start_bits; /* Now offset % 8 == 0 */
2823 len -= start_bits;
2824
2825 for( ; start_bits != 0; start_bits-- )
2826 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2827 }
2828
2829 end_bits = len % 8;
2830 if( end_bits != 0 )
2831 {
2832 size_t last_byte_idx = ( offset + len ) / 8;
2833
2834 len -= end_bits; /* Now len % 8 == 0 */
2835
2836 for( ; end_bits != 0; end_bits-- )
2837 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2838 }
2839
2840 memset( mask + offset / 8, 0xFF, len / 8 );
2841}
2842
2843/*
2844 * Check that bitmask is full
2845 */
2846static int ssl_bitmask_check( unsigned char *mask, size_t len )
2847{
2848 size_t i;
2849
2850 for( i = 0; i < len / 8; i++ )
2851 if( mask[i] != 0xFF )
2852 return( -1 );
2853
2854 for( i = 0; i < len % 8; i++ )
2855 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2856 return( -1 );
2857
2858 return( 0 );
2859}
2860
2861/*
2862 * Reassemble fragmented DTLS handshake messages.
2863 *
2864 * Use a temporary buffer for reassembly, divided in two parts:
2865 * - the first holds the reassembled message (including handshake header),
2866 * - the second holds a bitmask indicating which parts of the message
2867 * (excluding headers) have been received so far.
2868 */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002869static int ssl_reassemble_dtls_handshake( ssl_context *ssl )
2870{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002871 unsigned char *msg, *bitmask;
2872 size_t frag_len, frag_off;
2873 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
2874
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002875 if( ssl->handshake == NULL )
2876 {
2877 SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
2878 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2879 }
2880
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002881 /*
2882 * For first fragment, check size and allocate buffer
2883 */
2884 if( ssl->handshake->hs_msg == NULL )
2885 {
2886 size_t alloc_len;
2887
2888 SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
2889 msg_len ) );
2890
2891 if( ssl->in_hslen > SSL_MAX_CONTENT_LEN )
2892 {
2893 SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
2894 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2895 }
2896
2897 /* The bitmask needs one bit per byte of message excluding header */
2898 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
2899
2900 ssl->handshake->hs_msg = polarssl_malloc( alloc_len );
2901 if( ssl->handshake->hs_msg == NULL )
2902 {
2903 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", alloc_len ) );
2904 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2905 }
2906
2907 memset( ssl->handshake->hs_msg, 0, alloc_len );
2908
2909 /* Prepare final header: copy msg_type, length and message_seq,
2910 * then add standardised fragment_offset and fragment_length */
2911 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
2912 memset( ssl->handshake->hs_msg + 6, 0, 3 );
2913 memcpy( ssl->handshake->hs_msg + 9,
2914 ssl->handshake->hs_msg + 1, 3 );
2915 }
2916 else
2917 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002918 /* Make sure msg_type and length are consistent */
2919 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002920 {
2921 SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
2922 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2923 }
2924 }
2925
2926 msg = ssl->handshake->hs_msg + 12;
2927 bitmask = msg + msg_len;
2928
2929 /*
2930 * Check and copy current fragment
2931 */
2932 frag_off = ( ssl->in_msg[6] << 16 ) |
2933 ( ssl->in_msg[7] << 8 ) |
2934 ssl->in_msg[8];
2935 frag_len = ( ssl->in_msg[9] << 16 ) |
2936 ( ssl->in_msg[10] << 8 ) |
2937 ssl->in_msg[11];
2938
2939 if( frag_off + frag_len > msg_len )
2940 {
2941 SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
2942 frag_off, frag_len, msg_len ) );
2943 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2944 }
2945
2946 if( frag_len + 12 > ssl->in_msglen )
2947 {
2948 SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
2949 frag_len, ssl->in_msglen ) );
2950 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2951 }
2952
2953 SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
2954 frag_off, frag_len ) );
2955
2956 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
2957 ssl_bitmask_set( bitmask, frag_off, frag_len );
2958
2959 /*
2960 * Do we have the complete message by now?
2961 * If yes, finalize it, else ask to read the next record.
2962 */
2963 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
2964 {
2965 SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
2966 return( POLARSSL_ERR_NET_WANT_READ );
2967 }
2968
2969 SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
2970
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02002971 if( frag_len + 12 < ssl->in_msglen )
2972 {
2973 /*
2974 * We'got more handshake messages in the same record.
2975 * This case is not handled now because no know implementation does
2976 * that and it's hard to test, so we prefer to fail cleanly for now.
2977 */
2978 SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
2979 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2980 }
2981
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002982 if( ssl->in_left > ssl->next_record_offset )
2983 {
2984 /*
2985 * We've got more data in the buffer after the current record,
2986 * that we don't want to overwrite. Move it before writing the
2987 * reassembled message, and adjust in_left and next_record_offset.
2988 */
2989 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
2990 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
2991 size_t remain_len = ssl->in_left - ssl->next_record_offset;
2992
2993 /* First compute and check new lengths */
2994 ssl->next_record_offset = new_remain - ssl->in_hdr;
2995 ssl->in_left = ssl->next_record_offset + remain_len;
2996
2997 if( ssl->in_left > SSL_BUFFER_LEN -
2998 (size_t)( ssl->in_hdr - ssl->in_buf ) )
2999 {
3000 SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3001 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
3002 }
3003
3004 memmove( new_remain, cur_remain, remain_len );
3005 }
3006
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003007 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3008
3009 polarssl_free( ssl->handshake->hs_msg );
3010 ssl->handshake->hs_msg = NULL;
3011
3012 SSL_DEBUG_BUF( 3, "reassembled handshake message",
3013 ssl->in_msg, ssl->in_hslen );
3014
3015 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003016}
3017#endif /* POLARSSL_SSL_PROTO_DTLS */
3018
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003019static int ssl_prepare_handshake_record( ssl_context *ssl )
3020{
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003021 if( ssl->in_msglen < ssl_hs_hdr_len( ssl ) )
3022 {
3023 SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
3024 ssl->in_msglen ) );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003025 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003026 }
3027
3028 ssl->in_hslen = ssl_hs_hdr_len( ssl ) + (
3029 ( ssl->in_msg[1] << 16 ) |
3030 ( ssl->in_msg[2] << 8 ) |
3031 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003032
3033 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
3034 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003035 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003036
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003037#if defined(POLARSSL_SSL_PROTO_DTLS)
3038 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003039 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003040 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003041 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003042
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003043 /* ssl->handshake is NULL when receiving ClientHello for renego */
3044 if( ssl->handshake != NULL &&
3045 recv_msg_seq != ssl->handshake->in_msg_seq )
3046 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003047 /* Retransmit only on last message from previous flight, to avoid
3048 * too many retransmissions.
3049 * Besides, No sane server ever retransmits HelloVerifyRequest */
3050 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard93017de2014-09-19 22:42:40 +02003051 ssl->in_msg[0] != SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003052 {
3053 SSL_DEBUG_MSG( 2, ( "received message from last flight, "
3054 "message_seq = %d, start_of_flight = %d",
3055 recv_msg_seq,
3056 ssl->handshake->in_flight_start_seq ) );
3057
3058 if( ( ret = ssl_resend( ssl ) ) != 0 )
3059 {
3060 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3061 return( ret );
3062 }
3063 }
3064 else
3065 {
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02003066 SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003067 "message_seq = %d, expected = %d",
3068 recv_msg_seq,
3069 ssl->handshake->in_msg_seq ) );
3070 }
3071
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003072 return( POLARSSL_ERR_NET_WANT_READ );
3073 }
3074 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003075
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003076 /* Reassemble if current message is fragmented or reassembly is
3077 * already in progress */
3078 if( ssl->in_msglen < ssl->in_hslen ||
3079 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3080 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3081 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003082 {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003083 SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
3084
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003085 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3086 {
3087 SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
3088 return( ret );
3089 }
3090 }
3091 }
3092 else
3093#endif /* POLARSSL_SSL_PROTO_DTLS */
3094 /* With TLS we don't handle fragmentation (for now) */
3095 if( ssl->in_msglen < ssl->in_hslen )
3096 {
3097 SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02003098 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003099 }
3100
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003101 if( ssl->state != SSL_HANDSHAKE_OVER )
3102 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
3103
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003104 /* Handshake message is complete, increment counter */
3105#if defined(POLARSSL_SSL_PROTO_DTLS)
3106 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3107 ssl->handshake != NULL )
3108 {
3109 ssl->handshake->in_msg_seq++;
3110 }
3111#endif
3112
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003113 return( 0 );
3114}
3115
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003116/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003117 * DTLS anti-replay: RFC 6347 4.1.2.6
3118 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003119 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3120 * Bit n is set iff record number in_window_top - n has been seen.
3121 *
3122 * Usually, in_window_top is the last record number seen and the lsb of
3123 * in_window is set. The only exception is the initial state (record number 0
3124 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003125 */
3126#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3127static void ssl_dtls_replay_reset( ssl_context *ssl )
3128{
3129 ssl->in_window_top = 0;
3130 ssl->in_window = 0;
3131}
3132
3133static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3134{
3135 return( ( (uint64_t) buf[0] << 40 ) |
3136 ( (uint64_t) buf[1] << 32 ) |
3137 ( (uint64_t) buf[2] << 24 ) |
3138 ( (uint64_t) buf[3] << 16 ) |
3139 ( (uint64_t) buf[4] << 8 ) |
3140 ( (uint64_t) buf[5] ) );
3141}
3142
3143/*
3144 * Return 0 if sequence number is acceptable, -1 otherwise
3145 */
3146int ssl_dtls_replay_check( ssl_context *ssl )
3147{
3148 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3149 uint64_t bit;
3150
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003151 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
3152 return( 0 );
3153
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003154 if( rec_seqnum > ssl->in_window_top )
3155 return( 0 );
3156
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003157 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003158
3159 if( bit >= 64 )
3160 return( -1 );
3161
3162 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3163 return( -1 );
3164
3165 return( 0 );
3166}
3167
3168/*
3169 * Update replay window on new validated record
3170 */
3171void ssl_dtls_replay_update( ssl_context *ssl )
3172{
3173 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3174
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003175 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
3176 return;
3177
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003178 if( rec_seqnum > ssl->in_window_top )
3179 {
3180 /* Update window_top and the contents of the window */
3181 uint64_t shift = rec_seqnum - ssl->in_window_top;
3182
3183 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003184 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003185 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003186 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003187 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003188 ssl->in_window |= 1;
3189 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003190
3191 ssl->in_window_top = rec_seqnum;
3192 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003193 else
3194 {
3195 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003196 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003197
3198 if( bit < 64 ) /* Always true, but be extra sure */
3199 ssl->in_window |= (uint64_t) 1 << bit;
3200 }
3201}
3202#endif /* POLARSSL_SSL_DTLS_ANTI_REPLAY */
3203
3204/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003205 * ContentType type;
3206 * ProtocolVersion version;
3207 * uint16 epoch; // DTLS only
3208 * uint48 sequence_number; // DTLS only
3209 * uint16 length;
3210 */
3211static int ssl_parse_record_header( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003212{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003213 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003214 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003215
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003216 SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, ssl_hdr_len( ssl ) );
3217
Paul Bakker5121ce52009-01-03 21:22:43 +00003218 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003219 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003220 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003221
3222 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
3223 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003224 ssl->in_msgtype,
3225 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003226
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003227 /* Check record type */
3228 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
3229 ssl->in_msgtype != SSL_MSG_ALERT &&
3230 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
3231 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
3232 {
3233 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003234
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003235 if( ( ret = ssl_send_alert_message( ssl,
3236 SSL_ALERT_LEVEL_FATAL,
3237 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
3238 {
3239 return( ret );
3240 }
3241
3242 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3243 }
3244
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003245#if defined(POLARSSL_SSL_PROTO_DTLS)
3246 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3247 {
3248 /* Drop unexpected ChangeCipherSpec messages */
3249 if( ssl->in_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC &&
3250 ssl->state != SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3251 ssl->state != SSL_SERVER_CHANGE_CIPHER_SPEC )
3252 {
3253 SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3254 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3255 }
3256
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02003257 /* Drop unexpected ApplicationData records,
3258 * except at the beginning of renegotiations */
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003259 if( ssl->in_msgtype == SSL_MSG_APPLICATION_DATA &&
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00003260 ssl->state != SSL_HANDSHAKE_OVER
3261#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00003262 && ! ( ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS &&
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00003263 ssl->state == SSL_SERVER_HELLO )
3264#endif
3265 )
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003266 {
3267 SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
3268 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3269 }
3270 }
3271#endif
3272
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003273 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003274 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003275 {
3276 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003277 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003278 }
3279
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003280 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003281 {
3282 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003283 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003284 }
3285
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003286 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003287#if defined(POLARSSL_SSL_PROTO_DTLS)
3288 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3289 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003290 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003291
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003292 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003293 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003294 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003295 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003296 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003297 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003298 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003299
3300#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3301 if( ssl_dtls_replay_check( ssl ) != 0 )
3302 {
3303 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3304 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3305 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003306#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003307 }
3308#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003309
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003310 /* Check length against the size of our buffer */
3311 if( ssl->in_msglen > SSL_BUFFER_LEN
3312 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003313 {
3314 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3315 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3316 }
3317
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003318 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003319 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003320 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003321 if( ssl->in_msglen < 1 ||
3322 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003323 {
3324 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003325 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003326 }
3327 }
3328 else
3329 {
Paul Bakker48916f92012-09-16 19:57:18 +00003330 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003331 {
3332 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003333 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003334 }
3335
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003336#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003337 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00003338 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003339 {
3340 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003341 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003342 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003343#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003344#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3345 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003346 /*
3347 * TLS encrypted messages can have up to 256 bytes of padding
3348 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003349 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003350 ssl->in_msglen > ssl->transform_in->minlen +
3351 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003352 {
3353 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003354 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003355 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003356#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003357 }
3358
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003359 return( 0 );
3360}
Paul Bakker5121ce52009-01-03 21:22:43 +00003361
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003362/*
3363 * If applicable, decrypt (and decompress) record content
3364 */
3365static int ssl_prepare_record_content( ssl_context *ssl )
3366{
3367 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003368
Paul Bakker5121ce52009-01-03 21:22:43 +00003369 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003370 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003371
Paul Bakker05ef8352012-05-08 09:17:57 +00003372#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003373 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003374 {
3375 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
3376
3377 ret = ssl_hw_record_read( ssl );
3378 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
3379 {
3380 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003381 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003382 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003383
3384 if( ret == 0 )
3385 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003386 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003387#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003388 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003389 {
3390 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3391 {
3392 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
3393 return( ret );
3394 }
3395
3396 SSL_DEBUG_BUF( 4, "input payload after decrypt",
3397 ssl->in_msg, ssl->in_msglen );
3398
3399 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
3400 {
3401 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003402 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003403 }
3404 }
3405
Paul Bakker2770fbd2012-07-03 13:30:23 +00003406#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003407 if( ssl->transform_in != NULL &&
3408 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003409 {
3410 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3411 {
3412 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
3413 return( ret );
3414 }
3415
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003416 // TODO: what's the purpose of these lines? is in_len used?
3417 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3418 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003419 }
3420#endif /* POLARSSL_ZLIB_SUPPORT */
3421
Manuel Pégourié-Gonnard8464a462014-09-24 14:05:32 +02003422#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003423 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3424 {
3425 ssl_dtls_replay_update( ssl );
3426 }
3427#endif
3428
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003429 return( 0 );
3430}
3431
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003432static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
3433
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003434/*
3435 * Read a record.
3436 *
3437 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
3438 * and continue reading until a valid record is found.
3439 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003440int ssl_read_record( ssl_context *ssl )
3441{
3442 int ret;
3443
3444 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
3445
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02003446 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003447 {
3448 /*
3449 * Get next Handshake message in the current record
3450 */
3451 ssl->in_msglen -= ssl->in_hslen;
3452
3453 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3454 ssl->in_msglen );
3455
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003456 SSL_DEBUG_BUF( 4, "remaining content in record",
3457 ssl->in_msg, ssl->in_msglen );
3458
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003459 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3460 return( ret );
3461
3462 return( 0 );
3463 }
3464
3465 ssl->in_hslen = 0;
3466
3467 /*
3468 * Read the record header and parse it
3469 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003470#if defined(POLARSSL_SSL_PROTO_DTLS)
3471read_record_header:
3472#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003473 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3474 {
3475 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3476 return( ret );
3477 }
3478
3479 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003480 {
3481#if defined(POLARSSL_SSL_PROTO_DTLS)
3482 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3483 {
3484 /* Ignore bad record and get next one; drop the whole datagram
3485 * since current header cannot be trusted to find the next record
3486 * in current datagram */
3487 ssl->next_record_offset = 0;
3488 ssl->in_left = 0;
3489
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003490 SSL_DEBUG_MSG( 1, ( "discarding invalid record (header)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003491 goto read_record_header;
3492 }
3493#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003494 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003495 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003496
3497 /*
3498 * Read and optionally decrypt the message contents
3499 */
3500 if( ( ret = ssl_fetch_input( ssl,
3501 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3502 {
3503 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3504 return( ret );
3505 }
3506
3507 /* Done reading this record, get ready for the next one */
3508#if defined(POLARSSL_SSL_PROTO_DTLS)
3509 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3510 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3511 else
3512#endif
3513 ssl->in_left = 0;
3514
3515 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003516 {
3517#if defined(POLARSSL_SSL_PROTO_DTLS)
3518 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3519 {
3520 /* Silently discard invalid records */
3521 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3522 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3523 {
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02003524#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
3525 if( ssl->badmac_limit != 0 &&
3526 ++ssl->badmac_seen >= ssl->badmac_limit )
3527 {
3528 SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
3529 return( POLARSSL_ERR_SSL_INVALID_MAC );
3530 }
3531#endif
3532
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003533 SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003534 goto read_record_header;
3535 }
3536
3537 return( ret );
3538 }
3539 else
3540#endif
3541 {
3542 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard9b669902015-03-06 16:52:46 +00003543#if defined(POLARSSL_SSL_ALL_ALERT_MESSAGES)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003544 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3545 {
3546 ssl_send_alert_message( ssl,
3547 SSL_ALERT_LEVEL_FATAL,
3548 SSL_ALERT_MSG_BAD_RECORD_MAC );
3549 }
3550#endif
3551 return( ret );
3552 }
3553 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003554
3555 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003556 * When we sent the last flight of the handshake, we MUST respond to a
3557 * retransmit of the peer's previous flight with a retransmit. (In
3558 * practice, only the Finished message will make it, other messages
3559 * including CCS use the old transform so they're dropped as invalid.)
3560 *
3561 * If the record we received is not a handshake message, however, it
3562 * means the peer received our last flight so we can clean up
3563 * handshake info.
3564 *
3565 * This check needs to be done before prepare_handshake() due to an edge
3566 * case: if the client immediately requests renegotiation, this
3567 * finishes the current handshake first, avoiding the new ClientHello
3568 * being mistaken for an ancient message in the current handshake.
3569 */
3570#if defined(POLARSSL_SSL_PROTO_DTLS)
3571 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3572 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003573 ssl->state == SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003574 {
3575 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3576 ssl->in_msg[0] == SSL_HS_FINISHED )
3577 {
3578 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3579
3580 if( ( ret = ssl_resend( ssl ) ) != 0 )
3581 {
3582 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3583 return( ret );
3584 }
3585
3586 return( POLARSSL_ERR_NET_WANT_READ );
3587 }
3588 else
3589 {
3590 ssl_handshake_wrapup_free_hs_transform( ssl );
3591 }
3592 }
3593#endif
3594
3595 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003596 * Handle particular types of records
3597 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003598 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3599 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003600 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3601 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003602 }
3603
3604 if( ssl->in_msgtype == SSL_MSG_ALERT )
3605 {
3606 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3607 ssl->in_msg[0], ssl->in_msg[1] ) );
3608
3609 /*
3610 * Ignore non-fatal alerts, except close_notify
3611 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003612 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003613 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003614 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3615 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003616 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003617 }
3618
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003619 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3620 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003621 {
3622 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003623 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003624 }
3625 }
3626
Paul Bakker5121ce52009-01-03 21:22:43 +00003627 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3628
3629 return( 0 );
3630}
3631
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003632int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3633{
3634 int ret;
3635
3636 if( ( ret = ssl_send_alert_message( ssl,
3637 SSL_ALERT_LEVEL_FATAL,
3638 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3639 {
3640 return( ret );
3641 }
3642
3643 return( 0 );
3644}
3645
Paul Bakker0a925182012-04-16 06:46:41 +00003646int ssl_send_alert_message( ssl_context *ssl,
3647 unsigned char level,
3648 unsigned char message )
3649{
3650 int ret;
3651
3652 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3653
3654 ssl->out_msgtype = SSL_MSG_ALERT;
3655 ssl->out_msglen = 2;
3656 ssl->out_msg[0] = level;
3657 ssl->out_msg[1] = message;
3658
3659 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3660 {
3661 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3662 return( ret );
3663 }
3664
3665 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3666
3667 return( 0 );
3668}
3669
Paul Bakker5121ce52009-01-03 21:22:43 +00003670/*
3671 * Handshake functions
3672 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003673#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3674 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3675 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3676 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3677 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3678 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3679 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003680int ssl_write_certificate( ssl_context *ssl )
3681{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003682 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003683
3684 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3685
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003686 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003687 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3688 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003689 {
3690 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3691 ssl->state++;
3692 return( 0 );
3693 }
3694
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003695 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3696 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003697}
3698
3699int ssl_parse_certificate( ssl_context *ssl )
3700{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003701 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3702
3703 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3704
3705 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003706 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3707 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003708 {
3709 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3710 ssl->state++;
3711 return( 0 );
3712 }
3713
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003714 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3715 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003716}
3717#else
3718int ssl_write_certificate( ssl_context *ssl )
3719{
3720 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3721 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003722 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003723 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3724
3725 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3726
3727 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003728 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3729 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003730 {
3731 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3732 ssl->state++;
3733 return( 0 );
3734 }
3735
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003736#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003737 if( ssl->endpoint == SSL_IS_CLIENT )
3738 {
3739 if( ssl->client_auth == 0 )
3740 {
3741 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3742 ssl->state++;
3743 return( 0 );
3744 }
3745
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003746#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003747 /*
3748 * If using SSLv3 and got no cert, send an Alert message
3749 * (otherwise an empty Certificate message will be sent).
3750 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003751 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003752 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3753 {
3754 ssl->out_msglen = 2;
3755 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003756 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3757 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003758
3759 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3760 goto write_msg;
3761 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003762#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003763 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003764#endif /* POLARSSL_SSL_CLI_C */
3765#if defined(POLARSSL_SSL_SRV_C)
3766 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003767 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003768 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003769 {
3770 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003771 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003772 }
3773 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003774#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003775
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003776 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003777
3778 /*
3779 * 0 . 0 handshake type
3780 * 1 . 3 handshake length
3781 * 4 . 6 length of all certs
3782 * 7 . 9 length of cert. 1
3783 * 10 . n-1 peer certificate
3784 * n . n+2 length of cert. 2
3785 * n+3 . ... upper level cert, etc.
3786 */
3787 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003788 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003789
Paul Bakker29087132010-03-21 21:03:34 +00003790 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003791 {
3792 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003793 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003794 {
3795 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3796 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003797 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003798 }
3799
3800 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3801 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3802 ssl->out_msg[i + 2] = (unsigned char)( n );
3803
3804 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3805 i += n; crt = crt->next;
3806 }
3807
3808 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3809 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3810 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3811
3812 ssl->out_msglen = i;
3813 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3814 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3815
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003816#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003817write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003818#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003819
3820 ssl->state++;
3821
3822 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3823 {
3824 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3825 return( ret );
3826 }
3827
3828 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3829
Paul Bakkered27a042013-04-18 22:46:23 +02003830 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003831}
3832
3833int ssl_parse_certificate( ssl_context *ssl )
3834{
Paul Bakkered27a042013-04-18 22:46:23 +02003835 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003836 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003837 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003838
3839 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3840
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003841 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003842 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3843 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003844 {
3845 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3846 ssl->state++;
3847 return( 0 );
3848 }
3849
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003850#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003851 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003852 ( ssl->authmode == SSL_VERIFY_NONE ||
3853 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003854 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003855 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003856 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3857 ssl->state++;
3858 return( 0 );
3859 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003860#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003861
3862 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3863 {
3864 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3865 return( ret );
3866 }
3867
3868 ssl->state++;
3869
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003870#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003871#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003872 /*
3873 * Check if the client sent an empty certificate
3874 */
3875 if( ssl->endpoint == SSL_IS_SERVER &&
3876 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3877 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003878 if( ssl->in_msglen == 2 &&
3879 ssl->in_msgtype == SSL_MSG_ALERT &&
3880 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3881 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003882 {
3883 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3884
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003885 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003886 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3887 return( 0 );
3888 else
Paul Bakker40e46942009-01-03 21:51:57 +00003889 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003890 }
3891 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003892#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003893
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003894#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3895 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003896 if( ssl->endpoint == SSL_IS_SERVER &&
3897 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3898 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003899 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003900 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3901 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003902 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003903 {
3904 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3905
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003906 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003907 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003908 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003909 else
3910 return( 0 );
3911 }
3912 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003913#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3914 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003915#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003916
3917 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3918 {
3919 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003920 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003921 }
3922
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003923 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3924 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003925 {
3926 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003927 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003928 }
3929
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003930 i = ssl_hs_hdr_len( ssl );
3931
Paul Bakker5121ce52009-01-03 21:22:43 +00003932 /*
3933 * Same message structure as in ssl_write_certificate()
3934 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003935 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003936
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003937 if( ssl->in_msg[i] != 0 ||
3938 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003939 {
3940 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003941 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003942 }
3943
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003944 /* In case we tried to reuse a session but it failed */
3945 if( ssl->session_negotiate->peer_cert != NULL )
3946 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003947 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003948 polarssl_free( ssl->session_negotiate->peer_cert );
3949 }
3950
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003951 if( ( ssl->session_negotiate->peer_cert = polarssl_malloc(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003952 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003953 {
3954 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003955 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003956 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003957 }
3958
Paul Bakkerb6b09562013-09-18 14:17:41 +02003959 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003960
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003961 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003962
3963 while( i < ssl->in_hslen )
3964 {
3965 if( ssl->in_msg[i] != 0 )
3966 {
3967 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003968 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003969 }
3970
3971 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3972 | (unsigned int) ssl->in_msg[i + 2];
3973 i += 3;
3974
3975 if( n < 128 || i + n > ssl->in_hslen )
3976 {
3977 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003978 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003979 }
3980
Paul Bakkerddf26b42013-09-18 13:46:23 +02003981 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3982 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003983 if( ret != 0 )
3984 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003985 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003986 return( ret );
3987 }
3988
3989 i += n;
3990 }
3991
Paul Bakker48916f92012-09-16 19:57:18 +00003992 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003993
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003994 /*
3995 * On client, make sure the server cert doesn't change during renego to
3996 * avoid "triple handshake" attack: https://secure-resumption.com/
3997 */
Paul Bakkerf6080b82015-01-13 16:18:23 +01003998#if defined(POLARSSL_SSL_RENEGOTIATION) && defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003999 if( ssl->endpoint == SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00004000 ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004001 {
4002 if( ssl->session->peer_cert == NULL )
4003 {
4004 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
4005 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
4006 }
4007
4008 if( ssl->session->peer_cert->raw.len !=
4009 ssl->session_negotiate->peer_cert->raw.len ||
4010 memcmp( ssl->session->peer_cert->raw.p,
4011 ssl->session_negotiate->peer_cert->raw.p,
4012 ssl->session->peer_cert->raw.len ) != 0 )
4013 {
4014 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
4015 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
4016 }
4017 }
Paul Bakkerf6080b82015-01-13 16:18:23 +01004018#endif /* POLARSSL_SSL_RENEGOTIATION && POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004019
Paul Bakker5121ce52009-01-03 21:22:43 +00004020 if( ssl->authmode != SSL_VERIFY_NONE )
4021 {
4022 if( ssl->ca_chain == NULL )
4023 {
4024 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004025 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004026 }
4027
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004028 /*
4029 * Main check: verify certificate
4030 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02004031 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
4032 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
4033 &ssl->session_negotiate->verify_result,
4034 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00004035
4036 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004037 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004038 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004039 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004040
4041 /*
4042 * Secondary checks: always done, but change 'ret' only if it was 0
4043 */
4044
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004045#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004046 {
Manuel Pégourié-Gonnard0db107e2015-03-19 14:01:57 +00004047 const pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004048
4049 /* If certificate uses an EC key, make sure the curve is OK */
4050 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
4051 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
4052 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02004053 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004054 if( ret == 0 )
4055 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004056 }
4057 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004058#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00004059
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004060 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
4061 ciphersuite_info,
4062 ! ssl->endpoint ) != 0 )
4063 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02004064 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004065 if( ret == 0 )
4066 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
4067 }
4068
Paul Bakker5121ce52009-01-03 21:22:43 +00004069 if( ssl->authmode != SSL_VERIFY_REQUIRED )
4070 ret = 0;
4071 }
4072
4073 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
4074
4075 return( ret );
4076}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01004077#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
4078 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
4079 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
4080 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
4081 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
4082 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
4083 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004084
4085int ssl_write_change_cipher_spec( ssl_context *ssl )
4086{
4087 int ret;
4088
4089 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
4090
4091 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
4092 ssl->out_msglen = 1;
4093 ssl->out_msg[0] = 1;
4094
Paul Bakker5121ce52009-01-03 21:22:43 +00004095 ssl->state++;
4096
4097 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4098 {
4099 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4100 return( ret );
4101 }
4102
4103 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
4104
4105 return( 0 );
4106}
4107
4108int ssl_parse_change_cipher_spec( ssl_context *ssl )
4109{
4110 int ret;
4111
4112 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
4113
Paul Bakker5121ce52009-01-03 21:22:43 +00004114 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4115 {
4116 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4117 return( ret );
4118 }
4119
4120 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
4121 {
4122 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004123 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004124 }
4125
4126 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
4127 {
4128 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004129 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00004130 }
4131
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004132 /*
4133 * Switch to our negotiated transform and session parameters for inbound
4134 * data.
4135 */
4136 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
4137 ssl->transform_in = ssl->transform_negotiate;
4138 ssl->session_in = ssl->session_negotiate;
4139
4140#if defined(POLARSSL_SSL_PROTO_DTLS)
4141 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4142 {
4143#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4144 ssl_dtls_replay_reset( ssl );
4145#endif
4146
4147 /* Increment epoch */
4148 if( ++ssl->in_epoch == 0 )
4149 {
4150 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4151 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4152 }
4153 }
4154 else
4155#endif /* POLARSSL_SSL_PROTO_DTLS */
4156 memset( ssl->in_ctr, 0, 8 );
4157
4158 /*
4159 * Set the in_msg pointer to the correct location based on IV length
4160 */
4161 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4162 {
4163 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
4164 ssl->transform_negotiate->fixed_ivlen;
4165 }
4166 else
4167 ssl->in_msg = ssl->in_iv;
4168
4169#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4170 if( ssl_hw_record_activate != NULL )
4171 {
4172 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
4173 {
4174 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4175 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4176 }
4177 }
4178#endif
4179
Paul Bakker5121ce52009-01-03 21:22:43 +00004180 ssl->state++;
4181
4182 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
4183
4184 return( 0 );
4185}
4186
Paul Bakker41c83d32013-03-20 14:39:14 +01004187void ssl_optimize_checksum( ssl_context *ssl,
4188 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00004189{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02004190 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01004191
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004192#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4193 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00004194 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00004195 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00004196 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004197#endif
4198#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4199#if defined(POLARSSL_SHA512_C)
4200 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
4201 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
4202 else
4203#endif
4204#if defined(POLARSSL_SHA256_C)
4205 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00004206 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004207 else
4208#endif
4209#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004210 {
4211 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004212 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004213 }
Paul Bakker380da532012-04-18 16:10:25 +00004214}
Paul Bakkerf7abd422013-04-16 13:15:56 +02004215
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004216void ssl_reset_checksum( ssl_context *ssl )
4217{
4218#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4219 defined(POLARSSL_SSL_PROTO_TLS1_1)
4220 md5_starts( &ssl->handshake->fin_md5 );
4221 sha1_starts( &ssl->handshake->fin_sha1 );
4222#endif
4223#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4224#if defined(POLARSSL_SHA256_C)
4225 sha256_starts( &ssl->handshake->fin_sha256, 0 );
4226#endif
4227#if defined(POLARSSL_SHA512_C)
4228 sha512_starts( &ssl->handshake->fin_sha512, 1 );
4229#endif
4230#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4231}
4232
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004233static void ssl_update_checksum_start( ssl_context *ssl,
4234 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004235{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004236#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4237 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00004238 md5_update( &ssl->handshake->fin_md5 , buf, len );
4239 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004240#endif
4241#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4242#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02004243 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004244#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02004245#if defined(POLARSSL_SHA512_C)
4246 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01004247#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004248#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004249}
4250
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004251#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4252 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004253static void ssl_update_checksum_md5sha1( ssl_context *ssl,
4254 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004255{
Paul Bakker48916f92012-09-16 19:57:18 +00004256 md5_update( &ssl->handshake->fin_md5 , buf, len );
4257 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004258}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004259#endif
Paul Bakker380da532012-04-18 16:10:25 +00004260
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004261#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4262#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004263static void ssl_update_checksum_sha256( ssl_context *ssl,
4264 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004265{
Paul Bakker9e36f042013-06-30 14:34:05 +02004266 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004267}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004268#endif
Paul Bakker380da532012-04-18 16:10:25 +00004269
Paul Bakker9e36f042013-06-30 14:34:05 +02004270#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004271static void ssl_update_checksum_sha384( ssl_context *ssl,
4272 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004273{
Paul Bakker9e36f042013-06-30 14:34:05 +02004274 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004275}
Paul Bakker769075d2012-11-24 11:26:46 +01004276#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004277#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004278
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004279#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004280static void ssl_calc_finished_ssl(
4281 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004282{
Paul Bakker3c2122f2013-06-24 19:03:14 +02004283 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004284 md5_context md5;
4285 sha1_context sha1;
4286
Paul Bakker5121ce52009-01-03 21:22:43 +00004287 unsigned char padbuf[48];
4288 unsigned char md5sum[16];
4289 unsigned char sha1sum[20];
4290
Paul Bakker48916f92012-09-16 19:57:18 +00004291 ssl_session *session = ssl->session_negotiate;
4292 if( !session )
4293 session = ssl->session;
4294
Paul Bakker1ef83d62012-04-11 12:09:53 +00004295 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
4296
Paul Bakker48916f92012-09-16 19:57:18 +00004297 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4298 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004299
4300 /*
4301 * SSLv3:
4302 * hash =
4303 * MD5( master + pad2 +
4304 * MD5( handshake + sender + master + pad1 ) )
4305 * + SHA1( master + pad2 +
4306 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004307 */
4308
Paul Bakker90995b52013-06-24 19:20:35 +02004309#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004310 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004311 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004312#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004313
Paul Bakker90995b52013-06-24 19:20:35 +02004314#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004315 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004316 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004317#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004318
Paul Bakker3c2122f2013-06-24 19:03:14 +02004319 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
4320 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004321
Paul Bakker1ef83d62012-04-11 12:09:53 +00004322 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004323
Paul Bakker3c2122f2013-06-24 19:03:14 +02004324 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004325 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004326 md5_update( &md5, padbuf, 48 );
4327 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004328
Paul Bakker3c2122f2013-06-24 19:03:14 +02004329 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004330 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004331 sha1_update( &sha1, padbuf, 40 );
4332 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004333
Paul Bakker1ef83d62012-04-11 12:09:53 +00004334 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004335
Paul Bakker1ef83d62012-04-11 12:09:53 +00004336 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00004337 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004338 md5_update( &md5, padbuf, 48 );
4339 md5_update( &md5, md5sum, 16 );
4340 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004341
Paul Bakker1ef83d62012-04-11 12:09:53 +00004342 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00004343 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004344 sha1_update( &sha1, padbuf , 40 );
4345 sha1_update( &sha1, sha1sum, 20 );
4346 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004347
Paul Bakker1ef83d62012-04-11 12:09:53 +00004348 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004349
Paul Bakker5b4af392014-06-26 12:09:34 +02004350 md5_free( &md5 );
4351 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004352
Paul Bakker34617722014-06-13 17:20:13 +02004353 polarssl_zeroize( padbuf, sizeof( padbuf ) );
4354 polarssl_zeroize( md5sum, sizeof( md5sum ) );
4355 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004356
4357 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4358}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004359#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004360
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004361#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004362static void ssl_calc_finished_tls(
4363 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004364{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004365 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004366 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004367 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00004368 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004369 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004370
Paul Bakker48916f92012-09-16 19:57:18 +00004371 ssl_session *session = ssl->session_negotiate;
4372 if( !session )
4373 session = ssl->session;
4374
Paul Bakker1ef83d62012-04-11 12:09:53 +00004375 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004376
Paul Bakker48916f92012-09-16 19:57:18 +00004377 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4378 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004379
Paul Bakker1ef83d62012-04-11 12:09:53 +00004380 /*
4381 * TLSv1:
4382 * hash = PRF( master, finished_label,
4383 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4384 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004385
Paul Bakker90995b52013-06-24 19:20:35 +02004386#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004387 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4388 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004389#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004390
Paul Bakker90995b52013-06-24 19:20:35 +02004391#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004392 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4393 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004394#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004395
4396 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004397 ? "client finished"
4398 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004399
4400 md5_finish( &md5, padbuf );
4401 sha1_finish( &sha1, padbuf + 16 );
4402
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004403 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004404 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004405
4406 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4407
Paul Bakker5b4af392014-06-26 12:09:34 +02004408 md5_free( &md5 );
4409 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004410
Paul Bakker34617722014-06-13 17:20:13 +02004411 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004412
4413 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4414}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004415#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004416
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004417#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4418#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004419static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004420 ssl_context *ssl, unsigned char *buf, int from )
4421{
4422 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004423 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004424 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004425 unsigned char padbuf[32];
4426
Paul Bakker48916f92012-09-16 19:57:18 +00004427 ssl_session *session = ssl->session_negotiate;
4428 if( !session )
4429 session = ssl->session;
4430
Paul Bakker380da532012-04-18 16:10:25 +00004431 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004432
Paul Bakker9e36f042013-06-30 14:34:05 +02004433 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004434
4435 /*
4436 * TLSv1.2:
4437 * hash = PRF( master, finished_label,
4438 * Hash( handshake ) )[0.11]
4439 */
4440
Paul Bakker9e36f042013-06-30 14:34:05 +02004441#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004442 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004443 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004444#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004445
4446 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004447 ? "client finished"
4448 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004449
Paul Bakker9e36f042013-06-30 14:34:05 +02004450 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004451
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004452 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004453 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004454
4455 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4456
Paul Bakker5b4af392014-06-26 12:09:34 +02004457 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004458
Paul Bakker34617722014-06-13 17:20:13 +02004459 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004460
4461 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4462}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004463#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004464
Paul Bakker9e36f042013-06-30 14:34:05 +02004465#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004466static void ssl_calc_finished_tls_sha384(
4467 ssl_context *ssl, unsigned char *buf, int from )
4468{
4469 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004470 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004471 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004472 unsigned char padbuf[48];
4473
Paul Bakker48916f92012-09-16 19:57:18 +00004474 ssl_session *session = ssl->session_negotiate;
4475 if( !session )
4476 session = ssl->session;
4477
Paul Bakker380da532012-04-18 16:10:25 +00004478 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004479
Paul Bakker9e36f042013-06-30 14:34:05 +02004480 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004481
4482 /*
4483 * TLSv1.2:
4484 * hash = PRF( master, finished_label,
4485 * Hash( handshake ) )[0.11]
4486 */
4487
Paul Bakker9e36f042013-06-30 14:34:05 +02004488#if !defined(POLARSSL_SHA512_ALT)
4489 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4490 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004491#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004492
4493 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004494 ? "client finished"
4495 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004496
Paul Bakker9e36f042013-06-30 14:34:05 +02004497 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004498
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004499 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004500 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004501
4502 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4503
Paul Bakker5b4af392014-06-26 12:09:34 +02004504 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004505
Paul Bakker34617722014-06-13 17:20:13 +02004506 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004507
4508 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4509}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004510#endif /* POLARSSL_SHA512_C */
4511#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004512
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004513static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004514{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004515 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004516
4517 /*
4518 * Free our handshake params
4519 */
4520 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004521 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004522 ssl->handshake = NULL;
4523
4524 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004525 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004526 */
4527 if( ssl->transform )
4528 {
4529 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004530 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004531 }
4532 ssl->transform = ssl->transform_negotiate;
4533 ssl->transform_negotiate = NULL;
4534
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004535 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4536}
4537
4538void ssl_handshake_wrapup( ssl_context *ssl )
4539{
4540 int resume = ssl->handshake->resume;
4541
4542 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4543
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004544#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00004545 if( ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004546 {
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00004547 ssl->renego_status = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004548 ssl->renego_records_seen = 0;
4549 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004550#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004551
4552 /*
4553 * Free the previous session and switch in the current one
4554 */
Paul Bakker0a597072012-09-25 21:55:46 +00004555 if( ssl->session )
4556 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01004557#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4558 /* RFC 7366 3.1: keep the EtM state */
4559 ssl->session_negotiate->encrypt_then_mac =
4560 ssl->session->encrypt_then_mac;
4561#endif
4562
Paul Bakker0a597072012-09-25 21:55:46 +00004563 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004564 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004565 }
4566 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004567 ssl->session_negotiate = NULL;
4568
Paul Bakker0a597072012-09-25 21:55:46 +00004569 /*
4570 * Add cache entry
4571 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004572 if( ssl->f_set_cache != NULL &&
4573 ssl->session->length != 0 &&
4574 resume == 0 )
4575 {
Paul Bakker0a597072012-09-25 21:55:46 +00004576 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4577 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004578 }
Paul Bakker0a597072012-09-25 21:55:46 +00004579
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004580#if defined(POLARSSL_SSL_PROTO_DTLS)
4581 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4582 ssl->handshake->flight != NULL )
4583 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02004584 /* Cancel handshake timer */
4585 ssl_set_timer( ssl, 0 );
4586
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004587 /* Keep last flight around in case we need to resend it:
4588 * we need the handshake and transform structures for that */
4589 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4590 }
4591 else
4592#endif
4593 ssl_handshake_wrapup_free_hs_transform( ssl );
4594
Paul Bakker48916f92012-09-16 19:57:18 +00004595 ssl->state++;
4596
4597 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4598}
4599
Paul Bakker1ef83d62012-04-11 12:09:53 +00004600int ssl_write_finished( ssl_context *ssl )
4601{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004602 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004603
4604 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4605
Paul Bakker92be97b2013-01-02 17:30:03 +01004606 /*
4607 * Set the out_msg pointer to the correct location based on IV length
4608 */
4609 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4610 {
4611 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4612 ssl->transform_negotiate->fixed_ivlen;
4613 }
4614 else
4615 ssl->out_msg = ssl->out_iv;
4616
Paul Bakker48916f92012-09-16 19:57:18 +00004617 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004618
4619 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004620 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4621
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004622#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004623 ssl->verify_data_len = hash_len;
4624 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004625#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004626
Paul Bakker5121ce52009-01-03 21:22:43 +00004627 ssl->out_msglen = 4 + hash_len;
4628 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4629 ssl->out_msg[0] = SSL_HS_FINISHED;
4630
4631 /*
4632 * In case of session resuming, invert the client and server
4633 * ChangeCipherSpec messages order.
4634 */
Paul Bakker0a597072012-09-25 21:55:46 +00004635 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004636 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004637#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004638 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004639 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004640#endif
4641#if defined(POLARSSL_SSL_SRV_C)
4642 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004643 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004644#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004645 }
4646 else
4647 ssl->state++;
4648
Paul Bakker48916f92012-09-16 19:57:18 +00004649 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004650 * Switch to our negotiated transform and session parameters for outbound
4651 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004652 */
4653 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004654
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004655#if defined(POLARSSL_SSL_PROTO_DTLS)
4656 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4657 {
4658 unsigned char i;
4659
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004660 /* Remember current epoch settings for resending */
4661 ssl->handshake->alt_transform_out = ssl->transform_out;
4662 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4663
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004664 /* Set sequence_number to zero */
4665 memset( ssl->out_ctr + 2, 0, 6 );
4666
4667 /* Increment epoch */
4668 for( i = 2; i > 0; i-- )
4669 if( ++ssl->out_ctr[i - 1] != 0 )
4670 break;
4671
4672 /* The loop goes to its end iff the counter is wrapping */
4673 if( i == 0 )
4674 {
4675 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4676 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4677 }
4678 }
4679 else
4680#endif /* POLARSSL_SSL_PROTO_DTLS */
4681 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004682
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004683 ssl->transform_out = ssl->transform_negotiate;
4684 ssl->session_out = ssl->session_negotiate;
4685
Paul Bakker07eb38b2012-12-19 14:42:06 +01004686#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004687 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004688 {
4689 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4690 {
4691 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4692 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4693 }
4694 }
4695#endif
4696
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004697#if defined(POLARSSL_SSL_PROTO_DTLS)
4698 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4699 ssl_send_flight_completed( ssl );
4700#endif
4701
Paul Bakker5121ce52009-01-03 21:22:43 +00004702 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4703 {
4704 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4705 return( ret );
4706 }
4707
4708 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4709
4710 return( 0 );
4711}
4712
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004713#if defined(POLARSSL_SSL_PROTO_SSL3)
4714#define SSL_MAX_HASH_LEN 36
4715#else
4716#define SSL_MAX_HASH_LEN 12
4717#endif
4718
Paul Bakker5121ce52009-01-03 21:22:43 +00004719int ssl_parse_finished( ssl_context *ssl )
4720{
Paul Bakker23986e52011-04-24 08:57:21 +00004721 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004722 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004723 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004724
4725 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4726
Paul Bakker48916f92012-09-16 19:57:18 +00004727 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004728
Paul Bakker5121ce52009-01-03 21:22:43 +00004729 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4730 {
4731 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4732 return( ret );
4733 }
4734
4735 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4736 {
4737 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004738 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004739 }
4740
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004741 /* There is currently no ciphersuite using another length with TLS 1.2 */
4742#if defined(POLARSSL_SSL_PROTO_SSL3)
4743 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4744 hash_len = 36;
4745 else
4746#endif
4747 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004748
4749 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004750 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004751 {
4752 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004753 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004754 }
4755
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004756 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4757 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004758 {
4759 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004760 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004761 }
4762
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004763#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004764 ssl->verify_data_len = hash_len;
4765 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004766#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004767
Paul Bakker0a597072012-09-25 21:55:46 +00004768 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004769 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004770#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004771 if( ssl->endpoint == SSL_IS_CLIENT )
4772 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004773#endif
4774#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004775 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004776 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004777#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004778 }
4779 else
4780 ssl->state++;
4781
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004782#if defined(POLARSSL_SSL_PROTO_DTLS)
4783 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4784 ssl_recv_flight_completed( ssl );
4785#endif
4786
Paul Bakker5121ce52009-01-03 21:22:43 +00004787 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4788
4789 return( 0 );
4790}
4791
Paul Bakker968afaa2014-07-09 11:09:24 +02004792static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004793{
4794 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4795
4796#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4797 defined(POLARSSL_SSL_PROTO_TLS1_1)
4798 md5_init( &handshake->fin_md5 );
4799 sha1_init( &handshake->fin_sha1 );
4800 md5_starts( &handshake->fin_md5 );
4801 sha1_starts( &handshake->fin_sha1 );
4802#endif
4803#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4804#if defined(POLARSSL_SHA256_C)
4805 sha256_init( &handshake->fin_sha256 );
4806 sha256_starts( &handshake->fin_sha256, 0 );
4807#endif
4808#if defined(POLARSSL_SHA512_C)
4809 sha512_init( &handshake->fin_sha512 );
4810 sha512_starts( &handshake->fin_sha512, 1 );
4811#endif
4812#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4813
4814 handshake->update_checksum = ssl_update_checksum_start;
4815 handshake->sig_alg = SSL_HASH_SHA1;
4816
4817#if defined(POLARSSL_DHM_C)
4818 dhm_init( &handshake->dhm_ctx );
4819#endif
4820#if defined(POLARSSL_ECDH_C)
4821 ecdh_init( &handshake->ecdh_ctx );
4822#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004823}
4824
4825static void ssl_transform_init( ssl_transform *transform )
4826{
4827 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004828
4829 cipher_init( &transform->cipher_ctx_enc );
4830 cipher_init( &transform->cipher_ctx_dec );
4831
4832 md_init( &transform->md_ctx_enc );
4833 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004834}
4835
4836void ssl_session_init( ssl_session *session )
4837{
4838 memset( session, 0, sizeof(ssl_session) );
4839}
4840
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004841static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004842{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004843 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004844 if( ssl->transform_negotiate )
4845 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004846 if( ssl->session_negotiate )
4847 ssl_session_free( ssl->session_negotiate );
4848 if( ssl->handshake )
4849 ssl_handshake_free( ssl->handshake );
4850
4851 /*
4852 * Either the pointers are now NULL or cleared properly and can be freed.
4853 * Now allocate missing structures.
4854 */
4855 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004856 {
Mansour Moufid99b92592015-02-15 17:46:32 -05004857 ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004858 }
Paul Bakker48916f92012-09-16 19:57:18 +00004859
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004860 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004861 {
Mansour Moufid99b92592015-02-15 17:46:32 -05004862 ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004863 }
Paul Bakker48916f92012-09-16 19:57:18 +00004864
Paul Bakker82788fb2014-10-20 13:59:19 +02004865 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004866 {
Mansour Moufid99b92592015-02-15 17:46:32 -05004867 ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004868 }
Paul Bakker48916f92012-09-16 19:57:18 +00004869
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004870 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004871 if( ssl->handshake == NULL ||
4872 ssl->transform_negotiate == NULL ||
4873 ssl->session_negotiate == NULL )
4874 {
4875 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004876
4877 polarssl_free( ssl->handshake );
4878 polarssl_free( ssl->transform_negotiate );
4879 polarssl_free( ssl->session_negotiate );
4880
4881 ssl->handshake = NULL;
4882 ssl->transform_negotiate = NULL;
4883 ssl->session_negotiate = NULL;
4884
Paul Bakker48916f92012-09-16 19:57:18 +00004885 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4886 }
4887
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004888 /* Initialize structures */
4889 ssl_session_init( ssl->session_negotiate );
4890 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004891 ssl_handshake_params_init( ssl->handshake );
4892
4893#if defined(POLARSSL_X509_CRT_PARSE_C)
4894 ssl->handshake->key_cert = ssl->key_cert;
4895#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004896
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004897 /*
4898 * We may not know yet if we're using DTLS,
4899 * so always initiliase DTLS-specific fields.
4900 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004901#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004902 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004903
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004904 // TODO: not the right place, we may not know endpoint yet
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004905 if( ssl->endpoint == SSL_IS_CLIENT )
4906 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4907 else
4908 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004909#endif
4910
Paul Bakker48916f92012-09-16 19:57:18 +00004911 return( 0 );
4912}
4913
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004914#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4915/* Dummy cookie callbacks for defaults */
4916static int ssl_cookie_write_dummy( void *ctx,
4917 unsigned char **p, unsigned char *end,
4918 const unsigned char *cli_id, size_t cli_id_len )
4919{
4920 ((void) ctx);
4921 ((void) p);
4922 ((void) end);
4923 ((void) cli_id);
4924 ((void) cli_id_len);
4925
4926 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4927}
4928
4929static int ssl_cookie_check_dummy( void *ctx,
4930 const unsigned char *cookie, size_t cookie_len,
4931 const unsigned char *cli_id, size_t cli_id_len )
4932{
4933 ((void) ctx);
4934 ((void) cookie);
4935 ((void) cookie_len);
4936 ((void) cli_id);
4937 ((void) cli_id_len);
4938
4939 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4940}
4941#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4942
Paul Bakker5121ce52009-01-03 21:22:43 +00004943/*
4944 * Initialize an SSL context
4945 */
4946int ssl_init( ssl_context *ssl )
4947{
Paul Bakker48916f92012-09-16 19:57:18 +00004948 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004949 int len = SSL_BUFFER_LEN;
4950
4951 memset( ssl, 0, sizeof( ssl_context ) );
4952
Paul Bakker62f2dee2012-09-28 07:31:51 +00004953 /*
4954 * Sane defaults
4955 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004956 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4957 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4958 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4959 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004960
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004961 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004962
Manuel Pégourié-Gonnard849b1742015-03-20 19:13:22 +00004963 ssl_set_arc4_support( ssl, SSL_ARC4_DISABLED );
4964
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004965#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004966 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004967 memset( ssl->renego_period, 0xFF, 7 );
4968 ssl->renego_period[7] = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004969#endif
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004970
Paul Bakker62f2dee2012-09-28 07:31:51 +00004971#if defined(POLARSSL_DHM_C)
4972 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4973 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4974 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4975 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4976 {
4977 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4978 return( ret );
4979 }
4980#endif
4981
4982 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004983 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004984 */
Manuel Pégourié-Gonnard4e41c992015-02-18 10:39:49 +00004985 if( ( ssl->in_buf = polarssl_malloc( len ) ) == NULL ||
4986 ( ssl->out_buf = polarssl_malloc( len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004987 {
4988 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004989 polarssl_free( ssl->in_buf );
4990 ssl->in_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004991 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004992 }
4993
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004994 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4995 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004996
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004997 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4998 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4999
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01005000#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
5001 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
5002#endif
5003
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02005004#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
5005 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
5006#endif
5007
Paul Bakker606b4ba2013-08-14 16:52:14 +02005008#if defined(POLARSSL_SSL_SESSION_TICKETS)
5009 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
5010#endif
5011
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005012#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01005013 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01005014#endif
5015
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005016#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
5017 ssl->f_cookie_write = ssl_cookie_write_dummy;
5018 ssl->f_cookie_check = ssl_cookie_check_dummy;
5019#endif
5020
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005021#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
5022 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
5023#endif
5024
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02005025#if defined(POLARSSL_SSL_PROTO_DTLS)
5026 ssl->hs_timeout_min = SSL_DTLS_TIMEOUT_DFL_MIN;
5027 ssl->hs_timeout_max = SSL_DTLS_TIMEOUT_DFL_MAX;
5028#endif
5029
Paul Bakker48916f92012-09-16 19:57:18 +00005030 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5031 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005032
5033 return( 0 );
5034}
5035
5036/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00005037 * Reset an initialized and used SSL context for re-use while retaining
5038 * all application-set variables, function pointers and data.
5039 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005040int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00005041{
Paul Bakker48916f92012-09-16 19:57:18 +00005042 int ret;
5043
Paul Bakker7eb013f2011-10-06 12:37:39 +00005044 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005045
5046#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00005047 ssl->renego_status = SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005048 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00005049
5050 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard61860192014-11-04 13:05:42 +01005051 memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
5052 memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005053#endif
5054 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00005055
Paul Bakker7eb013f2011-10-06 12:37:39 +00005056 ssl->in_offt = NULL;
5057
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005058 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005059 ssl->in_msgtype = 0;
5060 ssl->in_msglen = 0;
5061 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005062#if defined(POLARSSL_SSL_PROTO_DTLS)
5063 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005064 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005065#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005066#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
5067 ssl_dtls_replay_reset( ssl );
5068#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005069
5070 ssl->in_hslen = 0;
5071 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005072 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005073
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005074 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005075 ssl->out_msgtype = 0;
5076 ssl->out_msglen = 0;
5077 ssl->out_left = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005078#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005079 if( ssl->split_done != SSL_CBC_RECORD_SPLITTING_DISABLED )
5080 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005081#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005082
Paul Bakker48916f92012-09-16 19:57:18 +00005083 ssl->transform_in = NULL;
5084 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005085
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005086 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
5087 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00005088
5089#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02005090 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005091 {
5092 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005093 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005094 {
5095 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
5096 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
5097 }
Paul Bakker05ef8352012-05-08 09:17:57 +00005098 }
5099#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00005100
Paul Bakker48916f92012-09-16 19:57:18 +00005101 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005102 {
Paul Bakker48916f92012-09-16 19:57:18 +00005103 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005104 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005105 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00005106 }
Paul Bakker48916f92012-09-16 19:57:18 +00005107
Paul Bakkerc0463502013-02-14 11:19:38 +01005108 if( ssl->session )
5109 {
5110 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005111 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005112 ssl->session = NULL;
5113 }
5114
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005115#if defined(POLARSSL_SSL_ALPN)
5116 ssl->alpn_chosen = NULL;
5117#endif
5118
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02005119#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005120 polarssl_free( ssl->cli_id );
5121 ssl->cli_id = NULL;
5122 ssl->cli_id_len = 0;
5123#endif
5124
Paul Bakker48916f92012-09-16 19:57:18 +00005125 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5126 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005127
5128 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00005129}
5130
Paul Bakkera503a632013-08-14 13:48:06 +02005131#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005132static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
5133{
5134 aes_free( &tkeys->enc );
5135 aes_free( &tkeys->dec );
5136
5137 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
5138}
5139
Paul Bakker7eb013f2011-10-06 12:37:39 +00005140/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005141 * Allocate and initialize ticket keys
5142 */
5143static int ssl_ticket_keys_init( ssl_context *ssl )
5144{
5145 int ret;
5146 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005147 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005148
5149 if( ssl->ticket_keys != NULL )
5150 return( 0 );
5151
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005152 tkeys = polarssl_malloc( sizeof(ssl_ticket_keys) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005153 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005154 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5155
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005156 aes_init( &tkeys->enc );
5157 aes_init( &tkeys->dec );
5158
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005159 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01005160 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005161 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005162 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005163 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005164 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005165
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02005166 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
5167 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
5168 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
5169 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005170 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005171 polarssl_free( tkeys );
5172 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02005173 }
5174
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005175 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01005176 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005177 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005178 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005179 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005180 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005181
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005182 ssl->ticket_keys = tkeys;
5183
5184 return( 0 );
5185}
Paul Bakkera503a632013-08-14 13:48:06 +02005186#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005187
5188/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005189 * SSL set accessors
5190 */
5191void ssl_set_endpoint( ssl_context *ssl, int endpoint )
5192{
5193 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005194
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005195#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
5196 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005197 if( endpoint == SSL_IS_CLIENT )
5198 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02005199#endif
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01005200
5201#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
5202 if( endpoint == SSL_IS_SERVER )
5203 ssl->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
5204#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005205}
5206
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005207int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005208{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005209#if defined(POLARSSL_SSL_PROTO_DTLS)
5210 if( transport == SSL_TRANSPORT_DATAGRAM )
5211 {
5212 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005213
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005214 ssl->out_hdr = ssl->out_buf;
5215 ssl->out_ctr = ssl->out_buf + 3;
5216 ssl->out_len = ssl->out_buf + 11;
5217 ssl->out_iv = ssl->out_buf + 13;
5218 ssl->out_msg = ssl->out_buf + 13;
5219
5220 ssl->in_hdr = ssl->in_buf;
5221 ssl->in_ctr = ssl->in_buf + 3;
5222 ssl->in_len = ssl->in_buf + 11;
5223 ssl->in_iv = ssl->in_buf + 13;
5224 ssl->in_msg = ssl->in_buf + 13;
5225
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005226 /* DTLS starts with TLS1.1 */
5227 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
5228 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005229
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005230 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
5231 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
5232
5233 return( 0 );
5234 }
5235#endif
5236
5237 if( transport == SSL_TRANSPORT_STREAM )
5238 {
5239 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005240
5241 ssl->out_ctr = ssl->out_buf;
5242 ssl->out_hdr = ssl->out_buf + 8;
5243 ssl->out_len = ssl->out_buf + 11;
5244 ssl->out_iv = ssl->out_buf + 13;
5245 ssl->out_msg = ssl->out_buf + 13;
5246
5247 ssl->in_ctr = ssl->in_buf;
5248 ssl->in_hdr = ssl->in_buf + 8;
5249 ssl->in_len = ssl->in_buf + 11;
5250 ssl->in_iv = ssl->in_buf + 13;
5251 ssl->in_msg = ssl->in_buf + 13;
5252
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005253 return( 0 );
5254 }
5255
5256 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005257}
5258
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005259#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
5260void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
5261{
5262 ssl->anti_replay = mode;
5263}
5264#endif
5265
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005266#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
5267void ssl_set_dtls_badmac_limit( ssl_context *ssl, unsigned limit )
5268{
5269 ssl->badmac_limit = limit;
5270}
5271#endif
5272
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02005273#if defined(POLARSSL_SSL_PROTO_DTLS)
5274void ssl_set_handshake_timeout( ssl_context *ssl, uint32_t min, uint32_t max )
5275{
5276 ssl->hs_timeout_min = min;
5277 ssl->hs_timeout_max = max;
5278}
5279#endif
5280
Paul Bakker5121ce52009-01-03 21:22:43 +00005281void ssl_set_authmode( ssl_context *ssl, int authmode )
5282{
5283 ssl->authmode = authmode;
5284}
5285
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005286#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005287void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005288 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005289 void *p_vrfy )
5290{
5291 ssl->f_vrfy = f_vrfy;
5292 ssl->p_vrfy = p_vrfy;
5293}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005294#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005295
Paul Bakker5121ce52009-01-03 21:22:43 +00005296void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00005297 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00005298 void *p_rng )
5299{
5300 ssl->f_rng = f_rng;
5301 ssl->p_rng = p_rng;
5302}
5303
5304void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00005305 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00005306 void *p_dbg )
5307{
5308 ssl->f_dbg = f_dbg;
5309 ssl->p_dbg = p_dbg;
5310}
5311
Manuel Pégourié-Gonnard9a65e802015-03-25 17:19:12 +01005312#if ! defined(POLARSSL_DEPRECATED_REMOVED)
Paul Bakker5121ce52009-01-03 21:22:43 +00005313void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00005314 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00005315 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00005316{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02005317 if( p_recv != p_send )
5318 {
5319 ssl->f_recv = NULL;
5320 ssl->f_send = NULL;
5321 ssl->p_bio = NULL;
5322 return;
5323 }
5324
Paul Bakker5121ce52009-01-03 21:22:43 +00005325 ssl->f_recv = f_recv;
5326 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02005327 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00005328}
Manuel Pégourié-Gonnard9a65e802015-03-25 17:19:12 +01005329#endif /* POLARSSL_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005330
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005331void ssl_set_bio_timeout( ssl_context *ssl,
5332 void *p_bio,
5333 int (*f_send)(void *, const unsigned char *, size_t),
5334 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +02005335 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t),
5336 uint32_t timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005337{
5338 ssl->p_bio = p_bio;
5339 ssl->f_send = f_send;
5340 ssl->f_recv = f_recv;
5341 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard27074302014-10-01 17:35:50 +02005342 ssl->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005343}
5344
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005345#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00005346void ssl_set_session_cache( ssl_context *ssl,
5347 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
5348 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00005349{
Paul Bakker0a597072012-09-25 21:55:46 +00005350 ssl->f_get_cache = f_get_cache;
5351 ssl->p_get_cache = p_get_cache;
5352 ssl->f_set_cache = f_set_cache;
5353 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00005354}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005355#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005356
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005357#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005358int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00005359{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005360 int ret;
5361
5362 if( ssl == NULL ||
5363 session == NULL ||
5364 ssl->session_negotiate == NULL ||
5365 ssl->endpoint != SSL_IS_CLIENT )
5366 {
5367 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5368 }
5369
5370 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5371 return( ret );
5372
Paul Bakker0a597072012-09-25 21:55:46 +00005373 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005374
5375 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005376}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005377#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005378
Paul Bakkerb68cad62012-08-23 08:34:18 +00005379void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005380{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005381 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
5382 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
5383 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
5384 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
5385}
5386
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005387void ssl_set_ciphersuites_for_version( ssl_context *ssl,
5388 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005389 int major, int minor )
5390{
5391 if( major != SSL_MAJOR_VERSION_3 )
5392 return;
5393
5394 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
5395 return;
5396
5397 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005398}
5399
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005400#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005401/* Add a new (empty) key_cert entry an return a pointer to it */
5402static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
5403{
5404 ssl_key_cert *key_cert, *last;
5405
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005406 key_cert = polarssl_malloc( sizeof(ssl_key_cert) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005407 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005408 return( NULL );
5409
5410 memset( key_cert, 0, sizeof( ssl_key_cert ) );
5411
5412 /* Append the new key_cert to the (possibly empty) current list */
5413 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005414 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005415 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01005416 if( ssl->handshake != NULL )
5417 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01005418 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005419 else
5420 {
5421 last = ssl->key_cert;
5422 while( last->next != NULL )
5423 last = last->next;
5424 last->next = key_cert;
5425 }
5426
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005427 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005428}
5429
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005430void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00005431 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00005432{
5433 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00005434 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005435 ssl->peer_cn = peer_cn;
5436}
5437
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005438int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005439 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00005440{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005441 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
5442
5443 if( key_cert == NULL )
5444 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5445
5446 key_cert->cert = own_cert;
5447 key_cert->key = pk_key;
5448
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00005449 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005450}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005451#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005452
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005453#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005454int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5455 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005456{
Paul Bakker6db455e2013-09-18 17:29:31 +02005457 if( psk == NULL || psk_identity == NULL )
5458 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5459
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005460 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005461 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5462
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00005463 if( ssl->psk != NULL || ssl->psk_identity != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02005464 {
5465 polarssl_free( ssl->psk );
5466 polarssl_free( ssl->psk_identity );
5467 }
5468
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00005469 if( ( ssl->psk = polarssl_malloc( psk_len ) ) == NULL ||
5470 ( ssl->psk_identity = polarssl_malloc( psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05005471 {
5472 polarssl_free( ssl->psk );
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00005473 ssl->psk = NULL;
Mansour Moufidf81088b2015-02-17 13:10:21 -05005474 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5475 }
Paul Bakker6db455e2013-09-18 17:29:31 +02005476
Paul Bakker6db455e2013-09-18 17:29:31 +02005477 ssl->psk_len = psk_len;
5478 ssl->psk_identity_len = psk_identity_len;
5479
Paul Bakker6db455e2013-09-18 17:29:31 +02005480 memcpy( ssl->psk, psk, ssl->psk_len );
5481 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
5482
5483 return( 0 );
5484}
5485
5486void ssl_set_psk_cb( ssl_context *ssl,
5487 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5488 size_t),
5489 void *p_psk )
5490{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005491 ssl->f_psk = f_psk;
5492 ssl->p_psk = p_psk;
Paul Bakker43b7e352011-01-18 15:27:19 +00005493}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005494#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005495
Paul Bakker48916f92012-09-16 19:57:18 +00005496#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005497int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
5498{
5499 int ret;
5500
Paul Bakker48916f92012-09-16 19:57:18 +00005501 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005502 {
5503 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5504 return( ret );
5505 }
5506
Paul Bakker48916f92012-09-16 19:57:18 +00005507 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005508 {
5509 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5510 return( ret );
5511 }
5512
5513 return( 0 );
5514}
5515
Paul Bakker1b57b062011-01-06 15:48:19 +00005516int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5517{
5518 int ret;
5519
Paul Bakker66d5d072014-06-17 16:39:18 +02005520 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005521 {
5522 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5523 return( ret );
5524 }
5525
Paul Bakker66d5d072014-06-17 16:39:18 +02005526 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005527 {
5528 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5529 return( ret );
5530 }
5531
5532 return( 0 );
5533}
Paul Bakker48916f92012-09-16 19:57:18 +00005534#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005535
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005536#if defined(POLARSSL_SSL_SET_CURVES)
5537/*
5538 * Set the allowed elliptic curves
5539 */
5540void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5541{
5542 ssl->curve_list = curve_list;
5543}
5544#endif
5545
Paul Bakker0be444a2013-08-27 21:55:01 +02005546#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005547int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005548{
5549 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005550 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005551
5552 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005553
5554 if( ssl->hostname_len + 1 == 0 )
5555 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5556
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005557 ssl->hostname = polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005558
Paul Bakkerb15b8512012-01-13 13:44:06 +00005559 if( ssl->hostname == NULL )
5560 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5561
Paul Bakker3c2122f2013-06-24 19:03:14 +02005562 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005563 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005564
Paul Bakker40ea7de2009-05-03 10:18:48 +00005565 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005566
5567 return( 0 );
5568}
5569
Paul Bakker5701cdc2012-09-27 21:49:42 +00005570void ssl_set_sni( ssl_context *ssl,
5571 int (*f_sni)(void *, ssl_context *,
5572 const unsigned char *, size_t),
5573 void *p_sni )
5574{
5575 ssl->f_sni = f_sni;
5576 ssl->p_sni = p_sni;
5577}
Paul Bakker0be444a2013-08-27 21:55:01 +02005578#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005579
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005580#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005581int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005582{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005583 size_t cur_len, tot_len;
5584 const char **p;
5585
5586 /*
5587 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5588 * truncated". Check lengths now rather than later.
5589 */
5590 tot_len = 0;
5591 for( p = protos; *p != NULL; p++ )
5592 {
5593 cur_len = strlen( *p );
5594 tot_len += cur_len;
5595
5596 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5597 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5598 }
5599
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005600 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005601
5602 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005603}
5604
5605const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5606{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005607 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005608}
5609#endif /* POLARSSL_SSL_ALPN */
5610
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005611static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005612{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005613 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005614 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005615 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005616 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005617 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005618
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005619#if defined(POLARSSL_SSL_PROTO_DTLS)
5620 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5621 minor < SSL_MINOR_VERSION_2 )
5622 {
5623 return( -1 );
5624 }
5625#else
5626 ((void) ssl);
5627#endif
5628
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005629 return( 0 );
5630}
5631
5632int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5633{
5634 if( ssl_check_version( ssl, major, minor ) != 0 )
5635 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5636
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005637 ssl->max_major_ver = major;
5638 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005639
5640 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005641}
5642
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005643int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005644{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005645 if( ssl_check_version( ssl, major, minor ) != 0 )
5646 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005647
5648 ssl->min_major_ver = major;
5649 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005650
5651 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005652}
5653
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02005654#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
5655void ssl_set_fallback( ssl_context *ssl, char fallback )
5656{
5657 ssl->fallback = fallback;
5658}
5659#endif
5660
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01005661#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
5662void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
5663{
5664 ssl->encrypt_then_mac = etm;
5665}
5666#endif
5667
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02005668#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
5669void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
5670{
5671 ssl->extended_ms = ems;
5672}
5673#endif
5674
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01005675void ssl_set_arc4_support( ssl_context *ssl, char arc4 )
5676{
5677 ssl->arc4_disabled = arc4;
5678}
5679
Paul Bakker05decb22013-08-15 13:33:48 +02005680#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005681int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5682{
Paul Bakker77e257e2013-12-16 15:29:52 +01005683 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005684 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005685 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005686 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005687 }
5688
5689 ssl->mfl_code = mfl_code;
5690
5691 return( 0 );
5692}
Paul Bakker05decb22013-08-15 13:33:48 +02005693#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005694
Paul Bakker1f2bc622013-08-15 13:45:55 +02005695#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005696int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005697{
Paul Bakker8c1ede62013-07-19 14:14:37 +02005698 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005699
5700 return( 0 );
5701}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005702#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005703
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005704#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
5705void ssl_set_cbc_record_splitting( ssl_context *ssl, char split )
5706{
5707 ssl->split_done = split;
5708}
5709#endif
5710
Paul Bakker48916f92012-09-16 19:57:18 +00005711void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5712{
5713 ssl->allow_legacy_renegotiation = allow_legacy;
5714}
5715
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005716#if defined(POLARSSL_SSL_RENEGOTIATION)
5717void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5718{
5719 ssl->disable_renegotiation = renegotiation;
5720}
5721
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005722void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5723{
5724 ssl->renego_max_records = max_records;
5725}
5726
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01005727void ssl_set_renegotiation_period( ssl_context *ssl,
5728 const unsigned char period[8] )
5729{
5730 memcpy( ssl->renego_period, period, 8 );
5731}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005732#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00005733
Paul Bakkera503a632013-08-14 13:48:06 +02005734#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005735int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5736{
5737 ssl->session_tickets = use_tickets;
5738
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005739#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005740 if( ssl->endpoint == SSL_IS_CLIENT )
5741 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005742#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005743
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01005744 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
5745 return( 0 );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005746
5747 if( ssl->f_rng == NULL )
5748 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5749
5750 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005751}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005752
5753void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5754{
5755 ssl->ticket_lifetime = lifetime;
5756}
Paul Bakkera503a632013-08-14 13:48:06 +02005757#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005758
Paul Bakker5121ce52009-01-03 21:22:43 +00005759/*
5760 * SSL get accessors
5761 */
5762size_t ssl_get_bytes_avail( const ssl_context *ssl )
5763{
5764 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5765}
5766
Paul Bakkerff60ee62010-03-16 21:09:09 +00005767int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005768{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00005769 if( ssl->session != NULL )
5770 return( ssl->session->verify_result );
5771
5772 if( ssl->session_negotiate != NULL )
5773 return( ssl->session_negotiate->verify_result );
5774
5775 return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005776}
5777
Paul Bakkere3166ce2011-01-27 17:40:50 +00005778const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005779{
Paul Bakker926c8e42013-03-06 10:23:34 +01005780 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005781 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005782
Paul Bakkere3166ce2011-01-27 17:40:50 +00005783 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005784}
5785
Paul Bakker43ca69c2011-01-15 17:35:19 +00005786const char *ssl_get_version( const ssl_context *ssl )
5787{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005788#if defined(POLARSSL_SSL_PROTO_DTLS)
5789 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5790 {
5791 switch( ssl->minor_ver )
5792 {
5793 case SSL_MINOR_VERSION_2:
5794 return( "DTLSv1.0" );
5795
5796 case SSL_MINOR_VERSION_3:
5797 return( "DTLSv1.2" );
5798
5799 default:
5800 return( "unknown (DTLS)" );
5801 }
5802 }
5803#endif
5804
Paul Bakker43ca69c2011-01-15 17:35:19 +00005805 switch( ssl->minor_ver )
5806 {
5807 case SSL_MINOR_VERSION_0:
5808 return( "SSLv3.0" );
5809
5810 case SSL_MINOR_VERSION_1:
5811 return( "TLSv1.0" );
5812
5813 case SSL_MINOR_VERSION_2:
5814 return( "TLSv1.1" );
5815
Paul Bakker1ef83d62012-04-11 12:09:53 +00005816 case SSL_MINOR_VERSION_3:
5817 return( "TLSv1.2" );
5818
Paul Bakker43ca69c2011-01-15 17:35:19 +00005819 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005820 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005821 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005822}
5823
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005824int ssl_get_record_expansion( const ssl_context *ssl )
5825{
5826 int transform_expansion;
5827 const ssl_transform *transform = ssl->transform_out;
5828
5829#if defined(POLARSSL_ZLIB_SUPPORT)
5830 if( ssl->session_out->compression != SSL_COMPRESS_NULL )
5831 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
5832#endif
5833
5834 if( transform == NULL )
5835 return( ssl_hdr_len( ssl ) );
5836
5837 switch( cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
5838 {
5839 case POLARSSL_MODE_GCM:
5840 case POLARSSL_MODE_CCM:
5841 case POLARSSL_MODE_STREAM:
5842 transform_expansion = transform->minlen;
5843 break;
5844
5845 case POLARSSL_MODE_CBC:
5846 transform_expansion = transform->maclen
5847 + cipher_get_block_size( &transform->cipher_ctx_enc );
5848 break;
5849
5850 default:
5851 SSL_DEBUG_MSG( 0, ( "should never happen" ) );
5852 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
5853 }
5854
5855 return( ssl_hdr_len( ssl ) + transform_expansion );
5856}
5857
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005858#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005859const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005860{
5861 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005862 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005863
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005864 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005865}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005866#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005867
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005868#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005869int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5870{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005871 if( ssl == NULL ||
5872 dst == NULL ||
5873 ssl->session == NULL ||
5874 ssl->endpoint != SSL_IS_CLIENT )
5875 {
5876 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5877 }
5878
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005879 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005880}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005881#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005882
Paul Bakker5121ce52009-01-03 21:22:43 +00005883/*
Paul Bakker1961b702013-01-25 14:49:24 +01005884 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005885 */
Paul Bakker1961b702013-01-25 14:49:24 +01005886int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005887{
Paul Bakker40e46942009-01-03 21:51:57 +00005888 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005889
Paul Bakker40e46942009-01-03 21:51:57 +00005890#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005891 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005892 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005893#endif
Paul Bakker40e46942009-01-03 21:51:57 +00005894#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005895 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005896 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005897#endif
5898
Paul Bakker1961b702013-01-25 14:49:24 +01005899 return( ret );
5900}
5901
5902/*
5903 * Perform the SSL handshake
5904 */
5905int ssl_handshake( ssl_context *ssl )
5906{
5907 int ret = 0;
5908
5909 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5910
5911 while( ssl->state != SSL_HANDSHAKE_OVER )
5912 {
5913 ret = ssl_handshake_step( ssl );
5914
5915 if( ret != 0 )
5916 break;
5917 }
5918
Paul Bakker5121ce52009-01-03 21:22:43 +00005919 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5920
5921 return( ret );
5922}
5923
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005924#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005925#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005926/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005927 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005928 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005929static int ssl_write_hello_request( ssl_context *ssl )
5930{
5931 int ret;
5932
5933 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5934
5935 ssl->out_msglen = 4;
5936 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5937 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5938
5939 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5940 {
5941 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5942 return( ret );
5943 }
5944
5945 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5946
5947 return( 0 );
5948}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005949#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005950
5951/*
5952 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005953 * - any side: calling ssl_renegotiate(),
5954 * - client: receiving a HelloRequest during ssl_read(),
5955 * - server: receiving any handshake message on server during ssl_read() after
5956 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005957 * If the handshake doesn't complete due to waiting for I/O, it will continue
5958 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005959 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005960static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005961{
5962 int ret;
5963
5964 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5965
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005966 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5967 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005968
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005969 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5970 * the ServerHello will have message_seq = 1" */
5971#if defined(POLARSSL_SSL_PROTO_DTLS)
5972 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00005973 ssl->renego_status == SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005974 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005975 if( ssl->endpoint == SSL_IS_SERVER )
5976 ssl->handshake->out_msg_seq = 1;
5977 else
5978 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005979 }
5980#endif
5981
Paul Bakker48916f92012-09-16 19:57:18 +00005982 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00005983 ssl->renego_status = SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00005984
Paul Bakker48916f92012-09-16 19:57:18 +00005985 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5986 {
5987 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5988 return( ret );
5989 }
5990
5991 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5992
5993 return( 0 );
5994}
5995
5996/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005997 * Renegotiate current connection on client,
5998 * or request renegotiation on server
5999 */
6000int ssl_renegotiate( ssl_context *ssl )
6001{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006002 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006003
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006004#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006005 /* On server, just send the request */
6006 if( ssl->endpoint == SSL_IS_SERVER )
6007 {
6008 if( ssl->state != SSL_HANDSHAKE_OVER )
6009 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
6010
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00006011 ssl->renego_status = SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02006012
6013 /* Did we already try/start sending HelloRequest? */
6014 if( ssl->out_left != 0 )
6015 return( ssl_flush_output( ssl ) );
6016
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006017 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006018 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006019#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006020
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006021#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006022 /*
6023 * On client, either start the renegotiation process or,
6024 * if already in progress, continue the handshake
6025 */
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00006026 if( ssl->renego_status != SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006027 {
6028 if( ssl->state != SSL_HANDSHAKE_OVER )
6029 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
6030
6031 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
6032 {
6033 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
6034 return( ret );
6035 }
6036 }
6037 else
6038 {
6039 if( ( ret = ssl_handshake( ssl ) ) != 0 )
6040 {
6041 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6042 return( ret );
6043 }
6044 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006045#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006046
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006047 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006048}
6049
6050/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006051 * Check record counters and renegotiate if they're above the limit.
6052 */
6053static int ssl_check_ctr_renegotiate( ssl_context *ssl )
6054{
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006055 if( ssl->state != SSL_HANDSHAKE_OVER ||
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00006056 ssl->renego_status == SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006057 ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED )
6058 {
6059 return( 0 );
6060 }
6061
6062 // TODO: adapt for DTLS
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006063 if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 &&
6064 memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006065 {
6066 return( 0 );
6067 }
6068
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006069 SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006070 return( ssl_renegotiate( ssl ) );
6071}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006072#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006073
6074/*
6075 * Receive application data decrypted from the SSL layer
6076 */
Paul Bakker23986e52011-04-24 08:57:21 +00006077int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006078{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006079 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00006080 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00006081
6082 SSL_DEBUG_MSG( 2, ( "=> read" ) );
6083
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006084#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006085 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006086 {
6087 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
6088 return( ret );
6089
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006090 if( ssl->handshake != NULL &&
6091 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
6092 {
6093 if( ( ret = ssl_resend( ssl ) ) != 0 )
6094 return( ret );
6095 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006096 }
6097#endif
6098
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006099#if defined(POLARSSL_SSL_RENEGOTIATION)
6100 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
6101 {
6102 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
6103 return( ret );
6104 }
6105#endif
6106
Paul Bakker5121ce52009-01-03 21:22:43 +00006107 if( ssl->state != SSL_HANDSHAKE_OVER )
6108 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006109 ret = ssl_handshake( ssl );
6110 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
6111 {
6112 record_read = 1;
6113 }
6114 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006115 {
6116 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6117 return( ret );
6118 }
6119 }
6120
6121 if( ssl->in_offt == NULL )
6122 {
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02006123#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006124 /* Start timer if not already running */
6125 if( ssl->time_limit == 0 )
6126 ssl_set_timer( ssl, ssl->read_timeout );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02006127#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006128
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006129 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00006130 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006131 if( ( ret = ssl_read_record( ssl ) ) != 0 )
6132 {
6133 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
6134 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00006135
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006136 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
6137 return( ret );
6138 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006139 }
6140
6141 if( ssl->in_msglen == 0 &&
6142 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
6143 {
6144 /*
6145 * OpenSSL sends empty messages to randomize the IV
6146 */
6147 if( ( ret = ssl_read_record( ssl ) ) != 0 )
6148 {
Paul Bakker831a7552011-05-18 13:32:51 +00006149 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
6150 return( 0 );
6151
Paul Bakker5121ce52009-01-03 21:22:43 +00006152 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
6153 return( ret );
6154 }
6155 }
6156
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006157#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006158 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
6159 {
6160 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
6161
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006162#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006163 if( ssl->endpoint == SSL_IS_CLIENT &&
6164 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00006165 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006166 {
6167 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006168
6169 /* With DTLS, drop the packet (probably from last handshake) */
6170#if defined(POLARSSL_SSL_PROTO_DTLS)
6171 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6172 return( POLARSSL_ERR_NET_WANT_READ );
6173#endif
6174 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6175 }
6176
6177 if( ssl->endpoint == SSL_IS_SERVER &&
6178 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
6179 {
6180 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
6181
6182 /* With DTLS, drop the packet (probably from last handshake) */
6183#if defined(POLARSSL_SSL_PROTO_DTLS)
6184 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6185 return( POLARSSL_ERR_NET_WANT_READ );
6186#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006187 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6188 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006189#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006190
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006191 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
6192 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006193 ssl->allow_legacy_renegotiation ==
6194 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006195 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006196 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006197
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006198#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006199 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006200 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006201 /*
6202 * SSLv3 does not have a "no_renegotiation" alert
6203 */
6204 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
6205 return( ret );
6206 }
6207 else
Paul Bakker9af723c2014-05-01 13:03:14 +02006208#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006209#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
6210 defined(POLARSSL_SSL_PROTO_TLS1_2)
6211 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006212 {
6213 if( ( ret = ssl_send_alert_message( ssl,
6214 SSL_ALERT_LEVEL_WARNING,
6215 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
6216 {
6217 return( ret );
6218 }
Paul Bakker48916f92012-09-16 19:57:18 +00006219 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006220 else
Paul Bakker9af723c2014-05-01 13:03:14 +02006221#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
6222 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02006223 {
6224 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006225 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02006226 }
Paul Bakker48916f92012-09-16 19:57:18 +00006227 }
6228 else
6229 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006230 /* DTLS clients need to know renego is server-initiated */
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02006231#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006232 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
6233 ssl->endpoint == SSL_IS_CLIENT )
6234 {
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00006235 ssl->renego_status = SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006236 }
6237#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006238 ret = ssl_start_renegotiation( ssl );
6239 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
6240 {
6241 record_read = 1;
6242 }
6243 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006244 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006245 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00006246 return( ret );
6247 }
Paul Bakker48916f92012-09-16 19:57:18 +00006248 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006249
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006250 /* If a non-handshake record was read during renego, fallthrough,
6251 * else tell the user they should call ssl_read() again */
6252 if( ! record_read )
6253 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00006254 }
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00006255 else if( ssl->renego_status == SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006256 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006257
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006258 if( ssl->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006259 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006260 if( ++ssl->renego_records_seen > ssl->renego_max_records )
6261 {
6262 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
6263 "but not honored by client" ) );
6264 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6265 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006266 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006267 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006268#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006269
6270 /* Fatal and closure alerts handled by ssl_read_record() */
6271 if( ssl->in_msgtype == SSL_MSG_ALERT )
6272 {
6273 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
6274 return( POLARSSL_ERR_NET_WANT_READ );
6275 }
6276
6277 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00006278 {
6279 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00006280 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006281 }
6282
6283 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006284
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02006285#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02006286 /* We're going to return something now, cancel timer,
6287 * except if handshake (renegotiation) is in progress */
6288 if( ssl->state == SSL_HANDSHAKE_OVER )
6289 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006290
6291 /* If we requested renego but received AppData, resend HelloRequest.
6292 * Do it now, after setting in_offt, to avoid taking this branch
6293 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00006294#if defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006295 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00006296 ssl->renego_status == SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006297 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006298 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006299 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006300 SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006301 return( ret );
6302 }
6303 }
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00006304#endif /* POLARSSL_SSL_SRV_C && POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02006305#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006306 }
6307
6308 n = ( len < ssl->in_msglen )
6309 ? len : ssl->in_msglen;
6310
6311 memcpy( buf, ssl->in_offt, n );
6312 ssl->in_msglen -= n;
6313
6314 if( ssl->in_msglen == 0 )
6315 /* all bytes consumed */
6316 ssl->in_offt = NULL;
6317 else
6318 /* more data available */
6319 ssl->in_offt += n;
6320
6321 SSL_DEBUG_MSG( 2, ( "<= read" ) );
6322
Paul Bakker23986e52011-04-24 08:57:21 +00006323 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006324}
6325
6326/*
6327 * Send application data to be encrypted by the SSL layer
6328 */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006329#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
6330static int ssl_write_real( ssl_context *ssl, const unsigned char *buf, size_t len )
6331#else
Paul Bakker23986e52011-04-24 08:57:21 +00006332int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006333#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006334{
Paul Bakker23986e52011-04-24 08:57:21 +00006335 int ret;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006336#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
6337 unsigned int max_len;
6338#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006339
6340 SSL_DEBUG_MSG( 2, ( "=> write" ) );
6341
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006342#if defined(POLARSSL_SSL_RENEGOTIATION)
6343 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
6344 {
6345 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
6346 return( ret );
6347 }
6348#endif
6349
Paul Bakker5121ce52009-01-03 21:22:43 +00006350 if( ssl->state != SSL_HANDSHAKE_OVER )
6351 {
6352 if( ( ret = ssl_handshake( ssl ) ) != 0 )
6353 {
6354 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6355 return( ret );
6356 }
6357 }
6358
Paul Bakker05decb22013-08-15 13:33:48 +02006359#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02006360 /*
6361 * Assume mfl_code is correct since it was checked when set
6362 */
6363 max_len = mfl_code_to_length[ssl->mfl_code];
6364
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006365 /*
Paul Bakker05decb22013-08-15 13:33:48 +02006366 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006367 */
6368 if( ssl->session_out != NULL &&
6369 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6370 {
6371 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6372 }
6373
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006374 if( len > max_len )
6375 {
6376#if defined(POLARSSL_SSL_PROTO_DTLS)
6377 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6378 {
6379 SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
6380 "maximum fragment length: %d > %d",
6381 len, max_len ) );
6382 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
6383 }
6384 else
6385#endif
6386 len = max_len;
6387 }
6388#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker887bd502011-06-08 13:10:54 +00006389
Paul Bakker5121ce52009-01-03 21:22:43 +00006390 if( ssl->out_left != 0 )
6391 {
6392 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
6393 {
6394 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
6395 return( ret );
6396 }
6397 }
Paul Bakker887bd502011-06-08 13:10:54 +00006398 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00006399 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006400 ssl->out_msglen = len;
Paul Bakker887bd502011-06-08 13:10:54 +00006401 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006402 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00006403
6404 if( ( ret = ssl_write_record( ssl ) ) != 0 )
6405 {
6406 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
6407 return( ret );
6408 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006409 }
6410
6411 SSL_DEBUG_MSG( 2, ( "<= write" ) );
6412
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006413 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006414}
6415
6416/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006417 * Write application data, doing 1/n-1 splitting if necessary.
6418 *
6419 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006420 * then the caller will call us again with the same arguments, so
6421 * remember wether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006422 */
6423#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
6424int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
6425{
6426 int ret;
6427
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006428 if( ssl->split_done == SSL_CBC_RECORD_SPLITTING_DISABLED ||
6429 len <= 1 ||
6430 ssl->minor_ver > SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006431 cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
6432 != POLARSSL_MODE_CBC )
6433 {
6434 return( ssl_write_real( ssl, buf, len ) );
6435 }
6436
6437 if( ssl->split_done == 0 )
6438 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006439 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006440 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006441 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006442 }
6443
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006444 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
6445 return( ret );
6446 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006447
6448 return( ret + 1 );
6449}
6450#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */
6451
6452/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006453 * Notify the peer that the connection is being closed
6454 */
6455int ssl_close_notify( ssl_context *ssl )
6456{
6457 int ret;
6458
6459 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
6460
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006461 if( ssl->out_left != 0 )
6462 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006463
6464 if( ssl->state == SSL_HANDSHAKE_OVER )
6465 {
Paul Bakker48916f92012-09-16 19:57:18 +00006466 if( ( ret = ssl_send_alert_message( ssl,
6467 SSL_ALERT_LEVEL_WARNING,
6468 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006469 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006470 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006471 return( ret );
6472 }
6473 }
6474
6475 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
6476
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006477 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006478}
6479
Paul Bakker48916f92012-09-16 19:57:18 +00006480void ssl_transform_free( ssl_transform *transform )
6481{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006482 if( transform == NULL )
6483 return;
6484
Paul Bakker48916f92012-09-16 19:57:18 +00006485#if defined(POLARSSL_ZLIB_SUPPORT)
6486 deflateEnd( &transform->ctx_deflate );
6487 inflateEnd( &transform->ctx_inflate );
6488#endif
6489
Paul Bakker84bbeb52014-07-01 14:53:22 +02006490 cipher_free( &transform->cipher_ctx_enc );
6491 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02006492
Paul Bakker84bbeb52014-07-01 14:53:22 +02006493 md_free( &transform->md_ctx_enc );
6494 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02006495
Paul Bakker34617722014-06-13 17:20:13 +02006496 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006497}
6498
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006499#if defined(POLARSSL_X509_CRT_PARSE_C)
6500static void ssl_key_cert_free( ssl_key_cert *key_cert )
6501{
6502 ssl_key_cert *cur = key_cert, *next;
6503
6504 while( cur != NULL )
6505 {
6506 next = cur->next;
6507
6508 if( cur->key_own_alloc )
6509 {
6510 pk_free( cur->key );
6511 polarssl_free( cur->key );
6512 }
6513 polarssl_free( cur );
6514
6515 cur = next;
6516 }
6517}
6518#endif /* POLARSSL_X509_CRT_PARSE_C */
6519
Paul Bakker48916f92012-09-16 19:57:18 +00006520void ssl_handshake_free( ssl_handshake_params *handshake )
6521{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006522 if( handshake == NULL )
6523 return;
6524
Paul Bakker48916f92012-09-16 19:57:18 +00006525#if defined(POLARSSL_DHM_C)
6526 dhm_free( &handshake->dhm_ctx );
6527#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006528#if defined(POLARSSL_ECDH_C)
6529 ecdh_free( &handshake->ecdh_ctx );
6530#endif
6531
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006532#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02006533 /* explicit void pointer cast for buggy MS compiler */
6534 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006535#endif
6536
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006537#if defined(POLARSSL_X509_CRT_PARSE_C) && \
6538 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
6539 /*
6540 * Free only the linked list wrapper, not the keys themselves
6541 * since the belong to the SNI callback
6542 */
6543 if( handshake->sni_key_cert != NULL )
6544 {
6545 ssl_key_cert *cur = handshake->sni_key_cert, *next;
6546
6547 while( cur != NULL )
6548 {
6549 next = cur->next;
6550 polarssl_free( cur );
6551 cur = next;
6552 }
6553 }
Paul Bakker9af723c2014-05-01 13:03:14 +02006554#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006555
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006556#if defined(POLARSSL_SSL_PROTO_DTLS)
6557 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02006558 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02006559 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006560#endif
6561
Paul Bakker34617722014-06-13 17:20:13 +02006562 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006563}
6564
6565void ssl_session_free( ssl_session *session )
6566{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006567 if( session == NULL )
6568 return;
6569
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006570#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00006571 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00006572 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006573 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02006574 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00006575 }
Paul Bakkered27a042013-04-18 22:46:23 +02006576#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006577
Paul Bakkera503a632013-08-14 13:48:06 +02006578#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006579 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02006580#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006581
Paul Bakker34617722014-06-13 17:20:13 +02006582 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006583}
6584
Paul Bakker5121ce52009-01-03 21:22:43 +00006585/*
6586 * Free an SSL context
6587 */
6588void ssl_free( ssl_context *ssl )
6589{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006590 if( ssl == NULL )
6591 return;
6592
Paul Bakker5121ce52009-01-03 21:22:43 +00006593 SSL_DEBUG_MSG( 2, ( "=> free" ) );
6594
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006595 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006596 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006597 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
6598 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006599 }
6600
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006601 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006602 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006603 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
6604 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006605 }
6606
Paul Bakker16770332013-10-11 09:59:44 +02006607#if defined(POLARSSL_ZLIB_SUPPORT)
6608 if( ssl->compress_buf != NULL )
6609 {
Paul Bakker34617722014-06-13 17:20:13 +02006610 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02006611 polarssl_free( ssl->compress_buf );
6612 }
6613#endif
6614
Paul Bakker40e46942009-01-03 21:51:57 +00006615#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006616 mpi_free( &ssl->dhm_P );
6617 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006618#endif
6619
Paul Bakker48916f92012-09-16 19:57:18 +00006620 if( ssl->transform )
6621 {
6622 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02006623 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006624 }
6625
6626 if( ssl->handshake )
6627 {
6628 ssl_handshake_free( ssl->handshake );
6629 ssl_transform_free( ssl->transform_negotiate );
6630 ssl_session_free( ssl->session_negotiate );
6631
Paul Bakker6e339b52013-07-03 13:37:05 +02006632 polarssl_free( ssl->handshake );
6633 polarssl_free( ssl->transform_negotiate );
6634 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00006635 }
6636
Paul Bakkerc0463502013-02-14 11:19:38 +01006637 if( ssl->session )
6638 {
6639 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02006640 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006641 }
6642
Paul Bakkera503a632013-08-14 13:48:06 +02006643#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02006644 if( ssl->ticket_keys )
6645 {
6646 ssl_ticket_keys_free( ssl->ticket_keys );
6647 polarssl_free( ssl->ticket_keys );
6648 }
Paul Bakkera503a632013-08-14 13:48:06 +02006649#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006650
Paul Bakker0be444a2013-08-27 21:55:01 +02006651#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02006652 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006653 {
Paul Bakker34617722014-06-13 17:20:13 +02006654 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006655 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006656 ssl->hostname_len = 0;
6657 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006658#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006659
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006660#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006661 if( ssl->psk != NULL )
6662 {
Paul Bakker34617722014-06-13 17:20:13 +02006663 polarssl_zeroize( ssl->psk, ssl->psk_len );
6664 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006665 polarssl_free( ssl->psk );
6666 polarssl_free( ssl->psk_identity );
6667 ssl->psk_len = 0;
6668 ssl->psk_identity_len = 0;
6669 }
6670#endif
6671
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006672#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006673 ssl_key_cert_free( ssl->key_cert );
6674#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006675
Paul Bakker05ef8352012-05-08 09:17:57 +00006676#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6677 if( ssl_hw_record_finish != NULL )
6678 {
6679 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6680 ssl_hw_record_finish( ssl );
6681 }
6682#endif
6683
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006684#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006685 polarssl_free( ssl->cli_id );
6686#endif
6687
Paul Bakker5121ce52009-01-03 21:22:43 +00006688 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006689
Paul Bakker86f04f42013-02-14 11:20:09 +01006690 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006691 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006692}
6693
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006694#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006695/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006696 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006697 */
6698unsigned char ssl_sig_from_pk( pk_context *pk )
6699{
6700#if defined(POLARSSL_RSA_C)
6701 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6702 return( SSL_SIG_RSA );
6703#endif
6704#if defined(POLARSSL_ECDSA_C)
6705 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6706 return( SSL_SIG_ECDSA );
6707#endif
6708 return( SSL_SIG_ANON );
6709}
6710
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006711pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6712{
6713 switch( sig )
6714 {
6715#if defined(POLARSSL_RSA_C)
6716 case SSL_SIG_RSA:
6717 return( POLARSSL_PK_RSA );
6718#endif
6719#if defined(POLARSSL_ECDSA_C)
6720 case SSL_SIG_ECDSA:
6721 return( POLARSSL_PK_ECDSA );
6722#endif
6723 default:
6724 return( POLARSSL_PK_NONE );
6725 }
6726}
Paul Bakker9af723c2014-05-01 13:03:14 +02006727#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006728
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006729/*
6730 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6731 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006732md_type_t ssl_md_alg_from_hash( unsigned char hash )
6733{
6734 switch( hash )
6735 {
6736#if defined(POLARSSL_MD5_C)
6737 case SSL_HASH_MD5:
6738 return( POLARSSL_MD_MD5 );
6739#endif
6740#if defined(POLARSSL_SHA1_C)
6741 case SSL_HASH_SHA1:
6742 return( POLARSSL_MD_SHA1 );
6743#endif
6744#if defined(POLARSSL_SHA256_C)
6745 case SSL_HASH_SHA224:
6746 return( POLARSSL_MD_SHA224 );
6747 case SSL_HASH_SHA256:
6748 return( POLARSSL_MD_SHA256 );
6749#endif
6750#if defined(POLARSSL_SHA512_C)
6751 case SSL_HASH_SHA384:
6752 return( POLARSSL_MD_SHA384 );
6753 case SSL_HASH_SHA512:
6754 return( POLARSSL_MD_SHA512 );
6755#endif
6756 default:
6757 return( POLARSSL_MD_NONE );
6758 }
6759}
6760
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006761#if defined(POLARSSL_SSL_SET_CURVES)
6762/*
6763 * Check is a curve proposed by the peer is in our list.
6764 * Return 1 if we're willing to use it, 0 otherwise.
6765 */
6766int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6767{
6768 const ecp_group_id *gid;
6769
6770 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6771 if( *gid == grp_id )
6772 return( 1 );
6773
6774 return( 0 );
6775}
Paul Bakker9af723c2014-05-01 13:03:14 +02006776#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006777
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006778#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006779int ssl_check_cert_usage( const x509_crt *cert,
6780 const ssl_ciphersuite_t *ciphersuite,
6781 int cert_endpoint )
6782{
6783#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6784 int usage = 0;
6785#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006786#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6787 const char *ext_oid;
6788 size_t ext_len;
6789#endif
6790
6791#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6792 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6793 ((void) cert);
6794 ((void) cert_endpoint);
6795#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006796
6797#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6798 if( cert_endpoint == SSL_IS_SERVER )
6799 {
6800 /* Server part of the key exchange */
6801 switch( ciphersuite->key_exchange )
6802 {
6803 case POLARSSL_KEY_EXCHANGE_RSA:
6804 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6805 usage = KU_KEY_ENCIPHERMENT;
6806 break;
6807
6808 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6809 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6810 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6811 usage = KU_DIGITAL_SIGNATURE;
6812 break;
6813
6814 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6815 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6816 usage = KU_KEY_AGREEMENT;
6817 break;
6818
6819 /* Don't use default: we want warnings when adding new values */
6820 case POLARSSL_KEY_EXCHANGE_NONE:
6821 case POLARSSL_KEY_EXCHANGE_PSK:
6822 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6823 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6824 usage = 0;
6825 }
6826 }
6827 else
6828 {
6829 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6830 usage = KU_DIGITAL_SIGNATURE;
6831 }
6832
6833 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6834 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006835#else
6836 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006837#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6838
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006839#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6840 if( cert_endpoint == SSL_IS_SERVER )
6841 {
6842 ext_oid = OID_SERVER_AUTH;
6843 ext_len = OID_SIZE( OID_SERVER_AUTH );
6844 }
6845 else
6846 {
6847 ext_oid = OID_CLIENT_AUTH;
6848 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6849 }
6850
6851 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6852 return( -1 );
6853#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6854
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006855 return( 0 );
6856}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006857#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006858
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006859/*
6860 * Convert version numbers to/from wire format
6861 * and, for DTLS, to/from TLS equivalent.
6862 *
6863 * For TLS this is the identity.
6864 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6865 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6866 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6867 */
6868void ssl_write_version( int major, int minor, int transport,
6869 unsigned char ver[2] )
6870{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006871#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006872 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006873 {
6874 if( minor == SSL_MINOR_VERSION_2 )
6875 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6876
6877 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6878 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6879 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006880 else
6881#else
6882 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006883#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006884 {
6885 ver[0] = (unsigned char) major;
6886 ver[1] = (unsigned char) minor;
6887 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006888}
6889
6890void ssl_read_version( int *major, int *minor, int transport,
6891 const unsigned char ver[2] )
6892{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006893#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006894 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006895 {
6896 *major = 255 - ver[0] + 2;
6897 *minor = 255 - ver[1] + 1;
6898
6899 if( *minor == SSL_MINOR_VERSION_1 )
6900 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6901 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006902 else
6903#else
6904 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006905#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006906 {
6907 *major = ver[0];
6908 *minor = ver[1];
6909 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006910}
6911
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006912#endif /* POLARSSL_SSL_TLS_C */