blob: 2a61a51a222719dc32390e5910efa56851f5ac28 [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;
Paul Bakker5121ce52009-01-03 21:22:43 +0000280
281 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000282 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000283
284 hs = ( slen + 1 ) / 2;
285 S1 = secret;
286 S2 = secret + slen - hs;
287
288 nb = strlen( label );
289 memcpy( tmp + 20, label, nb );
290 memcpy( tmp + 20 + nb, random, rlen );
291 nb += rlen;
292
293 /*
294 * First compute P_md5(secret,label+random)[0..dlen]
295 */
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100296 if( ( md_info = md_info_from_type( POLARSSL_MD_MD5 ) ) == NULL )
297 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
298
299 md_hmac( md_info, S1, hs, tmp + 20, nb, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000300
301 for( i = 0; i < dlen; i += 16 )
302 {
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100303 md_hmac( md_info, S1, hs, 4 + tmp, 16 + nb, h_i );
304 md_hmac( md_info, S1, hs, 4 + tmp, 16, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000305
306 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
307
308 for( j = 0; j < k; j++ )
309 dstbuf[i + j] = h_i[j];
310 }
311
312 /*
313 * XOR out with P_sha1(secret,label+random)[0..dlen]
314 */
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100315 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA1 ) ) == NULL )
316 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
317
318 md_hmac( md_info, S2, hs, tmp + 20, nb, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000319
320 for( i = 0; i < dlen; i += 20 )
321 {
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100322 md_hmac( md_info, S2, hs, tmp, 20 + nb, h_i );
323 md_hmac( md_info, S2, hs, tmp, 20, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000324
325 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
326
327 for( j = 0; j < k; j++ )
328 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
329 }
330
Paul Bakker34617722014-06-13 17:20:13 +0200331 polarssl_zeroize( tmp, sizeof( tmp ) );
332 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000333
334 return( 0 );
335}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200336#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000337
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200338#if defined(POLARSSL_SSL_PROTO_TLS1_2)
339#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200340static int tls_prf_sha256( const unsigned char *secret, size_t slen,
341 const char *label,
342 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000343 unsigned char *dstbuf, size_t dlen )
344{
345 size_t nb;
346 size_t i, j, k;
347 unsigned char tmp[128];
348 unsigned char h_i[32];
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100349 const md_info_t *md_info;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000350
351 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
352 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
353
354 nb = strlen( label );
355 memcpy( tmp + 32, label, nb );
356 memcpy( tmp + 32 + nb, random, rlen );
357 nb += rlen;
358
359 /*
360 * Compute P_<hash>(secret, label + random)[0..dlen]
361 */
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100362 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA256 ) ) == NULL )
363 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
364
365 md_hmac( md_info, secret, slen, tmp + 32, nb, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000366
367 for( i = 0; i < dlen; i += 32 )
368 {
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100369 md_hmac( md_info, secret, slen, tmp, 32 + nb, h_i );
370 md_hmac( md_info, secret, slen, tmp, 32, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000371
372 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
373
374 for( j = 0; j < k; j++ )
375 dstbuf[i + j] = h_i[j];
376 }
377
Paul Bakker34617722014-06-13 17:20:13 +0200378 polarssl_zeroize( tmp, sizeof( tmp ) );
379 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000380
381 return( 0 );
382}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200383#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000384
Paul Bakker9e36f042013-06-30 14:34:05 +0200385#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200386static int tls_prf_sha384( const unsigned char *secret, size_t slen,
387 const char *label,
388 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000389 unsigned char *dstbuf, size_t dlen )
390{
391 size_t nb;
392 size_t i, j, k;
393 unsigned char tmp[128];
394 unsigned char h_i[48];
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100395 const md_info_t *md_info;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000396
397 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
398 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
399
400 nb = strlen( label );
401 memcpy( tmp + 48, label, nb );
402 memcpy( tmp + 48 + nb, random, rlen );
403 nb += rlen;
404
405 /*
406 * Compute P_<hash>(secret, label + random)[0..dlen]
407 */
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100408 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA384 ) ) == NULL )
409 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
410
411 md_hmac( md_info, secret, slen, tmp + 48, nb, tmp );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000412
413 for( i = 0; i < dlen; i += 48 )
414 {
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100415 md_hmac( md_info, secret, slen, tmp, 48 + nb, h_i );
416 md_hmac( md_info, secret, slen, tmp, 48, tmp );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000417
418 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
419
420 for( j = 0; j < k; j++ )
421 dstbuf[i + j] = h_i[j];
422 }
423
Paul Bakker34617722014-06-13 17:20:13 +0200424 polarssl_zeroize( tmp, sizeof( tmp ) );
425 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000426
427 return( 0 );
428}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200429#endif /* POLARSSL_SHA512_C */
430#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000431
Paul Bakker66d5d072014-06-17 16:39:18 +0200432static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200433
434#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
435 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200436static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200437#endif
Paul Bakker380da532012-04-18 16:10:25 +0000438
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200439#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200440static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
441static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200442#endif
443
444#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200445static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
446static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200447#endif
448
449#if defined(POLARSSL_SSL_PROTO_TLS1_2)
450#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200451static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
452static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
453static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200454#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100455
Paul Bakker9e36f042013-06-30 14:34:05 +0200456#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200457static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
458static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
459static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100460#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200461#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000462
Paul Bakker5121ce52009-01-03 21:22:43 +0000463int ssl_derive_keys( ssl_context *ssl )
464{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200465 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000467 unsigned char keyblk[256];
468 unsigned char *key1;
469 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100470 unsigned char *mac_enc;
471 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200472 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100473 const cipher_info_t *cipher_info;
474 const md_info_t *md_info;
475
Paul Bakker48916f92012-09-16 19:57:18 +0000476 ssl_session *session = ssl->session_negotiate;
477 ssl_transform *transform = ssl->transform_negotiate;
478 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
480 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
481
Paul Bakker68884e32013-01-07 18:20:04 +0100482 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
483 if( cipher_info == NULL )
484 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200485 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100486 transform->ciphersuite_info->cipher ) );
487 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
488 }
489
490 md_info = md_info_from_type( transform->ciphersuite_info->mac );
491 if( md_info == NULL )
492 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200493 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100494 transform->ciphersuite_info->mac ) );
495 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
496 }
497
Paul Bakker5121ce52009-01-03 21:22:43 +0000498 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000499 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000500 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200501#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000502 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000503 {
Paul Bakker48916f92012-09-16 19:57:18 +0000504 handshake->tls_prf = ssl3_prf;
505 handshake->calc_verify = ssl_calc_verify_ssl;
506 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000507 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200508 else
509#endif
510#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
511 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000512 {
Paul Bakker48916f92012-09-16 19:57:18 +0000513 handshake->tls_prf = tls1_prf;
514 handshake->calc_verify = ssl_calc_verify_tls;
515 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000516 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200517 else
518#endif
519#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200520#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200521 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
522 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000523 {
Paul Bakker48916f92012-09-16 19:57:18 +0000524 handshake->tls_prf = tls_prf_sha384;
525 handshake->calc_verify = ssl_calc_verify_tls_sha384;
526 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000527 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000528 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200529#endif
530#if defined(POLARSSL_SHA256_C)
531 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000532 {
Paul Bakker48916f92012-09-16 19:57:18 +0000533 handshake->tls_prf = tls_prf_sha256;
534 handshake->calc_verify = ssl_calc_verify_tls_sha256;
535 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000536 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200537 else
538#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200539#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200540 {
541 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200542 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200543 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000544
545 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 * SSLv3:
547 * master =
548 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
549 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
550 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200551 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200552 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000553 * master = PRF( premaster, "master secret", randbytes )[0..47]
554 */
Paul Bakker0a597072012-09-25 21:55:46 +0000555 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 {
Paul Bakker48916f92012-09-16 19:57:18 +0000557 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
558 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200560#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
561 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200562 {
563 unsigned char session_hash[48];
564 size_t hash_len;
565
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200566 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200567
568 ssl->handshake->calc_verify( ssl, session_hash );
569
570#if defined(POLARSSL_SSL_PROTO_TLS1_2)
571 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
572 {
573#if defined(POLARSSL_SHA512_C)
574 if( ssl->transform_negotiate->ciphersuite_info->mac ==
575 POLARSSL_MD_SHA384 )
576 {
577 hash_len = 48;
578 }
579 else
580#endif
581 hash_len = 32;
582 }
583 else
584#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
585 hash_len = 36;
586
587 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
588
589 handshake->tls_prf( handshake->premaster, handshake->pmslen,
590 "extended master secret",
591 session_hash, hash_len, session->master, 48 );
592
593 }
594 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200595#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000596 handshake->tls_prf( handshake->premaster, handshake->pmslen,
597 "master secret",
598 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000599
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200600
Paul Bakker34617722014-06-13 17:20:13 +0200601 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000602 }
603 else
604 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
605
606 /*
607 * Swap the client and server random values.
608 */
Paul Bakker48916f92012-09-16 19:57:18 +0000609 memcpy( tmp, handshake->randbytes, 64 );
610 memcpy( handshake->randbytes, tmp + 32, 32 );
611 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200612 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000613
614 /*
615 * SSLv3:
616 * key block =
617 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
618 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
619 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
620 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
621 * ...
622 *
623 * TLSv1:
624 * key block = PRF( master, "key expansion", randbytes )
625 */
Paul Bakker48916f92012-09-16 19:57:18 +0000626 handshake->tls_prf( session->master, 48, "key expansion",
627 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000628
Paul Bakker48916f92012-09-16 19:57:18 +0000629 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
630 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
631 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
632 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000633 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
634
Paul Bakker34617722014-06-13 17:20:13 +0200635 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000636
637 /*
638 * Determine the appropriate key, IV and MAC length.
639 */
Paul Bakker68884e32013-01-07 18:20:04 +0100640
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200641 transform->keylen = cipher_info->key_length / 8;
642
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200643 if( cipher_info->mode == POLARSSL_MODE_GCM ||
644 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000645 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200646 transform->maclen = 0;
647
Paul Bakker68884e32013-01-07 18:20:04 +0100648 transform->ivlen = 12;
649 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200650
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200651 /* Minimum length is expicit IV + tag */
652 transform->minlen = transform->ivlen - transform->fixed_ivlen
653 + ( transform->ciphersuite_info->flags &
654 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100655 }
656 else
657 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200658 int ret;
659
660 /* Initialize HMAC contexts */
661 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
662 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100663 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200664 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
665 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100666 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000667
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200668 /* Get MAC length */
669 transform->maclen = md_get_size( md_info );
670
671#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
672 /*
673 * If HMAC is to be truncated, we shall keep the leftmost bytes,
674 * (rfc 6066 page 13 or rfc 2104 section 4),
675 * so we only need to adjust the length here.
676 */
677 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
678 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
679#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
680
681 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100682 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000683
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200684 /* Minimum length */
685 if( cipher_info->mode == POLARSSL_MODE_STREAM )
686 transform->minlen = transform->maclen;
687 else
Paul Bakker68884e32013-01-07 18:20:04 +0100688 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200689 /*
690 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100691 * 1. if EtM is in use: one block plus MAC
692 * otherwise: * first multiple of blocklen greater than maclen
693 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200694 */
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100695#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
696 if( session->encrypt_then_mac == SSL_ETM_ENABLED )
697 {
698 transform->minlen = transform->maclen
699 + cipher_info->block_size;
700 }
701 else
702#endif
703 {
704 transform->minlen = transform->maclen
705 + cipher_info->block_size
706 - transform->maclen % cipher_info->block_size;
707 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200708
709#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
710 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
711 ssl->minor_ver == SSL_MINOR_VERSION_1 )
712 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100713 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200714#endif
715#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
716 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
717 ssl->minor_ver == SSL_MINOR_VERSION_3 )
718 {
719 transform->minlen += transform->ivlen;
720 }
721 else
722#endif
723 {
724 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
725 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
726 }
Paul Bakker68884e32013-01-07 18:20:04 +0100727 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000728 }
729
730 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000731 transform->keylen, transform->minlen, transform->ivlen,
732 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000733
734 /*
735 * Finally setup the cipher contexts, IVs and MAC secrets.
736 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100737#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000738 if( ssl->endpoint == SSL_IS_CLIENT )
739 {
Paul Bakker48916f92012-09-16 19:57:18 +0000740 key1 = keyblk + transform->maclen * 2;
741 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000742
Paul Bakker68884e32013-01-07 18:20:04 +0100743 mac_enc = keyblk;
744 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000745
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000746 /*
747 * This is not used in TLS v1.1.
748 */
Paul Bakker48916f92012-09-16 19:57:18 +0000749 iv_copy_len = ( transform->fixed_ivlen ) ?
750 transform->fixed_ivlen : transform->ivlen;
751 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
752 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000753 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000754 }
755 else
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100756#endif /* POLARSSL_SSL_CLI_C */
757#if defined(POLARSSL_SSL_SRV_C)
758 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000759 {
Paul Bakker48916f92012-09-16 19:57:18 +0000760 key1 = keyblk + transform->maclen * 2 + transform->keylen;
761 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000762
Paul Bakker68884e32013-01-07 18:20:04 +0100763 mac_enc = keyblk + transform->maclen;
764 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000765
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000766 /*
767 * This is not used in TLS v1.1.
768 */
Paul Bakker48916f92012-09-16 19:57:18 +0000769 iv_copy_len = ( transform->fixed_ivlen ) ?
770 transform->fixed_ivlen : transform->ivlen;
771 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
772 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000773 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000774 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100775 else
776#endif /* POLARSSL_SSL_SRV_C */
777 {
778 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
779 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
780 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000781
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200782#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100783 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
784 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100785 if( transform->maclen > sizeof transform->mac_enc )
786 {
787 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200788 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100789 }
790
Paul Bakker68884e32013-01-07 18:20:04 +0100791 memcpy( transform->mac_enc, mac_enc, transform->maclen );
792 memcpy( transform->mac_dec, mac_dec, transform->maclen );
793 }
794 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200795#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200796#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
797 defined(POLARSSL_SSL_PROTO_TLS1_2)
798 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100799 {
800 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
801 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
802 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200803 else
804#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200805 {
806 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200807 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200808 }
Paul Bakker68884e32013-01-07 18:20:04 +0100809
Paul Bakker05ef8352012-05-08 09:17:57 +0000810#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200811 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000812 {
813 int ret = 0;
814
815 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
816
Paul Bakker07eb38b2012-12-19 14:42:06 +0100817 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
818 transform->iv_enc, transform->iv_dec,
819 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100820 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100821 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000822 {
823 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200824 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000825 }
826 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200827#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000828
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200829 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
830 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000831 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200832 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
833 return( ret );
834 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200835
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200836 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
837 cipher_info ) ) != 0 )
838 {
839 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
840 return( ret );
841 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200842
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200843 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
844 cipher_info->key_length,
845 POLARSSL_ENCRYPT ) ) != 0 )
846 {
847 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
848 return( ret );
849 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200850
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200851 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
852 cipher_info->key_length,
853 POLARSSL_DECRYPT ) ) != 0 )
854 {
855 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
856 return( ret );
857 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200858
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200859#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200860 if( cipher_info->mode == POLARSSL_MODE_CBC )
861 {
862 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
863 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200864 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200865 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
866 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200867 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200868
869 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
870 POLARSSL_PADDING_NONE ) ) != 0 )
871 {
872 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
873 return( ret );
874 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000875 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200876#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000877
Paul Bakker34617722014-06-13 17:20:13 +0200878 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000879
Paul Bakker2770fbd2012-07-03 13:30:23 +0000880#if defined(POLARSSL_ZLIB_SUPPORT)
881 // Initialize compression
882 //
Paul Bakker48916f92012-09-16 19:57:18 +0000883 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000884 {
Paul Bakker16770332013-10-11 09:59:44 +0200885 if( ssl->compress_buf == NULL )
886 {
887 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
888 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
889 if( ssl->compress_buf == NULL )
890 {
891 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
892 SSL_BUFFER_LEN ) );
893 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
894 }
895 }
896
Paul Bakker2770fbd2012-07-03 13:30:23 +0000897 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
898
Paul Bakker48916f92012-09-16 19:57:18 +0000899 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
900 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000901
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200902 if( deflateInit( &transform->ctx_deflate,
903 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000904 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000905 {
906 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
907 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
908 }
909 }
910#endif /* POLARSSL_ZLIB_SUPPORT */
911
Paul Bakker5121ce52009-01-03 21:22:43 +0000912 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
913
914 return( 0 );
915}
916
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200917#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000918void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000919{
920 md5_context md5;
921 sha1_context sha1;
922 unsigned char pad_1[48];
923 unsigned char pad_2[48];
924
Paul Bakker380da532012-04-18 16:10:25 +0000925 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000926
Paul Bakker48916f92012-09-16 19:57:18 +0000927 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
928 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000929
Paul Bakker380da532012-04-18 16:10:25 +0000930 memset( pad_1, 0x36, 48 );
931 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000932
Paul Bakker48916f92012-09-16 19:57:18 +0000933 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000934 md5_update( &md5, pad_1, 48 );
935 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000936
Paul Bakker380da532012-04-18 16:10:25 +0000937 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000938 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000939 md5_update( &md5, pad_2, 48 );
940 md5_update( &md5, hash, 16 );
941 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000942
Paul Bakker48916f92012-09-16 19:57:18 +0000943 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000944 sha1_update( &sha1, pad_1, 40 );
945 sha1_finish( &sha1, hash + 16 );
946
947 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000948 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000949 sha1_update( &sha1, pad_2, 40 );
950 sha1_update( &sha1, hash + 16, 20 );
951 sha1_finish( &sha1, hash + 16 );
952
953 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
954 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
955
Paul Bakker5b4af392014-06-26 12:09:34 +0200956 md5_free( &md5 );
957 sha1_free( &sha1 );
958
Paul Bakker380da532012-04-18 16:10:25 +0000959 return;
960}
Paul Bakker9af723c2014-05-01 13:03:14 +0200961#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000962
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200963#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000964void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
965{
966 md5_context md5;
967 sha1_context sha1;
968
969 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
970
Paul Bakker48916f92012-09-16 19:57:18 +0000971 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
972 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000973
Paul Bakker48916f92012-09-16 19:57:18 +0000974 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000975 sha1_finish( &sha1, hash + 16 );
976
977 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
978 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
979
Paul Bakker5b4af392014-06-26 12:09:34 +0200980 md5_free( &md5 );
981 sha1_free( &sha1 );
982
Paul Bakker380da532012-04-18 16:10:25 +0000983 return;
984}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200985#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000986
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200987#if defined(POLARSSL_SSL_PROTO_TLS1_2)
988#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000989void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
990{
Paul Bakker9e36f042013-06-30 14:34:05 +0200991 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000992
993 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
994
Paul Bakker9e36f042013-06-30 14:34:05 +0200995 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
996 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000997
998 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
999 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1000
Paul Bakker5b4af392014-06-26 12:09:34 +02001001 sha256_free( &sha256 );
1002
Paul Bakker380da532012-04-18 16:10:25 +00001003 return;
1004}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001005#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001006
Paul Bakker9e36f042013-06-30 14:34:05 +02001007#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +00001008void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
1009{
Paul Bakker9e36f042013-06-30 14:34:05 +02001010 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001011
1012 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1013
Paul Bakker9e36f042013-06-30 14:34:05 +02001014 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
1015 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001016
Paul Bakkerca4ab492012-04-18 14:23:57 +00001017 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001018 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1019
Paul Bakker5b4af392014-06-26 12:09:34 +02001020 sha512_free( &sha512 );
1021
Paul Bakker5121ce52009-01-03 21:22:43 +00001022 return;
1023}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001024#endif /* POLARSSL_SHA512_C */
1025#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001026
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001027#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001028int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
1029{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001030 unsigned char *p = ssl->handshake->premaster;
1031 unsigned char *end = p + sizeof( ssl->handshake->premaster );
1032
1033 /*
1034 * PMS = struct {
1035 * opaque other_secret<0..2^16-1>;
1036 * opaque psk<0..2^16-1>;
1037 * };
1038 * with "other_secret" depending on the particular key exchange
1039 */
1040#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
1041 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
1042 {
1043 if( end - p < 2 + (int) ssl->psk_len )
1044 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1045
1046 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1047 *(p++) = (unsigned char)( ssl->psk_len );
1048 p += ssl->psk_len;
1049 }
1050 else
1051#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001052#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1053 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1054 {
1055 /*
1056 * other_secret already set by the ClientKeyExchange message,
1057 * and is 48 bytes long
1058 */
1059 *p++ = 0;
1060 *p++ = 48;
1061 p += 48;
1062 }
1063 else
1064#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001065#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1066 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1067 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001068 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02001069 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001070
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001071 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001072 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001073 p + 2, &len,
1074 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001075 {
1076 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
1077 return( ret );
1078 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001079 *(p++) = (unsigned char)( len >> 8 );
1080 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001081 p += len;
1082
1083 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1084 }
1085 else
1086#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1087#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1088 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1089 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001090 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001091 size_t zlen;
1092
1093 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001094 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001095 ssl->f_rng, ssl->p_rng ) ) != 0 )
1096 {
1097 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1098 return( ret );
1099 }
1100
1101 *(p++) = (unsigned char)( zlen >> 8 );
1102 *(p++) = (unsigned char)( zlen );
1103 p += zlen;
1104
1105 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1106 }
1107 else
1108#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1109 {
1110 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001111 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001112 }
1113
1114 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001115 if( end - p < 2 + (int) ssl->psk_len )
1116 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1117
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001118 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1119 *(p++) = (unsigned char)( ssl->psk_len );
1120 memcpy( p, ssl->psk, ssl->psk_len );
1121 p += ssl->psk_len;
1122
1123 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1124
1125 return( 0 );
1126}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001127#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001128
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001129#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001130/*
1131 * SSLv3.0 MAC functions
1132 */
Paul Bakker68884e32013-01-07 18:20:04 +01001133static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1134 unsigned char *buf, size_t len,
1135 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001136{
1137 unsigned char header[11];
1138 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001139 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001140 int md_size = md_get_size( md_ctx->md_info );
1141 int md_type = md_get_type( md_ctx->md_info );
1142
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001143 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001144 if( md_type == POLARSSL_MD_MD5 )
1145 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001146 else
Paul Bakker68884e32013-01-07 18:20:04 +01001147 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001148
1149 memcpy( header, ctr, 8 );
1150 header[ 8] = (unsigned char) type;
1151 header[ 9] = (unsigned char)( len >> 8 );
1152 header[10] = (unsigned char)( len );
1153
Paul Bakker68884e32013-01-07 18:20:04 +01001154 memset( padding, 0x36, padlen );
1155 md_starts( md_ctx );
1156 md_update( md_ctx, secret, md_size );
1157 md_update( md_ctx, padding, padlen );
1158 md_update( md_ctx, header, 11 );
1159 md_update( md_ctx, buf, len );
1160 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001161
Paul Bakker68884e32013-01-07 18:20:04 +01001162 memset( padding, 0x5C, padlen );
1163 md_starts( md_ctx );
1164 md_update( md_ctx, secret, md_size );
1165 md_update( md_ctx, padding, padlen );
1166 md_update( md_ctx, buf + len, md_size );
1167 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001168}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001169#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001170
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001171#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1172 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1173 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1174#define POLARSSL_SOME_MODES_USE_MAC
1175#endif
1176
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001177/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001178 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001179 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001180static int ssl_encrypt_buf( ssl_context *ssl )
1181{
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001182 cipher_mode_t mode;
1183 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001184
1185 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1186
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001187 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1188 {
1189 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1190 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1191 }
1192
1193 mode = cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
1194
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001195 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1196 ssl->out_msg, ssl->out_msglen );
1197
Paul Bakker5121ce52009-01-03 21:22:43 +00001198 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001199 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001200 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001201#if defined(POLARSSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001202 if( mode == POLARSSL_MODE_STREAM ||
1203 ( mode == POLARSSL_MODE_CBC
1204#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1205 && ssl->session_out->encrypt_then_mac == SSL_ETM_DISABLED
1206#endif
1207 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001208 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001209#if defined(POLARSSL_SSL_PROTO_SSL3)
1210 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1211 {
1212 ssl_mac( &ssl->transform_out->md_ctx_enc,
1213 ssl->transform_out->mac_enc,
1214 ssl->out_msg, ssl->out_msglen,
1215 ssl->out_ctr, ssl->out_msgtype );
1216 }
1217 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001218#endif
1219#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001220 defined(POLARSSL_SSL_PROTO_TLS1_2)
1221 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1222 {
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001223 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1224 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1225 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001226 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1227 ssl->out_msg, ssl->out_msglen );
1228 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1229 ssl->out_msg + ssl->out_msglen );
1230 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1231 }
1232 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001233#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001234 {
1235 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001236 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001237 }
1238
1239 SSL_DEBUG_BUF( 4, "computed mac",
1240 ssl->out_msg + ssl->out_msglen,
1241 ssl->transform_out->maclen );
1242
1243 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001244 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001245 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001246#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001247
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001248 /*
1249 * Encrypt
1250 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001251#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001252 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001253 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001254 int ret;
1255 size_t olen = 0;
1256
Paul Bakker5121ce52009-01-03 21:22:43 +00001257 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1258 "including %d bytes of padding",
1259 ssl->out_msglen, 0 ) );
1260
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001261 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001262 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001263 ssl->transform_out->ivlen,
1264 ssl->out_msg, ssl->out_msglen,
1265 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001266 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001267 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001268 return( ret );
1269 }
1270
1271 if( ssl->out_msglen != olen )
1272 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001273 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001274 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001275 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001276 }
Paul Bakker68884e32013-01-07 18:20:04 +01001277 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001278#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001279#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1280 if( mode == POLARSSL_MODE_GCM ||
1281 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001282 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001283 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001284 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001285 unsigned char *enc_msg;
1286 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001287 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1288 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001289
Paul Bakkerca4ab492012-04-18 14:23:57 +00001290 memcpy( add_data, ssl->out_ctr, 8 );
1291 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001292 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1293 ssl->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001294 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1295 add_data[12] = ssl->out_msglen & 0xFF;
1296
1297 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1298 add_data, 13 );
1299
Paul Bakker68884e32013-01-07 18:20:04 +01001300 /*
1301 * Generate IV
1302 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001303#if defined(POLARSSL_SSL_AEAD_RANDOM_IV)
Paul Bakker68884e32013-01-07 18:20:04 +01001304 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001305 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1306 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001307 if( ret != 0 )
1308 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001309
Paul Bakker68884e32013-01-07 18:20:04 +01001310 memcpy( ssl->out_iv,
1311 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1312 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001313#else
1314 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1315 {
1316 /* Reminder if we ever add an AEAD mode with a different size */
1317 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1318 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1319 }
1320
1321 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1322 ssl->out_ctr, 8 );
1323 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1324#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00001325
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001326 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001327 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001328
Paul Bakker68884e32013-01-07 18:20:04 +01001329 /*
1330 * Fix pointer positions and message length with added IV
1331 */
1332 enc_msg = ssl->out_msg;
1333 enc_msglen = ssl->out_msglen;
1334 ssl->out_msglen += ssl->transform_out->ivlen -
1335 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001336
Paul Bakker68884e32013-01-07 18:20:04 +01001337 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1338 "including %d bytes of padding",
1339 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001340
Paul Bakker68884e32013-01-07 18:20:04 +01001341 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001342 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001343 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001344 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1345 ssl->transform_out->iv_enc,
1346 ssl->transform_out->ivlen,
1347 add_data, 13,
1348 enc_msg, enc_msglen,
1349 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001350 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001351 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001352 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001353 return( ret );
1354 }
1355
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001356 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001357 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001358 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001359 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001360 }
1361
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001362 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001363 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001364
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001365 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001366 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001367 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001368#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001369#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1370 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001371 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001372 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001373 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001374 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001375 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001376
Paul Bakker48916f92012-09-16 19:57:18 +00001377 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1378 ssl->transform_out->ivlen;
1379 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001380 padlen = 0;
1381
1382 for( i = 0; i <= padlen; i++ )
1383 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1384
1385 ssl->out_msglen += padlen + 1;
1386
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001387 enc_msglen = ssl->out_msglen;
1388 enc_msg = ssl->out_msg;
1389
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001390#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001391 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001392 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1393 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001394 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001395 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001396 {
1397 /*
1398 * Generate IV
1399 */
Paul Bakker48916f92012-09-16 19:57:18 +00001400 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1401 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001402 if( ret != 0 )
1403 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001404
Paul Bakker92be97b2013-01-02 17:30:03 +01001405 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001406 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001407
1408 /*
1409 * Fix pointer positions and message length with added IV
1410 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001411 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001412 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001413 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001414 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001415#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001416
Paul Bakker5121ce52009-01-03 21:22:43 +00001417 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001418 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001419 ssl->out_msglen, ssl->transform_out->ivlen,
1420 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001421
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001422 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001423 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001424 ssl->transform_out->ivlen,
1425 enc_msg, enc_msglen,
1426 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001427 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001428 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001429 return( ret );
1430 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001431
Paul Bakkercca5b812013-08-31 17:40:26 +02001432 if( enc_msglen != olen )
1433 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001434 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001435 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001436 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001437
1438#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001439 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1440 {
1441 /*
1442 * Save IV in SSL3 and TLS1
1443 */
1444 memcpy( ssl->transform_out->iv_enc,
1445 ssl->transform_out->cipher_ctx_enc.iv,
1446 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001448#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001449
1450#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001451 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001452 {
1453 /*
1454 * MAC(MAC_write_key, seq_num +
1455 * TLSCipherText.type +
1456 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001457 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001458 * IV + // except for TLS 1.0
1459 * ENC(content + padding + padding_length));
1460 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001461 unsigned char pseudo_hdr[13];
1462
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001463 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1464
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001465 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1466 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001467 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1468 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1469
1470 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001471
1472 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1473 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1474 ssl->out_iv, ssl->out_msglen );
1475 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1476 ssl->out_iv + ssl->out_msglen );
1477 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1478
1479 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001480 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001481 }
1482#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001483 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001484 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001485#endif /* POLARSSL_CIPHER_MODE_CBC &&
1486 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001487 {
1488 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001489 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001490 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001491
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001492 /* Make extra sure authentication was performed, exactly once */
1493 if( auth_done != 1 )
1494 {
1495 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1496 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1497 }
1498
Paul Bakker5121ce52009-01-03 21:22:43 +00001499 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1500
1501 return( 0 );
1502}
1503
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001504#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001505
Paul Bakker5121ce52009-01-03 21:22:43 +00001506static int ssl_decrypt_buf( ssl_context *ssl )
1507{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001508 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001509 cipher_mode_t mode;
1510 int auth_done = 0;
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001511#if defined(POLARSSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001512 size_t padlen = 0, correct = 1;
1513#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001514
1515 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1516
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001517 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1518 {
1519 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1520 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1521 }
1522
1523 mode = cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
1524
Paul Bakker48916f92012-09-16 19:57:18 +00001525 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 {
1527 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001528 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001529 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001530 }
1531
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001532#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001533 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001534 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001535 int ret;
1536 size_t olen = 0;
1537
Paul Bakker68884e32013-01-07 18:20:04 +01001538 padlen = 0;
1539
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001540 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001541 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001542 ssl->transform_in->ivlen,
1543 ssl->in_msg, ssl->in_msglen,
1544 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001545 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001546 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001547 return( ret );
1548 }
1549
1550 if( ssl->in_msglen != olen )
1551 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001552 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001553 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001554 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001555 }
Paul Bakker68884e32013-01-07 18:20:04 +01001556 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001557#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001558#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1559 if( mode == POLARSSL_MODE_GCM ||
1560 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001561 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001562 int ret;
1563 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001564 unsigned char *dec_msg;
1565 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001566 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001567 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1568 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001569 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1570 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001571
Manuel Pégourié-Gonnard06d75192015-02-11 14:54:11 +00001572 if( ssl->in_msglen < (size_t) explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001573 {
1574 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1575 "+ taglen (%d)", ssl->in_msglen,
1576 explicit_iv_len, taglen ) );
1577 return( POLARSSL_ERR_SSL_INVALID_MAC );
1578 }
1579 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1580
Paul Bakker68884e32013-01-07 18:20:04 +01001581 dec_msg = ssl->in_msg;
1582 dec_msg_result = ssl->in_msg;
1583 ssl->in_msglen = dec_msglen;
1584
1585 memcpy( add_data, ssl->in_ctr, 8 );
1586 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001587 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1588 ssl->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001589 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1590 add_data[12] = ssl->in_msglen & 0xFF;
1591
1592 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1593 add_data, 13 );
1594
1595 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1596 ssl->in_iv,
1597 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1598
1599 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1600 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001601 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001602
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001603 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001604 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001605 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001606 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1607 ssl->transform_in->iv_dec,
1608 ssl->transform_in->ivlen,
1609 add_data, 13,
1610 dec_msg, dec_msglen,
1611 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001612 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001613 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001614 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1615
1616 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1617 return( POLARSSL_ERR_SSL_INVALID_MAC );
1618
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001619 return( ret );
1620 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001621 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001622
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001623 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001624 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001625 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001626 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001627 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001628 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001629 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001630#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001631#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1632 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001633 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001634 {
Paul Bakker45829992013-01-03 14:52:21 +01001635 /*
1636 * Decrypt and check the padding
1637 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001638 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001639 unsigned char *dec_msg;
1640 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001641 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001642 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001643 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001644
Paul Bakker5121ce52009-01-03 21:22:43 +00001645 /*
Paul Bakker45829992013-01-03 14:52:21 +01001646 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001647 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001648#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001649 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1650 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001651#endif
Paul Bakker45829992013-01-03 14:52:21 +01001652
1653 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1654 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1655 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001656 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1657 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1658 ssl->transform_in->ivlen,
1659 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001660 return( POLARSSL_ERR_SSL_INVALID_MAC );
1661 }
1662
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001663 dec_msglen = ssl->in_msglen;
1664 dec_msg = ssl->in_msg;
1665 dec_msg_result = ssl->in_msg;
1666
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001667 /*
1668 * Authenticate before decrypt if enabled
1669 */
1670#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001671 if( ssl->session_in->encrypt_then_mac == SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001672 {
1673 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001674 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001675
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001676 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1677
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001678 dec_msglen -= ssl->transform_in->maclen;
1679 ssl->in_msglen -= ssl->transform_in->maclen;
1680
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001681 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1682 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1683 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1684 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1685
1686 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1687
1688 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001689 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1690 ssl->in_iv, ssl->in_msglen );
1691 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1692 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1693
1694 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1695 ssl->transform_in->maclen );
1696 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1697 ssl->transform_in->maclen );
1698
1699 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1700 ssl->transform_in->maclen ) != 0 )
1701 {
1702 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1703
1704 return( POLARSSL_ERR_SSL_INVALID_MAC );
1705 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001706 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001707 }
1708#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1709
1710 /*
1711 * Check length sanity
1712 */
1713 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1714 {
1715 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1716 ssl->in_msglen, ssl->transform_in->ivlen ) );
1717 return( POLARSSL_ERR_SSL_INVALID_MAC );
1718 }
1719
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001720#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001721 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001722 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001723 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001724 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001725 {
Paul Bakker48916f92012-09-16 19:57:18 +00001726 dec_msglen -= ssl->transform_in->ivlen;
1727 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001728
Paul Bakker48916f92012-09-16 19:57:18 +00001729 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001730 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001731 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001732#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001733
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001734 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001735 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001736 ssl->transform_in->ivlen,
1737 dec_msg, dec_msglen,
1738 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001739 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001740 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001741 return( ret );
1742 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001743
Paul Bakkercca5b812013-08-31 17:40:26 +02001744 if( dec_msglen != olen )
1745 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001746 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001747 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001748 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001749
1750#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001751 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1752 {
1753 /*
1754 * Save IV in SSL3 and TLS1
1755 */
1756 memcpy( ssl->transform_in->iv_dec,
1757 ssl->transform_in->cipher_ctx_dec.iv,
1758 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001759 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001760#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001761
1762 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001763
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001764 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001765 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001766 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001767#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001768 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1769 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001770#endif
Paul Bakker45829992013-01-03 14:52:21 +01001771 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001772 correct = 0;
1773 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001774
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001775#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001776 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1777 {
Paul Bakker48916f92012-09-16 19:57:18 +00001778 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001779 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001780#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001781 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1782 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001783 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001784#endif
Paul Bakker45829992013-01-03 14:52:21 +01001785 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001786 }
1787 }
1788 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001789#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001790#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1791 defined(POLARSSL_SSL_PROTO_TLS1_2)
1792 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001793 {
1794 /*
Paul Bakker45829992013-01-03 14:52:21 +01001795 * TLSv1+: always check the padding up to the first failure
1796 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001797 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001798 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001799 size_t padding_idx = ssl->in_msglen - padlen - 1;
1800
Paul Bakker956c9e02013-12-19 14:42:28 +01001801 /*
1802 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001803 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001804 *
Paul Bakker61885c72014-04-25 12:59:03 +02001805 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1806 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001807 *
1808 * In both cases we reset padding_idx to a safe value (0) to
1809 * prevent out-of-buffer reads.
1810 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001811 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001812 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1813 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001814
1815 padding_idx *= correct;
1816
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001817 for( i = 1; i <= 256; i++ )
1818 {
1819 real_count &= ( i <= padlen );
1820 pad_count += real_count *
1821 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1822 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001823
1824 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001825
Paul Bakkerd66f0702013-01-31 16:57:45 +01001826#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001827 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001828 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001829#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001830 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001831 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001832 else
1833#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1834 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001835 {
1836 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001837 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001838 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001839
1840 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001841 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001842 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001843#endif /* POLARSSL_CIPHER_MODE_CBC &&
1844 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001845 {
1846 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001847 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001848 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001849
1850 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1851 ssl->in_msg, ssl->in_msglen );
1852
1853 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001854 * Authenticate if not done yet.
1855 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001856 */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001857#if defined(POLARSSL_SOME_MODES_USE_MAC)
1858 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001859 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001860 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1861
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001862 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001863
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001864 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1865 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001866
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001867 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001868
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001869#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001870 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1871 {
1872 ssl_mac( &ssl->transform_in->md_ctx_dec,
1873 ssl->transform_in->mac_dec,
1874 ssl->in_msg, ssl->in_msglen,
1875 ssl->in_ctr, ssl->in_msgtype );
1876 }
1877 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001878#endif /* POLARSSL_SSL_PROTO_SSL3 */
1879#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001880 defined(POLARSSL_SSL_PROTO_TLS1_2)
1881 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1882 {
1883 /*
1884 * Process MAC and always update for padlen afterwards to make
1885 * total time independent of padlen
1886 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001887 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001888 *
1889 * Known timing attacks:
1890 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1891 *
1892 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1893 * correctly. (We round down instead of up, so -56 is the correct
1894 * value for our calculations instead of -55)
1895 */
1896 size_t j, extra_run = 0;
1897 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1898 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001899
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001900 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001901
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001902 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
1903 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
1904 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001905 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1906 ssl->in_msglen );
1907 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1908 ssl->in_msg + ssl->in_msglen );
1909 for( j = 0; j < extra_run; j++ )
1910 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001911
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001912 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1913 }
1914 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001915#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001916 POLARSSL_SSL_PROTO_TLS1_2 */
1917 {
1918 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001919 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001920 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001921
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001922 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1923 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1924 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001925
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001926 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001927 ssl->transform_in->maclen ) != 0 )
1928 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001929#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001930 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001931#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001932 correct = 0;
1933 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001934 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001935
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001936 /*
1937 * Finally check the correct flag
1938 */
1939 if( correct == 0 )
1940 return( POLARSSL_ERR_SSL_INVALID_MAC );
1941 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001942#endif /* POLARSSL_SOME_MODES_USE_MAC */
1943
1944 /* Make extra sure authentication was performed, exactly once */
1945 if( auth_done != 1 )
1946 {
1947 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1948 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1949 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001950
1951 if( ssl->in_msglen == 0 )
1952 {
1953 ssl->nb_zero++;
1954
1955 /*
1956 * Three or more empty messages may be a DoS attack
1957 * (excessive CPU consumption).
1958 */
1959 if( ssl->nb_zero > 3 )
1960 {
1961 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1962 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001963 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001964 }
1965 }
1966 else
1967 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001968
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001969#if defined(POLARSSL_SSL_PROTO_DTLS)
1970 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001971 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02001972 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001973 }
1974 else
1975#endif
1976 {
1977 for( i = 8; i > ssl_ep_len( ssl ); i-- )
1978 if( ++ssl->in_ctr[i - 1] != 0 )
1979 break;
1980
1981 /* The loop goes to its end iff the counter is wrapping */
1982 if( i == ssl_ep_len( ssl ) )
1983 {
1984 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1985 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1986 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001987 }
1988
Paul Bakker5121ce52009-01-03 21:22:43 +00001989 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1990
1991 return( 0 );
1992}
1993
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001994#undef MAC_NONE
1995#undef MAC_PLAINTEXT
1996#undef MAC_CIPHERTEXT
1997
Paul Bakker2770fbd2012-07-03 13:30:23 +00001998#if defined(POLARSSL_ZLIB_SUPPORT)
1999/*
2000 * Compression/decompression functions
2001 */
2002static int ssl_compress_buf( ssl_context *ssl )
2003{
2004 int ret;
2005 unsigned char *msg_post = ssl->out_msg;
2006 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002007 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002008
2009 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
2010
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002011 if( len_pre == 0 )
2012 return( 0 );
2013
Paul Bakker2770fbd2012-07-03 13:30:23 +00002014 memcpy( msg_pre, ssl->out_msg, len_pre );
2015
2016 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
2017 ssl->out_msglen ) );
2018
2019 SSL_DEBUG_BUF( 4, "before compression: output payload",
2020 ssl->out_msg, ssl->out_msglen );
2021
Paul Bakker48916f92012-09-16 19:57:18 +00002022 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2023 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2024 ssl->transform_out->ctx_deflate.next_out = msg_post;
2025 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002026
Paul Bakker48916f92012-09-16 19:57:18 +00002027 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002028 if( ret != Z_OK )
2029 {
2030 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2031 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
2032 }
2033
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002034 ssl->out_msglen = SSL_BUFFER_LEN -
2035 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002036
Paul Bakker2770fbd2012-07-03 13:30:23 +00002037 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
2038 ssl->out_msglen ) );
2039
2040 SSL_DEBUG_BUF( 4, "after compression: output payload",
2041 ssl->out_msg, ssl->out_msglen );
2042
2043 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
2044
2045 return( 0 );
2046}
2047
2048static int ssl_decompress_buf( ssl_context *ssl )
2049{
2050 int ret;
2051 unsigned char *msg_post = ssl->in_msg;
2052 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002053 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002054
2055 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
2056
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002057 if( len_pre == 0 )
2058 return( 0 );
2059
Paul Bakker2770fbd2012-07-03 13:30:23 +00002060 memcpy( msg_pre, ssl->in_msg, len_pre );
2061
2062 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
2063 ssl->in_msglen ) );
2064
2065 SSL_DEBUG_BUF( 4, "before decompression: input payload",
2066 ssl->in_msg, ssl->in_msglen );
2067
Paul Bakker48916f92012-09-16 19:57:18 +00002068 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2069 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2070 ssl->transform_in->ctx_inflate.next_out = msg_post;
2071 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002072
Paul Bakker48916f92012-09-16 19:57:18 +00002073 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002074 if( ret != Z_OK )
2075 {
2076 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2077 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
2078 }
2079
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002080 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
2081 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002082
Paul Bakker2770fbd2012-07-03 13:30:23 +00002083 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
2084 ssl->in_msglen ) );
2085
2086 SSL_DEBUG_BUF( 4, "after decompression: input payload",
2087 ssl->in_msg, ssl->in_msglen );
2088
2089 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
2090
2091 return( 0 );
2092}
2093#endif /* POLARSSL_ZLIB_SUPPORT */
2094
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002095#if defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002096static int ssl_write_hello_request( ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002097
2098#if defined(POLARSSL_SSL_PROTO_DTLS)
2099static int ssl_resend_hello_request( ssl_context *ssl )
2100{
2101 /* If renegotiation is not enforced, retransmit until we would reach max
2102 * timeout if we were using the usual handshake doubling scheme */
2103 if( ssl->renego_max_records < 0 )
2104 {
2105 uint32_t ratio = ssl->hs_timeout_max / ssl->hs_timeout_min + 1;
2106 unsigned char doublings = 1;
2107
2108 while( ratio != 0 )
2109 {
2110 ++doublings;
2111 ratio >>= 1;
2112 }
2113
2114 if( ++ssl->renego_records_seen > doublings )
2115 {
2116 SSL_DEBUG_MSG( 0, ( "no longer retransmitting hello request" ) );
2117 return( 0 );
2118 }
2119 }
2120
2121 return( ssl_write_hello_request( ssl ) );
2122}
2123#endif
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002124#endif /* POLARSSL_SSL_SRV_C && POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002125
Paul Bakker5121ce52009-01-03 21:22:43 +00002126/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002127 * Fill the input message buffer by appending data to it.
2128 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002129 *
2130 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2131 * available (from this read and/or a previous one). Otherwise, an error code
2132 * is returned (possibly EOF or WANT_READ).
2133 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002134 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2135 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2136 * since we always read a whole datagram at once.
2137 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002138 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002139 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 */
Paul Bakker23986e52011-04-24 08:57:21 +00002141int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002142{
Paul Bakker23986e52011-04-24 08:57:21 +00002143 int ret;
2144 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002145
2146 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
2147
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002148 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2149 {
2150 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2151 "or ssl_set_bio_timeout()" ) );
2152 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2153 }
2154
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002155 if( nb_want > SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002156 {
2157 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2158 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2159 }
2160
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002161#if defined(POLARSSL_SSL_PROTO_DTLS)
2162 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002164 uint32_t timeout;
2165
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002166 /*
2167 * The point is, we need to always read a full datagram at once, so we
2168 * sometimes read more then requested, and handle the additional data.
2169 * It could be the rest of the current record (while fetching the
2170 * header) and/or some other records in the same datagram.
2171 */
2172
2173 /*
2174 * Move to the next record in the already read datagram if applicable
2175 */
2176 if( ssl->next_record_offset != 0 )
2177 {
2178 if( ssl->in_left < ssl->next_record_offset )
2179 {
2180 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2181 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2182 }
2183
2184 ssl->in_left -= ssl->next_record_offset;
2185
2186 if( ssl->in_left != 0 )
2187 {
2188 SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
2189 ssl->next_record_offset ) );
2190 memmove( ssl->in_hdr,
2191 ssl->in_hdr + ssl->next_record_offset,
2192 ssl->in_left );
2193 }
2194
2195 ssl->next_record_offset = 0;
2196 }
2197
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2199 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002200
2201 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002202 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002203 */
2204 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002205 {
2206 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002207 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002208 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002209
2210 /*
2211 * A record can't be split accross datagrams. If we need to read but
2212 * are not at the beginning of a new record, the caller did something
2213 * wrong.
2214 */
2215 if( ssl->in_left != 0 )
2216 {
2217 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2218 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2219 }
2220
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002221 SSL_DEBUG_MSG( 3, ( "current timer: %u", ssl->time_limit ) );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002222
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002223 /*
2224 * Don't even try to read if time's out already.
2225 * This avoids by-passing the timer when repeatedly receiving messages
2226 * that will end up being dropped.
2227 */
2228 if( ssl_check_timer( ssl ) != 0 )
2229 ret = POLARSSL_ERR_NET_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002230 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002231 {
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002232 len = SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
2233
2234 if( ssl->state != SSL_HANDSHAKE_OVER )
2235 timeout = ssl->handshake->retransmit_timeout;
2236 else
2237 timeout = ssl->read_timeout;
2238
2239 SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
2240
2241 if( ssl->f_recv_timeout != NULL && timeout != 0 )
2242 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2243 timeout );
2244 else
2245 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2246
2247 SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
2248
2249 if( ret == 0 )
2250 return( POLARSSL_ERR_SSL_CONN_EOF );
2251 }
2252
2253 if( ret == POLARSSL_ERR_NET_TIMEOUT )
2254 {
2255 SSL_DEBUG_MSG( 2, ( "timeout" ) );
2256 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002257
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002258 if( ssl->state != SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002259 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002260 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2261 {
2262 SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
2263 return( POLARSSL_ERR_NET_TIMEOUT );
2264 }
2265
2266 if( ( ret = ssl_resend( ssl ) ) != 0 )
2267 {
2268 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2269 return( ret );
2270 }
2271
2272 return( POLARSSL_ERR_NET_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002273 }
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002274#if defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002275 else if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00002276 ssl->renego_status == SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002277 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002278 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002279 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002280 SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002281 return( ret );
2282 }
2283
2284 return( POLARSSL_ERR_NET_WANT_READ );
2285 }
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002286#endif /* POLARSSL_SSL_SRV_C && POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002287 }
2288
Paul Bakker5121ce52009-01-03 21:22:43 +00002289 if( ret < 0 )
2290 return( ret );
2291
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002292 ssl->in_left = ret;
2293 }
2294 else
2295#endif
2296 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002297 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2298 ssl->in_left, nb_want ) );
2299
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002300 while( ssl->in_left < nb_want )
2301 {
2302 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002303 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr + ssl->in_left, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002304
2305 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2306 ssl->in_left, nb_want ) );
2307 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2308
2309 if( ret == 0 )
2310 return( POLARSSL_ERR_SSL_CONN_EOF );
2311
2312 if( ret < 0 )
2313 return( ret );
2314
2315 ssl->in_left += ret;
2316 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002317 }
2318
2319 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2320
2321 return( 0 );
2322}
2323
2324/*
2325 * Flush any data not yet written
2326 */
2327int ssl_flush_output( ssl_context *ssl )
2328{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002329 int ret;
2330 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002331
2332 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2333
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002334 if( ssl->f_send == NULL )
2335 {
2336 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2337 "or ssl_set_bio_timeout()" ) );
2338 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2339 }
2340
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002341 /* Avoid incrementing counter if data is flushed */
2342 if( ssl->out_left == 0 )
2343 {
2344 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2345 return( 0 );
2346 }
2347
Paul Bakker5121ce52009-01-03 21:22:43 +00002348 while( ssl->out_left > 0 )
2349 {
2350 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002351 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002352
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002353 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
2354 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002355 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002356
Paul Bakker5121ce52009-01-03 21:22:43 +00002357 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2358
2359 if( ret <= 0 )
2360 return( ret );
2361
2362 ssl->out_left -= ret;
2363 }
2364
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002365 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002366 if( ++ssl->out_ctr[i - 1] != 0 )
2367 break;
2368
2369 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002370 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002371 {
2372 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2373 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
2374 }
2375
Paul Bakker5121ce52009-01-03 21:22:43 +00002376 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2377
2378 return( 0 );
2379}
2380
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002381/*
2382 * Functions to handle the DTLS retransmission state machine
2383 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002384#if defined(POLARSSL_SSL_PROTO_DTLS)
2385/*
2386 * Append current handshake message to current outgoing flight
2387 */
2388static int ssl_flight_append( ssl_context *ssl )
2389{
2390 ssl_flight_item *msg;
2391
2392 /* Allocate space for current message */
2393 if( ( msg = polarssl_malloc( sizeof( ssl_flight_item ) ) ) == NULL )
2394 {
2395 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed",
2396 sizeof( ssl_flight_item ) ) );
2397 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2398 }
2399
2400 if( ( msg->p = polarssl_malloc( ssl->out_msglen ) ) == NULL )
2401 {
2402 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard6b875fc2014-10-17 14:02:33 +02002403 polarssl_free( msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002404 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2405 }
2406
2407 /* Copy current handshake message with headers */
2408 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2409 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002410 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002411 msg->next = NULL;
2412
2413 /* Append to the current flight */
2414 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002415 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002416 else
2417 {
2418 ssl_flight_item *cur = ssl->handshake->flight;
2419 while( cur->next != NULL )
2420 cur = cur->next;
2421 cur->next = msg;
2422 }
2423
2424 return( 0 );
2425}
2426
2427/*
2428 * Free the current flight of handshake messages
2429 */
2430static void ssl_flight_free( ssl_flight_item *flight )
2431{
2432 ssl_flight_item *cur = flight;
2433 ssl_flight_item *next;
2434
2435 while( cur != NULL )
2436 {
2437 next = cur->next;
2438
2439 polarssl_free( cur->p );
2440 polarssl_free( cur );
2441
2442 cur = next;
2443 }
2444}
2445
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002446#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2447static void ssl_dtls_replay_reset( ssl_context *ssl );
2448#endif
2449
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002450/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002451 * Swap transform_out and out_ctr with the alternative ones
2452 */
2453static void ssl_swap_epochs( ssl_context *ssl )
2454{
2455 ssl_transform *tmp_transform;
2456 unsigned char tmp_out_ctr[8];
2457
2458 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2459 {
2460 SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
2461 return;
2462 }
2463
2464 SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
2465
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002466 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002467 tmp_transform = ssl->transform_out;
2468 ssl->transform_out = ssl->handshake->alt_transform_out;
2469 ssl->handshake->alt_transform_out = tmp_transform;
2470
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002471 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002472 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2473 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2474 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002475
2476 /* Adjust to the newly activated transform */
2477 if( ssl->transform_out != NULL &&
2478 ssl->minor_ver >= SSL_MINOR_VERSION_2 )
2479 {
2480 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2481 ssl->transform_out->fixed_ivlen;
2482 }
2483 else
2484 ssl->out_msg = ssl->out_iv;
2485
2486#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2487 if( ssl_hw_record_activate != NULL )
2488 {
2489 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
2490 {
2491 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
2492 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
2493 }
2494 }
2495#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002496}
2497
2498/*
2499 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002500 *
2501 * Need to remember the current message in case flush_output returns
2502 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002503 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002504 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002505int ssl_resend( ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002506{
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002507 SSL_DEBUG_MSG( 2, ( "=> ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002508
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002509 if( ssl->handshake->retransmit_state != SSL_RETRANS_SENDING )
2510 {
2511 SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
2512
2513 ssl->handshake->cur_msg = ssl->handshake->flight;
2514 ssl_swap_epochs( ssl );
2515
2516 ssl->handshake->retransmit_state = SSL_RETRANS_SENDING;
2517 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002518
2519 while( ssl->handshake->cur_msg != NULL )
2520 {
2521 int ret;
2522 ssl_flight_item *cur = ssl->handshake->cur_msg;
2523
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002524 /* Swap epochs before sending Finished: we can't do it after
2525 * sending ChangeCipherSpec, in case write returns WANT_READ.
2526 * Must be done before copying, may change out_msg pointer */
2527 if( cur->type == SSL_MSG_HANDSHAKE &&
2528 cur->p[0] == SSL_HS_FINISHED )
2529 {
2530 ssl_swap_epochs( ssl );
2531 }
2532
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002533 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002534 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002535 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002536
2537 ssl->handshake->cur_msg = cur->next;
2538
2539 SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
2540
2541 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2542 {
2543 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2544 return( ret );
2545 }
2546 }
2547
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002548 if( ssl->state == SSL_HANDSHAKE_OVER )
2549 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2550 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002551 {
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002552 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002553 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2554 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002555
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002556 SSL_DEBUG_MSG( 2, ( "<= ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002557
2558 return( 0 );
2559}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002560
2561/*
2562 * To be called when the last message of an incoming flight is received.
2563 */
2564void ssl_recv_flight_completed( ssl_context *ssl )
2565{
2566 /* We won't need to resend that one any more */
2567 ssl_flight_free( ssl->handshake->flight );
2568 ssl->handshake->flight = NULL;
2569 ssl->handshake->cur_msg = NULL;
2570
2571 /* The next incoming flight will start with this msg_seq */
2572 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2573
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002574 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002575 ssl_set_timer( ssl, 0 );
2576
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002577 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2578 ssl->in_msg[0] == SSL_HS_FINISHED )
2579 {
2580 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2581 }
2582 else
2583 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
2584}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002585
2586/*
2587 * To be called when the last message of an outgoing flight is send.
2588 */
2589void ssl_send_flight_completed( ssl_context *ssl )
2590{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002591 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002592 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002593
2594 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2595 ssl->in_msg[0] == SSL_HS_FINISHED )
2596 {
2597 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2598 }
2599 else
2600 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
2601}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002602#endif /* POLARSSL_SSL_PROTO_DTLS */
2603
Paul Bakker5121ce52009-01-03 21:22:43 +00002604/*
2605 * Record layer functions
2606 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002607
2608/*
2609 * Write current record.
2610 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2611 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002612int ssl_write_record( ssl_context *ssl )
2613{
Paul Bakker05ef8352012-05-08 09:17:57 +00002614 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002615 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002616
2617 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2618
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002619#if defined(POLARSSL_SSL_PROTO_DTLS)
2620 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2621 ssl->handshake != NULL &&
2622 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2623 {
2624 ; /* Skip special handshake treatment when resending */
2625 }
2626 else
2627#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002628 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2629 {
2630 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2631 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2632 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2633
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002634 /*
2635 * DTLS has additional fields in the Handshake layer,
2636 * between the length field and the actual payload:
2637 * uint16 message_seq;
2638 * uint24 fragment_offset;
2639 * uint24 fragment_length;
2640 */
2641#if defined(POLARSSL_SSL_PROTO_DTLS)
2642 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2643 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002644 /* Make room for the additional DTLS fields */
2645 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002646 ssl->out_msglen += 8;
2647 len += 8;
2648
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002649 /* Write message_seq and update it, except for HelloRequest */
2650 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2651 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002652 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2653 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2654 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002655 }
2656 else
2657 {
2658 ssl->out_msg[4] = 0;
2659 ssl->out_msg[5] = 0;
2660 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002661
2662 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2663 memset( ssl->out_msg + 6, 0x00, 3 );
2664 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002665 }
2666#endif /* POLARSSL_SSL_PROTO_DTLS */
2667
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002668 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2669 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002670 }
2671
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002672 /* Save handshake and CCS messages for resending */
2673#if defined(POLARSSL_SSL_PROTO_DTLS)
2674 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2675 ssl->handshake != NULL &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02002676 ssl->handshake->retransmit_state != SSL_RETRANS_SENDING &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002677 ( ssl->out_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC ||
2678 ssl->out_msgtype == SSL_MSG_HANDSHAKE ) )
2679 {
2680 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2681 {
2682 SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
2683 return( ret );
2684 }
2685 }
2686#endif
2687
Paul Bakker2770fbd2012-07-03 13:30:23 +00002688#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002689 if( ssl->transform_out != NULL &&
2690 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002691 {
2692 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2693 {
2694 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2695 return( ret );
2696 }
2697
2698 len = ssl->out_msglen;
2699 }
2700#endif /*POLARSSL_ZLIB_SUPPORT */
2701
Paul Bakker05ef8352012-05-08 09:17:57 +00002702#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002703 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002705 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002706
Paul Bakker05ef8352012-05-08 09:17:57 +00002707 ret = ssl_hw_record_write( ssl );
2708 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2709 {
2710 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002711 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002712 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002713
2714 if( ret == 0 )
2715 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002716 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002717#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002718 if( !done )
2719 {
2720 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002721 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2722 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002723
2724 ssl->out_len[0] = (unsigned char)( len >> 8 );
2725 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002726
Paul Bakker48916f92012-09-16 19:57:18 +00002727 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002728 {
2729 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2730 {
2731 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2732 return( ret );
2733 }
2734
2735 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002736 ssl->out_len[0] = (unsigned char)( len >> 8 );
2737 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002738 }
2739
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002740 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002741
2742 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2743 "version = [%d:%d], msglen = %d",
2744 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002745 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002746
2747 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002748 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002749 }
2750
Paul Bakker5121ce52009-01-03 21:22:43 +00002751 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2752 {
2753 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2754 return( ret );
2755 }
2756
2757 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2758
2759 return( 0 );
2760}
2761
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002762#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002763/*
2764 * Mark bits in bitmask (used for DTLS HS reassembly)
2765 */
2766static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2767{
2768 unsigned int start_bits, end_bits;
2769
2770 start_bits = 8 - ( offset % 8 );
2771 if( start_bits != 8 )
2772 {
2773 size_t first_byte_idx = offset / 8;
2774
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002775 /* Special case */
2776 if( len <= start_bits )
2777 {
2778 for( ; len != 0; len-- )
2779 mask[first_byte_idx] |= 1 << ( start_bits - len );
2780
2781 /* Avoid potential issues with offset or len becoming invalid */
2782 return;
2783 }
2784
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002785 offset += start_bits; /* Now offset % 8 == 0 */
2786 len -= start_bits;
2787
2788 for( ; start_bits != 0; start_bits-- )
2789 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2790 }
2791
2792 end_bits = len % 8;
2793 if( end_bits != 0 )
2794 {
2795 size_t last_byte_idx = ( offset + len ) / 8;
2796
2797 len -= end_bits; /* Now len % 8 == 0 */
2798
2799 for( ; end_bits != 0; end_bits-- )
2800 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2801 }
2802
2803 memset( mask + offset / 8, 0xFF, len / 8 );
2804}
2805
2806/*
2807 * Check that bitmask is full
2808 */
2809static int ssl_bitmask_check( unsigned char *mask, size_t len )
2810{
2811 size_t i;
2812
2813 for( i = 0; i < len / 8; i++ )
2814 if( mask[i] != 0xFF )
2815 return( -1 );
2816
2817 for( i = 0; i < len % 8; i++ )
2818 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2819 return( -1 );
2820
2821 return( 0 );
2822}
2823
2824/*
2825 * Reassemble fragmented DTLS handshake messages.
2826 *
2827 * Use a temporary buffer for reassembly, divided in two parts:
2828 * - the first holds the reassembled message (including handshake header),
2829 * - the second holds a bitmask indicating which parts of the message
2830 * (excluding headers) have been received so far.
2831 */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002832static int ssl_reassemble_dtls_handshake( ssl_context *ssl )
2833{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002834 unsigned char *msg, *bitmask;
2835 size_t frag_len, frag_off;
2836 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
2837
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002838 if( ssl->handshake == NULL )
2839 {
2840 SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
2841 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2842 }
2843
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002844 /*
2845 * For first fragment, check size and allocate buffer
2846 */
2847 if( ssl->handshake->hs_msg == NULL )
2848 {
2849 size_t alloc_len;
2850
2851 SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
2852 msg_len ) );
2853
2854 if( ssl->in_hslen > SSL_MAX_CONTENT_LEN )
2855 {
2856 SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
2857 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2858 }
2859
2860 /* The bitmask needs one bit per byte of message excluding header */
2861 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
2862
2863 ssl->handshake->hs_msg = polarssl_malloc( alloc_len );
2864 if( ssl->handshake->hs_msg == NULL )
2865 {
2866 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", alloc_len ) );
2867 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2868 }
2869
2870 memset( ssl->handshake->hs_msg, 0, alloc_len );
2871
2872 /* Prepare final header: copy msg_type, length and message_seq,
2873 * then add standardised fragment_offset and fragment_length */
2874 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
2875 memset( ssl->handshake->hs_msg + 6, 0, 3 );
2876 memcpy( ssl->handshake->hs_msg + 9,
2877 ssl->handshake->hs_msg + 1, 3 );
2878 }
2879 else
2880 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002881 /* Make sure msg_type and length are consistent */
2882 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002883 {
2884 SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
2885 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2886 }
2887 }
2888
2889 msg = ssl->handshake->hs_msg + 12;
2890 bitmask = msg + msg_len;
2891
2892 /*
2893 * Check and copy current fragment
2894 */
2895 frag_off = ( ssl->in_msg[6] << 16 ) |
2896 ( ssl->in_msg[7] << 8 ) |
2897 ssl->in_msg[8];
2898 frag_len = ( ssl->in_msg[9] << 16 ) |
2899 ( ssl->in_msg[10] << 8 ) |
2900 ssl->in_msg[11];
2901
2902 if( frag_off + frag_len > msg_len )
2903 {
2904 SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
2905 frag_off, frag_len, msg_len ) );
2906 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2907 }
2908
2909 if( frag_len + 12 > ssl->in_msglen )
2910 {
2911 SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
2912 frag_len, ssl->in_msglen ) );
2913 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2914 }
2915
2916 SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
2917 frag_off, frag_len ) );
2918
2919 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
2920 ssl_bitmask_set( bitmask, frag_off, frag_len );
2921
2922 /*
2923 * Do we have the complete message by now?
2924 * If yes, finalize it, else ask to read the next record.
2925 */
2926 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
2927 {
2928 SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
2929 return( POLARSSL_ERR_NET_WANT_READ );
2930 }
2931
2932 SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
2933
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02002934 if( frag_len + 12 < ssl->in_msglen )
2935 {
2936 /*
2937 * We'got more handshake messages in the same record.
2938 * This case is not handled now because no know implementation does
2939 * that and it's hard to test, so we prefer to fail cleanly for now.
2940 */
2941 SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
2942 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2943 }
2944
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002945 if( ssl->in_left > ssl->next_record_offset )
2946 {
2947 /*
2948 * We've got more data in the buffer after the current record,
2949 * that we don't want to overwrite. Move it before writing the
2950 * reassembled message, and adjust in_left and next_record_offset.
2951 */
2952 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
2953 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
2954 size_t remain_len = ssl->in_left - ssl->next_record_offset;
2955
2956 /* First compute and check new lengths */
2957 ssl->next_record_offset = new_remain - ssl->in_hdr;
2958 ssl->in_left = ssl->next_record_offset + remain_len;
2959
2960 if( ssl->in_left > SSL_BUFFER_LEN -
2961 (size_t)( ssl->in_hdr - ssl->in_buf ) )
2962 {
2963 SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
2964 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
2965 }
2966
2967 memmove( new_remain, cur_remain, remain_len );
2968 }
2969
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002970 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
2971
2972 polarssl_free( ssl->handshake->hs_msg );
2973 ssl->handshake->hs_msg = NULL;
2974
2975 SSL_DEBUG_BUF( 3, "reassembled handshake message",
2976 ssl->in_msg, ssl->in_hslen );
2977
2978 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002979}
2980#endif /* POLARSSL_SSL_PROTO_DTLS */
2981
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002982static int ssl_prepare_handshake_record( ssl_context *ssl )
2983{
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002984 if( ssl->in_msglen < ssl_hs_hdr_len( ssl ) )
2985 {
2986 SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
2987 ssl->in_msglen ) );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02002988 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002989 }
2990
2991 ssl->in_hslen = ssl_hs_hdr_len( ssl ) + (
2992 ( ssl->in_msg[1] << 16 ) |
2993 ( ssl->in_msg[2] << 8 ) |
2994 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002995
2996 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2997 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002998 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002999
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003000#if defined(POLARSSL_SSL_PROTO_DTLS)
3001 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003002 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003003 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003004 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003005
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003006 /* ssl->handshake is NULL when receiving ClientHello for renego */
3007 if( ssl->handshake != NULL &&
3008 recv_msg_seq != ssl->handshake->in_msg_seq )
3009 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003010 /* Retransmit only on last message from previous flight, to avoid
3011 * too many retransmissions.
3012 * Besides, No sane server ever retransmits HelloVerifyRequest */
3013 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard93017de2014-09-19 22:42:40 +02003014 ssl->in_msg[0] != SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003015 {
3016 SSL_DEBUG_MSG( 2, ( "received message from last flight, "
3017 "message_seq = %d, start_of_flight = %d",
3018 recv_msg_seq,
3019 ssl->handshake->in_flight_start_seq ) );
3020
3021 if( ( ret = ssl_resend( ssl ) ) != 0 )
3022 {
3023 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3024 return( ret );
3025 }
3026 }
3027 else
3028 {
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02003029 SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003030 "message_seq = %d, expected = %d",
3031 recv_msg_seq,
3032 ssl->handshake->in_msg_seq ) );
3033 }
3034
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003035 return( POLARSSL_ERR_NET_WANT_READ );
3036 }
3037 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003038
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003039 /* Reassemble if current message is fragmented or reassembly is
3040 * already in progress */
3041 if( ssl->in_msglen < ssl->in_hslen ||
3042 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3043 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3044 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003045 {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003046 SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
3047
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003048 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3049 {
3050 SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
3051 return( ret );
3052 }
3053 }
3054 }
3055 else
3056#endif /* POLARSSL_SSL_PROTO_DTLS */
3057 /* With TLS we don't handle fragmentation (for now) */
3058 if( ssl->in_msglen < ssl->in_hslen )
3059 {
3060 SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02003061 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003062 }
3063
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003064 if( ssl->state != SSL_HANDSHAKE_OVER )
3065 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
3066
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003067 /* Handshake message is complete, increment counter */
3068#if defined(POLARSSL_SSL_PROTO_DTLS)
3069 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3070 ssl->handshake != NULL )
3071 {
3072 ssl->handshake->in_msg_seq++;
3073 }
3074#endif
3075
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003076 return( 0 );
3077}
3078
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003079/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003080 * DTLS anti-replay: RFC 6347 4.1.2.6
3081 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003082 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3083 * Bit n is set iff record number in_window_top - n has been seen.
3084 *
3085 * Usually, in_window_top is the last record number seen and the lsb of
3086 * in_window is set. The only exception is the initial state (record number 0
3087 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003088 */
3089#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3090static void ssl_dtls_replay_reset( ssl_context *ssl )
3091{
3092 ssl->in_window_top = 0;
3093 ssl->in_window = 0;
3094}
3095
3096static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3097{
3098 return( ( (uint64_t) buf[0] << 40 ) |
3099 ( (uint64_t) buf[1] << 32 ) |
3100 ( (uint64_t) buf[2] << 24 ) |
3101 ( (uint64_t) buf[3] << 16 ) |
3102 ( (uint64_t) buf[4] << 8 ) |
3103 ( (uint64_t) buf[5] ) );
3104}
3105
3106/*
3107 * Return 0 if sequence number is acceptable, -1 otherwise
3108 */
3109int ssl_dtls_replay_check( ssl_context *ssl )
3110{
3111 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3112 uint64_t bit;
3113
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003114 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
3115 return( 0 );
3116
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003117 if( rec_seqnum > ssl->in_window_top )
3118 return( 0 );
3119
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003120 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003121
3122 if( bit >= 64 )
3123 return( -1 );
3124
3125 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3126 return( -1 );
3127
3128 return( 0 );
3129}
3130
3131/*
3132 * Update replay window on new validated record
3133 */
3134void ssl_dtls_replay_update( ssl_context *ssl )
3135{
3136 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3137
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003138 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
3139 return;
3140
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003141 if( rec_seqnum > ssl->in_window_top )
3142 {
3143 /* Update window_top and the contents of the window */
3144 uint64_t shift = rec_seqnum - ssl->in_window_top;
3145
3146 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003147 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003148 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003149 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003150 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003151 ssl->in_window |= 1;
3152 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003153
3154 ssl->in_window_top = rec_seqnum;
3155 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003156 else
3157 {
3158 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003159 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003160
3161 if( bit < 64 ) /* Always true, but be extra sure */
3162 ssl->in_window |= (uint64_t) 1 << bit;
3163 }
3164}
3165#endif /* POLARSSL_SSL_DTLS_ANTI_REPLAY */
3166
3167/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003168 * ContentType type;
3169 * ProtocolVersion version;
3170 * uint16 epoch; // DTLS only
3171 * uint48 sequence_number; // DTLS only
3172 * uint16 length;
3173 */
3174static int ssl_parse_record_header( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003175{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003176 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003177 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003178
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003179 SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, ssl_hdr_len( ssl ) );
3180
Paul Bakker5121ce52009-01-03 21:22:43 +00003181 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003182 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003183 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003184
3185 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
3186 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003187 ssl->in_msgtype,
3188 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003189
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003190 /* Check record type */
3191 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
3192 ssl->in_msgtype != SSL_MSG_ALERT &&
3193 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
3194 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
3195 {
3196 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003197
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003198 if( ( ret = ssl_send_alert_message( ssl,
3199 SSL_ALERT_LEVEL_FATAL,
3200 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
3201 {
3202 return( ret );
3203 }
3204
3205 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3206 }
3207
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003208#if defined(POLARSSL_SSL_PROTO_DTLS)
3209 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3210 {
3211 /* Drop unexpected ChangeCipherSpec messages */
3212 if( ssl->in_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC &&
3213 ssl->state != SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3214 ssl->state != SSL_SERVER_CHANGE_CIPHER_SPEC )
3215 {
3216 SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3217 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3218 }
3219
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02003220 /* Drop unexpected ApplicationData records,
3221 * except at the beginning of renegotiations */
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003222 if( ssl->in_msgtype == SSL_MSG_APPLICATION_DATA &&
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00003223 ssl->state != SSL_HANDSHAKE_OVER
3224#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00003225 && ! ( ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS &&
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00003226 ssl->state == SSL_SERVER_HELLO )
3227#endif
3228 )
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003229 {
3230 SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
3231 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3232 }
3233 }
3234#endif
3235
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003236 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003237 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003238 {
3239 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003240 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003241 }
3242
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003243 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003244 {
3245 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003246 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003247 }
3248
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003249 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003250#if defined(POLARSSL_SSL_PROTO_DTLS)
3251 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3252 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003253 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003254
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003255 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003256 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003257 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003258 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003259 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003260 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003261 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003262
3263#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3264 if( ssl_dtls_replay_check( ssl ) != 0 )
3265 {
3266 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3267 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3268 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003269#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003270 }
3271#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003272
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003273 /* Check length against the size of our buffer */
3274 if( ssl->in_msglen > SSL_BUFFER_LEN
3275 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003276 {
3277 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3278 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3279 }
3280
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003281 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003282 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003283 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003284 if( ssl->in_msglen < 1 ||
3285 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003286 {
3287 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003288 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003289 }
3290 }
3291 else
3292 {
Paul Bakker48916f92012-09-16 19:57:18 +00003293 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003294 {
3295 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003296 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003297 }
3298
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003299#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003300 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00003301 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003302 {
3303 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003304 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003305 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003306#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003307#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3308 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003309 /*
3310 * TLS encrypted messages can have up to 256 bytes of padding
3311 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003312 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003313 ssl->in_msglen > ssl->transform_in->minlen +
3314 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003315 {
3316 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003317 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003318 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003319#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003320 }
3321
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003322 return( 0 );
3323}
Paul Bakker5121ce52009-01-03 21:22:43 +00003324
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003325/*
3326 * If applicable, decrypt (and decompress) record content
3327 */
3328static int ssl_prepare_record_content( ssl_context *ssl )
3329{
3330 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003331
Paul Bakker5121ce52009-01-03 21:22:43 +00003332 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003333 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003334
Paul Bakker05ef8352012-05-08 09:17:57 +00003335#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003336 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003337 {
3338 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
3339
3340 ret = ssl_hw_record_read( ssl );
3341 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
3342 {
3343 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003344 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003345 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003346
3347 if( ret == 0 )
3348 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003349 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003350#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003351 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003352 {
3353 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3354 {
3355 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
3356 return( ret );
3357 }
3358
3359 SSL_DEBUG_BUF( 4, "input payload after decrypt",
3360 ssl->in_msg, ssl->in_msglen );
3361
3362 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
3363 {
3364 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003365 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003366 }
3367 }
3368
Paul Bakker2770fbd2012-07-03 13:30:23 +00003369#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003370 if( ssl->transform_in != NULL &&
3371 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003372 {
3373 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3374 {
3375 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
3376 return( ret );
3377 }
3378
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003379 // TODO: what's the purpose of these lines? is in_len used?
3380 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3381 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003382 }
3383#endif /* POLARSSL_ZLIB_SUPPORT */
3384
Manuel Pégourié-Gonnard8464a462014-09-24 14:05:32 +02003385#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003386 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3387 {
3388 ssl_dtls_replay_update( ssl );
3389 }
3390#endif
3391
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003392 return( 0 );
3393}
3394
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003395static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
3396
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003397/*
3398 * Read a record.
3399 *
3400 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
3401 * and continue reading until a valid record is found.
3402 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003403int ssl_read_record( ssl_context *ssl )
3404{
3405 int ret;
3406
3407 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
3408
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02003409 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003410 {
3411 /*
3412 * Get next Handshake message in the current record
3413 */
3414 ssl->in_msglen -= ssl->in_hslen;
3415
3416 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3417 ssl->in_msglen );
3418
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003419 SSL_DEBUG_BUF( 4, "remaining content in record",
3420 ssl->in_msg, ssl->in_msglen );
3421
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003422 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3423 return( ret );
3424
3425 return( 0 );
3426 }
3427
3428 ssl->in_hslen = 0;
3429
3430 /*
3431 * Read the record header and parse it
3432 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003433#if defined(POLARSSL_SSL_PROTO_DTLS)
3434read_record_header:
3435#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003436 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3437 {
3438 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3439 return( ret );
3440 }
3441
3442 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003443 {
3444#if defined(POLARSSL_SSL_PROTO_DTLS)
3445 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3446 {
3447 /* Ignore bad record and get next one; drop the whole datagram
3448 * since current header cannot be trusted to find the next record
3449 * in current datagram */
3450 ssl->next_record_offset = 0;
3451 ssl->in_left = 0;
3452
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003453 SSL_DEBUG_MSG( 1, ( "discarding invalid record (header)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003454 goto read_record_header;
3455 }
3456#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003457 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003458 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003459
3460 /*
3461 * Read and optionally decrypt the message contents
3462 */
3463 if( ( ret = ssl_fetch_input( ssl,
3464 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3465 {
3466 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3467 return( ret );
3468 }
3469
3470 /* Done reading this record, get ready for the next one */
3471#if defined(POLARSSL_SSL_PROTO_DTLS)
3472 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3473 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3474 else
3475#endif
3476 ssl->in_left = 0;
3477
3478 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003479 {
3480#if defined(POLARSSL_SSL_PROTO_DTLS)
3481 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3482 {
3483 /* Silently discard invalid records */
3484 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3485 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3486 {
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02003487#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
3488 if( ssl->badmac_limit != 0 &&
3489 ++ssl->badmac_seen >= ssl->badmac_limit )
3490 {
3491 SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
3492 return( POLARSSL_ERR_SSL_INVALID_MAC );
3493 }
3494#endif
3495
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003496 SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003497 goto read_record_header;
3498 }
3499
3500 return( ret );
3501 }
3502 else
3503#endif
3504 {
3505 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard9b669902015-03-06 16:52:46 +00003506#if defined(POLARSSL_SSL_ALL_ALERT_MESSAGES)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003507 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3508 {
3509 ssl_send_alert_message( ssl,
3510 SSL_ALERT_LEVEL_FATAL,
3511 SSL_ALERT_MSG_BAD_RECORD_MAC );
3512 }
3513#endif
3514 return( ret );
3515 }
3516 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003517
3518 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003519 * When we sent the last flight of the handshake, we MUST respond to a
3520 * retransmit of the peer's previous flight with a retransmit. (In
3521 * practice, only the Finished message will make it, other messages
3522 * including CCS use the old transform so they're dropped as invalid.)
3523 *
3524 * If the record we received is not a handshake message, however, it
3525 * means the peer received our last flight so we can clean up
3526 * handshake info.
3527 *
3528 * This check needs to be done before prepare_handshake() due to an edge
3529 * case: if the client immediately requests renegotiation, this
3530 * finishes the current handshake first, avoiding the new ClientHello
3531 * being mistaken for an ancient message in the current handshake.
3532 */
3533#if defined(POLARSSL_SSL_PROTO_DTLS)
3534 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3535 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003536 ssl->state == SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003537 {
3538 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3539 ssl->in_msg[0] == SSL_HS_FINISHED )
3540 {
3541 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3542
3543 if( ( ret = ssl_resend( ssl ) ) != 0 )
3544 {
3545 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3546 return( ret );
3547 }
3548
3549 return( POLARSSL_ERR_NET_WANT_READ );
3550 }
3551 else
3552 {
3553 ssl_handshake_wrapup_free_hs_transform( ssl );
3554 }
3555 }
3556#endif
3557
3558 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003559 * Handle particular types of records
3560 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003561 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3562 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003563 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3564 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003565 }
3566
3567 if( ssl->in_msgtype == SSL_MSG_ALERT )
3568 {
3569 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3570 ssl->in_msg[0], ssl->in_msg[1] ) );
3571
3572 /*
3573 * Ignore non-fatal alerts, except close_notify
3574 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003575 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003576 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003577 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3578 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003579 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003580 }
3581
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003582 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3583 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003584 {
3585 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003586 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003587 }
3588 }
3589
Paul Bakker5121ce52009-01-03 21:22:43 +00003590 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3591
3592 return( 0 );
3593}
3594
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003595int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3596{
3597 int ret;
3598
3599 if( ( ret = ssl_send_alert_message( ssl,
3600 SSL_ALERT_LEVEL_FATAL,
3601 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3602 {
3603 return( ret );
3604 }
3605
3606 return( 0 );
3607}
3608
Paul Bakker0a925182012-04-16 06:46:41 +00003609int ssl_send_alert_message( ssl_context *ssl,
3610 unsigned char level,
3611 unsigned char message )
3612{
3613 int ret;
3614
3615 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3616
3617 ssl->out_msgtype = SSL_MSG_ALERT;
3618 ssl->out_msglen = 2;
3619 ssl->out_msg[0] = level;
3620 ssl->out_msg[1] = message;
3621
3622 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3623 {
3624 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3625 return( ret );
3626 }
3627
3628 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3629
3630 return( 0 );
3631}
3632
Paul Bakker5121ce52009-01-03 21:22:43 +00003633/*
3634 * Handshake functions
3635 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003636#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3637 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3638 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3639 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3640 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3641 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3642 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003643int ssl_write_certificate( ssl_context *ssl )
3644{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003645 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003646
3647 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3648
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003649 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003650 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3651 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003652 {
3653 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3654 ssl->state++;
3655 return( 0 );
3656 }
3657
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003658 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3659 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003660}
3661
3662int ssl_parse_certificate( ssl_context *ssl )
3663{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003664 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3665
3666 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3667
3668 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003669 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3670 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003671 {
3672 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3673 ssl->state++;
3674 return( 0 );
3675 }
3676
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003677 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3678 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003679}
3680#else
3681int ssl_write_certificate( ssl_context *ssl )
3682{
3683 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3684 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003685 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003686 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3687
3688 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3689
3690 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003691 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3692 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003693 {
3694 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3695 ssl->state++;
3696 return( 0 );
3697 }
3698
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003699#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003700 if( ssl->endpoint == SSL_IS_CLIENT )
3701 {
3702 if( ssl->client_auth == 0 )
3703 {
3704 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3705 ssl->state++;
3706 return( 0 );
3707 }
3708
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003709#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003710 /*
3711 * If using SSLv3 and got no cert, send an Alert message
3712 * (otherwise an empty Certificate message will be sent).
3713 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003714 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003715 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3716 {
3717 ssl->out_msglen = 2;
3718 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003719 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3720 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003721
3722 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3723 goto write_msg;
3724 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003725#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003726 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003727#endif /* POLARSSL_SSL_CLI_C */
3728#if defined(POLARSSL_SSL_SRV_C)
3729 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003730 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003731 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003732 {
3733 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003734 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003735 }
3736 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003737#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003738
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003739 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003740
3741 /*
3742 * 0 . 0 handshake type
3743 * 1 . 3 handshake length
3744 * 4 . 6 length of all certs
3745 * 7 . 9 length of cert. 1
3746 * 10 . n-1 peer certificate
3747 * n . n+2 length of cert. 2
3748 * n+3 . ... upper level cert, etc.
3749 */
3750 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003751 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003752
Paul Bakker29087132010-03-21 21:03:34 +00003753 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003754 {
3755 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003756 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003757 {
3758 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3759 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003760 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003761 }
3762
3763 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3764 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3765 ssl->out_msg[i + 2] = (unsigned char)( n );
3766
3767 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3768 i += n; crt = crt->next;
3769 }
3770
3771 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3772 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3773 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3774
3775 ssl->out_msglen = i;
3776 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3777 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3778
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003779#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003780write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003781#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003782
3783 ssl->state++;
3784
3785 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3786 {
3787 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3788 return( ret );
3789 }
3790
3791 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3792
Paul Bakkered27a042013-04-18 22:46:23 +02003793 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003794}
3795
3796int ssl_parse_certificate( ssl_context *ssl )
3797{
Paul Bakkered27a042013-04-18 22:46:23 +02003798 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003799 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003800 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003801
3802 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3803
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003804 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003805 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3806 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003807 {
3808 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3809 ssl->state++;
3810 return( 0 );
3811 }
3812
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003813#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003814 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003815 ( ssl->authmode == SSL_VERIFY_NONE ||
3816 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003817 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003818 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003819 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3820 ssl->state++;
3821 return( 0 );
3822 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003823#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003824
3825 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3826 {
3827 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3828 return( ret );
3829 }
3830
3831 ssl->state++;
3832
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003833#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003834#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003835 /*
3836 * Check if the client sent an empty certificate
3837 */
3838 if( ssl->endpoint == SSL_IS_SERVER &&
3839 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3840 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003841 if( ssl->in_msglen == 2 &&
3842 ssl->in_msgtype == SSL_MSG_ALERT &&
3843 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3844 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003845 {
3846 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3847
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003848 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003849 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3850 return( 0 );
3851 else
Paul Bakker40e46942009-01-03 21:51:57 +00003852 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003853 }
3854 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003855#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003856
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003857#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3858 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003859 if( ssl->endpoint == SSL_IS_SERVER &&
3860 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3861 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003862 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003863 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3864 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003865 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003866 {
3867 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3868
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003869 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003870 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003871 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003872 else
3873 return( 0 );
3874 }
3875 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003876#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3877 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003878#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003879
3880 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3881 {
3882 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003883 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003884 }
3885
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003886 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3887 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003888 {
3889 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003890 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003891 }
3892
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003893 i = ssl_hs_hdr_len( ssl );
3894
Paul Bakker5121ce52009-01-03 21:22:43 +00003895 /*
3896 * Same message structure as in ssl_write_certificate()
3897 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003898 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003899
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003900 if( ssl->in_msg[i] != 0 ||
3901 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003902 {
3903 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003904 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003905 }
3906
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003907 /* In case we tried to reuse a session but it failed */
3908 if( ssl->session_negotiate->peer_cert != NULL )
3909 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003910 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003911 polarssl_free( ssl->session_negotiate->peer_cert );
3912 }
3913
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003914 if( ( ssl->session_negotiate->peer_cert = polarssl_malloc(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003915 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003916 {
3917 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003918 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003919 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003920 }
3921
Paul Bakkerb6b09562013-09-18 14:17:41 +02003922 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003923
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003924 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003925
3926 while( i < ssl->in_hslen )
3927 {
3928 if( ssl->in_msg[i] != 0 )
3929 {
3930 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003931 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003932 }
3933
3934 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3935 | (unsigned int) ssl->in_msg[i + 2];
3936 i += 3;
3937
3938 if( n < 128 || i + n > ssl->in_hslen )
3939 {
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
Paul Bakkerddf26b42013-09-18 13:46:23 +02003944 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3945 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003946 if( ret != 0 )
3947 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003948 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003949 return( ret );
3950 }
3951
3952 i += n;
3953 }
3954
Paul Bakker48916f92012-09-16 19:57:18 +00003955 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003956
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003957 /*
3958 * On client, make sure the server cert doesn't change during renego to
3959 * avoid "triple handshake" attack: https://secure-resumption.com/
3960 */
Paul Bakkerf6080b82015-01-13 16:18:23 +01003961#if defined(POLARSSL_SSL_RENEGOTIATION) && defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003962 if( ssl->endpoint == SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00003963 ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003964 {
3965 if( ssl->session->peer_cert == NULL )
3966 {
3967 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3968 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3969 }
3970
3971 if( ssl->session->peer_cert->raw.len !=
3972 ssl->session_negotiate->peer_cert->raw.len ||
3973 memcmp( ssl->session->peer_cert->raw.p,
3974 ssl->session_negotiate->peer_cert->raw.p,
3975 ssl->session->peer_cert->raw.len ) != 0 )
3976 {
3977 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3978 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3979 }
3980 }
Paul Bakkerf6080b82015-01-13 16:18:23 +01003981#endif /* POLARSSL_SSL_RENEGOTIATION && POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003982
Paul Bakker5121ce52009-01-03 21:22:43 +00003983 if( ssl->authmode != SSL_VERIFY_NONE )
3984 {
3985 if( ssl->ca_chain == NULL )
3986 {
3987 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003988 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003989 }
3990
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003991 /*
3992 * Main check: verify certificate
3993 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003994 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3995 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3996 &ssl->session_negotiate->verify_result,
3997 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003998
3999 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004000 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004001 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004002 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004003
4004 /*
4005 * Secondary checks: always done, but change 'ret' only if it was 0
4006 */
4007
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004008#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004009 {
Manuel Pégourié-Gonnard0db107e2015-03-19 14:01:57 +00004010 const pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004011
4012 /* If certificate uses an EC key, make sure the curve is OK */
4013 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
4014 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
4015 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02004016 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004017 if( ret == 0 )
4018 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004019 }
4020 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004021#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00004022
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004023 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
4024 ciphersuite_info,
4025 ! ssl->endpoint ) != 0 )
4026 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02004027 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004028 if( ret == 0 )
4029 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
4030 }
4031
Paul Bakker5121ce52009-01-03 21:22:43 +00004032 if( ssl->authmode != SSL_VERIFY_REQUIRED )
4033 ret = 0;
4034 }
4035
4036 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
4037
4038 return( ret );
4039}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01004040#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
4041 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
4042 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
4043 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
4044 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
4045 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
4046 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004047
4048int ssl_write_change_cipher_spec( ssl_context *ssl )
4049{
4050 int ret;
4051
4052 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
4053
4054 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
4055 ssl->out_msglen = 1;
4056 ssl->out_msg[0] = 1;
4057
Paul Bakker5121ce52009-01-03 21:22:43 +00004058 ssl->state++;
4059
4060 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4061 {
4062 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4063 return( ret );
4064 }
4065
4066 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
4067
4068 return( 0 );
4069}
4070
4071int ssl_parse_change_cipher_spec( ssl_context *ssl )
4072{
4073 int ret;
4074
4075 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
4076
Paul Bakker5121ce52009-01-03 21:22:43 +00004077 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4078 {
4079 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4080 return( ret );
4081 }
4082
4083 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
4084 {
4085 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004086 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004087 }
4088
4089 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
4090 {
4091 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004092 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00004093 }
4094
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004095 /*
4096 * Switch to our negotiated transform and session parameters for inbound
4097 * data.
4098 */
4099 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
4100 ssl->transform_in = ssl->transform_negotiate;
4101 ssl->session_in = ssl->session_negotiate;
4102
4103#if defined(POLARSSL_SSL_PROTO_DTLS)
4104 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4105 {
4106#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4107 ssl_dtls_replay_reset( ssl );
4108#endif
4109
4110 /* Increment epoch */
4111 if( ++ssl->in_epoch == 0 )
4112 {
4113 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4114 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4115 }
4116 }
4117 else
4118#endif /* POLARSSL_SSL_PROTO_DTLS */
4119 memset( ssl->in_ctr, 0, 8 );
4120
4121 /*
4122 * Set the in_msg pointer to the correct location based on IV length
4123 */
4124 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4125 {
4126 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
4127 ssl->transform_negotiate->fixed_ivlen;
4128 }
4129 else
4130 ssl->in_msg = ssl->in_iv;
4131
4132#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4133 if( ssl_hw_record_activate != NULL )
4134 {
4135 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
4136 {
4137 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4138 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4139 }
4140 }
4141#endif
4142
Paul Bakker5121ce52009-01-03 21:22:43 +00004143 ssl->state++;
4144
4145 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
4146
4147 return( 0 );
4148}
4149
Paul Bakker41c83d32013-03-20 14:39:14 +01004150void ssl_optimize_checksum( ssl_context *ssl,
4151 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00004152{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02004153 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01004154
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004155#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4156 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00004157 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00004158 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00004159 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004160#endif
4161#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4162#if defined(POLARSSL_SHA512_C)
4163 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
4164 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
4165 else
4166#endif
4167#if defined(POLARSSL_SHA256_C)
4168 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00004169 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004170 else
4171#endif
4172#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004173 {
4174 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004175 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004176 }
Paul Bakker380da532012-04-18 16:10:25 +00004177}
Paul Bakkerf7abd422013-04-16 13:15:56 +02004178
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004179void ssl_reset_checksum( ssl_context *ssl )
4180{
4181#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4182 defined(POLARSSL_SSL_PROTO_TLS1_1)
4183 md5_starts( &ssl->handshake->fin_md5 );
4184 sha1_starts( &ssl->handshake->fin_sha1 );
4185#endif
4186#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4187#if defined(POLARSSL_SHA256_C)
4188 sha256_starts( &ssl->handshake->fin_sha256, 0 );
4189#endif
4190#if defined(POLARSSL_SHA512_C)
4191 sha512_starts( &ssl->handshake->fin_sha512, 1 );
4192#endif
4193#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4194}
4195
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004196static void ssl_update_checksum_start( ssl_context *ssl,
4197 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004198{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004199#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4200 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00004201 md5_update( &ssl->handshake->fin_md5 , buf, len );
4202 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004203#endif
4204#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4205#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02004206 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004207#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02004208#if defined(POLARSSL_SHA512_C)
4209 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01004210#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004211#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004212}
4213
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004214#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4215 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004216static void ssl_update_checksum_md5sha1( ssl_context *ssl,
4217 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004218{
Paul Bakker48916f92012-09-16 19:57:18 +00004219 md5_update( &ssl->handshake->fin_md5 , buf, len );
4220 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004221}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004222#endif
Paul Bakker380da532012-04-18 16:10:25 +00004223
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004224#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4225#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004226static void ssl_update_checksum_sha256( ssl_context *ssl,
4227 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004228{
Paul Bakker9e36f042013-06-30 14:34:05 +02004229 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004230}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004231#endif
Paul Bakker380da532012-04-18 16:10:25 +00004232
Paul Bakker9e36f042013-06-30 14:34:05 +02004233#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004234static void ssl_update_checksum_sha384( ssl_context *ssl,
4235 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004236{
Paul Bakker9e36f042013-06-30 14:34:05 +02004237 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004238}
Paul Bakker769075d2012-11-24 11:26:46 +01004239#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004240#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004241
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004242#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004243static void ssl_calc_finished_ssl(
4244 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004245{
Paul Bakker3c2122f2013-06-24 19:03:14 +02004246 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004247 md5_context md5;
4248 sha1_context sha1;
4249
Paul Bakker5121ce52009-01-03 21:22:43 +00004250 unsigned char padbuf[48];
4251 unsigned char md5sum[16];
4252 unsigned char sha1sum[20];
4253
Paul Bakker48916f92012-09-16 19:57:18 +00004254 ssl_session *session = ssl->session_negotiate;
4255 if( !session )
4256 session = ssl->session;
4257
Paul Bakker1ef83d62012-04-11 12:09:53 +00004258 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
4259
Paul Bakker48916f92012-09-16 19:57:18 +00004260 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4261 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004262
4263 /*
4264 * SSLv3:
4265 * hash =
4266 * MD5( master + pad2 +
4267 * MD5( handshake + sender + master + pad1 ) )
4268 * + SHA1( master + pad2 +
4269 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004270 */
4271
Paul Bakker90995b52013-06-24 19:20:35 +02004272#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004273 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004274 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004275#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004276
Paul Bakker90995b52013-06-24 19:20:35 +02004277#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004278 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004279 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004280#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004281
Paul Bakker3c2122f2013-06-24 19:03:14 +02004282 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
4283 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004284
Paul Bakker1ef83d62012-04-11 12:09:53 +00004285 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004286
Paul Bakker3c2122f2013-06-24 19:03:14 +02004287 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004288 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004289 md5_update( &md5, padbuf, 48 );
4290 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004291
Paul Bakker3c2122f2013-06-24 19:03:14 +02004292 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004293 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004294 sha1_update( &sha1, padbuf, 40 );
4295 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004296
Paul Bakker1ef83d62012-04-11 12:09:53 +00004297 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004298
Paul Bakker1ef83d62012-04-11 12:09:53 +00004299 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00004300 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004301 md5_update( &md5, padbuf, 48 );
4302 md5_update( &md5, md5sum, 16 );
4303 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004304
Paul Bakker1ef83d62012-04-11 12:09:53 +00004305 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00004306 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004307 sha1_update( &sha1, padbuf , 40 );
4308 sha1_update( &sha1, sha1sum, 20 );
4309 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004310
Paul Bakker1ef83d62012-04-11 12:09:53 +00004311 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004312
Paul Bakker5b4af392014-06-26 12:09:34 +02004313 md5_free( &md5 );
4314 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004315
Paul Bakker34617722014-06-13 17:20:13 +02004316 polarssl_zeroize( padbuf, sizeof( padbuf ) );
4317 polarssl_zeroize( md5sum, sizeof( md5sum ) );
4318 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004319
4320 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4321}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004322#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004323
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004324#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004325static void ssl_calc_finished_tls(
4326 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004327{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004328 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004329 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004330 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00004331 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004332 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004333
Paul Bakker48916f92012-09-16 19:57:18 +00004334 ssl_session *session = ssl->session_negotiate;
4335 if( !session )
4336 session = ssl->session;
4337
Paul Bakker1ef83d62012-04-11 12:09:53 +00004338 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004339
Paul Bakker48916f92012-09-16 19:57:18 +00004340 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4341 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004342
Paul Bakker1ef83d62012-04-11 12:09:53 +00004343 /*
4344 * TLSv1:
4345 * hash = PRF( master, finished_label,
4346 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4347 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004348
Paul Bakker90995b52013-06-24 19:20:35 +02004349#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004350 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4351 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004352#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004353
Paul Bakker90995b52013-06-24 19:20:35 +02004354#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004355 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4356 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004357#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004358
4359 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004360 ? "client finished"
4361 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004362
4363 md5_finish( &md5, padbuf );
4364 sha1_finish( &sha1, padbuf + 16 );
4365
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004366 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004367 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004368
4369 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4370
Paul Bakker5b4af392014-06-26 12:09:34 +02004371 md5_free( &md5 );
4372 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004373
Paul Bakker34617722014-06-13 17:20:13 +02004374 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004375
4376 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4377}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004378#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004379
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004380#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4381#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004382static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004383 ssl_context *ssl, unsigned char *buf, int from )
4384{
4385 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004386 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004387 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004388 unsigned char padbuf[32];
4389
Paul Bakker48916f92012-09-16 19:57:18 +00004390 ssl_session *session = ssl->session_negotiate;
4391 if( !session )
4392 session = ssl->session;
4393
Paul Bakker380da532012-04-18 16:10:25 +00004394 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004395
Paul Bakker9e36f042013-06-30 14:34:05 +02004396 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004397
4398 /*
4399 * TLSv1.2:
4400 * hash = PRF( master, finished_label,
4401 * Hash( handshake ) )[0.11]
4402 */
4403
Paul Bakker9e36f042013-06-30 14:34:05 +02004404#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004405 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004406 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004407#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004408
4409 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004410 ? "client finished"
4411 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004412
Paul Bakker9e36f042013-06-30 14:34:05 +02004413 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004414
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004415 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004416 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004417
4418 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4419
Paul Bakker5b4af392014-06-26 12:09:34 +02004420 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004421
Paul Bakker34617722014-06-13 17:20:13 +02004422 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004423
4424 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4425}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004426#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004427
Paul Bakker9e36f042013-06-30 14:34:05 +02004428#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004429static void ssl_calc_finished_tls_sha384(
4430 ssl_context *ssl, unsigned char *buf, int from )
4431{
4432 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004433 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004434 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004435 unsigned char padbuf[48];
4436
Paul Bakker48916f92012-09-16 19:57:18 +00004437 ssl_session *session = ssl->session_negotiate;
4438 if( !session )
4439 session = ssl->session;
4440
Paul Bakker380da532012-04-18 16:10:25 +00004441 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004442
Paul Bakker9e36f042013-06-30 14:34:05 +02004443 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004444
4445 /*
4446 * TLSv1.2:
4447 * hash = PRF( master, finished_label,
4448 * Hash( handshake ) )[0.11]
4449 */
4450
Paul Bakker9e36f042013-06-30 14:34:05 +02004451#if !defined(POLARSSL_SHA512_ALT)
4452 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4453 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004454#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004455
4456 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004457 ? "client finished"
4458 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004459
Paul Bakker9e36f042013-06-30 14:34:05 +02004460 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004461
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004462 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004463 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004464
4465 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4466
Paul Bakker5b4af392014-06-26 12:09:34 +02004467 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004468
Paul Bakker34617722014-06-13 17:20:13 +02004469 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004470
4471 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4472}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004473#endif /* POLARSSL_SHA512_C */
4474#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004475
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004476static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004477{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004478 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004479
4480 /*
4481 * Free our handshake params
4482 */
4483 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004484 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004485 ssl->handshake = NULL;
4486
4487 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004488 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004489 */
4490 if( ssl->transform )
4491 {
4492 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004493 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004494 }
4495 ssl->transform = ssl->transform_negotiate;
4496 ssl->transform_negotiate = NULL;
4497
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004498 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4499}
4500
4501void ssl_handshake_wrapup( ssl_context *ssl )
4502{
4503 int resume = ssl->handshake->resume;
4504
4505 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4506
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004507#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00004508 if( ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004509 {
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00004510 ssl->renego_status = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004511 ssl->renego_records_seen = 0;
4512 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004513#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004514
4515 /*
4516 * Free the previous session and switch in the current one
4517 */
Paul Bakker0a597072012-09-25 21:55:46 +00004518 if( ssl->session )
4519 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01004520#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4521 /* RFC 7366 3.1: keep the EtM state */
4522 ssl->session_negotiate->encrypt_then_mac =
4523 ssl->session->encrypt_then_mac;
4524#endif
4525
Paul Bakker0a597072012-09-25 21:55:46 +00004526 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004527 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004528 }
4529 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004530 ssl->session_negotiate = NULL;
4531
Paul Bakker0a597072012-09-25 21:55:46 +00004532 /*
4533 * Add cache entry
4534 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004535 if( ssl->f_set_cache != NULL &&
4536 ssl->session->length != 0 &&
4537 resume == 0 )
4538 {
Paul Bakker0a597072012-09-25 21:55:46 +00004539 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4540 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004541 }
Paul Bakker0a597072012-09-25 21:55:46 +00004542
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004543#if defined(POLARSSL_SSL_PROTO_DTLS)
4544 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4545 ssl->handshake->flight != NULL )
4546 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02004547 /* Cancel handshake timer */
4548 ssl_set_timer( ssl, 0 );
4549
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004550 /* Keep last flight around in case we need to resend it:
4551 * we need the handshake and transform structures for that */
4552 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4553 }
4554 else
4555#endif
4556 ssl_handshake_wrapup_free_hs_transform( ssl );
4557
Paul Bakker48916f92012-09-16 19:57:18 +00004558 ssl->state++;
4559
4560 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4561}
4562
Paul Bakker1ef83d62012-04-11 12:09:53 +00004563int ssl_write_finished( ssl_context *ssl )
4564{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004565 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004566
4567 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4568
Paul Bakker92be97b2013-01-02 17:30:03 +01004569 /*
4570 * Set the out_msg pointer to the correct location based on IV length
4571 */
4572 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4573 {
4574 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4575 ssl->transform_negotiate->fixed_ivlen;
4576 }
4577 else
4578 ssl->out_msg = ssl->out_iv;
4579
Paul Bakker48916f92012-09-16 19:57:18 +00004580 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004581
4582 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004583 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4584
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004585#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004586 ssl->verify_data_len = hash_len;
4587 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004588#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004589
Paul Bakker5121ce52009-01-03 21:22:43 +00004590 ssl->out_msglen = 4 + hash_len;
4591 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4592 ssl->out_msg[0] = SSL_HS_FINISHED;
4593
4594 /*
4595 * In case of session resuming, invert the client and server
4596 * ChangeCipherSpec messages order.
4597 */
Paul Bakker0a597072012-09-25 21:55:46 +00004598 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004599 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004600#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004601 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004602 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004603#endif
4604#if defined(POLARSSL_SSL_SRV_C)
4605 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004606 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004607#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004608 }
4609 else
4610 ssl->state++;
4611
Paul Bakker48916f92012-09-16 19:57:18 +00004612 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004613 * Switch to our negotiated transform and session parameters for outbound
4614 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004615 */
4616 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004617
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004618#if defined(POLARSSL_SSL_PROTO_DTLS)
4619 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4620 {
4621 unsigned char i;
4622
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004623 /* Remember current epoch settings for resending */
4624 ssl->handshake->alt_transform_out = ssl->transform_out;
4625 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4626
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004627 /* Set sequence_number to zero */
4628 memset( ssl->out_ctr + 2, 0, 6 );
4629
4630 /* Increment epoch */
4631 for( i = 2; i > 0; i-- )
4632 if( ++ssl->out_ctr[i - 1] != 0 )
4633 break;
4634
4635 /* The loop goes to its end iff the counter is wrapping */
4636 if( i == 0 )
4637 {
4638 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4639 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4640 }
4641 }
4642 else
4643#endif /* POLARSSL_SSL_PROTO_DTLS */
4644 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004645
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004646 ssl->transform_out = ssl->transform_negotiate;
4647 ssl->session_out = ssl->session_negotiate;
4648
Paul Bakker07eb38b2012-12-19 14:42:06 +01004649#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004650 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004651 {
4652 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4653 {
4654 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4655 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4656 }
4657 }
4658#endif
4659
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004660#if defined(POLARSSL_SSL_PROTO_DTLS)
4661 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4662 ssl_send_flight_completed( ssl );
4663#endif
4664
Paul Bakker5121ce52009-01-03 21:22:43 +00004665 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4666 {
4667 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4668 return( ret );
4669 }
4670
4671 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4672
4673 return( 0 );
4674}
4675
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004676#if defined(POLARSSL_SSL_PROTO_SSL3)
4677#define SSL_MAX_HASH_LEN 36
4678#else
4679#define SSL_MAX_HASH_LEN 12
4680#endif
4681
Paul Bakker5121ce52009-01-03 21:22:43 +00004682int ssl_parse_finished( ssl_context *ssl )
4683{
Paul Bakker23986e52011-04-24 08:57:21 +00004684 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004685 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004686 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004687
4688 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4689
Paul Bakker48916f92012-09-16 19:57:18 +00004690 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004691
Paul Bakker5121ce52009-01-03 21:22:43 +00004692 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4693 {
4694 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4695 return( ret );
4696 }
4697
4698 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4699 {
4700 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004701 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004702 }
4703
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004704 /* There is currently no ciphersuite using another length with TLS 1.2 */
4705#if defined(POLARSSL_SSL_PROTO_SSL3)
4706 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4707 hash_len = 36;
4708 else
4709#endif
4710 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004711
4712 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004713 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004714 {
4715 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004716 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004717 }
4718
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004719 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4720 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004721 {
4722 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004723 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004724 }
4725
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004726#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004727 ssl->verify_data_len = hash_len;
4728 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004729#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004730
Paul Bakker0a597072012-09-25 21:55:46 +00004731 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004732 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004733#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004734 if( ssl->endpoint == SSL_IS_CLIENT )
4735 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004736#endif
4737#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004738 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004739 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004740#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004741 }
4742 else
4743 ssl->state++;
4744
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004745#if defined(POLARSSL_SSL_PROTO_DTLS)
4746 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4747 ssl_recv_flight_completed( ssl );
4748#endif
4749
Paul Bakker5121ce52009-01-03 21:22:43 +00004750 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4751
4752 return( 0 );
4753}
4754
Paul Bakker968afaa2014-07-09 11:09:24 +02004755static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004756{
4757 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4758
4759#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4760 defined(POLARSSL_SSL_PROTO_TLS1_1)
4761 md5_init( &handshake->fin_md5 );
4762 sha1_init( &handshake->fin_sha1 );
4763 md5_starts( &handshake->fin_md5 );
4764 sha1_starts( &handshake->fin_sha1 );
4765#endif
4766#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4767#if defined(POLARSSL_SHA256_C)
4768 sha256_init( &handshake->fin_sha256 );
4769 sha256_starts( &handshake->fin_sha256, 0 );
4770#endif
4771#if defined(POLARSSL_SHA512_C)
4772 sha512_init( &handshake->fin_sha512 );
4773 sha512_starts( &handshake->fin_sha512, 1 );
4774#endif
4775#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4776
4777 handshake->update_checksum = ssl_update_checksum_start;
4778 handshake->sig_alg = SSL_HASH_SHA1;
4779
4780#if defined(POLARSSL_DHM_C)
4781 dhm_init( &handshake->dhm_ctx );
4782#endif
4783#if defined(POLARSSL_ECDH_C)
4784 ecdh_init( &handshake->ecdh_ctx );
4785#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004786}
4787
4788static void ssl_transform_init( ssl_transform *transform )
4789{
4790 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004791
4792 cipher_init( &transform->cipher_ctx_enc );
4793 cipher_init( &transform->cipher_ctx_dec );
4794
4795 md_init( &transform->md_ctx_enc );
4796 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004797}
4798
4799void ssl_session_init( ssl_session *session )
4800{
4801 memset( session, 0, sizeof(ssl_session) );
4802}
4803
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004804static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004805{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004806 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004807 if( ssl->transform_negotiate )
4808 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004809 if( ssl->session_negotiate )
4810 ssl_session_free( ssl->session_negotiate );
4811 if( ssl->handshake )
4812 ssl_handshake_free( ssl->handshake );
4813
4814 /*
4815 * Either the pointers are now NULL or cleared properly and can be freed.
4816 * Now allocate missing structures.
4817 */
4818 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004819 {
Mansour Moufid99b92592015-02-15 17:46:32 -05004820 ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004821 }
Paul Bakker48916f92012-09-16 19:57:18 +00004822
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004823 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004824 {
Mansour Moufid99b92592015-02-15 17:46:32 -05004825 ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004826 }
Paul Bakker48916f92012-09-16 19:57:18 +00004827
Paul Bakker82788fb2014-10-20 13:59:19 +02004828 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004829 {
Mansour Moufid99b92592015-02-15 17:46:32 -05004830 ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004831 }
Paul Bakker48916f92012-09-16 19:57:18 +00004832
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004833 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004834 if( ssl->handshake == NULL ||
4835 ssl->transform_negotiate == NULL ||
4836 ssl->session_negotiate == NULL )
4837 {
4838 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004839
4840 polarssl_free( ssl->handshake );
4841 polarssl_free( ssl->transform_negotiate );
4842 polarssl_free( ssl->session_negotiate );
4843
4844 ssl->handshake = NULL;
4845 ssl->transform_negotiate = NULL;
4846 ssl->session_negotiate = NULL;
4847
Paul Bakker48916f92012-09-16 19:57:18 +00004848 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4849 }
4850
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004851 /* Initialize structures */
4852 ssl_session_init( ssl->session_negotiate );
4853 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004854 ssl_handshake_params_init( ssl->handshake );
4855
4856#if defined(POLARSSL_X509_CRT_PARSE_C)
4857 ssl->handshake->key_cert = ssl->key_cert;
4858#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004859
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004860 /*
4861 * We may not know yet if we're using DTLS,
4862 * so always initiliase DTLS-specific fields.
4863 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004864#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004865 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004866
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004867 // TODO: not the right place, we may not know endpoint yet
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004868 if( ssl->endpoint == SSL_IS_CLIENT )
4869 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4870 else
4871 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004872#endif
4873
Paul Bakker48916f92012-09-16 19:57:18 +00004874 return( 0 );
4875}
4876
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004877#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4878/* Dummy cookie callbacks for defaults */
4879static int ssl_cookie_write_dummy( void *ctx,
4880 unsigned char **p, unsigned char *end,
4881 const unsigned char *cli_id, size_t cli_id_len )
4882{
4883 ((void) ctx);
4884 ((void) p);
4885 ((void) end);
4886 ((void) cli_id);
4887 ((void) cli_id_len);
4888
4889 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4890}
4891
4892static int ssl_cookie_check_dummy( void *ctx,
4893 const unsigned char *cookie, size_t cookie_len,
4894 const unsigned char *cli_id, size_t cli_id_len )
4895{
4896 ((void) ctx);
4897 ((void) cookie);
4898 ((void) cookie_len);
4899 ((void) cli_id);
4900 ((void) cli_id_len);
4901
4902 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4903}
4904#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4905
Paul Bakker5121ce52009-01-03 21:22:43 +00004906/*
4907 * Initialize an SSL context
4908 */
4909int ssl_init( ssl_context *ssl )
4910{
Paul Bakker48916f92012-09-16 19:57:18 +00004911 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004912 int len = SSL_BUFFER_LEN;
4913
4914 memset( ssl, 0, sizeof( ssl_context ) );
4915
Paul Bakker62f2dee2012-09-28 07:31:51 +00004916 /*
4917 * Sane defaults
4918 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004919 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4920 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4921 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4922 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004923
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004924 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004925
Manuel Pégourié-Gonnard849b1742015-03-20 19:13:22 +00004926 ssl_set_arc4_support( ssl, SSL_ARC4_DISABLED );
4927
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004928#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004929 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004930 memset( ssl->renego_period, 0xFF, 7 );
4931 ssl->renego_period[7] = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004932#endif
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004933
Paul Bakker62f2dee2012-09-28 07:31:51 +00004934#if defined(POLARSSL_DHM_C)
4935 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4936 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4937 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4938 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4939 {
4940 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4941 return( ret );
4942 }
4943#endif
4944
4945 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004946 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004947 */
Manuel Pégourié-Gonnard4e41c992015-02-18 10:39:49 +00004948 if( ( ssl->in_buf = polarssl_malloc( len ) ) == NULL ||
4949 ( ssl->out_buf = polarssl_malloc( len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004950 {
4951 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004952 polarssl_free( ssl->in_buf );
4953 ssl->in_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004954 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004955 }
4956
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004957 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4958 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004959
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004960 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4961 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4962
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004963#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4964 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
4965#endif
4966
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004967#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4968 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
4969#endif
4970
Paul Bakker606b4ba2013-08-14 16:52:14 +02004971#if defined(POLARSSL_SSL_SESSION_TICKETS)
4972 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4973#endif
4974
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004975#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004976 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004977#endif
4978
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004979#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4980 ssl->f_cookie_write = ssl_cookie_write_dummy;
4981 ssl->f_cookie_check = ssl_cookie_check_dummy;
4982#endif
4983
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004984#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4985 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
4986#endif
4987
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004988#if defined(POLARSSL_SSL_PROTO_DTLS)
4989 ssl->hs_timeout_min = SSL_DTLS_TIMEOUT_DFL_MIN;
4990 ssl->hs_timeout_max = SSL_DTLS_TIMEOUT_DFL_MAX;
4991#endif
4992
Paul Bakker48916f92012-09-16 19:57:18 +00004993 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4994 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004995
4996 return( 0 );
4997}
4998
4999/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00005000 * Reset an initialized and used SSL context for re-use while retaining
5001 * all application-set variables, function pointers and data.
5002 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005003int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00005004{
Paul Bakker48916f92012-09-16 19:57:18 +00005005 int ret;
5006
Paul Bakker7eb013f2011-10-06 12:37:39 +00005007 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005008
5009#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00005010 ssl->renego_status = SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005011 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00005012
5013 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard61860192014-11-04 13:05:42 +01005014 memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
5015 memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005016#endif
5017 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00005018
Paul Bakker7eb013f2011-10-06 12:37:39 +00005019 ssl->in_offt = NULL;
5020
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005021 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005022 ssl->in_msgtype = 0;
5023 ssl->in_msglen = 0;
5024 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005025#if defined(POLARSSL_SSL_PROTO_DTLS)
5026 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005027 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005028#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005029#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
5030 ssl_dtls_replay_reset( ssl );
5031#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005032
5033 ssl->in_hslen = 0;
5034 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005035 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005036
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005037 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005038 ssl->out_msgtype = 0;
5039 ssl->out_msglen = 0;
5040 ssl->out_left = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005041#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005042 if( ssl->split_done != SSL_CBC_RECORD_SPLITTING_DISABLED )
5043 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005044#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005045
Paul Bakker48916f92012-09-16 19:57:18 +00005046 ssl->transform_in = NULL;
5047 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005048
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005049 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
5050 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00005051
5052#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02005053 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005054 {
5055 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005056 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005057 {
5058 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
5059 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
5060 }
Paul Bakker05ef8352012-05-08 09:17:57 +00005061 }
5062#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00005063
Paul Bakker48916f92012-09-16 19:57:18 +00005064 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005065 {
Paul Bakker48916f92012-09-16 19:57:18 +00005066 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005067 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005068 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00005069 }
Paul Bakker48916f92012-09-16 19:57:18 +00005070
Paul Bakkerc0463502013-02-14 11:19:38 +01005071 if( ssl->session )
5072 {
5073 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005074 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005075 ssl->session = NULL;
5076 }
5077
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005078#if defined(POLARSSL_SSL_ALPN)
5079 ssl->alpn_chosen = NULL;
5080#endif
5081
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02005082#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005083 polarssl_free( ssl->cli_id );
5084 ssl->cli_id = NULL;
5085 ssl->cli_id_len = 0;
5086#endif
5087
Paul Bakker48916f92012-09-16 19:57:18 +00005088 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5089 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005090
5091 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00005092}
5093
Paul Bakkera503a632013-08-14 13:48:06 +02005094#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005095static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
5096{
5097 aes_free( &tkeys->enc );
5098 aes_free( &tkeys->dec );
5099
5100 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
5101}
5102
Paul Bakker7eb013f2011-10-06 12:37:39 +00005103/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005104 * Allocate and initialize ticket keys
5105 */
5106static int ssl_ticket_keys_init( ssl_context *ssl )
5107{
5108 int ret;
5109 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005110 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005111
5112 if( ssl->ticket_keys != NULL )
5113 return( 0 );
5114
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005115 tkeys = polarssl_malloc( sizeof(ssl_ticket_keys) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005116 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005117 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5118
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005119 aes_init( &tkeys->enc );
5120 aes_init( &tkeys->dec );
5121
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005122 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01005123 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005124 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005125 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005126 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005127 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005128
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02005129 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
5130 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
5131 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
5132 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005133 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005134 polarssl_free( tkeys );
5135 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02005136 }
5137
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005138 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01005139 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005140 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005141 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005142 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005143 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005144
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005145 ssl->ticket_keys = tkeys;
5146
5147 return( 0 );
5148}
Paul Bakkera503a632013-08-14 13:48:06 +02005149#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005150
5151/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005152 * SSL set accessors
5153 */
5154void ssl_set_endpoint( ssl_context *ssl, int endpoint )
5155{
5156 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005157
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005158#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
5159 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005160 if( endpoint == SSL_IS_CLIENT )
5161 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02005162#endif
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01005163
5164#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
5165 if( endpoint == SSL_IS_SERVER )
5166 ssl->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
5167#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005168}
5169
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005170int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005171{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005172#if defined(POLARSSL_SSL_PROTO_DTLS)
5173 if( transport == SSL_TRANSPORT_DATAGRAM )
5174 {
5175 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005176
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005177 ssl->out_hdr = ssl->out_buf;
5178 ssl->out_ctr = ssl->out_buf + 3;
5179 ssl->out_len = ssl->out_buf + 11;
5180 ssl->out_iv = ssl->out_buf + 13;
5181 ssl->out_msg = ssl->out_buf + 13;
5182
5183 ssl->in_hdr = ssl->in_buf;
5184 ssl->in_ctr = ssl->in_buf + 3;
5185 ssl->in_len = ssl->in_buf + 11;
5186 ssl->in_iv = ssl->in_buf + 13;
5187 ssl->in_msg = ssl->in_buf + 13;
5188
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005189 /* DTLS starts with TLS1.1 */
5190 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
5191 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005192
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005193 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
5194 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
5195
5196 return( 0 );
5197 }
5198#endif
5199
5200 if( transport == SSL_TRANSPORT_STREAM )
5201 {
5202 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005203
5204 ssl->out_ctr = ssl->out_buf;
5205 ssl->out_hdr = ssl->out_buf + 8;
5206 ssl->out_len = ssl->out_buf + 11;
5207 ssl->out_iv = ssl->out_buf + 13;
5208 ssl->out_msg = ssl->out_buf + 13;
5209
5210 ssl->in_ctr = ssl->in_buf;
5211 ssl->in_hdr = ssl->in_buf + 8;
5212 ssl->in_len = ssl->in_buf + 11;
5213 ssl->in_iv = ssl->in_buf + 13;
5214 ssl->in_msg = ssl->in_buf + 13;
5215
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005216 return( 0 );
5217 }
5218
5219 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005220}
5221
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005222#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
5223void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
5224{
5225 ssl->anti_replay = mode;
5226}
5227#endif
5228
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005229#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
5230void ssl_set_dtls_badmac_limit( ssl_context *ssl, unsigned limit )
5231{
5232 ssl->badmac_limit = limit;
5233}
5234#endif
5235
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02005236#if defined(POLARSSL_SSL_PROTO_DTLS)
5237void ssl_set_handshake_timeout( ssl_context *ssl, uint32_t min, uint32_t max )
5238{
5239 ssl->hs_timeout_min = min;
5240 ssl->hs_timeout_max = max;
5241}
5242#endif
5243
Paul Bakker5121ce52009-01-03 21:22:43 +00005244void ssl_set_authmode( ssl_context *ssl, int authmode )
5245{
5246 ssl->authmode = authmode;
5247}
5248
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005249#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005250void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005251 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005252 void *p_vrfy )
5253{
5254 ssl->f_vrfy = f_vrfy;
5255 ssl->p_vrfy = p_vrfy;
5256}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005257#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005258
Paul Bakker5121ce52009-01-03 21:22:43 +00005259void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00005260 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00005261 void *p_rng )
5262{
5263 ssl->f_rng = f_rng;
5264 ssl->p_rng = p_rng;
5265}
5266
5267void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00005268 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00005269 void *p_dbg )
5270{
5271 ssl->f_dbg = f_dbg;
5272 ssl->p_dbg = p_dbg;
5273}
5274
Manuel Pégourié-Gonnard9a65e802015-03-25 17:19:12 +01005275#if ! defined(POLARSSL_DEPRECATED_REMOVED)
Paul Bakker5121ce52009-01-03 21:22:43 +00005276void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00005277 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00005278 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00005279{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02005280 if( p_recv != p_send )
5281 {
5282 ssl->f_recv = NULL;
5283 ssl->f_send = NULL;
5284 ssl->p_bio = NULL;
5285 return;
5286 }
5287
Paul Bakker5121ce52009-01-03 21:22:43 +00005288 ssl->f_recv = f_recv;
5289 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02005290 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00005291}
Manuel Pégourié-Gonnard9a65e802015-03-25 17:19:12 +01005292#endif /* POLARSSL_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005293
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005294void ssl_set_bio_timeout( ssl_context *ssl,
5295 void *p_bio,
5296 int (*f_send)(void *, const unsigned char *, size_t),
5297 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +02005298 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t),
5299 uint32_t timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005300{
5301 ssl->p_bio = p_bio;
5302 ssl->f_send = f_send;
5303 ssl->f_recv = f_recv;
5304 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard27074302014-10-01 17:35:50 +02005305 ssl->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005306}
5307
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005308#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00005309void ssl_set_session_cache( ssl_context *ssl,
5310 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
5311 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00005312{
Paul Bakker0a597072012-09-25 21:55:46 +00005313 ssl->f_get_cache = f_get_cache;
5314 ssl->p_get_cache = p_get_cache;
5315 ssl->f_set_cache = f_set_cache;
5316 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00005317}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005318#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005319
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005320#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005321int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00005322{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005323 int ret;
5324
5325 if( ssl == NULL ||
5326 session == NULL ||
5327 ssl->session_negotiate == NULL ||
5328 ssl->endpoint != SSL_IS_CLIENT )
5329 {
5330 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5331 }
5332
5333 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5334 return( ret );
5335
Paul Bakker0a597072012-09-25 21:55:46 +00005336 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005337
5338 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005339}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005340#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005341
Paul Bakkerb68cad62012-08-23 08:34:18 +00005342void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005343{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005344 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
5345 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
5346 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
5347 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
5348}
5349
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005350void ssl_set_ciphersuites_for_version( ssl_context *ssl,
5351 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005352 int major, int minor )
5353{
5354 if( major != SSL_MAJOR_VERSION_3 )
5355 return;
5356
5357 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
5358 return;
5359
5360 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005361}
5362
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005363#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005364/* Add a new (empty) key_cert entry an return a pointer to it */
5365static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
5366{
5367 ssl_key_cert *key_cert, *last;
5368
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005369 key_cert = polarssl_malloc( sizeof(ssl_key_cert) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005370 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005371 return( NULL );
5372
5373 memset( key_cert, 0, sizeof( ssl_key_cert ) );
5374
5375 /* Append the new key_cert to the (possibly empty) current list */
5376 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005377 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005378 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01005379 if( ssl->handshake != NULL )
5380 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01005381 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005382 else
5383 {
5384 last = ssl->key_cert;
5385 while( last->next != NULL )
5386 last = last->next;
5387 last->next = key_cert;
5388 }
5389
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005390 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005391}
5392
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005393void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00005394 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00005395{
5396 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00005397 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005398 ssl->peer_cn = peer_cn;
5399}
5400
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005401int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005402 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00005403{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005404 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
5405
5406 if( key_cert == NULL )
5407 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5408
5409 key_cert->cert = own_cert;
5410 key_cert->key = pk_key;
5411
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00005412 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005413}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005414#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005415
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005416#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005417int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5418 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005419{
Paul Bakker6db455e2013-09-18 17:29:31 +02005420 if( psk == NULL || psk_identity == NULL )
5421 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5422
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005423 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005424 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5425
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00005426 if( ssl->psk != NULL || ssl->psk_identity != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02005427 {
5428 polarssl_free( ssl->psk );
5429 polarssl_free( ssl->psk_identity );
5430 }
5431
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00005432 if( ( ssl->psk = polarssl_malloc( psk_len ) ) == NULL ||
5433 ( ssl->psk_identity = polarssl_malloc( psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05005434 {
5435 polarssl_free( ssl->psk );
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00005436 ssl->psk = NULL;
Mansour Moufidf81088b2015-02-17 13:10:21 -05005437 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5438 }
Paul Bakker6db455e2013-09-18 17:29:31 +02005439
Paul Bakker6db455e2013-09-18 17:29:31 +02005440 ssl->psk_len = psk_len;
5441 ssl->psk_identity_len = psk_identity_len;
5442
Paul Bakker6db455e2013-09-18 17:29:31 +02005443 memcpy( ssl->psk, psk, ssl->psk_len );
5444 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
5445
5446 return( 0 );
5447}
5448
5449void ssl_set_psk_cb( ssl_context *ssl,
5450 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5451 size_t),
5452 void *p_psk )
5453{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005454 ssl->f_psk = f_psk;
5455 ssl->p_psk = p_psk;
Paul Bakker43b7e352011-01-18 15:27:19 +00005456}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005457#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005458
Paul Bakker48916f92012-09-16 19:57:18 +00005459#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005460int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
5461{
5462 int ret;
5463
Paul Bakker48916f92012-09-16 19:57:18 +00005464 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005465 {
5466 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5467 return( ret );
5468 }
5469
Paul Bakker48916f92012-09-16 19:57:18 +00005470 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005471 {
5472 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5473 return( ret );
5474 }
5475
5476 return( 0 );
5477}
5478
Paul Bakker1b57b062011-01-06 15:48:19 +00005479int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5480{
5481 int ret;
5482
Paul Bakker66d5d072014-06-17 16:39:18 +02005483 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005484 {
5485 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5486 return( ret );
5487 }
5488
Paul Bakker66d5d072014-06-17 16:39:18 +02005489 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005490 {
5491 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5492 return( ret );
5493 }
5494
5495 return( 0 );
5496}
Paul Bakker48916f92012-09-16 19:57:18 +00005497#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005498
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005499#if defined(POLARSSL_SSL_SET_CURVES)
5500/*
5501 * Set the allowed elliptic curves
5502 */
5503void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5504{
5505 ssl->curve_list = curve_list;
5506}
5507#endif
5508
Paul Bakker0be444a2013-08-27 21:55:01 +02005509#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005510int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005511{
5512 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005513 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005514
5515 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005516
5517 if( ssl->hostname_len + 1 == 0 )
5518 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5519
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005520 ssl->hostname = polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005521
Paul Bakkerb15b8512012-01-13 13:44:06 +00005522 if( ssl->hostname == NULL )
5523 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5524
Paul Bakker3c2122f2013-06-24 19:03:14 +02005525 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005526 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005527
Paul Bakker40ea7de2009-05-03 10:18:48 +00005528 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005529
5530 return( 0 );
5531}
5532
Paul Bakker5701cdc2012-09-27 21:49:42 +00005533void ssl_set_sni( ssl_context *ssl,
5534 int (*f_sni)(void *, ssl_context *,
5535 const unsigned char *, size_t),
5536 void *p_sni )
5537{
5538 ssl->f_sni = f_sni;
5539 ssl->p_sni = p_sni;
5540}
Paul Bakker0be444a2013-08-27 21:55:01 +02005541#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005542
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005543#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005544int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005545{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005546 size_t cur_len, tot_len;
5547 const char **p;
5548
5549 /*
5550 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5551 * truncated". Check lengths now rather than later.
5552 */
5553 tot_len = 0;
5554 for( p = protos; *p != NULL; p++ )
5555 {
5556 cur_len = strlen( *p );
5557 tot_len += cur_len;
5558
5559 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5560 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5561 }
5562
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005563 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005564
5565 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005566}
5567
5568const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5569{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005570 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005571}
5572#endif /* POLARSSL_SSL_ALPN */
5573
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005574static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005575{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005576 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005577 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005578 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005579 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005580 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005581
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005582#if defined(POLARSSL_SSL_PROTO_DTLS)
5583 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5584 minor < SSL_MINOR_VERSION_2 )
5585 {
5586 return( -1 );
5587 }
5588#else
5589 ((void) ssl);
5590#endif
5591
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005592 return( 0 );
5593}
5594
5595int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5596{
5597 if( ssl_check_version( ssl, major, minor ) != 0 )
5598 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5599
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005600 ssl->max_major_ver = major;
5601 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005602
5603 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005604}
5605
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005606int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005607{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005608 if( ssl_check_version( ssl, major, minor ) != 0 )
5609 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005610
5611 ssl->min_major_ver = major;
5612 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005613
5614 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005615}
5616
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02005617#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
5618void ssl_set_fallback( ssl_context *ssl, char fallback )
5619{
5620 ssl->fallback = fallback;
5621}
5622#endif
5623
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01005624#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
5625void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
5626{
5627 ssl->encrypt_then_mac = etm;
5628}
5629#endif
5630
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02005631#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
5632void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
5633{
5634 ssl->extended_ms = ems;
5635}
5636#endif
5637
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01005638void ssl_set_arc4_support( ssl_context *ssl, char arc4 )
5639{
5640 ssl->arc4_disabled = arc4;
5641}
5642
Paul Bakker05decb22013-08-15 13:33:48 +02005643#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005644int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5645{
Paul Bakker77e257e2013-12-16 15:29:52 +01005646 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005647 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005648 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005649 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005650 }
5651
5652 ssl->mfl_code = mfl_code;
5653
5654 return( 0 );
5655}
Paul Bakker05decb22013-08-15 13:33:48 +02005656#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005657
Paul Bakker1f2bc622013-08-15 13:45:55 +02005658#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005659int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005660{
Paul Bakker8c1ede62013-07-19 14:14:37 +02005661 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005662
5663 return( 0 );
5664}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005665#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005666
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005667#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
5668void ssl_set_cbc_record_splitting( ssl_context *ssl, char split )
5669{
5670 ssl->split_done = split;
5671}
5672#endif
5673
Paul Bakker48916f92012-09-16 19:57:18 +00005674void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5675{
5676 ssl->allow_legacy_renegotiation = allow_legacy;
5677}
5678
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005679#if defined(POLARSSL_SSL_RENEGOTIATION)
5680void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5681{
5682 ssl->disable_renegotiation = renegotiation;
5683}
5684
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005685void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5686{
5687 ssl->renego_max_records = max_records;
5688}
5689
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01005690void ssl_set_renegotiation_period( ssl_context *ssl,
5691 const unsigned char period[8] )
5692{
5693 memcpy( ssl->renego_period, period, 8 );
5694}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005695#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00005696
Paul Bakkera503a632013-08-14 13:48:06 +02005697#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005698int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5699{
5700 ssl->session_tickets = use_tickets;
5701
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005702#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005703 if( ssl->endpoint == SSL_IS_CLIENT )
5704 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005705#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005706
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01005707 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
5708 return( 0 );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005709
5710 if( ssl->f_rng == NULL )
5711 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5712
5713 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005714}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005715
5716void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5717{
5718 ssl->ticket_lifetime = lifetime;
5719}
Paul Bakkera503a632013-08-14 13:48:06 +02005720#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005721
Paul Bakker5121ce52009-01-03 21:22:43 +00005722/*
5723 * SSL get accessors
5724 */
5725size_t ssl_get_bytes_avail( const ssl_context *ssl )
5726{
5727 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5728}
5729
Paul Bakkerff60ee62010-03-16 21:09:09 +00005730int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005731{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00005732 if( ssl->session != NULL )
5733 return( ssl->session->verify_result );
5734
5735 if( ssl->session_negotiate != NULL )
5736 return( ssl->session_negotiate->verify_result );
5737
5738 return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005739}
5740
Paul Bakkere3166ce2011-01-27 17:40:50 +00005741const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005742{
Paul Bakker926c8e42013-03-06 10:23:34 +01005743 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005744 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005745
Paul Bakkere3166ce2011-01-27 17:40:50 +00005746 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005747}
5748
Paul Bakker43ca69c2011-01-15 17:35:19 +00005749const char *ssl_get_version( const ssl_context *ssl )
5750{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005751#if defined(POLARSSL_SSL_PROTO_DTLS)
5752 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5753 {
5754 switch( ssl->minor_ver )
5755 {
5756 case SSL_MINOR_VERSION_2:
5757 return( "DTLSv1.0" );
5758
5759 case SSL_MINOR_VERSION_3:
5760 return( "DTLSv1.2" );
5761
5762 default:
5763 return( "unknown (DTLS)" );
5764 }
5765 }
5766#endif
5767
Paul Bakker43ca69c2011-01-15 17:35:19 +00005768 switch( ssl->minor_ver )
5769 {
5770 case SSL_MINOR_VERSION_0:
5771 return( "SSLv3.0" );
5772
5773 case SSL_MINOR_VERSION_1:
5774 return( "TLSv1.0" );
5775
5776 case SSL_MINOR_VERSION_2:
5777 return( "TLSv1.1" );
5778
Paul Bakker1ef83d62012-04-11 12:09:53 +00005779 case SSL_MINOR_VERSION_3:
5780 return( "TLSv1.2" );
5781
Paul Bakker43ca69c2011-01-15 17:35:19 +00005782 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005783 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005784 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005785}
5786
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005787int ssl_get_record_expansion( const ssl_context *ssl )
5788{
5789 int transform_expansion;
5790 const ssl_transform *transform = ssl->transform_out;
5791
5792#if defined(POLARSSL_ZLIB_SUPPORT)
5793 if( ssl->session_out->compression != SSL_COMPRESS_NULL )
5794 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
5795#endif
5796
5797 if( transform == NULL )
5798 return( ssl_hdr_len( ssl ) );
5799
5800 switch( cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
5801 {
5802 case POLARSSL_MODE_GCM:
5803 case POLARSSL_MODE_CCM:
5804 case POLARSSL_MODE_STREAM:
5805 transform_expansion = transform->minlen;
5806 break;
5807
5808 case POLARSSL_MODE_CBC:
5809 transform_expansion = transform->maclen
5810 + cipher_get_block_size( &transform->cipher_ctx_enc );
5811 break;
5812
5813 default:
5814 SSL_DEBUG_MSG( 0, ( "should never happen" ) );
5815 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
5816 }
5817
5818 return( ssl_hdr_len( ssl ) + transform_expansion );
5819}
5820
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005821#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005822const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005823{
5824 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005825 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005826
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005827 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005828}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005829#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005830
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005831#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005832int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5833{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005834 if( ssl == NULL ||
5835 dst == NULL ||
5836 ssl->session == NULL ||
5837 ssl->endpoint != SSL_IS_CLIENT )
5838 {
5839 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5840 }
5841
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005842 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005843}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005844#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005845
Paul Bakker5121ce52009-01-03 21:22:43 +00005846/*
Paul Bakker1961b702013-01-25 14:49:24 +01005847 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005848 */
Paul Bakker1961b702013-01-25 14:49:24 +01005849int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005850{
Paul Bakker40e46942009-01-03 21:51:57 +00005851 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005852
Paul Bakker40e46942009-01-03 21:51:57 +00005853#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005854 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005855 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005856#endif
Paul Bakker40e46942009-01-03 21:51:57 +00005857#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005858 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005859 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005860#endif
5861
Paul Bakker1961b702013-01-25 14:49:24 +01005862 return( ret );
5863}
5864
5865/*
5866 * Perform the SSL handshake
5867 */
5868int ssl_handshake( ssl_context *ssl )
5869{
5870 int ret = 0;
5871
5872 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5873
5874 while( ssl->state != SSL_HANDSHAKE_OVER )
5875 {
5876 ret = ssl_handshake_step( ssl );
5877
5878 if( ret != 0 )
5879 break;
5880 }
5881
Paul Bakker5121ce52009-01-03 21:22:43 +00005882 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5883
5884 return( ret );
5885}
5886
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005887#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005888#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005889/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005890 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005891 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005892static int ssl_write_hello_request( ssl_context *ssl )
5893{
5894 int ret;
5895
5896 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5897
5898 ssl->out_msglen = 4;
5899 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5900 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5901
5902 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5903 {
5904 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5905 return( ret );
5906 }
5907
5908 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5909
5910 return( 0 );
5911}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005912#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005913
5914/*
5915 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005916 * - any side: calling ssl_renegotiate(),
5917 * - client: receiving a HelloRequest during ssl_read(),
5918 * - server: receiving any handshake message on server during ssl_read() after
5919 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005920 * If the handshake doesn't complete due to waiting for I/O, it will continue
5921 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005922 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005923static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005924{
5925 int ret;
5926
5927 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5928
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005929 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5930 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005931
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005932 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5933 * the ServerHello will have message_seq = 1" */
5934#if defined(POLARSSL_SSL_PROTO_DTLS)
5935 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00005936 ssl->renego_status == SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005937 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005938 if( ssl->endpoint == SSL_IS_SERVER )
5939 ssl->handshake->out_msg_seq = 1;
5940 else
5941 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005942 }
5943#endif
5944
Paul Bakker48916f92012-09-16 19:57:18 +00005945 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00005946 ssl->renego_status = SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00005947
Paul Bakker48916f92012-09-16 19:57:18 +00005948 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5949 {
5950 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5951 return( ret );
5952 }
5953
5954 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5955
5956 return( 0 );
5957}
5958
5959/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005960 * Renegotiate current connection on client,
5961 * or request renegotiation on server
5962 */
5963int ssl_renegotiate( ssl_context *ssl )
5964{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005965 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005966
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005967#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005968 /* On server, just send the request */
5969 if( ssl->endpoint == SSL_IS_SERVER )
5970 {
5971 if( ssl->state != SSL_HANDSHAKE_OVER )
5972 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5973
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00005974 ssl->renego_status = SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005975
5976 /* Did we already try/start sending HelloRequest? */
5977 if( ssl->out_left != 0 )
5978 return( ssl_flush_output( ssl ) );
5979
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005980 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005981 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005982#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005983
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005984#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005985 /*
5986 * On client, either start the renegotiation process or,
5987 * if already in progress, continue the handshake
5988 */
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00005989 if( ssl->renego_status != SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005990 {
5991 if( ssl->state != SSL_HANDSHAKE_OVER )
5992 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5993
5994 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
5995 {
5996 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
5997 return( ret );
5998 }
5999 }
6000 else
6001 {
6002 if( ( ret = ssl_handshake( ssl ) ) != 0 )
6003 {
6004 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6005 return( ret );
6006 }
6007 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006008#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006009
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006010 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006011}
6012
6013/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006014 * Check record counters and renegotiate if they're above the limit.
6015 */
6016static int ssl_check_ctr_renegotiate( ssl_context *ssl )
6017{
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006018 if( ssl->state != SSL_HANDSHAKE_OVER ||
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00006019 ssl->renego_status == SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006020 ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED )
6021 {
6022 return( 0 );
6023 }
6024
6025 // TODO: adapt for DTLS
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006026 if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 &&
6027 memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006028 {
6029 return( 0 );
6030 }
6031
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006032 SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006033 return( ssl_renegotiate( ssl ) );
6034}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006035#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006036
6037/*
6038 * Receive application data decrypted from the SSL layer
6039 */
Paul Bakker23986e52011-04-24 08:57:21 +00006040int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006041{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006042 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00006043 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00006044
6045 SSL_DEBUG_MSG( 2, ( "=> read" ) );
6046
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006047#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006048 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006049 {
6050 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
6051 return( ret );
6052
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006053 if( ssl->handshake != NULL &&
6054 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
6055 {
6056 if( ( ret = ssl_resend( ssl ) ) != 0 )
6057 return( ret );
6058 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006059 }
6060#endif
6061
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006062#if defined(POLARSSL_SSL_RENEGOTIATION)
6063 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
6064 {
6065 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
6066 return( ret );
6067 }
6068#endif
6069
Paul Bakker5121ce52009-01-03 21:22:43 +00006070 if( ssl->state != SSL_HANDSHAKE_OVER )
6071 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006072 ret = ssl_handshake( ssl );
6073 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
6074 {
6075 record_read = 1;
6076 }
6077 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006078 {
6079 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6080 return( ret );
6081 }
6082 }
6083
6084 if( ssl->in_offt == NULL )
6085 {
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02006086#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006087 /* Start timer if not already running */
6088 if( ssl->time_limit == 0 )
6089 ssl_set_timer( ssl, ssl->read_timeout );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02006090#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006091
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006092 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00006093 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006094 if( ( ret = ssl_read_record( ssl ) ) != 0 )
6095 {
6096 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
6097 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00006098
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006099 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
6100 return( ret );
6101 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006102 }
6103
6104 if( ssl->in_msglen == 0 &&
6105 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
6106 {
6107 /*
6108 * OpenSSL sends empty messages to randomize the IV
6109 */
6110 if( ( ret = ssl_read_record( ssl ) ) != 0 )
6111 {
Paul Bakker831a7552011-05-18 13:32:51 +00006112 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
6113 return( 0 );
6114
Paul Bakker5121ce52009-01-03 21:22:43 +00006115 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
6116 return( ret );
6117 }
6118 }
6119
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006120#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006121 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
6122 {
6123 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
6124
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006125#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006126 if( ssl->endpoint == SSL_IS_CLIENT &&
6127 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00006128 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006129 {
6130 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006131
6132 /* With DTLS, drop the packet (probably from last handshake) */
6133#if defined(POLARSSL_SSL_PROTO_DTLS)
6134 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6135 return( POLARSSL_ERR_NET_WANT_READ );
6136#endif
6137 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6138 }
6139
6140 if( ssl->endpoint == SSL_IS_SERVER &&
6141 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
6142 {
6143 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
6144
6145 /* With DTLS, drop the packet (probably from last handshake) */
6146#if defined(POLARSSL_SSL_PROTO_DTLS)
6147 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6148 return( POLARSSL_ERR_NET_WANT_READ );
6149#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006150 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6151 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006152#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006153
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006154 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
6155 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006156 ssl->allow_legacy_renegotiation ==
6157 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006158 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006159 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006160
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006161#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006162 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006163 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006164 /*
6165 * SSLv3 does not have a "no_renegotiation" alert
6166 */
6167 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
6168 return( ret );
6169 }
6170 else
Paul Bakker9af723c2014-05-01 13:03:14 +02006171#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006172#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
6173 defined(POLARSSL_SSL_PROTO_TLS1_2)
6174 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006175 {
6176 if( ( ret = ssl_send_alert_message( ssl,
6177 SSL_ALERT_LEVEL_WARNING,
6178 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
6179 {
6180 return( ret );
6181 }
Paul Bakker48916f92012-09-16 19:57:18 +00006182 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006183 else
Paul Bakker9af723c2014-05-01 13:03:14 +02006184#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
6185 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02006186 {
6187 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006188 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02006189 }
Paul Bakker48916f92012-09-16 19:57:18 +00006190 }
6191 else
6192 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006193 /* DTLS clients need to know renego is server-initiated */
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02006194#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006195 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
6196 ssl->endpoint == SSL_IS_CLIENT )
6197 {
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00006198 ssl->renego_status = SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006199 }
6200#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006201 ret = ssl_start_renegotiation( ssl );
6202 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
6203 {
6204 record_read = 1;
6205 }
6206 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006207 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006208 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00006209 return( ret );
6210 }
Paul Bakker48916f92012-09-16 19:57:18 +00006211 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006212
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006213 /* If a non-handshake record was read during renego, fallthrough,
6214 * else tell the user they should call ssl_read() again */
6215 if( ! record_read )
6216 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00006217 }
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00006218 else if( ssl->renego_status == SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006219 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006220
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006221 if( ssl->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006222 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006223 if( ++ssl->renego_records_seen > ssl->renego_max_records )
6224 {
6225 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
6226 "but not honored by client" ) );
6227 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6228 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006229 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006230 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006231#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006232
6233 /* Fatal and closure alerts handled by ssl_read_record() */
6234 if( ssl->in_msgtype == SSL_MSG_ALERT )
6235 {
6236 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
6237 return( POLARSSL_ERR_NET_WANT_READ );
6238 }
6239
6240 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00006241 {
6242 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00006243 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006244 }
6245
6246 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006247
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02006248#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02006249 /* We're going to return something now, cancel timer,
6250 * except if handshake (renegotiation) is in progress */
6251 if( ssl->state == SSL_HANDSHAKE_OVER )
6252 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006253
6254 /* If we requested renego but received AppData, resend HelloRequest.
6255 * Do it now, after setting in_offt, to avoid taking this branch
6256 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00006257#if defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006258 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00006259 ssl->renego_status == SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006260 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006261 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006262 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006263 SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006264 return( ret );
6265 }
6266 }
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00006267#endif /* POLARSSL_SSL_SRV_C && POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02006268#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006269 }
6270
6271 n = ( len < ssl->in_msglen )
6272 ? len : ssl->in_msglen;
6273
6274 memcpy( buf, ssl->in_offt, n );
6275 ssl->in_msglen -= n;
6276
6277 if( ssl->in_msglen == 0 )
6278 /* all bytes consumed */
6279 ssl->in_offt = NULL;
6280 else
6281 /* more data available */
6282 ssl->in_offt += n;
6283
6284 SSL_DEBUG_MSG( 2, ( "<= read" ) );
6285
Paul Bakker23986e52011-04-24 08:57:21 +00006286 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006287}
6288
6289/*
6290 * Send application data to be encrypted by the SSL layer
6291 */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006292#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
6293static int ssl_write_real( ssl_context *ssl, const unsigned char *buf, size_t len )
6294#else
Paul Bakker23986e52011-04-24 08:57:21 +00006295int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006296#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006297{
Paul Bakker23986e52011-04-24 08:57:21 +00006298 int ret;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006299#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
6300 unsigned int max_len;
6301#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006302
6303 SSL_DEBUG_MSG( 2, ( "=> write" ) );
6304
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006305#if defined(POLARSSL_SSL_RENEGOTIATION)
6306 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
6307 {
6308 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
6309 return( ret );
6310 }
6311#endif
6312
Paul Bakker5121ce52009-01-03 21:22:43 +00006313 if( ssl->state != SSL_HANDSHAKE_OVER )
6314 {
6315 if( ( ret = ssl_handshake( ssl ) ) != 0 )
6316 {
6317 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6318 return( ret );
6319 }
6320 }
6321
Paul Bakker05decb22013-08-15 13:33:48 +02006322#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02006323 /*
6324 * Assume mfl_code is correct since it was checked when set
6325 */
6326 max_len = mfl_code_to_length[ssl->mfl_code];
6327
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006328 /*
Paul Bakker05decb22013-08-15 13:33:48 +02006329 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006330 */
6331 if( ssl->session_out != NULL &&
6332 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6333 {
6334 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6335 }
6336
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006337 if( len > max_len )
6338 {
6339#if defined(POLARSSL_SSL_PROTO_DTLS)
6340 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6341 {
6342 SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
6343 "maximum fragment length: %d > %d",
6344 len, max_len ) );
6345 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
6346 }
6347 else
6348#endif
6349 len = max_len;
6350 }
6351#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker887bd502011-06-08 13:10:54 +00006352
Paul Bakker5121ce52009-01-03 21:22:43 +00006353 if( ssl->out_left != 0 )
6354 {
6355 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
6356 {
6357 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
6358 return( ret );
6359 }
6360 }
Paul Bakker887bd502011-06-08 13:10:54 +00006361 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00006362 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006363 ssl->out_msglen = len;
Paul Bakker887bd502011-06-08 13:10:54 +00006364 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006365 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00006366
6367 if( ( ret = ssl_write_record( ssl ) ) != 0 )
6368 {
6369 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
6370 return( ret );
6371 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006372 }
6373
6374 SSL_DEBUG_MSG( 2, ( "<= write" ) );
6375
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006376 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006377}
6378
6379/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006380 * Write application data, doing 1/n-1 splitting if necessary.
6381 *
6382 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006383 * then the caller will call us again with the same arguments, so
6384 * remember wether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006385 */
6386#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
6387int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
6388{
6389 int ret;
6390
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006391 if( ssl->split_done == SSL_CBC_RECORD_SPLITTING_DISABLED ||
6392 len <= 1 ||
6393 ssl->minor_ver > SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006394 cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
6395 != POLARSSL_MODE_CBC )
6396 {
6397 return( ssl_write_real( ssl, buf, len ) );
6398 }
6399
6400 if( ssl->split_done == 0 )
6401 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006402 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006403 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006404 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006405 }
6406
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006407 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
6408 return( ret );
6409 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006410
6411 return( ret + 1 );
6412}
6413#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */
6414
6415/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006416 * Notify the peer that the connection is being closed
6417 */
6418int ssl_close_notify( ssl_context *ssl )
6419{
6420 int ret;
6421
6422 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
6423
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006424 if( ssl->out_left != 0 )
6425 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006426
6427 if( ssl->state == SSL_HANDSHAKE_OVER )
6428 {
Paul Bakker48916f92012-09-16 19:57:18 +00006429 if( ( ret = ssl_send_alert_message( ssl,
6430 SSL_ALERT_LEVEL_WARNING,
6431 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006432 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006433 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006434 return( ret );
6435 }
6436 }
6437
6438 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
6439
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006440 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006441}
6442
Paul Bakker48916f92012-09-16 19:57:18 +00006443void ssl_transform_free( ssl_transform *transform )
6444{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006445 if( transform == NULL )
6446 return;
6447
Paul Bakker48916f92012-09-16 19:57:18 +00006448#if defined(POLARSSL_ZLIB_SUPPORT)
6449 deflateEnd( &transform->ctx_deflate );
6450 inflateEnd( &transform->ctx_inflate );
6451#endif
6452
Paul Bakker84bbeb52014-07-01 14:53:22 +02006453 cipher_free( &transform->cipher_ctx_enc );
6454 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02006455
Paul Bakker84bbeb52014-07-01 14:53:22 +02006456 md_free( &transform->md_ctx_enc );
6457 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02006458
Paul Bakker34617722014-06-13 17:20:13 +02006459 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006460}
6461
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006462#if defined(POLARSSL_X509_CRT_PARSE_C)
6463static void ssl_key_cert_free( ssl_key_cert *key_cert )
6464{
6465 ssl_key_cert *cur = key_cert, *next;
6466
6467 while( cur != NULL )
6468 {
6469 next = cur->next;
6470
6471 if( cur->key_own_alloc )
6472 {
6473 pk_free( cur->key );
6474 polarssl_free( cur->key );
6475 }
6476 polarssl_free( cur );
6477
6478 cur = next;
6479 }
6480}
6481#endif /* POLARSSL_X509_CRT_PARSE_C */
6482
Paul Bakker48916f92012-09-16 19:57:18 +00006483void ssl_handshake_free( ssl_handshake_params *handshake )
6484{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006485 if( handshake == NULL )
6486 return;
6487
Paul Bakker48916f92012-09-16 19:57:18 +00006488#if defined(POLARSSL_DHM_C)
6489 dhm_free( &handshake->dhm_ctx );
6490#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006491#if defined(POLARSSL_ECDH_C)
6492 ecdh_free( &handshake->ecdh_ctx );
6493#endif
6494
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006495#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02006496 /* explicit void pointer cast for buggy MS compiler */
6497 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006498#endif
6499
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006500#if defined(POLARSSL_X509_CRT_PARSE_C) && \
6501 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
6502 /*
6503 * Free only the linked list wrapper, not the keys themselves
6504 * since the belong to the SNI callback
6505 */
6506 if( handshake->sni_key_cert != NULL )
6507 {
6508 ssl_key_cert *cur = handshake->sni_key_cert, *next;
6509
6510 while( cur != NULL )
6511 {
6512 next = cur->next;
6513 polarssl_free( cur );
6514 cur = next;
6515 }
6516 }
Paul Bakker9af723c2014-05-01 13:03:14 +02006517#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006518
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006519#if defined(POLARSSL_SSL_PROTO_DTLS)
6520 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02006521 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02006522 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006523#endif
6524
Paul Bakker34617722014-06-13 17:20:13 +02006525 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006526}
6527
6528void ssl_session_free( ssl_session *session )
6529{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006530 if( session == NULL )
6531 return;
6532
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006533#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00006534 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00006535 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006536 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02006537 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00006538 }
Paul Bakkered27a042013-04-18 22:46:23 +02006539#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006540
Paul Bakkera503a632013-08-14 13:48:06 +02006541#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006542 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02006543#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006544
Paul Bakker34617722014-06-13 17:20:13 +02006545 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006546}
6547
Paul Bakker5121ce52009-01-03 21:22:43 +00006548/*
6549 * Free an SSL context
6550 */
6551void ssl_free( ssl_context *ssl )
6552{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006553 if( ssl == NULL )
6554 return;
6555
Paul Bakker5121ce52009-01-03 21:22:43 +00006556 SSL_DEBUG_MSG( 2, ( "=> free" ) );
6557
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006558 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006559 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006560 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
6561 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006562 }
6563
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006564 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006565 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006566 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
6567 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006568 }
6569
Paul Bakker16770332013-10-11 09:59:44 +02006570#if defined(POLARSSL_ZLIB_SUPPORT)
6571 if( ssl->compress_buf != NULL )
6572 {
Paul Bakker34617722014-06-13 17:20:13 +02006573 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02006574 polarssl_free( ssl->compress_buf );
6575 }
6576#endif
6577
Paul Bakker40e46942009-01-03 21:51:57 +00006578#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006579 mpi_free( &ssl->dhm_P );
6580 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006581#endif
6582
Paul Bakker48916f92012-09-16 19:57:18 +00006583 if( ssl->transform )
6584 {
6585 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02006586 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006587 }
6588
6589 if( ssl->handshake )
6590 {
6591 ssl_handshake_free( ssl->handshake );
6592 ssl_transform_free( ssl->transform_negotiate );
6593 ssl_session_free( ssl->session_negotiate );
6594
Paul Bakker6e339b52013-07-03 13:37:05 +02006595 polarssl_free( ssl->handshake );
6596 polarssl_free( ssl->transform_negotiate );
6597 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00006598 }
6599
Paul Bakkerc0463502013-02-14 11:19:38 +01006600 if( ssl->session )
6601 {
6602 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02006603 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006604 }
6605
Paul Bakkera503a632013-08-14 13:48:06 +02006606#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02006607 if( ssl->ticket_keys )
6608 {
6609 ssl_ticket_keys_free( ssl->ticket_keys );
6610 polarssl_free( ssl->ticket_keys );
6611 }
Paul Bakkera503a632013-08-14 13:48:06 +02006612#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006613
Paul Bakker0be444a2013-08-27 21:55:01 +02006614#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02006615 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006616 {
Paul Bakker34617722014-06-13 17:20:13 +02006617 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006618 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006619 ssl->hostname_len = 0;
6620 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006621#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006622
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006623#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006624 if( ssl->psk != NULL )
6625 {
Paul Bakker34617722014-06-13 17:20:13 +02006626 polarssl_zeroize( ssl->psk, ssl->psk_len );
6627 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006628 polarssl_free( ssl->psk );
6629 polarssl_free( ssl->psk_identity );
6630 ssl->psk_len = 0;
6631 ssl->psk_identity_len = 0;
6632 }
6633#endif
6634
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006635#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006636 ssl_key_cert_free( ssl->key_cert );
6637#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006638
Paul Bakker05ef8352012-05-08 09:17:57 +00006639#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6640 if( ssl_hw_record_finish != NULL )
6641 {
6642 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6643 ssl_hw_record_finish( ssl );
6644 }
6645#endif
6646
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006647#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006648 polarssl_free( ssl->cli_id );
6649#endif
6650
Paul Bakker5121ce52009-01-03 21:22:43 +00006651 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006652
Paul Bakker86f04f42013-02-14 11:20:09 +01006653 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006654 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006655}
6656
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006657#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006658/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006659 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006660 */
6661unsigned char ssl_sig_from_pk( pk_context *pk )
6662{
6663#if defined(POLARSSL_RSA_C)
6664 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6665 return( SSL_SIG_RSA );
6666#endif
6667#if defined(POLARSSL_ECDSA_C)
6668 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6669 return( SSL_SIG_ECDSA );
6670#endif
6671 return( SSL_SIG_ANON );
6672}
6673
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006674pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6675{
6676 switch( sig )
6677 {
6678#if defined(POLARSSL_RSA_C)
6679 case SSL_SIG_RSA:
6680 return( POLARSSL_PK_RSA );
6681#endif
6682#if defined(POLARSSL_ECDSA_C)
6683 case SSL_SIG_ECDSA:
6684 return( POLARSSL_PK_ECDSA );
6685#endif
6686 default:
6687 return( POLARSSL_PK_NONE );
6688 }
6689}
Paul Bakker9af723c2014-05-01 13:03:14 +02006690#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006691
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006692/*
6693 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6694 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006695md_type_t ssl_md_alg_from_hash( unsigned char hash )
6696{
6697 switch( hash )
6698 {
6699#if defined(POLARSSL_MD5_C)
6700 case SSL_HASH_MD5:
6701 return( POLARSSL_MD_MD5 );
6702#endif
6703#if defined(POLARSSL_SHA1_C)
6704 case SSL_HASH_SHA1:
6705 return( POLARSSL_MD_SHA1 );
6706#endif
6707#if defined(POLARSSL_SHA256_C)
6708 case SSL_HASH_SHA224:
6709 return( POLARSSL_MD_SHA224 );
6710 case SSL_HASH_SHA256:
6711 return( POLARSSL_MD_SHA256 );
6712#endif
6713#if defined(POLARSSL_SHA512_C)
6714 case SSL_HASH_SHA384:
6715 return( POLARSSL_MD_SHA384 );
6716 case SSL_HASH_SHA512:
6717 return( POLARSSL_MD_SHA512 );
6718#endif
6719 default:
6720 return( POLARSSL_MD_NONE );
6721 }
6722}
6723
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006724#if defined(POLARSSL_SSL_SET_CURVES)
6725/*
6726 * Check is a curve proposed by the peer is in our list.
6727 * Return 1 if we're willing to use it, 0 otherwise.
6728 */
6729int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6730{
6731 const ecp_group_id *gid;
6732
6733 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6734 if( *gid == grp_id )
6735 return( 1 );
6736
6737 return( 0 );
6738}
Paul Bakker9af723c2014-05-01 13:03:14 +02006739#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006740
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006741#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006742int ssl_check_cert_usage( const x509_crt *cert,
6743 const ssl_ciphersuite_t *ciphersuite,
6744 int cert_endpoint )
6745{
6746#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6747 int usage = 0;
6748#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006749#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6750 const char *ext_oid;
6751 size_t ext_len;
6752#endif
6753
6754#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6755 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6756 ((void) cert);
6757 ((void) cert_endpoint);
6758#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006759
6760#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6761 if( cert_endpoint == SSL_IS_SERVER )
6762 {
6763 /* Server part of the key exchange */
6764 switch( ciphersuite->key_exchange )
6765 {
6766 case POLARSSL_KEY_EXCHANGE_RSA:
6767 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6768 usage = KU_KEY_ENCIPHERMENT;
6769 break;
6770
6771 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6772 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6773 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6774 usage = KU_DIGITAL_SIGNATURE;
6775 break;
6776
6777 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6778 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6779 usage = KU_KEY_AGREEMENT;
6780 break;
6781
6782 /* Don't use default: we want warnings when adding new values */
6783 case POLARSSL_KEY_EXCHANGE_NONE:
6784 case POLARSSL_KEY_EXCHANGE_PSK:
6785 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6786 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6787 usage = 0;
6788 }
6789 }
6790 else
6791 {
6792 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6793 usage = KU_DIGITAL_SIGNATURE;
6794 }
6795
6796 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6797 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006798#else
6799 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006800#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6801
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006802#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6803 if( cert_endpoint == SSL_IS_SERVER )
6804 {
6805 ext_oid = OID_SERVER_AUTH;
6806 ext_len = OID_SIZE( OID_SERVER_AUTH );
6807 }
6808 else
6809 {
6810 ext_oid = OID_CLIENT_AUTH;
6811 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6812 }
6813
6814 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6815 return( -1 );
6816#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6817
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006818 return( 0 );
6819}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006820#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006821
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006822/*
6823 * Convert version numbers to/from wire format
6824 * and, for DTLS, to/from TLS equivalent.
6825 *
6826 * For TLS this is the identity.
6827 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6828 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6829 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6830 */
6831void ssl_write_version( int major, int minor, int transport,
6832 unsigned char ver[2] )
6833{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006834#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006835 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006836 {
6837 if( minor == SSL_MINOR_VERSION_2 )
6838 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6839
6840 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6841 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6842 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006843 else
6844#else
6845 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006846#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006847 {
6848 ver[0] = (unsigned char) major;
6849 ver[1] = (unsigned char) minor;
6850 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006851}
6852
6853void ssl_read_version( int *major, int *minor, int transport,
6854 const unsigned char ver[2] )
6855{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006856#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006857 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006858 {
6859 *major = 255 - ver[0] + 2;
6860 *minor = 255 - ver[1] + 1;
6861
6862 if( *minor == SSL_MINOR_VERSION_1 )
6863 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6864 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006865 else
6866#else
6867 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006868#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006869 {
6870 *major = ver[0];
6871 *minor = ver[1];
6872 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006873}
6874
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006875#endif /* POLARSSL_SSL_TLS_C */