blob: cddeb74e7941e7d93e9559a28f717a6c9c717fda [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)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100339static int tls_prf_generic( md_type_t md_type,
340 const unsigned char *secret, size_t slen,
341 const char *label,
342 const unsigned char *random, size_t rlen,
343 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000344{
345 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100346 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000347 unsigned char tmp[128];
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100348 unsigned char h_i[POLARSSL_MD_MAX_SIZE];
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100349 const md_info_t *md_info;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000350
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100351 if( ( md_info = md_info_from_type( md_type ) ) == NULL )
352 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
353
354 md_len = md_get_size( md_info );
355
356 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000357 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
358
359 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100360 memcpy( tmp + md_len, label, nb );
361 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000362 nb += rlen;
363
364 /*
365 * Compute P_<hash>(secret, label + random)[0..dlen]
366 */
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100367 md_hmac( md_info, secret, slen, tmp + md_len, nb, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100368
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100369 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000370 {
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100371 md_hmac( md_info, secret, slen, tmp, md_len + nb, h_i );
372 md_hmac( md_info, secret, slen, tmp, md_len, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000373
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100374 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000375
376 for( j = 0; j < k; j++ )
377 dstbuf[i + j] = h_i[j];
378 }
379
Paul Bakker34617722014-06-13 17:20:13 +0200380 polarssl_zeroize( tmp, sizeof( tmp ) );
381 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000382
383 return( 0 );
384}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100385
386#if defined(POLARSSL_SHA256_C)
387static int tls_prf_sha256( const unsigned char *secret, size_t slen,
388 const char *label,
389 const unsigned char *random, size_t rlen,
390 unsigned char *dstbuf, size_t dlen )
391{
392 return( tls_prf_generic( POLARSSL_MD_SHA256, secret, slen,
393 label, random, rlen, dstbuf, dlen ) );
394}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200395#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000396
Paul Bakker9e36f042013-06-30 14:34:05 +0200397#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200398static int tls_prf_sha384( const unsigned char *secret, size_t slen,
399 const char *label,
400 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000401 unsigned char *dstbuf, size_t dlen )
402{
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100403 return( tls_prf_generic( POLARSSL_MD_SHA384, secret, slen,
404 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000405}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200406#endif /* POLARSSL_SHA512_C */
407#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000408
Paul Bakker66d5d072014-06-17 16:39:18 +0200409static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200410
411#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
412 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200413static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200414#endif
Paul Bakker380da532012-04-18 16:10:25 +0000415
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200416#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200417static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
418static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200419#endif
420
421#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200422static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
423static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200424#endif
425
426#if defined(POLARSSL_SSL_PROTO_TLS1_2)
427#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200428static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
429static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
430static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200431#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100432
Paul Bakker9e36f042013-06-30 14:34:05 +0200433#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200434static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
435static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
436static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100437#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200438#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000439
Paul Bakker5121ce52009-01-03 21:22:43 +0000440int ssl_derive_keys( ssl_context *ssl )
441{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200442 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000443 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 unsigned char keyblk[256];
445 unsigned char *key1;
446 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100447 unsigned char *mac_enc;
448 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200449 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100450 const cipher_info_t *cipher_info;
451 const md_info_t *md_info;
452
Paul Bakker48916f92012-09-16 19:57:18 +0000453 ssl_session *session = ssl->session_negotiate;
454 ssl_transform *transform = ssl->transform_negotiate;
455 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000456
457 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
458
Paul Bakker68884e32013-01-07 18:20:04 +0100459 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
460 if( cipher_info == NULL )
461 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200462 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100463 transform->ciphersuite_info->cipher ) );
464 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
465 }
466
467 md_info = md_info_from_type( transform->ciphersuite_info->mac );
468 if( md_info == NULL )
469 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200470 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100471 transform->ciphersuite_info->mac ) );
472 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
473 }
474
Paul Bakker5121ce52009-01-03 21:22:43 +0000475 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000476 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000477 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200478#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000479 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000480 {
Paul Bakker48916f92012-09-16 19:57:18 +0000481 handshake->tls_prf = ssl3_prf;
482 handshake->calc_verify = ssl_calc_verify_ssl;
483 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000484 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200485 else
486#endif
487#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
488 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000489 {
Paul Bakker48916f92012-09-16 19:57:18 +0000490 handshake->tls_prf = tls1_prf;
491 handshake->calc_verify = ssl_calc_verify_tls;
492 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000493 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200494 else
495#endif
496#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200497#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200498 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
499 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000500 {
Paul Bakker48916f92012-09-16 19:57:18 +0000501 handshake->tls_prf = tls_prf_sha384;
502 handshake->calc_verify = ssl_calc_verify_tls_sha384;
503 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000504 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000505 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200506#endif
507#if defined(POLARSSL_SHA256_C)
508 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000509 {
Paul Bakker48916f92012-09-16 19:57:18 +0000510 handshake->tls_prf = tls_prf_sha256;
511 handshake->calc_verify = ssl_calc_verify_tls_sha256;
512 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000513 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200514 else
515#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200516#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200517 {
518 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200519 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200520 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000521
522 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000523 * SSLv3:
524 * master =
525 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
526 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
527 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200528 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200529 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000530 * master = PRF( premaster, "master secret", randbytes )[0..47]
531 */
Paul Bakker0a597072012-09-25 21:55:46 +0000532 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 {
Paul Bakker48916f92012-09-16 19:57:18 +0000534 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
535 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000536
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200537#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
538 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200539 {
540 unsigned char session_hash[48];
541 size_t hash_len;
542
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200543 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200544
545 ssl->handshake->calc_verify( ssl, session_hash );
546
547#if defined(POLARSSL_SSL_PROTO_TLS1_2)
548 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
549 {
550#if defined(POLARSSL_SHA512_C)
551 if( ssl->transform_negotiate->ciphersuite_info->mac ==
552 POLARSSL_MD_SHA384 )
553 {
554 hash_len = 48;
555 }
556 else
557#endif
558 hash_len = 32;
559 }
560 else
561#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
562 hash_len = 36;
563
564 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
565
566 handshake->tls_prf( handshake->premaster, handshake->pmslen,
567 "extended master secret",
568 session_hash, hash_len, session->master, 48 );
569
570 }
571 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200572#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000573 handshake->tls_prf( handshake->premaster, handshake->pmslen,
574 "master secret",
575 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200577
Paul Bakker34617722014-06-13 17:20:13 +0200578 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 }
580 else
581 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
582
583 /*
584 * Swap the client and server random values.
585 */
Paul Bakker48916f92012-09-16 19:57:18 +0000586 memcpy( tmp, handshake->randbytes, 64 );
587 memcpy( handshake->randbytes, tmp + 32, 32 );
588 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200589 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
591 /*
592 * SSLv3:
593 * key block =
594 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
595 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
596 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
597 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
598 * ...
599 *
600 * TLSv1:
601 * key block = PRF( master, "key expansion", randbytes )
602 */
Paul Bakker48916f92012-09-16 19:57:18 +0000603 handshake->tls_prf( session->master, 48, "key expansion",
604 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000605
Paul Bakker48916f92012-09-16 19:57:18 +0000606 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
607 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
608 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
609 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000610 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
611
Paul Bakker34617722014-06-13 17:20:13 +0200612 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000613
614 /*
615 * Determine the appropriate key, IV and MAC length.
616 */
Paul Bakker68884e32013-01-07 18:20:04 +0100617
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200618 transform->keylen = cipher_info->key_length / 8;
619
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200620 if( cipher_info->mode == POLARSSL_MODE_GCM ||
621 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000622 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200623 transform->maclen = 0;
624
Paul Bakker68884e32013-01-07 18:20:04 +0100625 transform->ivlen = 12;
626 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200627
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200628 /* Minimum length is expicit IV + tag */
629 transform->minlen = transform->ivlen - transform->fixed_ivlen
630 + ( transform->ciphersuite_info->flags &
631 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100632 }
633 else
634 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200635 int ret;
636
637 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnardabb67442015-03-25 16:29:51 +0100638 if( ( ret = md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
639 ( ret = md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100640 {
Manuel Pégourié-Gonnardabb67442015-03-25 16:29:51 +0100641 SSL_DEBUG_RET( 1, "md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200642 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100643 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000644
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200645 /* Get MAC length */
646 transform->maclen = md_get_size( md_info );
647
648#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
649 /*
650 * If HMAC is to be truncated, we shall keep the leftmost bytes,
651 * (rfc 6066 page 13 or rfc 2104 section 4),
652 * so we only need to adjust the length here.
653 */
654 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
655 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
656#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
657
658 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100659 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000660
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200661 /* Minimum length */
662 if( cipher_info->mode == POLARSSL_MODE_STREAM )
663 transform->minlen = transform->maclen;
664 else
Paul Bakker68884e32013-01-07 18:20:04 +0100665 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200666 /*
667 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100668 * 1. if EtM is in use: one block plus MAC
669 * otherwise: * first multiple of blocklen greater than maclen
670 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200671 */
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100672#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
673 if( session->encrypt_then_mac == SSL_ETM_ENABLED )
674 {
675 transform->minlen = transform->maclen
676 + cipher_info->block_size;
677 }
678 else
679#endif
680 {
681 transform->minlen = transform->maclen
682 + cipher_info->block_size
683 - transform->maclen % cipher_info->block_size;
684 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200685
686#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
687 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
688 ssl->minor_ver == SSL_MINOR_VERSION_1 )
689 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100690 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200691#endif
692#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
693 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
694 ssl->minor_ver == SSL_MINOR_VERSION_3 )
695 {
696 transform->minlen += transform->ivlen;
697 }
698 else
699#endif
700 {
701 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
702 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
703 }
Paul Bakker68884e32013-01-07 18:20:04 +0100704 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000705 }
706
707 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000708 transform->keylen, transform->minlen, transform->ivlen,
709 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
711 /*
712 * Finally setup the cipher contexts, IVs and MAC secrets.
713 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100714#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000715 if( ssl->endpoint == SSL_IS_CLIENT )
716 {
Paul Bakker48916f92012-09-16 19:57:18 +0000717 key1 = keyblk + transform->maclen * 2;
718 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000719
Paul Bakker68884e32013-01-07 18:20:04 +0100720 mac_enc = keyblk;
721 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000722
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000723 /*
724 * This is not used in TLS v1.1.
725 */
Paul Bakker48916f92012-09-16 19:57:18 +0000726 iv_copy_len = ( transform->fixed_ivlen ) ?
727 transform->fixed_ivlen : transform->ivlen;
728 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
729 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000730 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000731 }
732 else
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100733#endif /* POLARSSL_SSL_CLI_C */
734#if defined(POLARSSL_SSL_SRV_C)
735 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000736 {
Paul Bakker48916f92012-09-16 19:57:18 +0000737 key1 = keyblk + transform->maclen * 2 + transform->keylen;
738 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000739
Paul Bakker68884e32013-01-07 18:20:04 +0100740 mac_enc = keyblk + transform->maclen;
741 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000742
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000743 /*
744 * This is not used in TLS v1.1.
745 */
Paul Bakker48916f92012-09-16 19:57:18 +0000746 iv_copy_len = ( transform->fixed_ivlen ) ?
747 transform->fixed_ivlen : transform->ivlen;
748 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
749 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000750 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000751 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100752 else
753#endif /* POLARSSL_SSL_SRV_C */
754 {
755 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
756 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
757 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000758
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200759#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100760 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
761 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100762 if( transform->maclen > sizeof transform->mac_enc )
763 {
764 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200765 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100766 }
767
Paul Bakker68884e32013-01-07 18:20:04 +0100768 memcpy( transform->mac_enc, mac_enc, transform->maclen );
769 memcpy( transform->mac_dec, mac_dec, transform->maclen );
770 }
771 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200772#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200773#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
774 defined(POLARSSL_SSL_PROTO_TLS1_2)
775 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100776 {
777 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
778 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
779 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200780 else
781#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200782 {
783 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200784 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200785 }
Paul Bakker68884e32013-01-07 18:20:04 +0100786
Paul Bakker05ef8352012-05-08 09:17:57 +0000787#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200788 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000789 {
790 int ret = 0;
791
792 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
793
Paul Bakker07eb38b2012-12-19 14:42:06 +0100794 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
795 transform->iv_enc, transform->iv_dec,
796 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100797 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100798 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000799 {
800 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200801 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000802 }
803 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200804#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000805
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200806 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
807 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000808 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200809 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
810 return( ret );
811 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200812
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200813 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
814 cipher_info ) ) != 0 )
815 {
816 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
817 return( ret );
818 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200819
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200820 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
821 cipher_info->key_length,
822 POLARSSL_ENCRYPT ) ) != 0 )
823 {
824 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
825 return( ret );
826 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200827
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200828 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
829 cipher_info->key_length,
830 POLARSSL_DECRYPT ) ) != 0 )
831 {
832 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
833 return( ret );
834 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200835
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200836#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200837 if( cipher_info->mode == POLARSSL_MODE_CBC )
838 {
839 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
840 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200841 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200842 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
843 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200844 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200845
846 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
847 POLARSSL_PADDING_NONE ) ) != 0 )
848 {
849 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
850 return( ret );
851 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000852 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200853#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000854
Paul Bakker34617722014-06-13 17:20:13 +0200855 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000856
Paul Bakker2770fbd2012-07-03 13:30:23 +0000857#if defined(POLARSSL_ZLIB_SUPPORT)
858 // Initialize compression
859 //
Paul Bakker48916f92012-09-16 19:57:18 +0000860 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000861 {
Paul Bakker16770332013-10-11 09:59:44 +0200862 if( ssl->compress_buf == NULL )
863 {
864 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
865 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
866 if( ssl->compress_buf == NULL )
867 {
868 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
869 SSL_BUFFER_LEN ) );
870 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
871 }
872 }
873
Paul Bakker2770fbd2012-07-03 13:30:23 +0000874 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
875
Paul Bakker48916f92012-09-16 19:57:18 +0000876 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
877 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000878
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200879 if( deflateInit( &transform->ctx_deflate,
880 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000881 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000882 {
883 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
884 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
885 }
886 }
887#endif /* POLARSSL_ZLIB_SUPPORT */
888
Paul Bakker5121ce52009-01-03 21:22:43 +0000889 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
890
891 return( 0 );
892}
893
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200894#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000895void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000896{
897 md5_context md5;
898 sha1_context sha1;
899 unsigned char pad_1[48];
900 unsigned char pad_2[48];
901
Paul Bakker380da532012-04-18 16:10:25 +0000902 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000903
Paul Bakker48916f92012-09-16 19:57:18 +0000904 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
905 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000906
Paul Bakker380da532012-04-18 16:10:25 +0000907 memset( pad_1, 0x36, 48 );
908 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000909
Paul Bakker48916f92012-09-16 19:57:18 +0000910 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000911 md5_update( &md5, pad_1, 48 );
912 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000913
Paul Bakker380da532012-04-18 16:10:25 +0000914 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000915 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000916 md5_update( &md5, pad_2, 48 );
917 md5_update( &md5, hash, 16 );
918 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000919
Paul Bakker48916f92012-09-16 19:57:18 +0000920 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000921 sha1_update( &sha1, pad_1, 40 );
922 sha1_finish( &sha1, hash + 16 );
923
924 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000925 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000926 sha1_update( &sha1, pad_2, 40 );
927 sha1_update( &sha1, hash + 16, 20 );
928 sha1_finish( &sha1, hash + 16 );
929
930 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
931 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
932
Paul Bakker5b4af392014-06-26 12:09:34 +0200933 md5_free( &md5 );
934 sha1_free( &sha1 );
935
Paul Bakker380da532012-04-18 16:10:25 +0000936 return;
937}
Paul Bakker9af723c2014-05-01 13:03:14 +0200938#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000939
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200940#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000941void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
942{
943 md5_context md5;
944 sha1_context sha1;
945
946 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
947
Paul Bakker48916f92012-09-16 19:57:18 +0000948 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
949 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000950
Paul Bakker48916f92012-09-16 19:57:18 +0000951 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000952 sha1_finish( &sha1, hash + 16 );
953
954 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
955 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
956
Paul Bakker5b4af392014-06-26 12:09:34 +0200957 md5_free( &md5 );
958 sha1_free( &sha1 );
959
Paul Bakker380da532012-04-18 16:10:25 +0000960 return;
961}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200962#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000963
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200964#if defined(POLARSSL_SSL_PROTO_TLS1_2)
965#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000966void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
967{
Paul Bakker9e36f042013-06-30 14:34:05 +0200968 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000969
970 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
971
Paul Bakker9e36f042013-06-30 14:34:05 +0200972 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
973 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000974
975 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
976 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
977
Paul Bakker5b4af392014-06-26 12:09:34 +0200978 sha256_free( &sha256 );
979
Paul Bakker380da532012-04-18 16:10:25 +0000980 return;
981}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200982#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000983
Paul Bakker9e36f042013-06-30 14:34:05 +0200984#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000985void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
986{
Paul Bakker9e36f042013-06-30 14:34:05 +0200987 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000988
989 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
990
Paul Bakker9e36f042013-06-30 14:34:05 +0200991 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
992 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000993
Paul Bakkerca4ab492012-04-18 14:23:57 +0000994 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000995 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
996
Paul Bakker5b4af392014-06-26 12:09:34 +0200997 sha512_free( &sha512 );
998
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 return;
1000}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001001#endif /* POLARSSL_SHA512_C */
1002#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001003
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001004#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001005int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
1006{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001007 unsigned char *p = ssl->handshake->premaster;
1008 unsigned char *end = p + sizeof( ssl->handshake->premaster );
1009
1010 /*
1011 * PMS = struct {
1012 * opaque other_secret<0..2^16-1>;
1013 * opaque psk<0..2^16-1>;
1014 * };
1015 * with "other_secret" depending on the particular key exchange
1016 */
1017#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
1018 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
1019 {
1020 if( end - p < 2 + (int) ssl->psk_len )
1021 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1022
1023 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1024 *(p++) = (unsigned char)( ssl->psk_len );
1025 p += ssl->psk_len;
1026 }
1027 else
1028#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001029#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1030 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1031 {
1032 /*
1033 * other_secret already set by the ClientKeyExchange message,
1034 * and is 48 bytes long
1035 */
1036 *p++ = 0;
1037 *p++ = 48;
1038 p += 48;
1039 }
1040 else
1041#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001042#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1043 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1044 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001045 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02001046 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001047
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001048 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001049 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001050 p + 2, &len,
1051 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001052 {
1053 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
1054 return( ret );
1055 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001056 *(p++) = (unsigned char)( len >> 8 );
1057 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001058 p += len;
1059
1060 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1061 }
1062 else
1063#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1064#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1065 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1066 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001067 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001068 size_t zlen;
1069
1070 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001071 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001072 ssl->f_rng, ssl->p_rng ) ) != 0 )
1073 {
1074 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1075 return( ret );
1076 }
1077
1078 *(p++) = (unsigned char)( zlen >> 8 );
1079 *(p++) = (unsigned char)( zlen );
1080 p += zlen;
1081
1082 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1083 }
1084 else
1085#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1086 {
1087 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001088 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001089 }
1090
1091 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001092 if( end - p < 2 + (int) ssl->psk_len )
1093 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1094
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001095 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1096 *(p++) = (unsigned char)( ssl->psk_len );
1097 memcpy( p, ssl->psk, ssl->psk_len );
1098 p += ssl->psk_len;
1099
1100 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1101
1102 return( 0 );
1103}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001104#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001105
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001106#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001107/*
1108 * SSLv3.0 MAC functions
1109 */
Paul Bakker68884e32013-01-07 18:20:04 +01001110static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1111 unsigned char *buf, size_t len,
1112 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001113{
1114 unsigned char header[11];
1115 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001116 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001117 int md_size = md_get_size( md_ctx->md_info );
1118 int md_type = md_get_type( md_ctx->md_info );
1119
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001120 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001121 if( md_type == POLARSSL_MD_MD5 )
1122 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001123 else
Paul Bakker68884e32013-01-07 18:20:04 +01001124 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001125
1126 memcpy( header, ctr, 8 );
1127 header[ 8] = (unsigned char) type;
1128 header[ 9] = (unsigned char)( len >> 8 );
1129 header[10] = (unsigned char)( len );
1130
Paul Bakker68884e32013-01-07 18:20:04 +01001131 memset( padding, 0x36, padlen );
1132 md_starts( md_ctx );
1133 md_update( md_ctx, secret, md_size );
1134 md_update( md_ctx, padding, padlen );
1135 md_update( md_ctx, header, 11 );
1136 md_update( md_ctx, buf, len );
1137 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001138
Paul Bakker68884e32013-01-07 18:20:04 +01001139 memset( padding, 0x5C, padlen );
1140 md_starts( md_ctx );
1141 md_update( md_ctx, secret, md_size );
1142 md_update( md_ctx, padding, padlen );
1143 md_update( md_ctx, buf + len, md_size );
1144 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001145}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001146#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001147
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001148#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1149 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1150 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1151#define POLARSSL_SOME_MODES_USE_MAC
1152#endif
1153
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001154/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001155 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001156 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001157static int ssl_encrypt_buf( ssl_context *ssl )
1158{
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001159 cipher_mode_t mode;
1160 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001161
1162 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1163
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001164 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1165 {
1166 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1167 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1168 }
1169
1170 mode = cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
1171
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001172 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1173 ssl->out_msg, ssl->out_msglen );
1174
Paul Bakker5121ce52009-01-03 21:22:43 +00001175 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001176 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001177 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001178#if defined(POLARSSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001179 if( mode == POLARSSL_MODE_STREAM ||
1180 ( mode == POLARSSL_MODE_CBC
1181#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1182 && ssl->session_out->encrypt_then_mac == SSL_ETM_DISABLED
1183#endif
1184 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001185 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001186#if defined(POLARSSL_SSL_PROTO_SSL3)
1187 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1188 {
1189 ssl_mac( &ssl->transform_out->md_ctx_enc,
1190 ssl->transform_out->mac_enc,
1191 ssl->out_msg, ssl->out_msglen,
1192 ssl->out_ctr, ssl->out_msgtype );
1193 }
1194 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001195#endif
1196#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001197 defined(POLARSSL_SSL_PROTO_TLS1_2)
1198 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1199 {
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001200 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1201 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1202 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001203 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1204 ssl->out_msg, ssl->out_msglen );
1205 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1206 ssl->out_msg + ssl->out_msglen );
1207 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1208 }
1209 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001210#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001211 {
1212 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001213 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001214 }
1215
1216 SSL_DEBUG_BUF( 4, "computed mac",
1217 ssl->out_msg + ssl->out_msglen,
1218 ssl->transform_out->maclen );
1219
1220 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001221 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001222 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001223#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001224
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001225 /*
1226 * Encrypt
1227 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001228#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001229 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001230 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001231 int ret;
1232 size_t olen = 0;
1233
Paul Bakker5121ce52009-01-03 21:22:43 +00001234 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1235 "including %d bytes of padding",
1236 ssl->out_msglen, 0 ) );
1237
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001238 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001239 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001240 ssl->transform_out->ivlen,
1241 ssl->out_msg, ssl->out_msglen,
1242 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001243 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001244 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001245 return( ret );
1246 }
1247
1248 if( ssl->out_msglen != olen )
1249 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001250 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001251 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001252 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001253 }
Paul Bakker68884e32013-01-07 18:20:04 +01001254 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001255#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001256#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1257 if( mode == POLARSSL_MODE_GCM ||
1258 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001259 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001260 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001261 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001262 unsigned char *enc_msg;
1263 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001264 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1265 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001266
Paul Bakkerca4ab492012-04-18 14:23:57 +00001267 memcpy( add_data, ssl->out_ctr, 8 );
1268 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001269 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1270 ssl->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001271 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1272 add_data[12] = ssl->out_msglen & 0xFF;
1273
1274 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1275 add_data, 13 );
1276
Paul Bakker68884e32013-01-07 18:20:04 +01001277 /*
1278 * Generate IV
1279 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001280#if defined(POLARSSL_SSL_AEAD_RANDOM_IV)
Paul Bakker68884e32013-01-07 18:20:04 +01001281 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001282 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1283 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001284 if( ret != 0 )
1285 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001286
Paul Bakker68884e32013-01-07 18:20:04 +01001287 memcpy( ssl->out_iv,
1288 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1289 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001290#else
1291 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1292 {
1293 /* Reminder if we ever add an AEAD mode with a different size */
1294 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1295 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1296 }
1297
1298 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1299 ssl->out_ctr, 8 );
1300 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1301#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00001302
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001303 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001304 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001305
Paul Bakker68884e32013-01-07 18:20:04 +01001306 /*
1307 * Fix pointer positions and message length with added IV
1308 */
1309 enc_msg = ssl->out_msg;
1310 enc_msglen = ssl->out_msglen;
1311 ssl->out_msglen += ssl->transform_out->ivlen -
1312 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001313
Paul Bakker68884e32013-01-07 18:20:04 +01001314 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1315 "including %d bytes of padding",
1316 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001317
Paul Bakker68884e32013-01-07 18:20:04 +01001318 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001319 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001320 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001321 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1322 ssl->transform_out->iv_enc,
1323 ssl->transform_out->ivlen,
1324 add_data, 13,
1325 enc_msg, enc_msglen,
1326 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001327 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001328 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001329 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001330 return( ret );
1331 }
1332
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001333 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001334 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001335 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001336 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001337 }
1338
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001339 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001340 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001341
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001342 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001343 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001344 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001345#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001346#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1347 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001348 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001349 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001350 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001351 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001352 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001353
Paul Bakker48916f92012-09-16 19:57:18 +00001354 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1355 ssl->transform_out->ivlen;
1356 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 padlen = 0;
1358
1359 for( i = 0; i <= padlen; i++ )
1360 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1361
1362 ssl->out_msglen += padlen + 1;
1363
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001364 enc_msglen = ssl->out_msglen;
1365 enc_msg = ssl->out_msg;
1366
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001367#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001368 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001369 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1370 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001371 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001372 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001373 {
1374 /*
1375 * Generate IV
1376 */
Paul Bakker48916f92012-09-16 19:57:18 +00001377 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1378 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001379 if( ret != 0 )
1380 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001381
Paul Bakker92be97b2013-01-02 17:30:03 +01001382 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001383 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001384
1385 /*
1386 * Fix pointer positions and message length with added IV
1387 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001388 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001389 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001390 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001391 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001392#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001393
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001395 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001396 ssl->out_msglen, ssl->transform_out->ivlen,
1397 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001399 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001400 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001401 ssl->transform_out->ivlen,
1402 enc_msg, enc_msglen,
1403 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001404 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001405 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001406 return( ret );
1407 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001408
Paul Bakkercca5b812013-08-31 17:40:26 +02001409 if( enc_msglen != olen )
1410 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001411 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001412 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001413 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001414
1415#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001416 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1417 {
1418 /*
1419 * Save IV in SSL3 and TLS1
1420 */
1421 memcpy( ssl->transform_out->iv_enc,
1422 ssl->transform_out->cipher_ctx_enc.iv,
1423 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001425#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001426
1427#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001428 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001429 {
1430 /*
1431 * MAC(MAC_write_key, seq_num +
1432 * TLSCipherText.type +
1433 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001434 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001435 * IV + // except for TLS 1.0
1436 * ENC(content + padding + padding_length));
1437 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001438 unsigned char pseudo_hdr[13];
1439
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001440 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1441
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001442 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1443 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001444 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1445 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1446
1447 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001448
1449 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1450 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1451 ssl->out_iv, ssl->out_msglen );
1452 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1453 ssl->out_iv + ssl->out_msglen );
1454 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1455
1456 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001457 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001458 }
1459#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001460 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001461 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001462#endif /* POLARSSL_CIPHER_MODE_CBC &&
1463 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001464 {
1465 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001466 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001467 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001468
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001469 /* Make extra sure authentication was performed, exactly once */
1470 if( auth_done != 1 )
1471 {
1472 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1473 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1474 }
1475
Paul Bakker5121ce52009-01-03 21:22:43 +00001476 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1477
1478 return( 0 );
1479}
1480
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001481#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001482
Paul Bakker5121ce52009-01-03 21:22:43 +00001483static int ssl_decrypt_buf( ssl_context *ssl )
1484{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001485 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001486 cipher_mode_t mode;
1487 int auth_done = 0;
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001488#if defined(POLARSSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001489 size_t padlen = 0, correct = 1;
1490#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001491
1492 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1493
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001494 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1495 {
1496 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1497 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1498 }
1499
1500 mode = cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
1501
Paul Bakker48916f92012-09-16 19:57:18 +00001502 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001503 {
1504 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001505 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001506 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001507 }
1508
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001509#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001510 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001511 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001512 int ret;
1513 size_t olen = 0;
1514
Paul Bakker68884e32013-01-07 18:20:04 +01001515 padlen = 0;
1516
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001517 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001518 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001519 ssl->transform_in->ivlen,
1520 ssl->in_msg, ssl->in_msglen,
1521 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001522 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001523 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001524 return( ret );
1525 }
1526
1527 if( ssl->in_msglen != olen )
1528 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001529 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001530 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001531 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001532 }
Paul Bakker68884e32013-01-07 18:20:04 +01001533 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001534#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001535#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1536 if( mode == POLARSSL_MODE_GCM ||
1537 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001538 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001539 int ret;
1540 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001541 unsigned char *dec_msg;
1542 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001543 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001544 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1545 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001546 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1547 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001548
Manuel Pégourié-Gonnard06d75192015-02-11 14:54:11 +00001549 if( ssl->in_msglen < (size_t) explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001550 {
1551 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1552 "+ taglen (%d)", ssl->in_msglen,
1553 explicit_iv_len, taglen ) );
1554 return( POLARSSL_ERR_SSL_INVALID_MAC );
1555 }
1556 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1557
Paul Bakker68884e32013-01-07 18:20:04 +01001558 dec_msg = ssl->in_msg;
1559 dec_msg_result = ssl->in_msg;
1560 ssl->in_msglen = dec_msglen;
1561
1562 memcpy( add_data, ssl->in_ctr, 8 );
1563 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001564 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1565 ssl->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001566 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1567 add_data[12] = ssl->in_msglen & 0xFF;
1568
1569 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1570 add_data, 13 );
1571
1572 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1573 ssl->in_iv,
1574 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1575
1576 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1577 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001578 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001579
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001580 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001581 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001582 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001583 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1584 ssl->transform_in->iv_dec,
1585 ssl->transform_in->ivlen,
1586 add_data, 13,
1587 dec_msg, dec_msglen,
1588 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001589 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001590 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001591 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1592
1593 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1594 return( POLARSSL_ERR_SSL_INVALID_MAC );
1595
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001596 return( ret );
1597 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001598 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001599
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001600 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001601 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001602 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001603 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001604 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001605 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001606 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001607#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001608#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1609 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001610 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001611 {
Paul Bakker45829992013-01-03 14:52:21 +01001612 /*
1613 * Decrypt and check the padding
1614 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001615 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001616 unsigned char *dec_msg;
1617 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001618 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001619 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001620 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001621
Paul Bakker5121ce52009-01-03 21:22:43 +00001622 /*
Paul Bakker45829992013-01-03 14:52:21 +01001623 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001624 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001625#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001626 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1627 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001628#endif
Paul Bakker45829992013-01-03 14:52:21 +01001629
1630 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1631 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1632 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001633 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1634 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1635 ssl->transform_in->ivlen,
1636 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001637 return( POLARSSL_ERR_SSL_INVALID_MAC );
1638 }
1639
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001640 dec_msglen = ssl->in_msglen;
1641 dec_msg = ssl->in_msg;
1642 dec_msg_result = ssl->in_msg;
1643
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001644 /*
1645 * Authenticate before decrypt if enabled
1646 */
1647#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001648 if( ssl->session_in->encrypt_then_mac == SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001649 {
1650 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001651 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001652
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001653 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1654
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001655 dec_msglen -= ssl->transform_in->maclen;
1656 ssl->in_msglen -= ssl->transform_in->maclen;
1657
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001658 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1659 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1660 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1661 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1662
1663 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1664
1665 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001666 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1667 ssl->in_iv, ssl->in_msglen );
1668 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1669 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1670
1671 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1672 ssl->transform_in->maclen );
1673 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1674 ssl->transform_in->maclen );
1675
1676 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1677 ssl->transform_in->maclen ) != 0 )
1678 {
1679 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1680
1681 return( POLARSSL_ERR_SSL_INVALID_MAC );
1682 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001683 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001684 }
1685#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1686
1687 /*
1688 * Check length sanity
1689 */
1690 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1691 {
1692 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1693 ssl->in_msglen, ssl->transform_in->ivlen ) );
1694 return( POLARSSL_ERR_SSL_INVALID_MAC );
1695 }
1696
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001697#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001698 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001699 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001700 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001701 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001702 {
Paul Bakker48916f92012-09-16 19:57:18 +00001703 dec_msglen -= ssl->transform_in->ivlen;
1704 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001705
Paul Bakker48916f92012-09-16 19:57:18 +00001706 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001707 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001708 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001709#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001710
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001711 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001712 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001713 ssl->transform_in->ivlen,
1714 dec_msg, dec_msglen,
1715 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001716 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001717 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001718 return( ret );
1719 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001720
Paul Bakkercca5b812013-08-31 17:40:26 +02001721 if( dec_msglen != olen )
1722 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001723 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001724 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001725 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001726
1727#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001728 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1729 {
1730 /*
1731 * Save IV in SSL3 and TLS1
1732 */
1733 memcpy( ssl->transform_in->iv_dec,
1734 ssl->transform_in->cipher_ctx_dec.iv,
1735 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001736 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001737#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001738
1739 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001740
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001741 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001742 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001743 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001744#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001745 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1746 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001747#endif
Paul Bakker45829992013-01-03 14:52:21 +01001748 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001749 correct = 0;
1750 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001751
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001752#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001753 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1754 {
Paul Bakker48916f92012-09-16 19:57:18 +00001755 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001756 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001757#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001758 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1759 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001760 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001761#endif
Paul Bakker45829992013-01-03 14:52:21 +01001762 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001763 }
1764 }
1765 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001766#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001767#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1768 defined(POLARSSL_SSL_PROTO_TLS1_2)
1769 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001770 {
1771 /*
Paul Bakker45829992013-01-03 14:52:21 +01001772 * TLSv1+: always check the padding up to the first failure
1773 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001774 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001775 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001776 size_t padding_idx = ssl->in_msglen - padlen - 1;
1777
Paul Bakker956c9e02013-12-19 14:42:28 +01001778 /*
1779 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001780 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001781 *
Paul Bakker61885c72014-04-25 12:59:03 +02001782 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1783 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001784 *
1785 * In both cases we reset padding_idx to a safe value (0) to
1786 * prevent out-of-buffer reads.
1787 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001788 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001789 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1790 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001791
1792 padding_idx *= correct;
1793
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001794 for( i = 1; i <= 256; i++ )
1795 {
1796 real_count &= ( i <= padlen );
1797 pad_count += real_count *
1798 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1799 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001800
1801 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001802
Paul Bakkerd66f0702013-01-31 16:57:45 +01001803#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001804 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001805 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001806#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001807 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001808 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001809 else
1810#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1811 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001812 {
1813 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001814 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001815 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001816
1817 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001818 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001819 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001820#endif /* POLARSSL_CIPHER_MODE_CBC &&
1821 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001822 {
1823 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001824 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001825 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001826
1827 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1828 ssl->in_msg, ssl->in_msglen );
1829
1830 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001831 * Authenticate if not done yet.
1832 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001833 */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001834#if defined(POLARSSL_SOME_MODES_USE_MAC)
1835 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001836 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001837 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1838
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001839 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001840
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001841 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1842 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001843
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001844 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001845
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001846#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001847 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1848 {
1849 ssl_mac( &ssl->transform_in->md_ctx_dec,
1850 ssl->transform_in->mac_dec,
1851 ssl->in_msg, ssl->in_msglen,
1852 ssl->in_ctr, ssl->in_msgtype );
1853 }
1854 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001855#endif /* POLARSSL_SSL_PROTO_SSL3 */
1856#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001857 defined(POLARSSL_SSL_PROTO_TLS1_2)
1858 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1859 {
1860 /*
1861 * Process MAC and always update for padlen afterwards to make
1862 * total time independent of padlen
1863 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001864 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001865 *
1866 * Known timing attacks:
1867 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1868 *
1869 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1870 * correctly. (We round down instead of up, so -56 is the correct
1871 * value for our calculations instead of -55)
1872 */
1873 size_t j, extra_run = 0;
1874 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1875 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001876
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001877 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001878
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001879 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
1880 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
1881 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001882 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1883 ssl->in_msglen );
1884 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1885 ssl->in_msg + ssl->in_msglen );
1886 for( j = 0; j < extra_run; j++ )
1887 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001888
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001889 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1890 }
1891 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001892#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001893 POLARSSL_SSL_PROTO_TLS1_2 */
1894 {
1895 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001896 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001897 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001898
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001899 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1900 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1901 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001902
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001903 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001904 ssl->transform_in->maclen ) != 0 )
1905 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001906#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001907 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001908#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001909 correct = 0;
1910 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001911 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001912
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001913 /*
1914 * Finally check the correct flag
1915 */
1916 if( correct == 0 )
1917 return( POLARSSL_ERR_SSL_INVALID_MAC );
1918 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001919#endif /* POLARSSL_SOME_MODES_USE_MAC */
1920
1921 /* Make extra sure authentication was performed, exactly once */
1922 if( auth_done != 1 )
1923 {
1924 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1925 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1926 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001927
1928 if( ssl->in_msglen == 0 )
1929 {
1930 ssl->nb_zero++;
1931
1932 /*
1933 * Three or more empty messages may be a DoS attack
1934 * (excessive CPU consumption).
1935 */
1936 if( ssl->nb_zero > 3 )
1937 {
1938 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1939 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001940 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001941 }
1942 }
1943 else
1944 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001945
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001946#if defined(POLARSSL_SSL_PROTO_DTLS)
1947 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001948 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02001949 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001950 }
1951 else
1952#endif
1953 {
1954 for( i = 8; i > ssl_ep_len( ssl ); i-- )
1955 if( ++ssl->in_ctr[i - 1] != 0 )
1956 break;
1957
1958 /* The loop goes to its end iff the counter is wrapping */
1959 if( i == ssl_ep_len( ssl ) )
1960 {
1961 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1962 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1963 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001964 }
1965
Paul Bakker5121ce52009-01-03 21:22:43 +00001966 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1967
1968 return( 0 );
1969}
1970
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001971#undef MAC_NONE
1972#undef MAC_PLAINTEXT
1973#undef MAC_CIPHERTEXT
1974
Paul Bakker2770fbd2012-07-03 13:30:23 +00001975#if defined(POLARSSL_ZLIB_SUPPORT)
1976/*
1977 * Compression/decompression functions
1978 */
1979static int ssl_compress_buf( ssl_context *ssl )
1980{
1981 int ret;
1982 unsigned char *msg_post = ssl->out_msg;
1983 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001984 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001985
1986 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1987
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001988 if( len_pre == 0 )
1989 return( 0 );
1990
Paul Bakker2770fbd2012-07-03 13:30:23 +00001991 memcpy( msg_pre, ssl->out_msg, len_pre );
1992
1993 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1994 ssl->out_msglen ) );
1995
1996 SSL_DEBUG_BUF( 4, "before compression: output payload",
1997 ssl->out_msg, ssl->out_msglen );
1998
Paul Bakker48916f92012-09-16 19:57:18 +00001999 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2000 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2001 ssl->transform_out->ctx_deflate.next_out = msg_post;
2002 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002003
Paul Bakker48916f92012-09-16 19:57:18 +00002004 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002005 if( ret != Z_OK )
2006 {
2007 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2008 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
2009 }
2010
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002011 ssl->out_msglen = SSL_BUFFER_LEN -
2012 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002013
Paul Bakker2770fbd2012-07-03 13:30:23 +00002014 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
2015 ssl->out_msglen ) );
2016
2017 SSL_DEBUG_BUF( 4, "after compression: output payload",
2018 ssl->out_msg, ssl->out_msglen );
2019
2020 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
2021
2022 return( 0 );
2023}
2024
2025static int ssl_decompress_buf( ssl_context *ssl )
2026{
2027 int ret;
2028 unsigned char *msg_post = ssl->in_msg;
2029 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002030 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002031
2032 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
2033
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002034 if( len_pre == 0 )
2035 return( 0 );
2036
Paul Bakker2770fbd2012-07-03 13:30:23 +00002037 memcpy( msg_pre, ssl->in_msg, len_pre );
2038
2039 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
2040 ssl->in_msglen ) );
2041
2042 SSL_DEBUG_BUF( 4, "before decompression: input payload",
2043 ssl->in_msg, ssl->in_msglen );
2044
Paul Bakker48916f92012-09-16 19:57:18 +00002045 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2046 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2047 ssl->transform_in->ctx_inflate.next_out = msg_post;
2048 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002049
Paul Bakker48916f92012-09-16 19:57:18 +00002050 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002051 if( ret != Z_OK )
2052 {
2053 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2054 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
2055 }
2056
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002057 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
2058 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002059
Paul Bakker2770fbd2012-07-03 13:30:23 +00002060 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
2061 ssl->in_msglen ) );
2062
2063 SSL_DEBUG_BUF( 4, "after decompression: input payload",
2064 ssl->in_msg, ssl->in_msglen );
2065
2066 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
2067
2068 return( 0 );
2069}
2070#endif /* POLARSSL_ZLIB_SUPPORT */
2071
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002072#if defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002073static int ssl_write_hello_request( ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002074
2075#if defined(POLARSSL_SSL_PROTO_DTLS)
2076static int ssl_resend_hello_request( ssl_context *ssl )
2077{
2078 /* If renegotiation is not enforced, retransmit until we would reach max
2079 * timeout if we were using the usual handshake doubling scheme */
2080 if( ssl->renego_max_records < 0 )
2081 {
2082 uint32_t ratio = ssl->hs_timeout_max / ssl->hs_timeout_min + 1;
2083 unsigned char doublings = 1;
2084
2085 while( ratio != 0 )
2086 {
2087 ++doublings;
2088 ratio >>= 1;
2089 }
2090
2091 if( ++ssl->renego_records_seen > doublings )
2092 {
2093 SSL_DEBUG_MSG( 0, ( "no longer retransmitting hello request" ) );
2094 return( 0 );
2095 }
2096 }
2097
2098 return( ssl_write_hello_request( ssl ) );
2099}
2100#endif
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002101#endif /* POLARSSL_SSL_SRV_C && POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002102
Paul Bakker5121ce52009-01-03 21:22:43 +00002103/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002104 * Fill the input message buffer by appending data to it.
2105 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002106 *
2107 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2108 * available (from this read and/or a previous one). Otherwise, an error code
2109 * is returned (possibly EOF or WANT_READ).
2110 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002111 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2112 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2113 * since we always read a whole datagram at once.
2114 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002115 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002116 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002117 */
Paul Bakker23986e52011-04-24 08:57:21 +00002118int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002119{
Paul Bakker23986e52011-04-24 08:57:21 +00002120 int ret;
2121 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002122
2123 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
2124
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002125 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2126 {
2127 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2128 "or ssl_set_bio_timeout()" ) );
2129 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2130 }
2131
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002132 if( nb_want > SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002133 {
2134 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2135 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2136 }
2137
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002138#if defined(POLARSSL_SSL_PROTO_DTLS)
2139 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002141 uint32_t timeout;
2142
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002143 /*
2144 * The point is, we need to always read a full datagram at once, so we
2145 * sometimes read more then requested, and handle the additional data.
2146 * It could be the rest of the current record (while fetching the
2147 * header) and/or some other records in the same datagram.
2148 */
2149
2150 /*
2151 * Move to the next record in the already read datagram if applicable
2152 */
2153 if( ssl->next_record_offset != 0 )
2154 {
2155 if( ssl->in_left < ssl->next_record_offset )
2156 {
2157 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2158 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2159 }
2160
2161 ssl->in_left -= ssl->next_record_offset;
2162
2163 if( ssl->in_left != 0 )
2164 {
2165 SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
2166 ssl->next_record_offset ) );
2167 memmove( ssl->in_hdr,
2168 ssl->in_hdr + ssl->next_record_offset,
2169 ssl->in_left );
2170 }
2171
2172 ssl->next_record_offset = 0;
2173 }
2174
Paul Bakker5121ce52009-01-03 21:22:43 +00002175 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2176 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002177
2178 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002179 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002180 */
2181 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002182 {
2183 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002184 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002185 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002186
2187 /*
2188 * A record can't be split accross datagrams. If we need to read but
2189 * are not at the beginning of a new record, the caller did something
2190 * wrong.
2191 */
2192 if( ssl->in_left != 0 )
2193 {
2194 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2195 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2196 }
2197
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002198 SSL_DEBUG_MSG( 3, ( "current timer: %u", ssl->time_limit ) );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002199
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002200 /*
2201 * Don't even try to read if time's out already.
2202 * This avoids by-passing the timer when repeatedly receiving messages
2203 * that will end up being dropped.
2204 */
2205 if( ssl_check_timer( ssl ) != 0 )
2206 ret = POLARSSL_ERR_NET_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002207 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002208 {
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002209 len = SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
2210
2211 if( ssl->state != SSL_HANDSHAKE_OVER )
2212 timeout = ssl->handshake->retransmit_timeout;
2213 else
2214 timeout = ssl->read_timeout;
2215
2216 SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
2217
2218 if( ssl->f_recv_timeout != NULL && timeout != 0 )
2219 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2220 timeout );
2221 else
2222 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2223
2224 SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
2225
2226 if( ret == 0 )
2227 return( POLARSSL_ERR_SSL_CONN_EOF );
2228 }
2229
2230 if( ret == POLARSSL_ERR_NET_TIMEOUT )
2231 {
2232 SSL_DEBUG_MSG( 2, ( "timeout" ) );
2233 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002234
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002235 if( ssl->state != SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002236 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002237 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2238 {
2239 SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
2240 return( POLARSSL_ERR_NET_TIMEOUT );
2241 }
2242
2243 if( ( ret = ssl_resend( ssl ) ) != 0 )
2244 {
2245 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2246 return( ret );
2247 }
2248
2249 return( POLARSSL_ERR_NET_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002250 }
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002251#if defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002252 else if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00002253 ssl->renego_status == SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002254 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002255 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002256 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002257 SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002258 return( ret );
2259 }
2260
2261 return( POLARSSL_ERR_NET_WANT_READ );
2262 }
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002263#endif /* POLARSSL_SSL_SRV_C && POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002264 }
2265
Paul Bakker5121ce52009-01-03 21:22:43 +00002266 if( ret < 0 )
2267 return( ret );
2268
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002269 ssl->in_left = ret;
2270 }
2271 else
2272#endif
2273 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002274 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2275 ssl->in_left, nb_want ) );
2276
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002277 while( ssl->in_left < nb_want )
2278 {
2279 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002280 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr + ssl->in_left, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002281
2282 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2283 ssl->in_left, nb_want ) );
2284 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2285
2286 if( ret == 0 )
2287 return( POLARSSL_ERR_SSL_CONN_EOF );
2288
2289 if( ret < 0 )
2290 return( ret );
2291
2292 ssl->in_left += ret;
2293 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002294 }
2295
2296 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2297
2298 return( 0 );
2299}
2300
2301/*
2302 * Flush any data not yet written
2303 */
2304int ssl_flush_output( ssl_context *ssl )
2305{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002306 int ret;
2307 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002308
2309 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2310
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002311 if( ssl->f_send == NULL )
2312 {
2313 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2314 "or ssl_set_bio_timeout()" ) );
2315 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2316 }
2317
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002318 /* Avoid incrementing counter if data is flushed */
2319 if( ssl->out_left == 0 )
2320 {
2321 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2322 return( 0 );
2323 }
2324
Paul Bakker5121ce52009-01-03 21:22:43 +00002325 while( ssl->out_left > 0 )
2326 {
2327 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002328 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002329
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002330 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
2331 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002332 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002333
Paul Bakker5121ce52009-01-03 21:22:43 +00002334 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2335
2336 if( ret <= 0 )
2337 return( ret );
2338
2339 ssl->out_left -= ret;
2340 }
2341
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002342 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002343 if( ++ssl->out_ctr[i - 1] != 0 )
2344 break;
2345
2346 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002347 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002348 {
2349 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2350 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
2351 }
2352
Paul Bakker5121ce52009-01-03 21:22:43 +00002353 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2354
2355 return( 0 );
2356}
2357
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002358/*
2359 * Functions to handle the DTLS retransmission state machine
2360 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002361#if defined(POLARSSL_SSL_PROTO_DTLS)
2362/*
2363 * Append current handshake message to current outgoing flight
2364 */
2365static int ssl_flight_append( ssl_context *ssl )
2366{
2367 ssl_flight_item *msg;
2368
2369 /* Allocate space for current message */
2370 if( ( msg = polarssl_malloc( sizeof( ssl_flight_item ) ) ) == NULL )
2371 {
2372 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed",
2373 sizeof( ssl_flight_item ) ) );
2374 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2375 }
2376
2377 if( ( msg->p = polarssl_malloc( ssl->out_msglen ) ) == NULL )
2378 {
2379 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard6b875fc2014-10-17 14:02:33 +02002380 polarssl_free( msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002381 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2382 }
2383
2384 /* Copy current handshake message with headers */
2385 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2386 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002387 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002388 msg->next = NULL;
2389
2390 /* Append to the current flight */
2391 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002392 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002393 else
2394 {
2395 ssl_flight_item *cur = ssl->handshake->flight;
2396 while( cur->next != NULL )
2397 cur = cur->next;
2398 cur->next = msg;
2399 }
2400
2401 return( 0 );
2402}
2403
2404/*
2405 * Free the current flight of handshake messages
2406 */
2407static void ssl_flight_free( ssl_flight_item *flight )
2408{
2409 ssl_flight_item *cur = flight;
2410 ssl_flight_item *next;
2411
2412 while( cur != NULL )
2413 {
2414 next = cur->next;
2415
2416 polarssl_free( cur->p );
2417 polarssl_free( cur );
2418
2419 cur = next;
2420 }
2421}
2422
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002423#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2424static void ssl_dtls_replay_reset( ssl_context *ssl );
2425#endif
2426
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002427/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002428 * Swap transform_out and out_ctr with the alternative ones
2429 */
2430static void ssl_swap_epochs( ssl_context *ssl )
2431{
2432 ssl_transform *tmp_transform;
2433 unsigned char tmp_out_ctr[8];
2434
2435 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2436 {
2437 SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
2438 return;
2439 }
2440
2441 SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
2442
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002443 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002444 tmp_transform = ssl->transform_out;
2445 ssl->transform_out = ssl->handshake->alt_transform_out;
2446 ssl->handshake->alt_transform_out = tmp_transform;
2447
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002448 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002449 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2450 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2451 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002452
2453 /* Adjust to the newly activated transform */
2454 if( ssl->transform_out != NULL &&
2455 ssl->minor_ver >= SSL_MINOR_VERSION_2 )
2456 {
2457 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2458 ssl->transform_out->fixed_ivlen;
2459 }
2460 else
2461 ssl->out_msg = ssl->out_iv;
2462
2463#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2464 if( ssl_hw_record_activate != NULL )
2465 {
2466 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
2467 {
2468 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
2469 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
2470 }
2471 }
2472#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002473}
2474
2475/*
2476 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002477 *
2478 * Need to remember the current message in case flush_output returns
2479 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002480 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002481 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002482int ssl_resend( ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002483{
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002484 SSL_DEBUG_MSG( 2, ( "=> ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002485
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002486 if( ssl->handshake->retransmit_state != SSL_RETRANS_SENDING )
2487 {
2488 SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
2489
2490 ssl->handshake->cur_msg = ssl->handshake->flight;
2491 ssl_swap_epochs( ssl );
2492
2493 ssl->handshake->retransmit_state = SSL_RETRANS_SENDING;
2494 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002495
2496 while( ssl->handshake->cur_msg != NULL )
2497 {
2498 int ret;
2499 ssl_flight_item *cur = ssl->handshake->cur_msg;
2500
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002501 /* Swap epochs before sending Finished: we can't do it after
2502 * sending ChangeCipherSpec, in case write returns WANT_READ.
2503 * Must be done before copying, may change out_msg pointer */
2504 if( cur->type == SSL_MSG_HANDSHAKE &&
2505 cur->p[0] == SSL_HS_FINISHED )
2506 {
2507 ssl_swap_epochs( ssl );
2508 }
2509
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002510 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002511 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002512 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002513
2514 ssl->handshake->cur_msg = cur->next;
2515
2516 SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
2517
2518 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2519 {
2520 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2521 return( ret );
2522 }
2523 }
2524
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002525 if( ssl->state == SSL_HANDSHAKE_OVER )
2526 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2527 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002528 {
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002529 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002530 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2531 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002532
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002533 SSL_DEBUG_MSG( 2, ( "<= ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002534
2535 return( 0 );
2536}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002537
2538/*
2539 * To be called when the last message of an incoming flight is received.
2540 */
2541void ssl_recv_flight_completed( ssl_context *ssl )
2542{
2543 /* We won't need to resend that one any more */
2544 ssl_flight_free( ssl->handshake->flight );
2545 ssl->handshake->flight = NULL;
2546 ssl->handshake->cur_msg = NULL;
2547
2548 /* The next incoming flight will start with this msg_seq */
2549 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2550
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002551 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002552 ssl_set_timer( ssl, 0 );
2553
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002554 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2555 ssl->in_msg[0] == SSL_HS_FINISHED )
2556 {
2557 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2558 }
2559 else
2560 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
2561}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002562
2563/*
2564 * To be called when the last message of an outgoing flight is send.
2565 */
2566void ssl_send_flight_completed( ssl_context *ssl )
2567{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002568 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002569 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002570
2571 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2572 ssl->in_msg[0] == SSL_HS_FINISHED )
2573 {
2574 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2575 }
2576 else
2577 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
2578}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002579#endif /* POLARSSL_SSL_PROTO_DTLS */
2580
Paul Bakker5121ce52009-01-03 21:22:43 +00002581/*
2582 * Record layer functions
2583 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002584
2585/*
2586 * Write current record.
2587 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2588 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002589int ssl_write_record( ssl_context *ssl )
2590{
Paul Bakker05ef8352012-05-08 09:17:57 +00002591 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002592 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002593
2594 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2595
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002596#if defined(POLARSSL_SSL_PROTO_DTLS)
2597 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2598 ssl->handshake != NULL &&
2599 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2600 {
2601 ; /* Skip special handshake treatment when resending */
2602 }
2603 else
2604#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002605 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2606 {
2607 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2608 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2609 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2610
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002611 /*
2612 * DTLS has additional fields in the Handshake layer,
2613 * between the length field and the actual payload:
2614 * uint16 message_seq;
2615 * uint24 fragment_offset;
2616 * uint24 fragment_length;
2617 */
2618#if defined(POLARSSL_SSL_PROTO_DTLS)
2619 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2620 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002621 /* Make room for the additional DTLS fields */
2622 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002623 ssl->out_msglen += 8;
2624 len += 8;
2625
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002626 /* Write message_seq and update it, except for HelloRequest */
2627 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2628 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002629 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2630 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2631 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002632 }
2633 else
2634 {
2635 ssl->out_msg[4] = 0;
2636 ssl->out_msg[5] = 0;
2637 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002638
2639 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2640 memset( ssl->out_msg + 6, 0x00, 3 );
2641 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002642 }
2643#endif /* POLARSSL_SSL_PROTO_DTLS */
2644
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002645 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2646 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002647 }
2648
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002649 /* Save handshake and CCS messages for resending */
2650#if defined(POLARSSL_SSL_PROTO_DTLS)
2651 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2652 ssl->handshake != NULL &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02002653 ssl->handshake->retransmit_state != SSL_RETRANS_SENDING &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002654 ( ssl->out_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC ||
2655 ssl->out_msgtype == SSL_MSG_HANDSHAKE ) )
2656 {
2657 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2658 {
2659 SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
2660 return( ret );
2661 }
2662 }
2663#endif
2664
Paul Bakker2770fbd2012-07-03 13:30:23 +00002665#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002666 if( ssl->transform_out != NULL &&
2667 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002668 {
2669 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2670 {
2671 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2672 return( ret );
2673 }
2674
2675 len = ssl->out_msglen;
2676 }
2677#endif /*POLARSSL_ZLIB_SUPPORT */
2678
Paul Bakker05ef8352012-05-08 09:17:57 +00002679#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002680 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002681 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002682 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002683
Paul Bakker05ef8352012-05-08 09:17:57 +00002684 ret = ssl_hw_record_write( ssl );
2685 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2686 {
2687 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002688 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002689 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002690
2691 if( ret == 0 )
2692 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002693 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002694#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002695 if( !done )
2696 {
2697 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002698 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2699 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002700
2701 ssl->out_len[0] = (unsigned char)( len >> 8 );
2702 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002703
Paul Bakker48916f92012-09-16 19:57:18 +00002704 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002705 {
2706 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2707 {
2708 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2709 return( ret );
2710 }
2711
2712 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002713 ssl->out_len[0] = (unsigned char)( len >> 8 );
2714 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002715 }
2716
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002717 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002718
2719 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2720 "version = [%d:%d], msglen = %d",
2721 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002722 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002723
2724 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002725 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002726 }
2727
Paul Bakker5121ce52009-01-03 21:22:43 +00002728 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2729 {
2730 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2731 return( ret );
2732 }
2733
2734 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2735
2736 return( 0 );
2737}
2738
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002739#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002740/*
2741 * Mark bits in bitmask (used for DTLS HS reassembly)
2742 */
2743static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2744{
2745 unsigned int start_bits, end_bits;
2746
2747 start_bits = 8 - ( offset % 8 );
2748 if( start_bits != 8 )
2749 {
2750 size_t first_byte_idx = offset / 8;
2751
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002752 /* Special case */
2753 if( len <= start_bits )
2754 {
2755 for( ; len != 0; len-- )
2756 mask[first_byte_idx] |= 1 << ( start_bits - len );
2757
2758 /* Avoid potential issues with offset or len becoming invalid */
2759 return;
2760 }
2761
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002762 offset += start_bits; /* Now offset % 8 == 0 */
2763 len -= start_bits;
2764
2765 for( ; start_bits != 0; start_bits-- )
2766 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2767 }
2768
2769 end_bits = len % 8;
2770 if( end_bits != 0 )
2771 {
2772 size_t last_byte_idx = ( offset + len ) / 8;
2773
2774 len -= end_bits; /* Now len % 8 == 0 */
2775
2776 for( ; end_bits != 0; end_bits-- )
2777 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2778 }
2779
2780 memset( mask + offset / 8, 0xFF, len / 8 );
2781}
2782
2783/*
2784 * Check that bitmask is full
2785 */
2786static int ssl_bitmask_check( unsigned char *mask, size_t len )
2787{
2788 size_t i;
2789
2790 for( i = 0; i < len / 8; i++ )
2791 if( mask[i] != 0xFF )
2792 return( -1 );
2793
2794 for( i = 0; i < len % 8; i++ )
2795 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2796 return( -1 );
2797
2798 return( 0 );
2799}
2800
2801/*
2802 * Reassemble fragmented DTLS handshake messages.
2803 *
2804 * Use a temporary buffer for reassembly, divided in two parts:
2805 * - the first holds the reassembled message (including handshake header),
2806 * - the second holds a bitmask indicating which parts of the message
2807 * (excluding headers) have been received so far.
2808 */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002809static int ssl_reassemble_dtls_handshake( ssl_context *ssl )
2810{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002811 unsigned char *msg, *bitmask;
2812 size_t frag_len, frag_off;
2813 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
2814
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002815 if( ssl->handshake == NULL )
2816 {
2817 SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
2818 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2819 }
2820
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002821 /*
2822 * For first fragment, check size and allocate buffer
2823 */
2824 if( ssl->handshake->hs_msg == NULL )
2825 {
2826 size_t alloc_len;
2827
2828 SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
2829 msg_len ) );
2830
2831 if( ssl->in_hslen > SSL_MAX_CONTENT_LEN )
2832 {
2833 SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
2834 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2835 }
2836
2837 /* The bitmask needs one bit per byte of message excluding header */
2838 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
2839
2840 ssl->handshake->hs_msg = polarssl_malloc( alloc_len );
2841 if( ssl->handshake->hs_msg == NULL )
2842 {
2843 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", alloc_len ) );
2844 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2845 }
2846
2847 memset( ssl->handshake->hs_msg, 0, alloc_len );
2848
2849 /* Prepare final header: copy msg_type, length and message_seq,
2850 * then add standardised fragment_offset and fragment_length */
2851 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
2852 memset( ssl->handshake->hs_msg + 6, 0, 3 );
2853 memcpy( ssl->handshake->hs_msg + 9,
2854 ssl->handshake->hs_msg + 1, 3 );
2855 }
2856 else
2857 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002858 /* Make sure msg_type and length are consistent */
2859 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002860 {
2861 SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
2862 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2863 }
2864 }
2865
2866 msg = ssl->handshake->hs_msg + 12;
2867 bitmask = msg + msg_len;
2868
2869 /*
2870 * Check and copy current fragment
2871 */
2872 frag_off = ( ssl->in_msg[6] << 16 ) |
2873 ( ssl->in_msg[7] << 8 ) |
2874 ssl->in_msg[8];
2875 frag_len = ( ssl->in_msg[9] << 16 ) |
2876 ( ssl->in_msg[10] << 8 ) |
2877 ssl->in_msg[11];
2878
2879 if( frag_off + frag_len > msg_len )
2880 {
2881 SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
2882 frag_off, frag_len, msg_len ) );
2883 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2884 }
2885
2886 if( frag_len + 12 > ssl->in_msglen )
2887 {
2888 SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
2889 frag_len, ssl->in_msglen ) );
2890 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2891 }
2892
2893 SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
2894 frag_off, frag_len ) );
2895
2896 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
2897 ssl_bitmask_set( bitmask, frag_off, frag_len );
2898
2899 /*
2900 * Do we have the complete message by now?
2901 * If yes, finalize it, else ask to read the next record.
2902 */
2903 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
2904 {
2905 SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
2906 return( POLARSSL_ERR_NET_WANT_READ );
2907 }
2908
2909 SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
2910
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02002911 if( frag_len + 12 < ssl->in_msglen )
2912 {
2913 /*
2914 * We'got more handshake messages in the same record.
2915 * This case is not handled now because no know implementation does
2916 * that and it's hard to test, so we prefer to fail cleanly for now.
2917 */
2918 SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
2919 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2920 }
2921
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002922 if( ssl->in_left > ssl->next_record_offset )
2923 {
2924 /*
2925 * We've got more data in the buffer after the current record,
2926 * that we don't want to overwrite. Move it before writing the
2927 * reassembled message, and adjust in_left and next_record_offset.
2928 */
2929 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
2930 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
2931 size_t remain_len = ssl->in_left - ssl->next_record_offset;
2932
2933 /* First compute and check new lengths */
2934 ssl->next_record_offset = new_remain - ssl->in_hdr;
2935 ssl->in_left = ssl->next_record_offset + remain_len;
2936
2937 if( ssl->in_left > SSL_BUFFER_LEN -
2938 (size_t)( ssl->in_hdr - ssl->in_buf ) )
2939 {
2940 SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
2941 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
2942 }
2943
2944 memmove( new_remain, cur_remain, remain_len );
2945 }
2946
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002947 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
2948
2949 polarssl_free( ssl->handshake->hs_msg );
2950 ssl->handshake->hs_msg = NULL;
2951
2952 SSL_DEBUG_BUF( 3, "reassembled handshake message",
2953 ssl->in_msg, ssl->in_hslen );
2954
2955 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002956}
2957#endif /* POLARSSL_SSL_PROTO_DTLS */
2958
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002959static int ssl_prepare_handshake_record( ssl_context *ssl )
2960{
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002961 if( ssl->in_msglen < ssl_hs_hdr_len( ssl ) )
2962 {
2963 SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
2964 ssl->in_msglen ) );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02002965 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002966 }
2967
2968 ssl->in_hslen = ssl_hs_hdr_len( ssl ) + (
2969 ( ssl->in_msg[1] << 16 ) |
2970 ( ssl->in_msg[2] << 8 ) |
2971 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002972
2973 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2974 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002975 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002976
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002977#if defined(POLARSSL_SSL_PROTO_DTLS)
2978 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002979 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002980 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002981 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002982
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002983 /* ssl->handshake is NULL when receiving ClientHello for renego */
2984 if( ssl->handshake != NULL &&
2985 recv_msg_seq != ssl->handshake->in_msg_seq )
2986 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002987 /* Retransmit only on last message from previous flight, to avoid
2988 * too many retransmissions.
2989 * Besides, No sane server ever retransmits HelloVerifyRequest */
2990 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard93017de2014-09-19 22:42:40 +02002991 ssl->in_msg[0] != SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002992 {
2993 SSL_DEBUG_MSG( 2, ( "received message from last flight, "
2994 "message_seq = %d, start_of_flight = %d",
2995 recv_msg_seq,
2996 ssl->handshake->in_flight_start_seq ) );
2997
2998 if( ( ret = ssl_resend( ssl ) ) != 0 )
2999 {
3000 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3001 return( ret );
3002 }
3003 }
3004 else
3005 {
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02003006 SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003007 "message_seq = %d, expected = %d",
3008 recv_msg_seq,
3009 ssl->handshake->in_msg_seq ) );
3010 }
3011
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003012 return( POLARSSL_ERR_NET_WANT_READ );
3013 }
3014 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003015
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003016 /* Reassemble if current message is fragmented or reassembly is
3017 * already in progress */
3018 if( ssl->in_msglen < ssl->in_hslen ||
3019 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3020 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3021 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003022 {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003023 SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
3024
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003025 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3026 {
3027 SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
3028 return( ret );
3029 }
3030 }
3031 }
3032 else
3033#endif /* POLARSSL_SSL_PROTO_DTLS */
3034 /* With TLS we don't handle fragmentation (for now) */
3035 if( ssl->in_msglen < ssl->in_hslen )
3036 {
3037 SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02003038 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003039 }
3040
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003041 if( ssl->state != SSL_HANDSHAKE_OVER )
3042 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
3043
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003044 /* Handshake message is complete, increment counter */
3045#if defined(POLARSSL_SSL_PROTO_DTLS)
3046 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3047 ssl->handshake != NULL )
3048 {
3049 ssl->handshake->in_msg_seq++;
3050 }
3051#endif
3052
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003053 return( 0 );
3054}
3055
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003056/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003057 * DTLS anti-replay: RFC 6347 4.1.2.6
3058 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003059 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3060 * Bit n is set iff record number in_window_top - n has been seen.
3061 *
3062 * Usually, in_window_top is the last record number seen and the lsb of
3063 * in_window is set. The only exception is the initial state (record number 0
3064 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003065 */
3066#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3067static void ssl_dtls_replay_reset( ssl_context *ssl )
3068{
3069 ssl->in_window_top = 0;
3070 ssl->in_window = 0;
3071}
3072
3073static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3074{
3075 return( ( (uint64_t) buf[0] << 40 ) |
3076 ( (uint64_t) buf[1] << 32 ) |
3077 ( (uint64_t) buf[2] << 24 ) |
3078 ( (uint64_t) buf[3] << 16 ) |
3079 ( (uint64_t) buf[4] << 8 ) |
3080 ( (uint64_t) buf[5] ) );
3081}
3082
3083/*
3084 * Return 0 if sequence number is acceptable, -1 otherwise
3085 */
3086int ssl_dtls_replay_check( ssl_context *ssl )
3087{
3088 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3089 uint64_t bit;
3090
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003091 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
3092 return( 0 );
3093
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003094 if( rec_seqnum > ssl->in_window_top )
3095 return( 0 );
3096
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003097 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003098
3099 if( bit >= 64 )
3100 return( -1 );
3101
3102 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3103 return( -1 );
3104
3105 return( 0 );
3106}
3107
3108/*
3109 * Update replay window on new validated record
3110 */
3111void ssl_dtls_replay_update( ssl_context *ssl )
3112{
3113 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3114
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003115 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
3116 return;
3117
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003118 if( rec_seqnum > ssl->in_window_top )
3119 {
3120 /* Update window_top and the contents of the window */
3121 uint64_t shift = rec_seqnum - ssl->in_window_top;
3122
3123 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003124 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003125 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003126 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003127 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003128 ssl->in_window |= 1;
3129 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003130
3131 ssl->in_window_top = rec_seqnum;
3132 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003133 else
3134 {
3135 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003136 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003137
3138 if( bit < 64 ) /* Always true, but be extra sure */
3139 ssl->in_window |= (uint64_t) 1 << bit;
3140 }
3141}
3142#endif /* POLARSSL_SSL_DTLS_ANTI_REPLAY */
3143
3144/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003145 * ContentType type;
3146 * ProtocolVersion version;
3147 * uint16 epoch; // DTLS only
3148 * uint48 sequence_number; // DTLS only
3149 * uint16 length;
3150 */
3151static int ssl_parse_record_header( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003152{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003153 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003154 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003155
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003156 SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, ssl_hdr_len( ssl ) );
3157
Paul Bakker5121ce52009-01-03 21:22:43 +00003158 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003159 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003160 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003161
3162 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
3163 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003164 ssl->in_msgtype,
3165 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003166
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003167 /* Check record type */
3168 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
3169 ssl->in_msgtype != SSL_MSG_ALERT &&
3170 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
3171 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
3172 {
3173 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003174
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003175 if( ( ret = ssl_send_alert_message( ssl,
3176 SSL_ALERT_LEVEL_FATAL,
3177 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
3178 {
3179 return( ret );
3180 }
3181
3182 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3183 }
3184
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003185#if defined(POLARSSL_SSL_PROTO_DTLS)
3186 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3187 {
3188 /* Drop unexpected ChangeCipherSpec messages */
3189 if( ssl->in_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC &&
3190 ssl->state != SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3191 ssl->state != SSL_SERVER_CHANGE_CIPHER_SPEC )
3192 {
3193 SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3194 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3195 }
3196
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02003197 /* Drop unexpected ApplicationData records,
3198 * except at the beginning of renegotiations */
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003199 if( ssl->in_msgtype == SSL_MSG_APPLICATION_DATA &&
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00003200 ssl->state != SSL_HANDSHAKE_OVER
3201#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00003202 && ! ( ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS &&
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00003203 ssl->state == SSL_SERVER_HELLO )
3204#endif
3205 )
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003206 {
3207 SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
3208 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3209 }
3210 }
3211#endif
3212
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003213 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003214 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003215 {
3216 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003217 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003218 }
3219
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003220 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003221 {
3222 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003223 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003224 }
3225
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003226 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003227#if defined(POLARSSL_SSL_PROTO_DTLS)
3228 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3229 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003230 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003231
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003232 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003233 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003234 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003235 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003236 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003237 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003238 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003239
3240#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3241 if( ssl_dtls_replay_check( ssl ) != 0 )
3242 {
3243 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3244 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3245 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003246#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003247 }
3248#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003249
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003250 /* Check length against the size of our buffer */
3251 if( ssl->in_msglen > SSL_BUFFER_LEN
3252 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003253 {
3254 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3255 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3256 }
3257
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003258 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003259 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003260 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003261 if( ssl->in_msglen < 1 ||
3262 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003263 {
3264 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003265 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003266 }
3267 }
3268 else
3269 {
Paul Bakker48916f92012-09-16 19:57:18 +00003270 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003271 {
3272 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003273 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003274 }
3275
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003276#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003277 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00003278 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003279 {
3280 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003281 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003282 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003283#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003284#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3285 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003286 /*
3287 * TLS encrypted messages can have up to 256 bytes of padding
3288 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003289 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003290 ssl->in_msglen > ssl->transform_in->minlen +
3291 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003292 {
3293 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003294 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003295 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003296#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003297 }
3298
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003299 return( 0 );
3300}
Paul Bakker5121ce52009-01-03 21:22:43 +00003301
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003302/*
3303 * If applicable, decrypt (and decompress) record content
3304 */
3305static int ssl_prepare_record_content( ssl_context *ssl )
3306{
3307 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003308
Paul Bakker5121ce52009-01-03 21:22:43 +00003309 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003310 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003311
Paul Bakker05ef8352012-05-08 09:17:57 +00003312#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003313 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003314 {
3315 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
3316
3317 ret = ssl_hw_record_read( ssl );
3318 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
3319 {
3320 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003321 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003322 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003323
3324 if( ret == 0 )
3325 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003326 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003327#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003328 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003329 {
3330 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3331 {
3332 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
3333 return( ret );
3334 }
3335
3336 SSL_DEBUG_BUF( 4, "input payload after decrypt",
3337 ssl->in_msg, ssl->in_msglen );
3338
3339 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
3340 {
3341 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003342 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003343 }
3344 }
3345
Paul Bakker2770fbd2012-07-03 13:30:23 +00003346#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003347 if( ssl->transform_in != NULL &&
3348 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003349 {
3350 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3351 {
3352 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
3353 return( ret );
3354 }
3355
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003356 // TODO: what's the purpose of these lines? is in_len used?
3357 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3358 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003359 }
3360#endif /* POLARSSL_ZLIB_SUPPORT */
3361
Manuel Pégourié-Gonnard8464a462014-09-24 14:05:32 +02003362#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003363 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3364 {
3365 ssl_dtls_replay_update( ssl );
3366 }
3367#endif
3368
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003369 return( 0 );
3370}
3371
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003372static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
3373
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003374/*
3375 * Read a record.
3376 *
3377 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
3378 * and continue reading until a valid record is found.
3379 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003380int ssl_read_record( ssl_context *ssl )
3381{
3382 int ret;
3383
3384 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
3385
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02003386 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003387 {
3388 /*
3389 * Get next Handshake message in the current record
3390 */
3391 ssl->in_msglen -= ssl->in_hslen;
3392
3393 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3394 ssl->in_msglen );
3395
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003396 SSL_DEBUG_BUF( 4, "remaining content in record",
3397 ssl->in_msg, ssl->in_msglen );
3398
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003399 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3400 return( ret );
3401
3402 return( 0 );
3403 }
3404
3405 ssl->in_hslen = 0;
3406
3407 /*
3408 * Read the record header and parse it
3409 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003410#if defined(POLARSSL_SSL_PROTO_DTLS)
3411read_record_header:
3412#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003413 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3414 {
3415 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3416 return( ret );
3417 }
3418
3419 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003420 {
3421#if defined(POLARSSL_SSL_PROTO_DTLS)
3422 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3423 {
3424 /* Ignore bad record and get next one; drop the whole datagram
3425 * since current header cannot be trusted to find the next record
3426 * in current datagram */
3427 ssl->next_record_offset = 0;
3428 ssl->in_left = 0;
3429
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003430 SSL_DEBUG_MSG( 1, ( "discarding invalid record (header)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003431 goto read_record_header;
3432 }
3433#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003434 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003435 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003436
3437 /*
3438 * Read and optionally decrypt the message contents
3439 */
3440 if( ( ret = ssl_fetch_input( ssl,
3441 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3442 {
3443 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3444 return( ret );
3445 }
3446
3447 /* Done reading this record, get ready for the next one */
3448#if defined(POLARSSL_SSL_PROTO_DTLS)
3449 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3450 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3451 else
3452#endif
3453 ssl->in_left = 0;
3454
3455 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003456 {
3457#if defined(POLARSSL_SSL_PROTO_DTLS)
3458 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3459 {
3460 /* Silently discard invalid records */
3461 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3462 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3463 {
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02003464#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
3465 if( ssl->badmac_limit != 0 &&
3466 ++ssl->badmac_seen >= ssl->badmac_limit )
3467 {
3468 SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
3469 return( POLARSSL_ERR_SSL_INVALID_MAC );
3470 }
3471#endif
3472
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003473 SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003474 goto read_record_header;
3475 }
3476
3477 return( ret );
3478 }
3479 else
3480#endif
3481 {
3482 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard9b669902015-03-06 16:52:46 +00003483#if defined(POLARSSL_SSL_ALL_ALERT_MESSAGES)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003484 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3485 {
3486 ssl_send_alert_message( ssl,
3487 SSL_ALERT_LEVEL_FATAL,
3488 SSL_ALERT_MSG_BAD_RECORD_MAC );
3489 }
3490#endif
3491 return( ret );
3492 }
3493 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003494
3495 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003496 * When we sent the last flight of the handshake, we MUST respond to a
3497 * retransmit of the peer's previous flight with a retransmit. (In
3498 * practice, only the Finished message will make it, other messages
3499 * including CCS use the old transform so they're dropped as invalid.)
3500 *
3501 * If the record we received is not a handshake message, however, it
3502 * means the peer received our last flight so we can clean up
3503 * handshake info.
3504 *
3505 * This check needs to be done before prepare_handshake() due to an edge
3506 * case: if the client immediately requests renegotiation, this
3507 * finishes the current handshake first, avoiding the new ClientHello
3508 * being mistaken for an ancient message in the current handshake.
3509 */
3510#if defined(POLARSSL_SSL_PROTO_DTLS)
3511 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3512 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003513 ssl->state == SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003514 {
3515 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3516 ssl->in_msg[0] == SSL_HS_FINISHED )
3517 {
3518 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3519
3520 if( ( ret = ssl_resend( ssl ) ) != 0 )
3521 {
3522 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3523 return( ret );
3524 }
3525
3526 return( POLARSSL_ERR_NET_WANT_READ );
3527 }
3528 else
3529 {
3530 ssl_handshake_wrapup_free_hs_transform( ssl );
3531 }
3532 }
3533#endif
3534
3535 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003536 * Handle particular types of records
3537 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003538 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3539 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003540 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3541 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003542 }
3543
3544 if( ssl->in_msgtype == SSL_MSG_ALERT )
3545 {
3546 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3547 ssl->in_msg[0], ssl->in_msg[1] ) );
3548
3549 /*
3550 * Ignore non-fatal alerts, except close_notify
3551 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003552 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003553 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003554 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3555 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003556 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003557 }
3558
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003559 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3560 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003561 {
3562 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003563 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003564 }
3565 }
3566
Paul Bakker5121ce52009-01-03 21:22:43 +00003567 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3568
3569 return( 0 );
3570}
3571
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003572int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3573{
3574 int ret;
3575
3576 if( ( ret = ssl_send_alert_message( ssl,
3577 SSL_ALERT_LEVEL_FATAL,
3578 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3579 {
3580 return( ret );
3581 }
3582
3583 return( 0 );
3584}
3585
Paul Bakker0a925182012-04-16 06:46:41 +00003586int ssl_send_alert_message( ssl_context *ssl,
3587 unsigned char level,
3588 unsigned char message )
3589{
3590 int ret;
3591
3592 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3593
3594 ssl->out_msgtype = SSL_MSG_ALERT;
3595 ssl->out_msglen = 2;
3596 ssl->out_msg[0] = level;
3597 ssl->out_msg[1] = message;
3598
3599 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3600 {
3601 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3602 return( ret );
3603 }
3604
3605 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3606
3607 return( 0 );
3608}
3609
Paul Bakker5121ce52009-01-03 21:22:43 +00003610/*
3611 * Handshake functions
3612 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003613#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3614 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3615 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3616 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3617 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3618 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3619 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003620int ssl_write_certificate( ssl_context *ssl )
3621{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003622 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003623
3624 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3625
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003626 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003627 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3628 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003629 {
3630 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3631 ssl->state++;
3632 return( 0 );
3633 }
3634
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003635 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3636 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003637}
3638
3639int ssl_parse_certificate( ssl_context *ssl )
3640{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003641 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3642
3643 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3644
3645 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003646 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3647 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003648 {
3649 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3650 ssl->state++;
3651 return( 0 );
3652 }
3653
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003654 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3655 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003656}
3657#else
3658int ssl_write_certificate( ssl_context *ssl )
3659{
3660 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3661 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003662 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003663 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3664
3665 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3666
3667 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003668 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3669 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003670 {
3671 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3672 ssl->state++;
3673 return( 0 );
3674 }
3675
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003676#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003677 if( ssl->endpoint == SSL_IS_CLIENT )
3678 {
3679 if( ssl->client_auth == 0 )
3680 {
3681 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3682 ssl->state++;
3683 return( 0 );
3684 }
3685
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003686#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003687 /*
3688 * If using SSLv3 and got no cert, send an Alert message
3689 * (otherwise an empty Certificate message will be sent).
3690 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003691 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003692 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3693 {
3694 ssl->out_msglen = 2;
3695 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003696 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3697 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003698
3699 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3700 goto write_msg;
3701 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003702#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003703 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003704#endif /* POLARSSL_SSL_CLI_C */
3705#if defined(POLARSSL_SSL_SRV_C)
3706 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003707 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003708 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003709 {
3710 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003711 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003712 }
3713 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003714#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003715
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003716 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003717
3718 /*
3719 * 0 . 0 handshake type
3720 * 1 . 3 handshake length
3721 * 4 . 6 length of all certs
3722 * 7 . 9 length of cert. 1
3723 * 10 . n-1 peer certificate
3724 * n . n+2 length of cert. 2
3725 * n+3 . ... upper level cert, etc.
3726 */
3727 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003728 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003729
Paul Bakker29087132010-03-21 21:03:34 +00003730 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003731 {
3732 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003733 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003734 {
3735 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3736 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003737 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003738 }
3739
3740 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3741 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3742 ssl->out_msg[i + 2] = (unsigned char)( n );
3743
3744 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3745 i += n; crt = crt->next;
3746 }
3747
3748 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3749 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3750 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3751
3752 ssl->out_msglen = i;
3753 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3754 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3755
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003756#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003757write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003758#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003759
3760 ssl->state++;
3761
3762 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3763 {
3764 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3765 return( ret );
3766 }
3767
3768 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3769
Paul Bakkered27a042013-04-18 22:46:23 +02003770 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003771}
3772
3773int ssl_parse_certificate( ssl_context *ssl )
3774{
Paul Bakkered27a042013-04-18 22:46:23 +02003775 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003776 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003777 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003778
3779 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3780
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003781 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003782 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3783 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003784 {
3785 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3786 ssl->state++;
3787 return( 0 );
3788 }
3789
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003790#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003791 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003792 ( ssl->authmode == SSL_VERIFY_NONE ||
3793 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003794 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003795 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003796 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3797 ssl->state++;
3798 return( 0 );
3799 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003800#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003801
3802 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3803 {
3804 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3805 return( ret );
3806 }
3807
3808 ssl->state++;
3809
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003810#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003811#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003812 /*
3813 * Check if the client sent an empty certificate
3814 */
3815 if( ssl->endpoint == SSL_IS_SERVER &&
3816 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3817 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003818 if( ssl->in_msglen == 2 &&
3819 ssl->in_msgtype == SSL_MSG_ALERT &&
3820 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3821 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003822 {
3823 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3824
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003825 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003826 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3827 return( 0 );
3828 else
Paul Bakker40e46942009-01-03 21:51:57 +00003829 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003830 }
3831 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003832#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003833
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003834#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3835 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003836 if( ssl->endpoint == SSL_IS_SERVER &&
3837 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3838 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003839 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003840 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3841 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003842 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003843 {
3844 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3845
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003846 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003847 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003848 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003849 else
3850 return( 0 );
3851 }
3852 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003853#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3854 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003855#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003856
3857 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3858 {
3859 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003860 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003861 }
3862
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003863 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3864 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003865 {
3866 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003867 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003868 }
3869
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003870 i = ssl_hs_hdr_len( ssl );
3871
Paul Bakker5121ce52009-01-03 21:22:43 +00003872 /*
3873 * Same message structure as in ssl_write_certificate()
3874 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003875 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003876
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003877 if( ssl->in_msg[i] != 0 ||
3878 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003879 {
3880 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003881 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003882 }
3883
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003884 /* In case we tried to reuse a session but it failed */
3885 if( ssl->session_negotiate->peer_cert != NULL )
3886 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003887 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003888 polarssl_free( ssl->session_negotiate->peer_cert );
3889 }
3890
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003891 if( ( ssl->session_negotiate->peer_cert = polarssl_malloc(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003892 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003893 {
3894 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003895 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003896 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003897 }
3898
Paul Bakkerb6b09562013-09-18 14:17:41 +02003899 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003900
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003901 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003902
3903 while( i < ssl->in_hslen )
3904 {
3905 if( ssl->in_msg[i] != 0 )
3906 {
3907 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003908 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003909 }
3910
3911 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3912 | (unsigned int) ssl->in_msg[i + 2];
3913 i += 3;
3914
3915 if( n < 128 || i + n > ssl->in_hslen )
3916 {
3917 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003918 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003919 }
3920
Paul Bakkerddf26b42013-09-18 13:46:23 +02003921 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3922 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003923 if( ret != 0 )
3924 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003925 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003926 return( ret );
3927 }
3928
3929 i += n;
3930 }
3931
Paul Bakker48916f92012-09-16 19:57:18 +00003932 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003933
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003934 /*
3935 * On client, make sure the server cert doesn't change during renego to
3936 * avoid "triple handshake" attack: https://secure-resumption.com/
3937 */
Paul Bakkerf6080b82015-01-13 16:18:23 +01003938#if defined(POLARSSL_SSL_RENEGOTIATION) && defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003939 if( ssl->endpoint == SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00003940 ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003941 {
3942 if( ssl->session->peer_cert == NULL )
3943 {
3944 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3945 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3946 }
3947
3948 if( ssl->session->peer_cert->raw.len !=
3949 ssl->session_negotiate->peer_cert->raw.len ||
3950 memcmp( ssl->session->peer_cert->raw.p,
3951 ssl->session_negotiate->peer_cert->raw.p,
3952 ssl->session->peer_cert->raw.len ) != 0 )
3953 {
3954 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3955 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3956 }
3957 }
Paul Bakkerf6080b82015-01-13 16:18:23 +01003958#endif /* POLARSSL_SSL_RENEGOTIATION && POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003959
Paul Bakker5121ce52009-01-03 21:22:43 +00003960 if( ssl->authmode != SSL_VERIFY_NONE )
3961 {
3962 if( ssl->ca_chain == NULL )
3963 {
3964 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003965 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003966 }
3967
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003968 /*
3969 * Main check: verify certificate
3970 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003971 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3972 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3973 &ssl->session_negotiate->verify_result,
3974 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003975
3976 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003977 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003978 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003979 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003980
3981 /*
3982 * Secondary checks: always done, but change 'ret' only if it was 0
3983 */
3984
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003985#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003986 {
Manuel Pégourié-Gonnard0db107e2015-03-19 14:01:57 +00003987 const pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003988
3989 /* If certificate uses an EC key, make sure the curve is OK */
3990 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3991 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
3992 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003993 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003994 if( ret == 0 )
3995 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003996 }
3997 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003998#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00003999
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004000 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
4001 ciphersuite_info,
4002 ! ssl->endpoint ) != 0 )
4003 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02004004 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004005 if( ret == 0 )
4006 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
4007 }
4008
Paul Bakker5121ce52009-01-03 21:22:43 +00004009 if( ssl->authmode != SSL_VERIFY_REQUIRED )
4010 ret = 0;
4011 }
4012
4013 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
4014
4015 return( ret );
4016}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01004017#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
4018 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
4019 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
4020 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
4021 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
4022 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
4023 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004024
4025int ssl_write_change_cipher_spec( ssl_context *ssl )
4026{
4027 int ret;
4028
4029 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
4030
4031 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
4032 ssl->out_msglen = 1;
4033 ssl->out_msg[0] = 1;
4034
Paul Bakker5121ce52009-01-03 21:22:43 +00004035 ssl->state++;
4036
4037 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4038 {
4039 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4040 return( ret );
4041 }
4042
4043 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
4044
4045 return( 0 );
4046}
4047
4048int ssl_parse_change_cipher_spec( ssl_context *ssl )
4049{
4050 int ret;
4051
4052 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
4053
Paul Bakker5121ce52009-01-03 21:22:43 +00004054 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4055 {
4056 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4057 return( ret );
4058 }
4059
4060 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
4061 {
4062 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004063 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004064 }
4065
4066 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
4067 {
4068 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004069 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00004070 }
4071
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004072 /*
4073 * Switch to our negotiated transform and session parameters for inbound
4074 * data.
4075 */
4076 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
4077 ssl->transform_in = ssl->transform_negotiate;
4078 ssl->session_in = ssl->session_negotiate;
4079
4080#if defined(POLARSSL_SSL_PROTO_DTLS)
4081 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4082 {
4083#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4084 ssl_dtls_replay_reset( ssl );
4085#endif
4086
4087 /* Increment epoch */
4088 if( ++ssl->in_epoch == 0 )
4089 {
4090 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4091 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4092 }
4093 }
4094 else
4095#endif /* POLARSSL_SSL_PROTO_DTLS */
4096 memset( ssl->in_ctr, 0, 8 );
4097
4098 /*
4099 * Set the in_msg pointer to the correct location based on IV length
4100 */
4101 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4102 {
4103 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
4104 ssl->transform_negotiate->fixed_ivlen;
4105 }
4106 else
4107 ssl->in_msg = ssl->in_iv;
4108
4109#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4110 if( ssl_hw_record_activate != NULL )
4111 {
4112 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
4113 {
4114 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4115 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4116 }
4117 }
4118#endif
4119
Paul Bakker5121ce52009-01-03 21:22:43 +00004120 ssl->state++;
4121
4122 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
4123
4124 return( 0 );
4125}
4126
Paul Bakker41c83d32013-03-20 14:39:14 +01004127void ssl_optimize_checksum( ssl_context *ssl,
4128 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00004129{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02004130 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01004131
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004132#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4133 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00004134 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00004135 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00004136 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004137#endif
4138#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4139#if defined(POLARSSL_SHA512_C)
4140 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
4141 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
4142 else
4143#endif
4144#if defined(POLARSSL_SHA256_C)
4145 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00004146 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004147 else
4148#endif
4149#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004150 {
4151 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004152 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004153 }
Paul Bakker380da532012-04-18 16:10:25 +00004154}
Paul Bakkerf7abd422013-04-16 13:15:56 +02004155
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004156void ssl_reset_checksum( ssl_context *ssl )
4157{
4158#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4159 defined(POLARSSL_SSL_PROTO_TLS1_1)
4160 md5_starts( &ssl->handshake->fin_md5 );
4161 sha1_starts( &ssl->handshake->fin_sha1 );
4162#endif
4163#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4164#if defined(POLARSSL_SHA256_C)
4165 sha256_starts( &ssl->handshake->fin_sha256, 0 );
4166#endif
4167#if defined(POLARSSL_SHA512_C)
4168 sha512_starts( &ssl->handshake->fin_sha512, 1 );
4169#endif
4170#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4171}
4172
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004173static void ssl_update_checksum_start( ssl_context *ssl,
4174 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004175{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004176#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4177 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00004178 md5_update( &ssl->handshake->fin_md5 , buf, len );
4179 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004180#endif
4181#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4182#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02004183 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004184#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02004185#if defined(POLARSSL_SHA512_C)
4186 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01004187#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004188#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004189}
4190
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004191#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4192 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004193static void ssl_update_checksum_md5sha1( ssl_context *ssl,
4194 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004195{
Paul Bakker48916f92012-09-16 19:57:18 +00004196 md5_update( &ssl->handshake->fin_md5 , buf, len );
4197 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004198}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004199#endif
Paul Bakker380da532012-04-18 16:10:25 +00004200
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004201#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4202#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004203static void ssl_update_checksum_sha256( ssl_context *ssl,
4204 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004205{
Paul Bakker9e36f042013-06-30 14:34:05 +02004206 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004207}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004208#endif
Paul Bakker380da532012-04-18 16:10:25 +00004209
Paul Bakker9e36f042013-06-30 14:34:05 +02004210#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004211static void ssl_update_checksum_sha384( ssl_context *ssl,
4212 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004213{
Paul Bakker9e36f042013-06-30 14:34:05 +02004214 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004215}
Paul Bakker769075d2012-11-24 11:26:46 +01004216#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004217#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004218
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004219#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004220static void ssl_calc_finished_ssl(
4221 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004222{
Paul Bakker3c2122f2013-06-24 19:03:14 +02004223 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004224 md5_context md5;
4225 sha1_context sha1;
4226
Paul Bakker5121ce52009-01-03 21:22:43 +00004227 unsigned char padbuf[48];
4228 unsigned char md5sum[16];
4229 unsigned char sha1sum[20];
4230
Paul Bakker48916f92012-09-16 19:57:18 +00004231 ssl_session *session = ssl->session_negotiate;
4232 if( !session )
4233 session = ssl->session;
4234
Paul Bakker1ef83d62012-04-11 12:09:53 +00004235 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
4236
Paul Bakker48916f92012-09-16 19:57:18 +00004237 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4238 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004239
4240 /*
4241 * SSLv3:
4242 * hash =
4243 * MD5( master + pad2 +
4244 * MD5( handshake + sender + master + pad1 ) )
4245 * + SHA1( master + pad2 +
4246 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004247 */
4248
Paul Bakker90995b52013-06-24 19:20:35 +02004249#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004250 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004251 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004252#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004253
Paul Bakker90995b52013-06-24 19:20:35 +02004254#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004255 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004256 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004257#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004258
Paul Bakker3c2122f2013-06-24 19:03:14 +02004259 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
4260 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004261
Paul Bakker1ef83d62012-04-11 12:09:53 +00004262 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004263
Paul Bakker3c2122f2013-06-24 19:03:14 +02004264 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004265 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004266 md5_update( &md5, padbuf, 48 );
4267 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004268
Paul Bakker3c2122f2013-06-24 19:03:14 +02004269 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004270 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004271 sha1_update( &sha1, padbuf, 40 );
4272 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004273
Paul Bakker1ef83d62012-04-11 12:09:53 +00004274 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004275
Paul Bakker1ef83d62012-04-11 12:09:53 +00004276 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00004277 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004278 md5_update( &md5, padbuf, 48 );
4279 md5_update( &md5, md5sum, 16 );
4280 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004281
Paul Bakker1ef83d62012-04-11 12:09:53 +00004282 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00004283 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004284 sha1_update( &sha1, padbuf , 40 );
4285 sha1_update( &sha1, sha1sum, 20 );
4286 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004287
Paul Bakker1ef83d62012-04-11 12:09:53 +00004288 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004289
Paul Bakker5b4af392014-06-26 12:09:34 +02004290 md5_free( &md5 );
4291 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004292
Paul Bakker34617722014-06-13 17:20:13 +02004293 polarssl_zeroize( padbuf, sizeof( padbuf ) );
4294 polarssl_zeroize( md5sum, sizeof( md5sum ) );
4295 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004296
4297 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4298}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004299#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004300
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004301#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004302static void ssl_calc_finished_tls(
4303 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004304{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004305 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004306 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004307 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00004308 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004309 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004310
Paul Bakker48916f92012-09-16 19:57:18 +00004311 ssl_session *session = ssl->session_negotiate;
4312 if( !session )
4313 session = ssl->session;
4314
Paul Bakker1ef83d62012-04-11 12:09:53 +00004315 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004316
Paul Bakker48916f92012-09-16 19:57:18 +00004317 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4318 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004319
Paul Bakker1ef83d62012-04-11 12:09:53 +00004320 /*
4321 * TLSv1:
4322 * hash = PRF( master, finished_label,
4323 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4324 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004325
Paul Bakker90995b52013-06-24 19:20:35 +02004326#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004327 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4328 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004329#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004330
Paul Bakker90995b52013-06-24 19:20:35 +02004331#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004332 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4333 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004334#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004335
4336 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004337 ? "client finished"
4338 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004339
4340 md5_finish( &md5, padbuf );
4341 sha1_finish( &sha1, padbuf + 16 );
4342
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004343 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004344 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004345
4346 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4347
Paul Bakker5b4af392014-06-26 12:09:34 +02004348 md5_free( &md5 );
4349 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004350
Paul Bakker34617722014-06-13 17:20:13 +02004351 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004352
4353 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4354}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004355#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004356
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004357#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4358#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004359static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004360 ssl_context *ssl, unsigned char *buf, int from )
4361{
4362 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004363 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004364 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004365 unsigned char padbuf[32];
4366
Paul Bakker48916f92012-09-16 19:57:18 +00004367 ssl_session *session = ssl->session_negotiate;
4368 if( !session )
4369 session = ssl->session;
4370
Paul Bakker380da532012-04-18 16:10:25 +00004371 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004372
Paul Bakker9e36f042013-06-30 14:34:05 +02004373 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004374
4375 /*
4376 * TLSv1.2:
4377 * hash = PRF( master, finished_label,
4378 * Hash( handshake ) )[0.11]
4379 */
4380
Paul Bakker9e36f042013-06-30 14:34:05 +02004381#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004382 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004383 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004384#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004385
4386 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004387 ? "client finished"
4388 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004389
Paul Bakker9e36f042013-06-30 14:34:05 +02004390 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004391
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004392 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004393 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004394
4395 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4396
Paul Bakker5b4af392014-06-26 12:09:34 +02004397 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004398
Paul Bakker34617722014-06-13 17:20:13 +02004399 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004400
4401 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4402}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004403#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004404
Paul Bakker9e36f042013-06-30 14:34:05 +02004405#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004406static void ssl_calc_finished_tls_sha384(
4407 ssl_context *ssl, unsigned char *buf, int from )
4408{
4409 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004410 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004411 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004412 unsigned char padbuf[48];
4413
Paul Bakker48916f92012-09-16 19:57:18 +00004414 ssl_session *session = ssl->session_negotiate;
4415 if( !session )
4416 session = ssl->session;
4417
Paul Bakker380da532012-04-18 16:10:25 +00004418 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004419
Paul Bakker9e36f042013-06-30 14:34:05 +02004420 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004421
4422 /*
4423 * TLSv1.2:
4424 * hash = PRF( master, finished_label,
4425 * Hash( handshake ) )[0.11]
4426 */
4427
Paul Bakker9e36f042013-06-30 14:34:05 +02004428#if !defined(POLARSSL_SHA512_ALT)
4429 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4430 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004431#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004432
4433 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004434 ? "client finished"
4435 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004436
Paul Bakker9e36f042013-06-30 14:34:05 +02004437 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004438
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004439 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004440 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004441
4442 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4443
Paul Bakker5b4af392014-06-26 12:09:34 +02004444 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004445
Paul Bakker34617722014-06-13 17:20:13 +02004446 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004447
4448 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4449}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004450#endif /* POLARSSL_SHA512_C */
4451#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004452
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004453static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004454{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004455 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004456
4457 /*
4458 * Free our handshake params
4459 */
4460 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004461 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004462 ssl->handshake = NULL;
4463
4464 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004465 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004466 */
4467 if( ssl->transform )
4468 {
4469 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004470 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004471 }
4472 ssl->transform = ssl->transform_negotiate;
4473 ssl->transform_negotiate = NULL;
4474
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004475 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4476}
4477
4478void ssl_handshake_wrapup( ssl_context *ssl )
4479{
4480 int resume = ssl->handshake->resume;
4481
4482 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4483
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004484#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00004485 if( ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004486 {
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00004487 ssl->renego_status = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004488 ssl->renego_records_seen = 0;
4489 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004490#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004491
4492 /*
4493 * Free the previous session and switch in the current one
4494 */
Paul Bakker0a597072012-09-25 21:55:46 +00004495 if( ssl->session )
4496 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01004497#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4498 /* RFC 7366 3.1: keep the EtM state */
4499 ssl->session_negotiate->encrypt_then_mac =
4500 ssl->session->encrypt_then_mac;
4501#endif
4502
Paul Bakker0a597072012-09-25 21:55:46 +00004503 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004504 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004505 }
4506 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004507 ssl->session_negotiate = NULL;
4508
Paul Bakker0a597072012-09-25 21:55:46 +00004509 /*
4510 * Add cache entry
4511 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004512 if( ssl->f_set_cache != NULL &&
4513 ssl->session->length != 0 &&
4514 resume == 0 )
4515 {
Paul Bakker0a597072012-09-25 21:55:46 +00004516 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4517 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004518 }
Paul Bakker0a597072012-09-25 21:55:46 +00004519
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004520#if defined(POLARSSL_SSL_PROTO_DTLS)
4521 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4522 ssl->handshake->flight != NULL )
4523 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02004524 /* Cancel handshake timer */
4525 ssl_set_timer( ssl, 0 );
4526
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004527 /* Keep last flight around in case we need to resend it:
4528 * we need the handshake and transform structures for that */
4529 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4530 }
4531 else
4532#endif
4533 ssl_handshake_wrapup_free_hs_transform( ssl );
4534
Paul Bakker48916f92012-09-16 19:57:18 +00004535 ssl->state++;
4536
4537 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4538}
4539
Paul Bakker1ef83d62012-04-11 12:09:53 +00004540int ssl_write_finished( ssl_context *ssl )
4541{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004542 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004543
4544 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4545
Paul Bakker92be97b2013-01-02 17:30:03 +01004546 /*
4547 * Set the out_msg pointer to the correct location based on IV length
4548 */
4549 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4550 {
4551 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4552 ssl->transform_negotiate->fixed_ivlen;
4553 }
4554 else
4555 ssl->out_msg = ssl->out_iv;
4556
Paul Bakker48916f92012-09-16 19:57:18 +00004557 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004558
4559 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004560 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4561
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004562#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004563 ssl->verify_data_len = hash_len;
4564 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004565#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004566
Paul Bakker5121ce52009-01-03 21:22:43 +00004567 ssl->out_msglen = 4 + hash_len;
4568 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4569 ssl->out_msg[0] = SSL_HS_FINISHED;
4570
4571 /*
4572 * In case of session resuming, invert the client and server
4573 * ChangeCipherSpec messages order.
4574 */
Paul Bakker0a597072012-09-25 21:55:46 +00004575 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004576 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004577#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004578 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004579 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004580#endif
4581#if defined(POLARSSL_SSL_SRV_C)
4582 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004583 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004584#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004585 }
4586 else
4587 ssl->state++;
4588
Paul Bakker48916f92012-09-16 19:57:18 +00004589 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004590 * Switch to our negotiated transform and session parameters for outbound
4591 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004592 */
4593 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004594
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004595#if defined(POLARSSL_SSL_PROTO_DTLS)
4596 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4597 {
4598 unsigned char i;
4599
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004600 /* Remember current epoch settings for resending */
4601 ssl->handshake->alt_transform_out = ssl->transform_out;
4602 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4603
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004604 /* Set sequence_number to zero */
4605 memset( ssl->out_ctr + 2, 0, 6 );
4606
4607 /* Increment epoch */
4608 for( i = 2; i > 0; i-- )
4609 if( ++ssl->out_ctr[i - 1] != 0 )
4610 break;
4611
4612 /* The loop goes to its end iff the counter is wrapping */
4613 if( i == 0 )
4614 {
4615 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4616 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4617 }
4618 }
4619 else
4620#endif /* POLARSSL_SSL_PROTO_DTLS */
4621 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004622
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004623 ssl->transform_out = ssl->transform_negotiate;
4624 ssl->session_out = ssl->session_negotiate;
4625
Paul Bakker07eb38b2012-12-19 14:42:06 +01004626#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004627 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004628 {
4629 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4630 {
4631 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4632 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4633 }
4634 }
4635#endif
4636
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004637#if defined(POLARSSL_SSL_PROTO_DTLS)
4638 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4639 ssl_send_flight_completed( ssl );
4640#endif
4641
Paul Bakker5121ce52009-01-03 21:22:43 +00004642 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4643 {
4644 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4645 return( ret );
4646 }
4647
4648 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4649
4650 return( 0 );
4651}
4652
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004653#if defined(POLARSSL_SSL_PROTO_SSL3)
4654#define SSL_MAX_HASH_LEN 36
4655#else
4656#define SSL_MAX_HASH_LEN 12
4657#endif
4658
Paul Bakker5121ce52009-01-03 21:22:43 +00004659int ssl_parse_finished( ssl_context *ssl )
4660{
Paul Bakker23986e52011-04-24 08:57:21 +00004661 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004662 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004663 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004664
4665 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4666
Paul Bakker48916f92012-09-16 19:57:18 +00004667 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004668
Paul Bakker5121ce52009-01-03 21:22:43 +00004669 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4670 {
4671 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4672 return( ret );
4673 }
4674
4675 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4676 {
4677 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004678 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004679 }
4680
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004681 /* There is currently no ciphersuite using another length with TLS 1.2 */
4682#if defined(POLARSSL_SSL_PROTO_SSL3)
4683 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4684 hash_len = 36;
4685 else
4686#endif
4687 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004688
4689 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004690 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004691 {
4692 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004693 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004694 }
4695
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004696 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4697 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004698 {
4699 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004700 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004701 }
4702
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004703#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004704 ssl->verify_data_len = hash_len;
4705 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004706#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004707
Paul Bakker0a597072012-09-25 21:55:46 +00004708 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004709 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004710#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004711 if( ssl->endpoint == SSL_IS_CLIENT )
4712 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004713#endif
4714#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004715 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004716 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004717#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004718 }
4719 else
4720 ssl->state++;
4721
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004722#if defined(POLARSSL_SSL_PROTO_DTLS)
4723 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4724 ssl_recv_flight_completed( ssl );
4725#endif
4726
Paul Bakker5121ce52009-01-03 21:22:43 +00004727 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4728
4729 return( 0 );
4730}
4731
Paul Bakker968afaa2014-07-09 11:09:24 +02004732static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004733{
4734 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4735
4736#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4737 defined(POLARSSL_SSL_PROTO_TLS1_1)
4738 md5_init( &handshake->fin_md5 );
4739 sha1_init( &handshake->fin_sha1 );
4740 md5_starts( &handshake->fin_md5 );
4741 sha1_starts( &handshake->fin_sha1 );
4742#endif
4743#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4744#if defined(POLARSSL_SHA256_C)
4745 sha256_init( &handshake->fin_sha256 );
4746 sha256_starts( &handshake->fin_sha256, 0 );
4747#endif
4748#if defined(POLARSSL_SHA512_C)
4749 sha512_init( &handshake->fin_sha512 );
4750 sha512_starts( &handshake->fin_sha512, 1 );
4751#endif
4752#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4753
4754 handshake->update_checksum = ssl_update_checksum_start;
4755 handshake->sig_alg = SSL_HASH_SHA1;
4756
4757#if defined(POLARSSL_DHM_C)
4758 dhm_init( &handshake->dhm_ctx );
4759#endif
4760#if defined(POLARSSL_ECDH_C)
4761 ecdh_init( &handshake->ecdh_ctx );
4762#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004763}
4764
4765static void ssl_transform_init( ssl_transform *transform )
4766{
4767 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004768
4769 cipher_init( &transform->cipher_ctx_enc );
4770 cipher_init( &transform->cipher_ctx_dec );
4771
4772 md_init( &transform->md_ctx_enc );
4773 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004774}
4775
4776void ssl_session_init( ssl_session *session )
4777{
4778 memset( session, 0, sizeof(ssl_session) );
4779}
4780
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004781static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004782{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004783 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004784 if( ssl->transform_negotiate )
4785 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004786 if( ssl->session_negotiate )
4787 ssl_session_free( ssl->session_negotiate );
4788 if( ssl->handshake )
4789 ssl_handshake_free( ssl->handshake );
4790
4791 /*
4792 * Either the pointers are now NULL or cleared properly and can be freed.
4793 * Now allocate missing structures.
4794 */
4795 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004796 {
Mansour Moufid99b92592015-02-15 17:46:32 -05004797 ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004798 }
Paul Bakker48916f92012-09-16 19:57:18 +00004799
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004800 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004801 {
Mansour Moufid99b92592015-02-15 17:46:32 -05004802 ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004803 }
Paul Bakker48916f92012-09-16 19:57:18 +00004804
Paul Bakker82788fb2014-10-20 13:59:19 +02004805 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004806 {
Mansour Moufid99b92592015-02-15 17:46:32 -05004807 ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004808 }
Paul Bakker48916f92012-09-16 19:57:18 +00004809
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004810 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004811 if( ssl->handshake == NULL ||
4812 ssl->transform_negotiate == NULL ||
4813 ssl->session_negotiate == NULL )
4814 {
4815 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004816
4817 polarssl_free( ssl->handshake );
4818 polarssl_free( ssl->transform_negotiate );
4819 polarssl_free( ssl->session_negotiate );
4820
4821 ssl->handshake = NULL;
4822 ssl->transform_negotiate = NULL;
4823 ssl->session_negotiate = NULL;
4824
Paul Bakker48916f92012-09-16 19:57:18 +00004825 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4826 }
4827
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004828 /* Initialize structures */
4829 ssl_session_init( ssl->session_negotiate );
4830 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004831 ssl_handshake_params_init( ssl->handshake );
4832
4833#if defined(POLARSSL_X509_CRT_PARSE_C)
4834 ssl->handshake->key_cert = ssl->key_cert;
4835#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004836
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004837 /*
4838 * We may not know yet if we're using DTLS,
4839 * so always initiliase DTLS-specific fields.
4840 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004841#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004842 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004843
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004844 // TODO: not the right place, we may not know endpoint yet
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004845 if( ssl->endpoint == SSL_IS_CLIENT )
4846 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4847 else
4848 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004849#endif
4850
Paul Bakker48916f92012-09-16 19:57:18 +00004851 return( 0 );
4852}
4853
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004854#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4855/* Dummy cookie callbacks for defaults */
4856static int ssl_cookie_write_dummy( void *ctx,
4857 unsigned char **p, unsigned char *end,
4858 const unsigned char *cli_id, size_t cli_id_len )
4859{
4860 ((void) ctx);
4861 ((void) p);
4862 ((void) end);
4863 ((void) cli_id);
4864 ((void) cli_id_len);
4865
4866 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4867}
4868
4869static int ssl_cookie_check_dummy( void *ctx,
4870 const unsigned char *cookie, size_t cookie_len,
4871 const unsigned char *cli_id, size_t cli_id_len )
4872{
4873 ((void) ctx);
4874 ((void) cookie);
4875 ((void) cookie_len);
4876 ((void) cli_id);
4877 ((void) cli_id_len);
4878
4879 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4880}
4881#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4882
Paul Bakker5121ce52009-01-03 21:22:43 +00004883/*
4884 * Initialize an SSL context
4885 */
4886int ssl_init( ssl_context *ssl )
4887{
Paul Bakker48916f92012-09-16 19:57:18 +00004888 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004889 int len = SSL_BUFFER_LEN;
4890
4891 memset( ssl, 0, sizeof( ssl_context ) );
4892
Paul Bakker62f2dee2012-09-28 07:31:51 +00004893 /*
4894 * Sane defaults
4895 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004896 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4897 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4898 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4899 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004900
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004901 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004902
Manuel Pégourié-Gonnard849b1742015-03-20 19:13:22 +00004903 ssl_set_arc4_support( ssl, SSL_ARC4_DISABLED );
4904
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004905#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004906 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004907 memset( ssl->renego_period, 0xFF, 7 );
4908 ssl->renego_period[7] = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004909#endif
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004910
Paul Bakker62f2dee2012-09-28 07:31:51 +00004911#if defined(POLARSSL_DHM_C)
4912 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4913 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4914 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4915 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4916 {
4917 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4918 return( ret );
4919 }
4920#endif
4921
4922 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004923 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004924 */
Manuel Pégourié-Gonnard4e41c992015-02-18 10:39:49 +00004925 if( ( ssl->in_buf = polarssl_malloc( len ) ) == NULL ||
4926 ( ssl->out_buf = polarssl_malloc( len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004927 {
4928 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004929 polarssl_free( ssl->in_buf );
4930 ssl->in_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004931 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004932 }
4933
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004934 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4935 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004936
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004937 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4938 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4939
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004940#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4941 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
4942#endif
4943
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004944#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4945 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
4946#endif
4947
Paul Bakker606b4ba2013-08-14 16:52:14 +02004948#if defined(POLARSSL_SSL_SESSION_TICKETS)
4949 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4950#endif
4951
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004952#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004953 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004954#endif
4955
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004956#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4957 ssl->f_cookie_write = ssl_cookie_write_dummy;
4958 ssl->f_cookie_check = ssl_cookie_check_dummy;
4959#endif
4960
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004961#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4962 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
4963#endif
4964
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004965#if defined(POLARSSL_SSL_PROTO_DTLS)
4966 ssl->hs_timeout_min = SSL_DTLS_TIMEOUT_DFL_MIN;
4967 ssl->hs_timeout_max = SSL_DTLS_TIMEOUT_DFL_MAX;
4968#endif
4969
Paul Bakker48916f92012-09-16 19:57:18 +00004970 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4971 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004972
4973 return( 0 );
4974}
4975
4976/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004977 * Reset an initialized and used SSL context for re-use while retaining
4978 * all application-set variables, function pointers and data.
4979 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004980int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004981{
Paul Bakker48916f92012-09-16 19:57:18 +00004982 int ret;
4983
Paul Bakker7eb013f2011-10-06 12:37:39 +00004984 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004985
4986#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00004987 ssl->renego_status = SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004988 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004989
4990 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard61860192014-11-04 13:05:42 +01004991 memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
4992 memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004993#endif
4994 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00004995
Paul Bakker7eb013f2011-10-06 12:37:39 +00004996 ssl->in_offt = NULL;
4997
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004998 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004999 ssl->in_msgtype = 0;
5000 ssl->in_msglen = 0;
5001 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005002#if defined(POLARSSL_SSL_PROTO_DTLS)
5003 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005004 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005005#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005006#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
5007 ssl_dtls_replay_reset( ssl );
5008#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005009
5010 ssl->in_hslen = 0;
5011 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005012 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005013
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005014 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005015 ssl->out_msgtype = 0;
5016 ssl->out_msglen = 0;
5017 ssl->out_left = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005018#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005019 if( ssl->split_done != SSL_CBC_RECORD_SPLITTING_DISABLED )
5020 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005021#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005022
Paul Bakker48916f92012-09-16 19:57:18 +00005023 ssl->transform_in = NULL;
5024 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005025
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005026 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
5027 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00005028
5029#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02005030 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005031 {
5032 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005033 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005034 {
5035 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
5036 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
5037 }
Paul Bakker05ef8352012-05-08 09:17:57 +00005038 }
5039#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00005040
Paul Bakker48916f92012-09-16 19:57:18 +00005041 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005042 {
Paul Bakker48916f92012-09-16 19:57:18 +00005043 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005044 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005045 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00005046 }
Paul Bakker48916f92012-09-16 19:57:18 +00005047
Paul Bakkerc0463502013-02-14 11:19:38 +01005048 if( ssl->session )
5049 {
5050 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005051 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005052 ssl->session = NULL;
5053 }
5054
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005055#if defined(POLARSSL_SSL_ALPN)
5056 ssl->alpn_chosen = NULL;
5057#endif
5058
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02005059#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005060 polarssl_free( ssl->cli_id );
5061 ssl->cli_id = NULL;
5062 ssl->cli_id_len = 0;
5063#endif
5064
Paul Bakker48916f92012-09-16 19:57:18 +00005065 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5066 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005067
5068 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00005069}
5070
Paul Bakkera503a632013-08-14 13:48:06 +02005071#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005072static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
5073{
5074 aes_free( &tkeys->enc );
5075 aes_free( &tkeys->dec );
5076
5077 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
5078}
5079
Paul Bakker7eb013f2011-10-06 12:37:39 +00005080/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005081 * Allocate and initialize ticket keys
5082 */
5083static int ssl_ticket_keys_init( ssl_context *ssl )
5084{
5085 int ret;
5086 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005087 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005088
5089 if( ssl->ticket_keys != NULL )
5090 return( 0 );
5091
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005092 tkeys = polarssl_malloc( sizeof(ssl_ticket_keys) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005093 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005094 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5095
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005096 aes_init( &tkeys->enc );
5097 aes_init( &tkeys->dec );
5098
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005099 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01005100 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005101 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005102 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005103 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005104 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005105
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02005106 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
5107 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
5108 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
5109 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005110 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005111 polarssl_free( tkeys );
5112 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02005113 }
5114
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005115 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01005116 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005117 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005118 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005119 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005120 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005121
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005122 ssl->ticket_keys = tkeys;
5123
5124 return( 0 );
5125}
Paul Bakkera503a632013-08-14 13:48:06 +02005126#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005127
5128/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005129 * SSL set accessors
5130 */
5131void ssl_set_endpoint( ssl_context *ssl, int endpoint )
5132{
5133 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005134
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005135#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
5136 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005137 if( endpoint == SSL_IS_CLIENT )
5138 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02005139#endif
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01005140
5141#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
5142 if( endpoint == SSL_IS_SERVER )
5143 ssl->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
5144#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005145}
5146
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005147int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005148{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005149#if defined(POLARSSL_SSL_PROTO_DTLS)
5150 if( transport == SSL_TRANSPORT_DATAGRAM )
5151 {
5152 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005153
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005154 ssl->out_hdr = ssl->out_buf;
5155 ssl->out_ctr = ssl->out_buf + 3;
5156 ssl->out_len = ssl->out_buf + 11;
5157 ssl->out_iv = ssl->out_buf + 13;
5158 ssl->out_msg = ssl->out_buf + 13;
5159
5160 ssl->in_hdr = ssl->in_buf;
5161 ssl->in_ctr = ssl->in_buf + 3;
5162 ssl->in_len = ssl->in_buf + 11;
5163 ssl->in_iv = ssl->in_buf + 13;
5164 ssl->in_msg = ssl->in_buf + 13;
5165
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005166 /* DTLS starts with TLS1.1 */
5167 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
5168 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005169
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005170 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
5171 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
5172
5173 return( 0 );
5174 }
5175#endif
5176
5177 if( transport == SSL_TRANSPORT_STREAM )
5178 {
5179 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005180
5181 ssl->out_ctr = ssl->out_buf;
5182 ssl->out_hdr = ssl->out_buf + 8;
5183 ssl->out_len = ssl->out_buf + 11;
5184 ssl->out_iv = ssl->out_buf + 13;
5185 ssl->out_msg = ssl->out_buf + 13;
5186
5187 ssl->in_ctr = ssl->in_buf;
5188 ssl->in_hdr = ssl->in_buf + 8;
5189 ssl->in_len = ssl->in_buf + 11;
5190 ssl->in_iv = ssl->in_buf + 13;
5191 ssl->in_msg = ssl->in_buf + 13;
5192
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005193 return( 0 );
5194 }
5195
5196 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005197}
5198
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005199#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
5200void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
5201{
5202 ssl->anti_replay = mode;
5203}
5204#endif
5205
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005206#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
5207void ssl_set_dtls_badmac_limit( ssl_context *ssl, unsigned limit )
5208{
5209 ssl->badmac_limit = limit;
5210}
5211#endif
5212
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02005213#if defined(POLARSSL_SSL_PROTO_DTLS)
5214void ssl_set_handshake_timeout( ssl_context *ssl, uint32_t min, uint32_t max )
5215{
5216 ssl->hs_timeout_min = min;
5217 ssl->hs_timeout_max = max;
5218}
5219#endif
5220
Paul Bakker5121ce52009-01-03 21:22:43 +00005221void ssl_set_authmode( ssl_context *ssl, int authmode )
5222{
5223 ssl->authmode = authmode;
5224}
5225
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005226#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005227void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005228 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005229 void *p_vrfy )
5230{
5231 ssl->f_vrfy = f_vrfy;
5232 ssl->p_vrfy = p_vrfy;
5233}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005234#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005235
Paul Bakker5121ce52009-01-03 21:22:43 +00005236void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00005237 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00005238 void *p_rng )
5239{
5240 ssl->f_rng = f_rng;
5241 ssl->p_rng = p_rng;
5242}
5243
5244void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00005245 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00005246 void *p_dbg )
5247{
5248 ssl->f_dbg = f_dbg;
5249 ssl->p_dbg = p_dbg;
5250}
5251
Manuel Pégourié-Gonnard9a65e802015-03-25 17:19:12 +01005252#if ! defined(POLARSSL_DEPRECATED_REMOVED)
Paul Bakker5121ce52009-01-03 21:22:43 +00005253void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00005254 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00005255 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00005256{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02005257 if( p_recv != p_send )
5258 {
5259 ssl->f_recv = NULL;
5260 ssl->f_send = NULL;
5261 ssl->p_bio = NULL;
5262 return;
5263 }
5264
Paul Bakker5121ce52009-01-03 21:22:43 +00005265 ssl->f_recv = f_recv;
5266 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02005267 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00005268}
Manuel Pégourié-Gonnard9a65e802015-03-25 17:19:12 +01005269#endif /* POLARSSL_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005270
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005271void ssl_set_bio_timeout( ssl_context *ssl,
5272 void *p_bio,
5273 int (*f_send)(void *, const unsigned char *, size_t),
5274 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +02005275 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t),
5276 uint32_t timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005277{
5278 ssl->p_bio = p_bio;
5279 ssl->f_send = f_send;
5280 ssl->f_recv = f_recv;
5281 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard27074302014-10-01 17:35:50 +02005282 ssl->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005283}
5284
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005285#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00005286void ssl_set_session_cache( ssl_context *ssl,
5287 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
5288 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00005289{
Paul Bakker0a597072012-09-25 21:55:46 +00005290 ssl->f_get_cache = f_get_cache;
5291 ssl->p_get_cache = p_get_cache;
5292 ssl->f_set_cache = f_set_cache;
5293 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00005294}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005295#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005296
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005297#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005298int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00005299{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005300 int ret;
5301
5302 if( ssl == NULL ||
5303 session == NULL ||
5304 ssl->session_negotiate == NULL ||
5305 ssl->endpoint != SSL_IS_CLIENT )
5306 {
5307 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5308 }
5309
5310 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5311 return( ret );
5312
Paul Bakker0a597072012-09-25 21:55:46 +00005313 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005314
5315 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005316}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005317#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005318
Paul Bakkerb68cad62012-08-23 08:34:18 +00005319void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005320{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005321 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
5322 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
5323 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
5324 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
5325}
5326
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005327void ssl_set_ciphersuites_for_version( ssl_context *ssl,
5328 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005329 int major, int minor )
5330{
5331 if( major != SSL_MAJOR_VERSION_3 )
5332 return;
5333
5334 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
5335 return;
5336
5337 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005338}
5339
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005340#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005341/* Add a new (empty) key_cert entry an return a pointer to it */
5342static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
5343{
5344 ssl_key_cert *key_cert, *last;
5345
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005346 key_cert = polarssl_malloc( sizeof(ssl_key_cert) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005347 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005348 return( NULL );
5349
5350 memset( key_cert, 0, sizeof( ssl_key_cert ) );
5351
5352 /* Append the new key_cert to the (possibly empty) current list */
5353 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005354 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005355 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01005356 if( ssl->handshake != NULL )
5357 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01005358 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005359 else
5360 {
5361 last = ssl->key_cert;
5362 while( last->next != NULL )
5363 last = last->next;
5364 last->next = key_cert;
5365 }
5366
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005367 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005368}
5369
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005370void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00005371 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00005372{
5373 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00005374 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005375 ssl->peer_cn = peer_cn;
5376}
5377
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005378int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005379 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00005380{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005381 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
5382
5383 if( key_cert == NULL )
5384 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5385
5386 key_cert->cert = own_cert;
5387 key_cert->key = pk_key;
5388
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00005389 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005390}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005391#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005392
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005393#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005394int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5395 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005396{
Paul Bakker6db455e2013-09-18 17:29:31 +02005397 if( psk == NULL || psk_identity == NULL )
5398 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5399
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005400 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005401 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5402
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00005403 if( ssl->psk != NULL || ssl->psk_identity != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02005404 {
5405 polarssl_free( ssl->psk );
5406 polarssl_free( ssl->psk_identity );
5407 }
5408
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00005409 if( ( ssl->psk = polarssl_malloc( psk_len ) ) == NULL ||
5410 ( ssl->psk_identity = polarssl_malloc( psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05005411 {
5412 polarssl_free( ssl->psk );
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00005413 ssl->psk = NULL;
Mansour Moufidf81088b2015-02-17 13:10:21 -05005414 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5415 }
Paul Bakker6db455e2013-09-18 17:29:31 +02005416
Paul Bakker6db455e2013-09-18 17:29:31 +02005417 ssl->psk_len = psk_len;
5418 ssl->psk_identity_len = psk_identity_len;
5419
Paul Bakker6db455e2013-09-18 17:29:31 +02005420 memcpy( ssl->psk, psk, ssl->psk_len );
5421 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
5422
5423 return( 0 );
5424}
5425
5426void ssl_set_psk_cb( ssl_context *ssl,
5427 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5428 size_t),
5429 void *p_psk )
5430{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005431 ssl->f_psk = f_psk;
5432 ssl->p_psk = p_psk;
Paul Bakker43b7e352011-01-18 15:27:19 +00005433}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005434#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005435
Paul Bakker48916f92012-09-16 19:57:18 +00005436#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005437int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
5438{
5439 int ret;
5440
Paul Bakker48916f92012-09-16 19:57:18 +00005441 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005442 {
5443 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5444 return( ret );
5445 }
5446
Paul Bakker48916f92012-09-16 19:57:18 +00005447 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005448 {
5449 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5450 return( ret );
5451 }
5452
5453 return( 0 );
5454}
5455
Paul Bakker1b57b062011-01-06 15:48:19 +00005456int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5457{
5458 int ret;
5459
Paul Bakker66d5d072014-06-17 16:39:18 +02005460 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005461 {
5462 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5463 return( ret );
5464 }
5465
Paul Bakker66d5d072014-06-17 16:39:18 +02005466 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005467 {
5468 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5469 return( ret );
5470 }
5471
5472 return( 0 );
5473}
Paul Bakker48916f92012-09-16 19:57:18 +00005474#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005475
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005476#if defined(POLARSSL_SSL_SET_CURVES)
5477/*
5478 * Set the allowed elliptic curves
5479 */
5480void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5481{
5482 ssl->curve_list = curve_list;
5483}
5484#endif
5485
Paul Bakker0be444a2013-08-27 21:55:01 +02005486#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005487int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005488{
5489 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005490 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005491
5492 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005493
5494 if( ssl->hostname_len + 1 == 0 )
5495 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5496
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005497 ssl->hostname = polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005498
Paul Bakkerb15b8512012-01-13 13:44:06 +00005499 if( ssl->hostname == NULL )
5500 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5501
Paul Bakker3c2122f2013-06-24 19:03:14 +02005502 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005503 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005504
Paul Bakker40ea7de2009-05-03 10:18:48 +00005505 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005506
5507 return( 0 );
5508}
5509
Paul Bakker5701cdc2012-09-27 21:49:42 +00005510void ssl_set_sni( ssl_context *ssl,
5511 int (*f_sni)(void *, ssl_context *,
5512 const unsigned char *, size_t),
5513 void *p_sni )
5514{
5515 ssl->f_sni = f_sni;
5516 ssl->p_sni = p_sni;
5517}
Paul Bakker0be444a2013-08-27 21:55:01 +02005518#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005519
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005520#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005521int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005522{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005523 size_t cur_len, tot_len;
5524 const char **p;
5525
5526 /*
5527 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5528 * truncated". Check lengths now rather than later.
5529 */
5530 tot_len = 0;
5531 for( p = protos; *p != NULL; p++ )
5532 {
5533 cur_len = strlen( *p );
5534 tot_len += cur_len;
5535
5536 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5537 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5538 }
5539
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005540 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005541
5542 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005543}
5544
5545const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5546{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005547 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005548}
5549#endif /* POLARSSL_SSL_ALPN */
5550
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005551static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005552{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005553 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005554 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005555 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005556 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005557 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005558
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005559#if defined(POLARSSL_SSL_PROTO_DTLS)
5560 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5561 minor < SSL_MINOR_VERSION_2 )
5562 {
5563 return( -1 );
5564 }
5565#else
5566 ((void) ssl);
5567#endif
5568
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005569 return( 0 );
5570}
5571
5572int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5573{
5574 if( ssl_check_version( ssl, major, minor ) != 0 )
5575 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5576
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005577 ssl->max_major_ver = major;
5578 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005579
5580 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005581}
5582
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005583int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005584{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005585 if( ssl_check_version( ssl, major, minor ) != 0 )
5586 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005587
5588 ssl->min_major_ver = major;
5589 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005590
5591 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005592}
5593
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02005594#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
5595void ssl_set_fallback( ssl_context *ssl, char fallback )
5596{
5597 ssl->fallback = fallback;
5598}
5599#endif
5600
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01005601#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
5602void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
5603{
5604 ssl->encrypt_then_mac = etm;
5605}
5606#endif
5607
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02005608#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
5609void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
5610{
5611 ssl->extended_ms = ems;
5612}
5613#endif
5614
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01005615void ssl_set_arc4_support( ssl_context *ssl, char arc4 )
5616{
5617 ssl->arc4_disabled = arc4;
5618}
5619
Paul Bakker05decb22013-08-15 13:33:48 +02005620#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005621int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5622{
Paul Bakker77e257e2013-12-16 15:29:52 +01005623 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005624 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005625 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005626 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005627 }
5628
5629 ssl->mfl_code = mfl_code;
5630
5631 return( 0 );
5632}
Paul Bakker05decb22013-08-15 13:33:48 +02005633#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005634
Paul Bakker1f2bc622013-08-15 13:45:55 +02005635#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005636int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005637{
Paul Bakker8c1ede62013-07-19 14:14:37 +02005638 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005639
5640 return( 0 );
5641}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005642#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005643
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005644#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
5645void ssl_set_cbc_record_splitting( ssl_context *ssl, char split )
5646{
5647 ssl->split_done = split;
5648}
5649#endif
5650
Paul Bakker48916f92012-09-16 19:57:18 +00005651void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5652{
5653 ssl->allow_legacy_renegotiation = allow_legacy;
5654}
5655
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005656#if defined(POLARSSL_SSL_RENEGOTIATION)
5657void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5658{
5659 ssl->disable_renegotiation = renegotiation;
5660}
5661
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005662void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5663{
5664 ssl->renego_max_records = max_records;
5665}
5666
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01005667void ssl_set_renegotiation_period( ssl_context *ssl,
5668 const unsigned char period[8] )
5669{
5670 memcpy( ssl->renego_period, period, 8 );
5671}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005672#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00005673
Paul Bakkera503a632013-08-14 13:48:06 +02005674#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005675int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5676{
5677 ssl->session_tickets = use_tickets;
5678
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005679#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005680 if( ssl->endpoint == SSL_IS_CLIENT )
5681 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005682#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005683
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01005684 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
5685 return( 0 );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005686
5687 if( ssl->f_rng == NULL )
5688 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5689
5690 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005691}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005692
5693void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5694{
5695 ssl->ticket_lifetime = lifetime;
5696}
Paul Bakkera503a632013-08-14 13:48:06 +02005697#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005698
Paul Bakker5121ce52009-01-03 21:22:43 +00005699/*
5700 * SSL get accessors
5701 */
5702size_t ssl_get_bytes_avail( const ssl_context *ssl )
5703{
5704 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5705}
5706
Paul Bakkerff60ee62010-03-16 21:09:09 +00005707int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005708{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00005709 if( ssl->session != NULL )
5710 return( ssl->session->verify_result );
5711
5712 if( ssl->session_negotiate != NULL )
5713 return( ssl->session_negotiate->verify_result );
5714
5715 return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005716}
5717
Paul Bakkere3166ce2011-01-27 17:40:50 +00005718const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005719{
Paul Bakker926c8e42013-03-06 10:23:34 +01005720 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005721 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005722
Paul Bakkere3166ce2011-01-27 17:40:50 +00005723 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005724}
5725
Paul Bakker43ca69c2011-01-15 17:35:19 +00005726const char *ssl_get_version( const ssl_context *ssl )
5727{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005728#if defined(POLARSSL_SSL_PROTO_DTLS)
5729 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5730 {
5731 switch( ssl->minor_ver )
5732 {
5733 case SSL_MINOR_VERSION_2:
5734 return( "DTLSv1.0" );
5735
5736 case SSL_MINOR_VERSION_3:
5737 return( "DTLSv1.2" );
5738
5739 default:
5740 return( "unknown (DTLS)" );
5741 }
5742 }
5743#endif
5744
Paul Bakker43ca69c2011-01-15 17:35:19 +00005745 switch( ssl->minor_ver )
5746 {
5747 case SSL_MINOR_VERSION_0:
5748 return( "SSLv3.0" );
5749
5750 case SSL_MINOR_VERSION_1:
5751 return( "TLSv1.0" );
5752
5753 case SSL_MINOR_VERSION_2:
5754 return( "TLSv1.1" );
5755
Paul Bakker1ef83d62012-04-11 12:09:53 +00005756 case SSL_MINOR_VERSION_3:
5757 return( "TLSv1.2" );
5758
Paul Bakker43ca69c2011-01-15 17:35:19 +00005759 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005760 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005761 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005762}
5763
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005764int ssl_get_record_expansion( const ssl_context *ssl )
5765{
5766 int transform_expansion;
5767 const ssl_transform *transform = ssl->transform_out;
5768
5769#if defined(POLARSSL_ZLIB_SUPPORT)
5770 if( ssl->session_out->compression != SSL_COMPRESS_NULL )
5771 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
5772#endif
5773
5774 if( transform == NULL )
5775 return( ssl_hdr_len( ssl ) );
5776
5777 switch( cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
5778 {
5779 case POLARSSL_MODE_GCM:
5780 case POLARSSL_MODE_CCM:
5781 case POLARSSL_MODE_STREAM:
5782 transform_expansion = transform->minlen;
5783 break;
5784
5785 case POLARSSL_MODE_CBC:
5786 transform_expansion = transform->maclen
5787 + cipher_get_block_size( &transform->cipher_ctx_enc );
5788 break;
5789
5790 default:
5791 SSL_DEBUG_MSG( 0, ( "should never happen" ) );
5792 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
5793 }
5794
5795 return( ssl_hdr_len( ssl ) + transform_expansion );
5796}
5797
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005798#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005799const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005800{
5801 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005802 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005803
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005804 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005805}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005806#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005807
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005808#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005809int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5810{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005811 if( ssl == NULL ||
5812 dst == NULL ||
5813 ssl->session == NULL ||
5814 ssl->endpoint != SSL_IS_CLIENT )
5815 {
5816 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5817 }
5818
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005819 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005820}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005821#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005822
Paul Bakker5121ce52009-01-03 21:22:43 +00005823/*
Paul Bakker1961b702013-01-25 14:49:24 +01005824 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005825 */
Paul Bakker1961b702013-01-25 14:49:24 +01005826int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005827{
Paul Bakker40e46942009-01-03 21:51:57 +00005828 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005829
Paul Bakker40e46942009-01-03 21:51:57 +00005830#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005831 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005832 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005833#endif
Paul Bakker40e46942009-01-03 21:51:57 +00005834#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005835 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005836 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005837#endif
5838
Paul Bakker1961b702013-01-25 14:49:24 +01005839 return( ret );
5840}
5841
5842/*
5843 * Perform the SSL handshake
5844 */
5845int ssl_handshake( ssl_context *ssl )
5846{
5847 int ret = 0;
5848
5849 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5850
5851 while( ssl->state != SSL_HANDSHAKE_OVER )
5852 {
5853 ret = ssl_handshake_step( ssl );
5854
5855 if( ret != 0 )
5856 break;
5857 }
5858
Paul Bakker5121ce52009-01-03 21:22:43 +00005859 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5860
5861 return( ret );
5862}
5863
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005864#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005865#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005866/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005867 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005868 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005869static int ssl_write_hello_request( ssl_context *ssl )
5870{
5871 int ret;
5872
5873 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5874
5875 ssl->out_msglen = 4;
5876 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5877 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5878
5879 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5880 {
5881 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5882 return( ret );
5883 }
5884
5885 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5886
5887 return( 0 );
5888}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005889#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005890
5891/*
5892 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005893 * - any side: calling ssl_renegotiate(),
5894 * - client: receiving a HelloRequest during ssl_read(),
5895 * - server: receiving any handshake message on server during ssl_read() after
5896 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005897 * If the handshake doesn't complete due to waiting for I/O, it will continue
5898 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005899 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005900static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005901{
5902 int ret;
5903
5904 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5905
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005906 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5907 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005908
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005909 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5910 * the ServerHello will have message_seq = 1" */
5911#if defined(POLARSSL_SSL_PROTO_DTLS)
5912 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00005913 ssl->renego_status == SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005914 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005915 if( ssl->endpoint == SSL_IS_SERVER )
5916 ssl->handshake->out_msg_seq = 1;
5917 else
5918 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005919 }
5920#endif
5921
Paul Bakker48916f92012-09-16 19:57:18 +00005922 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00005923 ssl->renego_status = SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00005924
Paul Bakker48916f92012-09-16 19:57:18 +00005925 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5926 {
5927 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5928 return( ret );
5929 }
5930
5931 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5932
5933 return( 0 );
5934}
5935
5936/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005937 * Renegotiate current connection on client,
5938 * or request renegotiation on server
5939 */
5940int ssl_renegotiate( ssl_context *ssl )
5941{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005942 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005943
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005944#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005945 /* On server, just send the request */
5946 if( ssl->endpoint == SSL_IS_SERVER )
5947 {
5948 if( ssl->state != SSL_HANDSHAKE_OVER )
5949 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5950
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00005951 ssl->renego_status = SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005952
5953 /* Did we already try/start sending HelloRequest? */
5954 if( ssl->out_left != 0 )
5955 return( ssl_flush_output( ssl ) );
5956
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005957 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005958 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005959#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005960
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005961#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005962 /*
5963 * On client, either start the renegotiation process or,
5964 * if already in progress, continue the handshake
5965 */
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00005966 if( ssl->renego_status != SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005967 {
5968 if( ssl->state != SSL_HANDSHAKE_OVER )
5969 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5970
5971 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
5972 {
5973 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
5974 return( ret );
5975 }
5976 }
5977 else
5978 {
5979 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5980 {
5981 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5982 return( ret );
5983 }
5984 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005985#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005986
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005987 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005988}
5989
5990/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005991 * Check record counters and renegotiate if they're above the limit.
5992 */
5993static int ssl_check_ctr_renegotiate( ssl_context *ssl )
5994{
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005995 if( ssl->state != SSL_HANDSHAKE_OVER ||
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00005996 ssl->renego_status == SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005997 ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED )
5998 {
5999 return( 0 );
6000 }
6001
6002 // TODO: adapt for DTLS
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006003 if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 &&
6004 memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006005 {
6006 return( 0 );
6007 }
6008
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006009 SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006010 return( ssl_renegotiate( ssl ) );
6011}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006012#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006013
6014/*
6015 * Receive application data decrypted from the SSL layer
6016 */
Paul Bakker23986e52011-04-24 08:57:21 +00006017int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006018{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006019 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00006020 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00006021
6022 SSL_DEBUG_MSG( 2, ( "=> read" ) );
6023
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006024#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006025 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006026 {
6027 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
6028 return( ret );
6029
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006030 if( ssl->handshake != NULL &&
6031 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
6032 {
6033 if( ( ret = ssl_resend( ssl ) ) != 0 )
6034 return( ret );
6035 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006036 }
6037#endif
6038
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006039#if defined(POLARSSL_SSL_RENEGOTIATION)
6040 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
6041 {
6042 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
6043 return( ret );
6044 }
6045#endif
6046
Paul Bakker5121ce52009-01-03 21:22:43 +00006047 if( ssl->state != SSL_HANDSHAKE_OVER )
6048 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006049 ret = ssl_handshake( ssl );
6050 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
6051 {
6052 record_read = 1;
6053 }
6054 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006055 {
6056 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6057 return( ret );
6058 }
6059 }
6060
6061 if( ssl->in_offt == NULL )
6062 {
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02006063#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006064 /* Start timer if not already running */
6065 if( ssl->time_limit == 0 )
6066 ssl_set_timer( ssl, ssl->read_timeout );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02006067#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006068
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006069 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00006070 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006071 if( ( ret = ssl_read_record( ssl ) ) != 0 )
6072 {
6073 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
6074 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00006075
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006076 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
6077 return( ret );
6078 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006079 }
6080
6081 if( ssl->in_msglen == 0 &&
6082 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
6083 {
6084 /*
6085 * OpenSSL sends empty messages to randomize the IV
6086 */
6087 if( ( ret = ssl_read_record( ssl ) ) != 0 )
6088 {
Paul Bakker831a7552011-05-18 13:32:51 +00006089 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
6090 return( 0 );
6091
Paul Bakker5121ce52009-01-03 21:22:43 +00006092 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
6093 return( ret );
6094 }
6095 }
6096
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006097#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006098 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
6099 {
6100 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
6101
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006102#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006103 if( ssl->endpoint == SSL_IS_CLIENT &&
6104 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00006105 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006106 {
6107 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006108
6109 /* With DTLS, drop the packet (probably from last handshake) */
6110#if defined(POLARSSL_SSL_PROTO_DTLS)
6111 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6112 return( POLARSSL_ERR_NET_WANT_READ );
6113#endif
6114 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6115 }
6116
6117 if( ssl->endpoint == SSL_IS_SERVER &&
6118 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
6119 {
6120 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
6121
6122 /* With DTLS, drop the packet (probably from last handshake) */
6123#if defined(POLARSSL_SSL_PROTO_DTLS)
6124 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6125 return( POLARSSL_ERR_NET_WANT_READ );
6126#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006127 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6128 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006129#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006130
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006131 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
6132 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006133 ssl->allow_legacy_renegotiation ==
6134 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006135 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006136 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006137
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006138#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006139 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006140 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006141 /*
6142 * SSLv3 does not have a "no_renegotiation" alert
6143 */
6144 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
6145 return( ret );
6146 }
6147 else
Paul Bakker9af723c2014-05-01 13:03:14 +02006148#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006149#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
6150 defined(POLARSSL_SSL_PROTO_TLS1_2)
6151 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006152 {
6153 if( ( ret = ssl_send_alert_message( ssl,
6154 SSL_ALERT_LEVEL_WARNING,
6155 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
6156 {
6157 return( ret );
6158 }
Paul Bakker48916f92012-09-16 19:57:18 +00006159 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006160 else
Paul Bakker9af723c2014-05-01 13:03:14 +02006161#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
6162 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02006163 {
6164 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006165 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02006166 }
Paul Bakker48916f92012-09-16 19:57:18 +00006167 }
6168 else
6169 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006170 /* DTLS clients need to know renego is server-initiated */
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02006171#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006172 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
6173 ssl->endpoint == SSL_IS_CLIENT )
6174 {
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00006175 ssl->renego_status = SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006176 }
6177#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006178 ret = ssl_start_renegotiation( ssl );
6179 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
6180 {
6181 record_read = 1;
6182 }
6183 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006184 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006185 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00006186 return( ret );
6187 }
Paul Bakker48916f92012-09-16 19:57:18 +00006188 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006189
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006190 /* If a non-handshake record was read during renego, fallthrough,
6191 * else tell the user they should call ssl_read() again */
6192 if( ! record_read )
6193 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00006194 }
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00006195 else if( ssl->renego_status == SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006196 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006197
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006198 if( ssl->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006199 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006200 if( ++ssl->renego_records_seen > ssl->renego_max_records )
6201 {
6202 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
6203 "but not honored by client" ) );
6204 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6205 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006206 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006207 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006208#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006209
6210 /* Fatal and closure alerts handled by ssl_read_record() */
6211 if( ssl->in_msgtype == SSL_MSG_ALERT )
6212 {
6213 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
6214 return( POLARSSL_ERR_NET_WANT_READ );
6215 }
6216
6217 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00006218 {
6219 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00006220 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006221 }
6222
6223 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006224
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02006225#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02006226 /* We're going to return something now, cancel timer,
6227 * except if handshake (renegotiation) is in progress */
6228 if( ssl->state == SSL_HANDSHAKE_OVER )
6229 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006230
6231 /* If we requested renego but received AppData, resend HelloRequest.
6232 * Do it now, after setting in_offt, to avoid taking this branch
6233 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00006234#if defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006235 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00006236 ssl->renego_status == SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006237 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006238 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006239 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006240 SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006241 return( ret );
6242 }
6243 }
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00006244#endif /* POLARSSL_SSL_SRV_C && POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02006245#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006246 }
6247
6248 n = ( len < ssl->in_msglen )
6249 ? len : ssl->in_msglen;
6250
6251 memcpy( buf, ssl->in_offt, n );
6252 ssl->in_msglen -= n;
6253
6254 if( ssl->in_msglen == 0 )
6255 /* all bytes consumed */
6256 ssl->in_offt = NULL;
6257 else
6258 /* more data available */
6259 ssl->in_offt += n;
6260
6261 SSL_DEBUG_MSG( 2, ( "<= read" ) );
6262
Paul Bakker23986e52011-04-24 08:57:21 +00006263 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006264}
6265
6266/*
6267 * Send application data to be encrypted by the SSL layer
6268 */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006269#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
6270static int ssl_write_real( ssl_context *ssl, const unsigned char *buf, size_t len )
6271#else
Paul Bakker23986e52011-04-24 08:57:21 +00006272int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006273#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006274{
Paul Bakker23986e52011-04-24 08:57:21 +00006275 int ret;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006276#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
6277 unsigned int max_len;
6278#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006279
6280 SSL_DEBUG_MSG( 2, ( "=> write" ) );
6281
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006282#if defined(POLARSSL_SSL_RENEGOTIATION)
6283 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
6284 {
6285 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
6286 return( ret );
6287 }
6288#endif
6289
Paul Bakker5121ce52009-01-03 21:22:43 +00006290 if( ssl->state != SSL_HANDSHAKE_OVER )
6291 {
6292 if( ( ret = ssl_handshake( ssl ) ) != 0 )
6293 {
6294 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6295 return( ret );
6296 }
6297 }
6298
Paul Bakker05decb22013-08-15 13:33:48 +02006299#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02006300 /*
6301 * Assume mfl_code is correct since it was checked when set
6302 */
6303 max_len = mfl_code_to_length[ssl->mfl_code];
6304
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006305 /*
Paul Bakker05decb22013-08-15 13:33:48 +02006306 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006307 */
6308 if( ssl->session_out != NULL &&
6309 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6310 {
6311 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6312 }
6313
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006314 if( len > max_len )
6315 {
6316#if defined(POLARSSL_SSL_PROTO_DTLS)
6317 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6318 {
6319 SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
6320 "maximum fragment length: %d > %d",
6321 len, max_len ) );
6322 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
6323 }
6324 else
6325#endif
6326 len = max_len;
6327 }
6328#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker887bd502011-06-08 13:10:54 +00006329
Paul Bakker5121ce52009-01-03 21:22:43 +00006330 if( ssl->out_left != 0 )
6331 {
6332 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
6333 {
6334 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
6335 return( ret );
6336 }
6337 }
Paul Bakker887bd502011-06-08 13:10:54 +00006338 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00006339 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006340 ssl->out_msglen = len;
Paul Bakker887bd502011-06-08 13:10:54 +00006341 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006342 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00006343
6344 if( ( ret = ssl_write_record( ssl ) ) != 0 )
6345 {
6346 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
6347 return( ret );
6348 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006349 }
6350
6351 SSL_DEBUG_MSG( 2, ( "<= write" ) );
6352
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006353 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006354}
6355
6356/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006357 * Write application data, doing 1/n-1 splitting if necessary.
6358 *
6359 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006360 * then the caller will call us again with the same arguments, so
6361 * remember wether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006362 */
6363#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
6364int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
6365{
6366 int ret;
6367
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006368 if( ssl->split_done == SSL_CBC_RECORD_SPLITTING_DISABLED ||
6369 len <= 1 ||
6370 ssl->minor_ver > SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006371 cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
6372 != POLARSSL_MODE_CBC )
6373 {
6374 return( ssl_write_real( ssl, buf, len ) );
6375 }
6376
6377 if( ssl->split_done == 0 )
6378 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006379 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006380 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006381 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006382 }
6383
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006384 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
6385 return( ret );
6386 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006387
6388 return( ret + 1 );
6389}
6390#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */
6391
6392/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006393 * Notify the peer that the connection is being closed
6394 */
6395int ssl_close_notify( ssl_context *ssl )
6396{
6397 int ret;
6398
6399 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
6400
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006401 if( ssl->out_left != 0 )
6402 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006403
6404 if( ssl->state == SSL_HANDSHAKE_OVER )
6405 {
Paul Bakker48916f92012-09-16 19:57:18 +00006406 if( ( ret = ssl_send_alert_message( ssl,
6407 SSL_ALERT_LEVEL_WARNING,
6408 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006409 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006410 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006411 return( ret );
6412 }
6413 }
6414
6415 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
6416
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006417 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006418}
6419
Paul Bakker48916f92012-09-16 19:57:18 +00006420void ssl_transform_free( ssl_transform *transform )
6421{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006422 if( transform == NULL )
6423 return;
6424
Paul Bakker48916f92012-09-16 19:57:18 +00006425#if defined(POLARSSL_ZLIB_SUPPORT)
6426 deflateEnd( &transform->ctx_deflate );
6427 inflateEnd( &transform->ctx_inflate );
6428#endif
6429
Paul Bakker84bbeb52014-07-01 14:53:22 +02006430 cipher_free( &transform->cipher_ctx_enc );
6431 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02006432
Paul Bakker84bbeb52014-07-01 14:53:22 +02006433 md_free( &transform->md_ctx_enc );
6434 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02006435
Paul Bakker34617722014-06-13 17:20:13 +02006436 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006437}
6438
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006439#if defined(POLARSSL_X509_CRT_PARSE_C)
6440static void ssl_key_cert_free( ssl_key_cert *key_cert )
6441{
6442 ssl_key_cert *cur = key_cert, *next;
6443
6444 while( cur != NULL )
6445 {
6446 next = cur->next;
6447
6448 if( cur->key_own_alloc )
6449 {
6450 pk_free( cur->key );
6451 polarssl_free( cur->key );
6452 }
6453 polarssl_free( cur );
6454
6455 cur = next;
6456 }
6457}
6458#endif /* POLARSSL_X509_CRT_PARSE_C */
6459
Paul Bakker48916f92012-09-16 19:57:18 +00006460void ssl_handshake_free( ssl_handshake_params *handshake )
6461{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006462 if( handshake == NULL )
6463 return;
6464
Paul Bakker48916f92012-09-16 19:57:18 +00006465#if defined(POLARSSL_DHM_C)
6466 dhm_free( &handshake->dhm_ctx );
6467#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006468#if defined(POLARSSL_ECDH_C)
6469 ecdh_free( &handshake->ecdh_ctx );
6470#endif
6471
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006472#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02006473 /* explicit void pointer cast for buggy MS compiler */
6474 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006475#endif
6476
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006477#if defined(POLARSSL_X509_CRT_PARSE_C) && \
6478 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
6479 /*
6480 * Free only the linked list wrapper, not the keys themselves
6481 * since the belong to the SNI callback
6482 */
6483 if( handshake->sni_key_cert != NULL )
6484 {
6485 ssl_key_cert *cur = handshake->sni_key_cert, *next;
6486
6487 while( cur != NULL )
6488 {
6489 next = cur->next;
6490 polarssl_free( cur );
6491 cur = next;
6492 }
6493 }
Paul Bakker9af723c2014-05-01 13:03:14 +02006494#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006495
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006496#if defined(POLARSSL_SSL_PROTO_DTLS)
6497 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02006498 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02006499 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006500#endif
6501
Paul Bakker34617722014-06-13 17:20:13 +02006502 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006503}
6504
6505void ssl_session_free( ssl_session *session )
6506{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006507 if( session == NULL )
6508 return;
6509
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006510#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00006511 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00006512 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006513 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02006514 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00006515 }
Paul Bakkered27a042013-04-18 22:46:23 +02006516#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006517
Paul Bakkera503a632013-08-14 13:48:06 +02006518#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006519 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02006520#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006521
Paul Bakker34617722014-06-13 17:20:13 +02006522 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006523}
6524
Paul Bakker5121ce52009-01-03 21:22:43 +00006525/*
6526 * Free an SSL context
6527 */
6528void ssl_free( ssl_context *ssl )
6529{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006530 if( ssl == NULL )
6531 return;
6532
Paul Bakker5121ce52009-01-03 21:22:43 +00006533 SSL_DEBUG_MSG( 2, ( "=> free" ) );
6534
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006535 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006536 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006537 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
6538 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006539 }
6540
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006541 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006542 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006543 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
6544 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006545 }
6546
Paul Bakker16770332013-10-11 09:59:44 +02006547#if defined(POLARSSL_ZLIB_SUPPORT)
6548 if( ssl->compress_buf != NULL )
6549 {
Paul Bakker34617722014-06-13 17:20:13 +02006550 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02006551 polarssl_free( ssl->compress_buf );
6552 }
6553#endif
6554
Paul Bakker40e46942009-01-03 21:51:57 +00006555#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006556 mpi_free( &ssl->dhm_P );
6557 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006558#endif
6559
Paul Bakker48916f92012-09-16 19:57:18 +00006560 if( ssl->transform )
6561 {
6562 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02006563 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006564 }
6565
6566 if( ssl->handshake )
6567 {
6568 ssl_handshake_free( ssl->handshake );
6569 ssl_transform_free( ssl->transform_negotiate );
6570 ssl_session_free( ssl->session_negotiate );
6571
Paul Bakker6e339b52013-07-03 13:37:05 +02006572 polarssl_free( ssl->handshake );
6573 polarssl_free( ssl->transform_negotiate );
6574 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00006575 }
6576
Paul Bakkerc0463502013-02-14 11:19:38 +01006577 if( ssl->session )
6578 {
6579 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02006580 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006581 }
6582
Paul Bakkera503a632013-08-14 13:48:06 +02006583#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02006584 if( ssl->ticket_keys )
6585 {
6586 ssl_ticket_keys_free( ssl->ticket_keys );
6587 polarssl_free( ssl->ticket_keys );
6588 }
Paul Bakkera503a632013-08-14 13:48:06 +02006589#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006590
Paul Bakker0be444a2013-08-27 21:55:01 +02006591#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02006592 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006593 {
Paul Bakker34617722014-06-13 17:20:13 +02006594 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006595 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006596 ssl->hostname_len = 0;
6597 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006598#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006599
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006600#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006601 if( ssl->psk != NULL )
6602 {
Paul Bakker34617722014-06-13 17:20:13 +02006603 polarssl_zeroize( ssl->psk, ssl->psk_len );
6604 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006605 polarssl_free( ssl->psk );
6606 polarssl_free( ssl->psk_identity );
6607 ssl->psk_len = 0;
6608 ssl->psk_identity_len = 0;
6609 }
6610#endif
6611
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006612#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006613 ssl_key_cert_free( ssl->key_cert );
6614#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006615
Paul Bakker05ef8352012-05-08 09:17:57 +00006616#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6617 if( ssl_hw_record_finish != NULL )
6618 {
6619 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6620 ssl_hw_record_finish( ssl );
6621 }
6622#endif
6623
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006624#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006625 polarssl_free( ssl->cli_id );
6626#endif
6627
Paul Bakker5121ce52009-01-03 21:22:43 +00006628 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006629
Paul Bakker86f04f42013-02-14 11:20:09 +01006630 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006631 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006632}
6633
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006634#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006635/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006636 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006637 */
6638unsigned char ssl_sig_from_pk( pk_context *pk )
6639{
6640#if defined(POLARSSL_RSA_C)
6641 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6642 return( SSL_SIG_RSA );
6643#endif
6644#if defined(POLARSSL_ECDSA_C)
6645 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6646 return( SSL_SIG_ECDSA );
6647#endif
6648 return( SSL_SIG_ANON );
6649}
6650
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006651pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6652{
6653 switch( sig )
6654 {
6655#if defined(POLARSSL_RSA_C)
6656 case SSL_SIG_RSA:
6657 return( POLARSSL_PK_RSA );
6658#endif
6659#if defined(POLARSSL_ECDSA_C)
6660 case SSL_SIG_ECDSA:
6661 return( POLARSSL_PK_ECDSA );
6662#endif
6663 default:
6664 return( POLARSSL_PK_NONE );
6665 }
6666}
Paul Bakker9af723c2014-05-01 13:03:14 +02006667#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006668
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006669/*
6670 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6671 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006672md_type_t ssl_md_alg_from_hash( unsigned char hash )
6673{
6674 switch( hash )
6675 {
6676#if defined(POLARSSL_MD5_C)
6677 case SSL_HASH_MD5:
6678 return( POLARSSL_MD_MD5 );
6679#endif
6680#if defined(POLARSSL_SHA1_C)
6681 case SSL_HASH_SHA1:
6682 return( POLARSSL_MD_SHA1 );
6683#endif
6684#if defined(POLARSSL_SHA256_C)
6685 case SSL_HASH_SHA224:
6686 return( POLARSSL_MD_SHA224 );
6687 case SSL_HASH_SHA256:
6688 return( POLARSSL_MD_SHA256 );
6689#endif
6690#if defined(POLARSSL_SHA512_C)
6691 case SSL_HASH_SHA384:
6692 return( POLARSSL_MD_SHA384 );
6693 case SSL_HASH_SHA512:
6694 return( POLARSSL_MD_SHA512 );
6695#endif
6696 default:
6697 return( POLARSSL_MD_NONE );
6698 }
6699}
6700
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006701#if defined(POLARSSL_SSL_SET_CURVES)
6702/*
6703 * Check is a curve proposed by the peer is in our list.
6704 * Return 1 if we're willing to use it, 0 otherwise.
6705 */
6706int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6707{
6708 const ecp_group_id *gid;
6709
6710 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6711 if( *gid == grp_id )
6712 return( 1 );
6713
6714 return( 0 );
6715}
Paul Bakker9af723c2014-05-01 13:03:14 +02006716#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006717
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006718#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006719int ssl_check_cert_usage( const x509_crt *cert,
6720 const ssl_ciphersuite_t *ciphersuite,
6721 int cert_endpoint )
6722{
6723#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6724 int usage = 0;
6725#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006726#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6727 const char *ext_oid;
6728 size_t ext_len;
6729#endif
6730
6731#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6732 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6733 ((void) cert);
6734 ((void) cert_endpoint);
6735#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006736
6737#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6738 if( cert_endpoint == SSL_IS_SERVER )
6739 {
6740 /* Server part of the key exchange */
6741 switch( ciphersuite->key_exchange )
6742 {
6743 case POLARSSL_KEY_EXCHANGE_RSA:
6744 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6745 usage = KU_KEY_ENCIPHERMENT;
6746 break;
6747
6748 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6749 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6750 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6751 usage = KU_DIGITAL_SIGNATURE;
6752 break;
6753
6754 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6755 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6756 usage = KU_KEY_AGREEMENT;
6757 break;
6758
6759 /* Don't use default: we want warnings when adding new values */
6760 case POLARSSL_KEY_EXCHANGE_NONE:
6761 case POLARSSL_KEY_EXCHANGE_PSK:
6762 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6763 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6764 usage = 0;
6765 }
6766 }
6767 else
6768 {
6769 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6770 usage = KU_DIGITAL_SIGNATURE;
6771 }
6772
6773 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6774 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006775#else
6776 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006777#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6778
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006779#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6780 if( cert_endpoint == SSL_IS_SERVER )
6781 {
6782 ext_oid = OID_SERVER_AUTH;
6783 ext_len = OID_SIZE( OID_SERVER_AUTH );
6784 }
6785 else
6786 {
6787 ext_oid = OID_CLIENT_AUTH;
6788 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6789 }
6790
6791 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6792 return( -1 );
6793#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6794
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006795 return( 0 );
6796}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006797#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006798
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006799/*
6800 * Convert version numbers to/from wire format
6801 * and, for DTLS, to/from TLS equivalent.
6802 *
6803 * For TLS this is the identity.
6804 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6805 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6806 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6807 */
6808void ssl_write_version( int major, int minor, int transport,
6809 unsigned char ver[2] )
6810{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006811#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006812 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006813 {
6814 if( minor == SSL_MINOR_VERSION_2 )
6815 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6816
6817 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6818 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6819 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006820 else
6821#else
6822 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006823#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006824 {
6825 ver[0] = (unsigned char) major;
6826 ver[1] = (unsigned char) minor;
6827 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006828}
6829
6830void ssl_read_version( int *major, int *minor, int transport,
6831 const unsigned char ver[2] )
6832{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006833#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006834 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006835 {
6836 *major = 255 - ver[0] + 2;
6837 *minor = 255 - ver[1] + 1;
6838
6839 if( *minor == SSL_MINOR_VERSION_1 )
6840 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6841 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006842 else
6843#else
6844 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006845#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006846 {
6847 *major = ver[0];
6848 *minor = ver[1];
6849 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006850}
6851
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006852#endif /* POLARSSL_SSL_TLS_C */