blob: 41ae2beac073a7311f0455ba367068d28e3caca3 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * The SSL 3.0 specification was drafted by Netscape in 1996,
24 * and became an IETF standard in 1999.
25 *
26 * http://wp.netscape.com/eng/ssl3/
27 * http://www.ietf.org/rfc/rfc2246.txt
28 * http://www.ietf.org/rfc/rfc4346.txt
29 */
30
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#else
34#include POLARSSL_CONFIG_FILE
35#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker40e46942009-01-03 21:51:57 +000037#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/debug.h"
40#include "mbedtls/ssl.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020041
Rich Evans00ab4702015-02-06 13:43:58 +000042#include <string.h>
43
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020044#if defined(POLARSSL_X509_CRT_PARSE_C) && \
45 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020047#endif
48
Paul Bakker7dc4c442014-02-01 22:50:26 +010049#if defined(POLARSSL_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020051#else
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <stdlib.h>
Paul Bakker6e339b52013-07-03 13:37:05 +020053#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker6edcd412013-10-29 15:22:54 +010057#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
58 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000059#define strcasecmp _stricmp
60#endif
61
Paul Bakker34617722014-06-13 17:20:13 +020062/* Implementation that should never be optimized out by the compiler */
63static void polarssl_zeroize( void *v, size_t n ) {
64 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
65}
66
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010067/* Length of the "epoch" field in the record header */
68static inline size_t ssl_ep_len( const ssl_context *ssl )
69{
70#if defined(POLARSSL_SSL_PROTO_DTLS)
71 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
72 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010073#else
74 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010075#endif
76 return( 0 );
77}
78
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020079
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +020080#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081/*
82 * Start a timer.
83 * Passing millisecs = 0 cancels a running timer.
84 * The timer is already running iff time_limit != 0.
85 */
Manuel Pégourié-Gonnard46fb9422014-10-02 18:10:40 +020086static void ssl_set_timer( ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020087{
88 ssl->time_limit = millisecs;
89 get_timer( &ssl->time_info, 1 );
90}
91
92/*
93 * Return -1 is timer is expired, 0 if it isn't.
94 */
Manuel Pégourié-Gonnard46fb9422014-10-02 18:10:40 +020095static int ssl_check_timer( ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020096{
97 if( ssl->time_limit != 0 &&
98 get_timer( &ssl->time_info, 0 ) > ssl->time_limit )
99 {
100 return( -1 );
101 }
102
103 return( 0 );
104}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200105
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200106/*
107 * Double the retransmit timeout value, within the allowed range,
108 * returning -1 if the maximum value has already been reached.
109 */
110static int ssl_double_retransmit_timeout( ssl_context *ssl )
111{
112 uint32_t new_timeout;
113
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200114 if( ssl->handshake->retransmit_timeout >= ssl->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200115 return( -1 );
116
117 new_timeout = 2 * ssl->handshake->retransmit_timeout;
118
119 /* Avoid arithmetic overflow and range overflow */
120 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200121 new_timeout > ssl->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200122 {
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200123 new_timeout = ssl->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200124 }
125
126 ssl->handshake->retransmit_timeout = new_timeout;
127 SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
128 ssl->handshake->retransmit_timeout ) );
129
130 return( 0 );
131}
132
133static void ssl_reset_retransmit_timeout( ssl_context *ssl )
134{
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200135 ssl->handshake->retransmit_timeout = ssl->hs_timeout_min;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200136 SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
137 ssl->handshake->retransmit_timeout ) );
138}
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +0200139#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200140
Paul Bakker05decb22013-08-15 13:33:48 +0200141#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200142/*
143 * Convert max_fragment_length codes to length.
144 * RFC 6066 says:
145 * enum{
146 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
147 * } MaxFragmentLength;
148 * and we add 0 -> extension unused
149 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200150static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200151{
152 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
153 512, /* SSL_MAX_FRAG_LEN_512 */
154 1024, /* SSL_MAX_FRAG_LEN_1024 */
155 2048, /* SSL_MAX_FRAG_LEN_2048 */
156 4096, /* SSL_MAX_FRAG_LEN_4096 */
157};
Paul Bakker05decb22013-08-15 13:33:48 +0200158#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200159
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200160static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
161{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200162 ssl_session_free( dst );
163 memcpy( dst, src, sizeof( ssl_session ) );
164
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200165#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200166 if( src->peer_cert != NULL )
167 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200168 int ret;
169
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500170 dst->peer_cert = polarssl_malloc( sizeof(x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200171 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200172 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
173
Paul Bakkerb6b09562013-09-18 14:17:41 +0200174 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200175
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200176 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
177 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200178 {
179 polarssl_free( dst->peer_cert );
180 dst->peer_cert = NULL;
181 return( ret );
182 }
183 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200184#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200185
Paul Bakkera503a632013-08-14 13:48:06 +0200186#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200187 if( src->ticket != NULL )
188 {
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500189 dst->ticket = polarssl_malloc( src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200190 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200191 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
192
193 memcpy( dst->ticket, src->ticket, src->ticket_len );
194 }
Paul Bakkera503a632013-08-14 13:48:06 +0200195#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200196
197 return( 0 );
198}
199
Paul Bakker05ef8352012-05-08 09:17:57 +0000200#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200201int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200202 const unsigned char *key_enc, const unsigned char *key_dec,
203 size_t keylen,
204 const unsigned char *iv_enc, const unsigned char *iv_dec,
205 size_t ivlen,
206 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200207 size_t maclen ) = NULL;
208int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
209int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
210int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
211int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
212int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200213#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000214
Paul Bakker5121ce52009-01-03 21:22:43 +0000215/*
216 * Key material generation
217 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200218#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200219static int ssl3_prf( const unsigned char *secret, size_t slen,
220 const char *label,
221 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000222 unsigned char *dstbuf, size_t dlen )
223{
224 size_t i;
225 md5_context md5;
226 sha1_context sha1;
227 unsigned char padding[16];
228 unsigned char sha1sum[20];
229 ((void)label);
230
Paul Bakker5b4af392014-06-26 12:09:34 +0200231 md5_init( &md5 );
232 sha1_init( &sha1 );
233
Paul Bakker5f70b252012-09-13 14:23:06 +0000234 /*
235 * SSLv3:
236 * block =
237 * MD5( secret + SHA1( 'A' + secret + random ) ) +
238 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
239 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
240 * ...
241 */
242 for( i = 0; i < dlen / 16; i++ )
243 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200244 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000245
246 sha1_starts( &sha1 );
247 sha1_update( &sha1, padding, 1 + i );
248 sha1_update( &sha1, secret, slen );
249 sha1_update( &sha1, random, rlen );
250 sha1_finish( &sha1, sha1sum );
251
252 md5_starts( &md5 );
253 md5_update( &md5, secret, slen );
254 md5_update( &md5, sha1sum, 20 );
255 md5_finish( &md5, dstbuf + i * 16 );
256 }
257
Paul Bakker5b4af392014-06-26 12:09:34 +0200258 md5_free( &md5 );
259 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000260
Paul Bakker34617722014-06-13 17:20:13 +0200261 polarssl_zeroize( padding, sizeof( padding ) );
262 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000263
264 return( 0 );
265}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200266#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000267
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200268#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200269static int tls1_prf( const unsigned char *secret, size_t slen,
270 const char *label,
271 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000272 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000273{
Paul Bakker23986e52011-04-24 08:57:21 +0000274 size_t nb, hs;
275 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200276 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 unsigned char tmp[128];
278 unsigned char h_i[20];
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100279 const md_info_t *md_info;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100280 md_context_t md_ctx;
281 int ret;
282
283 md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000284
285 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000286 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000287
288 hs = ( slen + 1 ) / 2;
289 S1 = secret;
290 S2 = secret + slen - hs;
291
292 nb = strlen( label );
293 memcpy( tmp + 20, label, nb );
294 memcpy( tmp + 20 + nb, random, rlen );
295 nb += rlen;
296
297 /*
298 * First compute P_md5(secret,label+random)[0..dlen]
299 */
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100300 if( ( md_info = md_info_from_type( POLARSSL_MD_MD5 ) ) == NULL )
301 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
302
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100303 if( ( ret = md_setup( &md_ctx, md_info, 1 ) ) != 0 )
304 return( ret );
305
306 md_hmac_starts( &md_ctx, S1, hs );
307 md_hmac_update( &md_ctx, tmp + 20, nb );
308 md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000309
310 for( i = 0; i < dlen; i += 16 )
311 {
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100312 md_hmac_reset ( &md_ctx );
313 md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
314 md_hmac_finish( &md_ctx, h_i );
315
316 md_hmac_reset ( &md_ctx );
317 md_hmac_update( &md_ctx, 4 + tmp, 16 );
318 md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000319
320 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
321
322 for( j = 0; j < k; j++ )
323 dstbuf[i + j] = h_i[j];
324 }
325
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100326 md_free( &md_ctx );
327
Paul Bakker5121ce52009-01-03 21:22:43 +0000328 /*
329 * XOR out with P_sha1(secret,label+random)[0..dlen]
330 */
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100331 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA1 ) ) == NULL )
332 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
333
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100334 if( ( ret = md_setup( &md_ctx, md_info, 1 ) ) != 0 )
335 return( ret );
336
337 md_hmac_starts( &md_ctx, S2, hs );
338 md_hmac_update( &md_ctx, tmp + 20, nb );
339 md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000340
341 for( i = 0; i < dlen; i += 20 )
342 {
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100343 md_hmac_reset ( &md_ctx );
344 md_hmac_update( &md_ctx, tmp, 20 + nb );
345 md_hmac_finish( &md_ctx, h_i );
346
347 md_hmac_reset ( &md_ctx );
348 md_hmac_update( &md_ctx, tmp, 20 );
349 md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000350
351 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
352
353 for( j = 0; j < k; j++ )
354 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
355 }
356
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100357 md_free( &md_ctx );
358
Paul Bakker34617722014-06-13 17:20:13 +0200359 polarssl_zeroize( tmp, sizeof( tmp ) );
360 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000361
362 return( 0 );
363}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200364#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000365
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200366#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100367static int tls_prf_generic( md_type_t md_type,
368 const unsigned char *secret, size_t slen,
369 const char *label,
370 const unsigned char *random, size_t rlen,
371 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000372{
373 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100374 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000375 unsigned char tmp[128];
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100376 unsigned char h_i[POLARSSL_MD_MAX_SIZE];
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100377 const md_info_t *md_info;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100378 md_context_t md_ctx;
379 int ret;
380
381 md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000382
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100383 if( ( md_info = md_info_from_type( md_type ) ) == NULL )
384 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
385
386 md_len = md_get_size( md_info );
387
388 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000389 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
390
391 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100392 memcpy( tmp + md_len, label, nb );
393 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000394 nb += rlen;
395
396 /*
397 * Compute P_<hash>(secret, label + random)[0..dlen]
398 */
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100399 if ( ( ret = md_setup( &md_ctx, md_info, 1 ) ) != 0 )
400 return( ret );
401
402 md_hmac_starts( &md_ctx, secret, slen );
403 md_hmac_update( &md_ctx, tmp + md_len, nb );
404 md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100405
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100406 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000407 {
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100408 md_hmac_reset ( &md_ctx );
409 md_hmac_update( &md_ctx, tmp, md_len + nb );
410 md_hmac_finish( &md_ctx, h_i );
411
412 md_hmac_reset ( &md_ctx );
413 md_hmac_update( &md_ctx, tmp, md_len );
414 md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000415
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100416 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000417
418 for( j = 0; j < k; j++ )
419 dstbuf[i + j] = h_i[j];
420 }
421
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100422 md_free( &md_ctx );
423
Paul Bakker34617722014-06-13 17:20:13 +0200424 polarssl_zeroize( tmp, sizeof( tmp ) );
425 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000426
427 return( 0 );
428}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100429
430#if defined(POLARSSL_SHA256_C)
431static int tls_prf_sha256( const unsigned char *secret, size_t slen,
432 const char *label,
433 const unsigned char *random, size_t rlen,
434 unsigned char *dstbuf, size_t dlen )
435{
436 return( tls_prf_generic( POLARSSL_MD_SHA256, secret, slen,
437 label, random, rlen, dstbuf, dlen ) );
438}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200439#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000440
Paul Bakker9e36f042013-06-30 14:34:05 +0200441#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200442static int tls_prf_sha384( const unsigned char *secret, size_t slen,
443 const char *label,
444 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000445 unsigned char *dstbuf, size_t dlen )
446{
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100447 return( tls_prf_generic( POLARSSL_MD_SHA384, secret, slen,
448 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000449}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200450#endif /* POLARSSL_SHA512_C */
451#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000452
Paul Bakker66d5d072014-06-17 16:39:18 +0200453static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200454
455#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
456 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200457static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200458#endif
Paul Bakker380da532012-04-18 16:10:25 +0000459
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200460#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200461static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
462static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200463#endif
464
465#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200466static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
467static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200468#endif
469
470#if defined(POLARSSL_SSL_PROTO_TLS1_2)
471#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200472static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
473static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
474static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200475#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100476
Paul Bakker9e36f042013-06-30 14:34:05 +0200477#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200478static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
479static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
480static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100481#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200482#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000483
Paul Bakker5121ce52009-01-03 21:22:43 +0000484int ssl_derive_keys( ssl_context *ssl )
485{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200486 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000487 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000488 unsigned char keyblk[256];
489 unsigned char *key1;
490 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100491 unsigned char *mac_enc;
492 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200493 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100494 const cipher_info_t *cipher_info;
495 const md_info_t *md_info;
496
Paul Bakker48916f92012-09-16 19:57:18 +0000497 ssl_session *session = ssl->session_negotiate;
498 ssl_transform *transform = ssl->transform_negotiate;
499 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000500
501 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
502
Paul Bakker68884e32013-01-07 18:20:04 +0100503 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
504 if( cipher_info == NULL )
505 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200506 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100507 transform->ciphersuite_info->cipher ) );
508 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
509 }
510
511 md_info = md_info_from_type( transform->ciphersuite_info->mac );
512 if( md_info == NULL )
513 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200514 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100515 transform->ciphersuite_info->mac ) );
516 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
517 }
518
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000520 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000521 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200522#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000523 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000524 {
Paul Bakker48916f92012-09-16 19:57:18 +0000525 handshake->tls_prf = ssl3_prf;
526 handshake->calc_verify = ssl_calc_verify_ssl;
527 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000528 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200529 else
530#endif
531#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
532 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000533 {
Paul Bakker48916f92012-09-16 19:57:18 +0000534 handshake->tls_prf = tls1_prf;
535 handshake->calc_verify = ssl_calc_verify_tls;
536 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000537 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200538 else
539#endif
540#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200541#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200542 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
543 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000544 {
Paul Bakker48916f92012-09-16 19:57:18 +0000545 handshake->tls_prf = tls_prf_sha384;
546 handshake->calc_verify = ssl_calc_verify_tls_sha384;
547 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000548 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000549 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200550#endif
551#if defined(POLARSSL_SHA256_C)
552 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000553 {
Paul Bakker48916f92012-09-16 19:57:18 +0000554 handshake->tls_prf = tls_prf_sha256;
555 handshake->calc_verify = ssl_calc_verify_tls_sha256;
556 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000557 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200558 else
559#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200560#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200561 {
562 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200563 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200564 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000565
566 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 * SSLv3:
568 * master =
569 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
570 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
571 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200572 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200573 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 * master = PRF( premaster, "master secret", randbytes )[0..47]
575 */
Paul Bakker0a597072012-09-25 21:55:46 +0000576 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 {
Paul Bakker48916f92012-09-16 19:57:18 +0000578 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
579 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200581#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
582 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200583 {
584 unsigned char session_hash[48];
585 size_t hash_len;
586
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200587 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200588
589 ssl->handshake->calc_verify( ssl, session_hash );
590
591#if defined(POLARSSL_SSL_PROTO_TLS1_2)
592 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
593 {
594#if defined(POLARSSL_SHA512_C)
595 if( ssl->transform_negotiate->ciphersuite_info->mac ==
596 POLARSSL_MD_SHA384 )
597 {
598 hash_len = 48;
599 }
600 else
601#endif
602 hash_len = 32;
603 }
604 else
605#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
606 hash_len = 36;
607
608 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
609
610 handshake->tls_prf( handshake->premaster, handshake->pmslen,
611 "extended master secret",
612 session_hash, hash_len, session->master, 48 );
613
614 }
615 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200616#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000617 handshake->tls_prf( handshake->premaster, handshake->pmslen,
618 "master secret",
619 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200621
Paul Bakker34617722014-06-13 17:20:13 +0200622 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 }
624 else
625 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
626
627 /*
628 * Swap the client and server random values.
629 */
Paul Bakker48916f92012-09-16 19:57:18 +0000630 memcpy( tmp, handshake->randbytes, 64 );
631 memcpy( handshake->randbytes, tmp + 32, 32 );
632 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200633 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000634
635 /*
636 * SSLv3:
637 * key block =
638 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
639 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
640 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
641 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
642 * ...
643 *
644 * TLSv1:
645 * key block = PRF( master, "key expansion", randbytes )
646 */
Paul Bakker48916f92012-09-16 19:57:18 +0000647 handshake->tls_prf( session->master, 48, "key expansion",
648 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000649
Paul Bakker48916f92012-09-16 19:57:18 +0000650 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
651 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
652 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
653 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000654 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
655
Paul Bakker34617722014-06-13 17:20:13 +0200656 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000657
658 /*
659 * Determine the appropriate key, IV and MAC length.
660 */
Paul Bakker68884e32013-01-07 18:20:04 +0100661
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200662 transform->keylen = cipher_info->key_length / 8;
663
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200664 if( cipher_info->mode == POLARSSL_MODE_GCM ||
665 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000666 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200667 transform->maclen = 0;
668
Paul Bakker68884e32013-01-07 18:20:04 +0100669 transform->ivlen = 12;
670 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200671
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200672 /* Minimum length is expicit IV + tag */
673 transform->minlen = transform->ivlen - transform->fixed_ivlen
674 + ( transform->ciphersuite_info->flags &
675 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100676 }
677 else
678 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200679 int ret;
680
681 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnardabb67442015-03-25 16:29:51 +0100682 if( ( ret = md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
683 ( ret = md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100684 {
Manuel Pégourié-Gonnardabb67442015-03-25 16:29:51 +0100685 SSL_DEBUG_RET( 1, "md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200686 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100687 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200689 /* Get MAC length */
690 transform->maclen = md_get_size( md_info );
691
692#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
693 /*
694 * If HMAC is to be truncated, we shall keep the leftmost bytes,
695 * (rfc 6066 page 13 or rfc 2104 section 4),
696 * so we only need to adjust the length here.
697 */
698 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
699 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
700#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
701
702 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100703 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000704
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200705 /* Minimum length */
706 if( cipher_info->mode == POLARSSL_MODE_STREAM )
707 transform->minlen = transform->maclen;
708 else
Paul Bakker68884e32013-01-07 18:20:04 +0100709 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200710 /*
711 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100712 * 1. if EtM is in use: one block plus MAC
713 * otherwise: * first multiple of blocklen greater than maclen
714 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200715 */
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100716#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
717 if( session->encrypt_then_mac == SSL_ETM_ENABLED )
718 {
719 transform->minlen = transform->maclen
720 + cipher_info->block_size;
721 }
722 else
723#endif
724 {
725 transform->minlen = transform->maclen
726 + cipher_info->block_size
727 - transform->maclen % cipher_info->block_size;
728 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200729
730#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
731 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
732 ssl->minor_ver == SSL_MINOR_VERSION_1 )
733 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100734 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200735#endif
736#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
737 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
738 ssl->minor_ver == SSL_MINOR_VERSION_3 )
739 {
740 transform->minlen += transform->ivlen;
741 }
742 else
743#endif
744 {
745 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
746 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
747 }
Paul Bakker68884e32013-01-07 18:20:04 +0100748 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000749 }
750
751 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000752 transform->keylen, transform->minlen, transform->ivlen,
753 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000754
755 /*
756 * Finally setup the cipher contexts, IVs and MAC secrets.
757 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100758#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000759 if( ssl->endpoint == SSL_IS_CLIENT )
760 {
Paul Bakker48916f92012-09-16 19:57:18 +0000761 key1 = keyblk + transform->maclen * 2;
762 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000763
Paul Bakker68884e32013-01-07 18:20:04 +0100764 mac_enc = keyblk;
765 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000766
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000767 /*
768 * This is not used in TLS v1.1.
769 */
Paul Bakker48916f92012-09-16 19:57:18 +0000770 iv_copy_len = ( transform->fixed_ivlen ) ?
771 transform->fixed_ivlen : transform->ivlen;
772 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
773 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000774 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000775 }
776 else
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100777#endif /* POLARSSL_SSL_CLI_C */
778#if defined(POLARSSL_SSL_SRV_C)
779 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000780 {
Paul Bakker48916f92012-09-16 19:57:18 +0000781 key1 = keyblk + transform->maclen * 2 + transform->keylen;
782 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000783
Paul Bakker68884e32013-01-07 18:20:04 +0100784 mac_enc = keyblk + transform->maclen;
785 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000786
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000787 /*
788 * This is not used in TLS v1.1.
789 */
Paul Bakker48916f92012-09-16 19:57:18 +0000790 iv_copy_len = ( transform->fixed_ivlen ) ?
791 transform->fixed_ivlen : transform->ivlen;
792 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
793 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000794 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000795 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100796 else
797#endif /* POLARSSL_SSL_SRV_C */
798 {
799 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
800 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
801 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000802
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200803#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100804 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
805 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100806 if( transform->maclen > sizeof transform->mac_enc )
807 {
808 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200809 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100810 }
811
Paul Bakker68884e32013-01-07 18:20:04 +0100812 memcpy( transform->mac_enc, mac_enc, transform->maclen );
813 memcpy( transform->mac_dec, mac_dec, transform->maclen );
814 }
815 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200816#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200817#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
818 defined(POLARSSL_SSL_PROTO_TLS1_2)
819 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100820 {
821 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
822 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
823 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200824 else
825#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200826 {
827 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200828 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200829 }
Paul Bakker68884e32013-01-07 18:20:04 +0100830
Paul Bakker05ef8352012-05-08 09:17:57 +0000831#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200832 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000833 {
834 int ret = 0;
835
836 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
837
Paul Bakker07eb38b2012-12-19 14:42:06 +0100838 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
839 transform->iv_enc, transform->iv_dec,
840 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100841 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100842 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000843 {
844 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200845 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000846 }
847 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200848#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000849
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200850 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
851 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000852 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200853 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
854 return( ret );
855 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200856
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200857 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
858 cipher_info ) ) != 0 )
859 {
860 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
861 return( ret );
862 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200863
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200864 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
865 cipher_info->key_length,
866 POLARSSL_ENCRYPT ) ) != 0 )
867 {
868 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
869 return( ret );
870 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200871
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200872 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
873 cipher_info->key_length,
874 POLARSSL_DECRYPT ) ) != 0 )
875 {
876 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
877 return( ret );
878 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200879
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200880#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200881 if( cipher_info->mode == POLARSSL_MODE_CBC )
882 {
883 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
884 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200885 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200886 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
887 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200888 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200889
890 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
891 POLARSSL_PADDING_NONE ) ) != 0 )
892 {
893 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
894 return( ret );
895 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000896 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200897#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000898
Paul Bakker34617722014-06-13 17:20:13 +0200899 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000900
Paul Bakker2770fbd2012-07-03 13:30:23 +0000901#if defined(POLARSSL_ZLIB_SUPPORT)
902 // Initialize compression
903 //
Paul Bakker48916f92012-09-16 19:57:18 +0000904 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000905 {
Paul Bakker16770332013-10-11 09:59:44 +0200906 if( ssl->compress_buf == NULL )
907 {
908 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
909 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
910 if( ssl->compress_buf == NULL )
911 {
912 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
913 SSL_BUFFER_LEN ) );
914 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
915 }
916 }
917
Paul Bakker2770fbd2012-07-03 13:30:23 +0000918 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
919
Paul Bakker48916f92012-09-16 19:57:18 +0000920 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
921 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000922
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200923 if( deflateInit( &transform->ctx_deflate,
924 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000925 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000926 {
927 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
928 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
929 }
930 }
931#endif /* POLARSSL_ZLIB_SUPPORT */
932
Paul Bakker5121ce52009-01-03 21:22:43 +0000933 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
934
935 return( 0 );
936}
937
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200938#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000939void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000940{
941 md5_context md5;
942 sha1_context sha1;
943 unsigned char pad_1[48];
944 unsigned char pad_2[48];
945
Paul Bakker380da532012-04-18 16:10:25 +0000946 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000947
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 Bakker5121ce52009-01-03 21:22:43 +0000950
Paul Bakker380da532012-04-18 16:10:25 +0000951 memset( pad_1, 0x36, 48 );
952 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000953
Paul Bakker48916f92012-09-16 19:57:18 +0000954 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000955 md5_update( &md5, pad_1, 48 );
956 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000957
Paul Bakker380da532012-04-18 16:10:25 +0000958 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000959 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000960 md5_update( &md5, pad_2, 48 );
961 md5_update( &md5, hash, 16 );
962 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000963
Paul Bakker48916f92012-09-16 19:57:18 +0000964 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000965 sha1_update( &sha1, pad_1, 40 );
966 sha1_finish( &sha1, hash + 16 );
967
968 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000969 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000970 sha1_update( &sha1, pad_2, 40 );
971 sha1_update( &sha1, hash + 16, 20 );
972 sha1_finish( &sha1, hash + 16 );
973
974 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
975 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
976
Paul Bakker5b4af392014-06-26 12:09:34 +0200977 md5_free( &md5 );
978 sha1_free( &sha1 );
979
Paul Bakker380da532012-04-18 16:10:25 +0000980 return;
981}
Paul Bakker9af723c2014-05-01 13:03:14 +0200982#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000983
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200984#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000985void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
986{
987 md5_context md5;
988 sha1_context sha1;
989
990 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
991
Paul Bakker48916f92012-09-16 19:57:18 +0000992 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
993 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000994
Paul Bakker48916f92012-09-16 19:57:18 +0000995 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000996 sha1_finish( &sha1, hash + 16 );
997
998 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
999 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1000
Paul Bakker5b4af392014-06-26 12:09:34 +02001001 md5_free( &md5 );
1002 sha1_free( &sha1 );
1003
Paul Bakker380da532012-04-18 16:10:25 +00001004 return;
1005}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001006#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001007
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001008#if defined(POLARSSL_SSL_PROTO_TLS1_2)
1009#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +00001010void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
1011{
Paul Bakker9e36f042013-06-30 14:34:05 +02001012 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001013
1014 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1015
Paul Bakker9e36f042013-06-30 14:34:05 +02001016 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
1017 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001018
1019 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1020 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1021
Paul Bakker5b4af392014-06-26 12:09:34 +02001022 sha256_free( &sha256 );
1023
Paul Bakker380da532012-04-18 16:10:25 +00001024 return;
1025}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001026#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001027
Paul Bakker9e36f042013-06-30 14:34:05 +02001028#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +00001029void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
1030{
Paul Bakker9e36f042013-06-30 14:34:05 +02001031 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001032
1033 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1034
Paul Bakker9e36f042013-06-30 14:34:05 +02001035 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
1036 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001037
Paul Bakkerca4ab492012-04-18 14:23:57 +00001038 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001039 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1040
Paul Bakker5b4af392014-06-26 12:09:34 +02001041 sha512_free( &sha512 );
1042
Paul Bakker5121ce52009-01-03 21:22:43 +00001043 return;
1044}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001045#endif /* POLARSSL_SHA512_C */
1046#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001047
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001048#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001049int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
1050{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001051 unsigned char *p = ssl->handshake->premaster;
1052 unsigned char *end = p + sizeof( ssl->handshake->premaster );
1053
1054 /*
1055 * PMS = struct {
1056 * opaque other_secret<0..2^16-1>;
1057 * opaque psk<0..2^16-1>;
1058 * };
1059 * with "other_secret" depending on the particular key exchange
1060 */
1061#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
1062 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
1063 {
1064 if( end - p < 2 + (int) ssl->psk_len )
1065 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1066
1067 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1068 *(p++) = (unsigned char)( ssl->psk_len );
1069 p += ssl->psk_len;
1070 }
1071 else
1072#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001073#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1074 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1075 {
1076 /*
1077 * other_secret already set by the ClientKeyExchange message,
1078 * and is 48 bytes long
1079 */
1080 *p++ = 0;
1081 *p++ = 48;
1082 p += 48;
1083 }
1084 else
1085#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001086#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1087 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1088 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001089 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02001090 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001091
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001092 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001093 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001094 p + 2, &len,
1095 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001096 {
1097 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
1098 return( ret );
1099 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001100 *(p++) = (unsigned char)( len >> 8 );
1101 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001102 p += len;
1103
1104 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1105 }
1106 else
1107#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1108#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1109 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1110 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001111 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001112 size_t zlen;
1113
1114 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001115 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001116 ssl->f_rng, ssl->p_rng ) ) != 0 )
1117 {
1118 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1119 return( ret );
1120 }
1121
1122 *(p++) = (unsigned char)( zlen >> 8 );
1123 *(p++) = (unsigned char)( zlen );
1124 p += zlen;
1125
1126 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1127 }
1128 else
1129#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1130 {
1131 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001132 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001133 }
1134
1135 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001136 if( end - p < 2 + (int) ssl->psk_len )
1137 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1138
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001139 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1140 *(p++) = (unsigned char)( ssl->psk_len );
1141 memcpy( p, ssl->psk, ssl->psk_len );
1142 p += ssl->psk_len;
1143
1144 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1145
1146 return( 0 );
1147}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001148#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001149
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001150#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001151/*
1152 * SSLv3.0 MAC functions
1153 */
Paul Bakker68884e32013-01-07 18:20:04 +01001154static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1155 unsigned char *buf, size_t len,
1156 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001157{
1158 unsigned char header[11];
1159 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001160 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001161 int md_size = md_get_size( md_ctx->md_info );
1162 int md_type = md_get_type( md_ctx->md_info );
1163
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001164 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001165 if( md_type == POLARSSL_MD_MD5 )
1166 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001167 else
Paul Bakker68884e32013-01-07 18:20:04 +01001168 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001169
1170 memcpy( header, ctr, 8 );
1171 header[ 8] = (unsigned char) type;
1172 header[ 9] = (unsigned char)( len >> 8 );
1173 header[10] = (unsigned char)( len );
1174
Paul Bakker68884e32013-01-07 18:20:04 +01001175 memset( padding, 0x36, padlen );
1176 md_starts( md_ctx );
1177 md_update( md_ctx, secret, md_size );
1178 md_update( md_ctx, padding, padlen );
1179 md_update( md_ctx, header, 11 );
1180 md_update( md_ctx, buf, len );
1181 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001182
Paul Bakker68884e32013-01-07 18:20:04 +01001183 memset( padding, 0x5C, padlen );
1184 md_starts( md_ctx );
1185 md_update( md_ctx, secret, md_size );
1186 md_update( md_ctx, padding, padlen );
1187 md_update( md_ctx, buf + len, md_size );
1188 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001189}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001190#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001191
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001192#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1193 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1194 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1195#define POLARSSL_SOME_MODES_USE_MAC
1196#endif
1197
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001198/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001199 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001200 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001201static int ssl_encrypt_buf( ssl_context *ssl )
1202{
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001203 cipher_mode_t mode;
1204 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001205
1206 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1207
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001208 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1209 {
1210 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1211 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1212 }
1213
1214 mode = cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
1215
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001216 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1217 ssl->out_msg, ssl->out_msglen );
1218
Paul Bakker5121ce52009-01-03 21:22:43 +00001219 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001220 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001221 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001222#if defined(POLARSSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001223 if( mode == POLARSSL_MODE_STREAM ||
1224 ( mode == POLARSSL_MODE_CBC
1225#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1226 && ssl->session_out->encrypt_then_mac == SSL_ETM_DISABLED
1227#endif
1228 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001229 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001230#if defined(POLARSSL_SSL_PROTO_SSL3)
1231 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1232 {
1233 ssl_mac( &ssl->transform_out->md_ctx_enc,
1234 ssl->transform_out->mac_enc,
1235 ssl->out_msg, ssl->out_msglen,
1236 ssl->out_ctr, ssl->out_msgtype );
1237 }
1238 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001239#endif
1240#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001241 defined(POLARSSL_SSL_PROTO_TLS1_2)
1242 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1243 {
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001244 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1245 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1246 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001247 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1248 ssl->out_msg, ssl->out_msglen );
1249 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1250 ssl->out_msg + ssl->out_msglen );
1251 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1252 }
1253 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001254#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001255 {
1256 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001257 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001258 }
1259
1260 SSL_DEBUG_BUF( 4, "computed mac",
1261 ssl->out_msg + ssl->out_msglen,
1262 ssl->transform_out->maclen );
1263
1264 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001265 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001266 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001267#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001268
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001269 /*
1270 * Encrypt
1271 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001272#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001273 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001274 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001275 int ret;
1276 size_t olen = 0;
1277
Paul Bakker5121ce52009-01-03 21:22:43 +00001278 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1279 "including %d bytes of padding",
1280 ssl->out_msglen, 0 ) );
1281
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001282 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001283 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001284 ssl->transform_out->ivlen,
1285 ssl->out_msg, ssl->out_msglen,
1286 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001287 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001288 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001289 return( ret );
1290 }
1291
1292 if( ssl->out_msglen != olen )
1293 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001294 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001295 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001296 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001297 }
Paul Bakker68884e32013-01-07 18:20:04 +01001298 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001299#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001300#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1301 if( mode == POLARSSL_MODE_GCM ||
1302 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001303 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001304 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001305 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001306 unsigned char *enc_msg;
1307 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001308 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1309 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001310
Paul Bakkerca4ab492012-04-18 14:23:57 +00001311 memcpy( add_data, ssl->out_ctr, 8 );
1312 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001313 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1314 ssl->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001315 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1316 add_data[12] = ssl->out_msglen & 0xFF;
1317
1318 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1319 add_data, 13 );
1320
Paul Bakker68884e32013-01-07 18:20:04 +01001321 /*
1322 * Generate IV
1323 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001324#if defined(POLARSSL_SSL_AEAD_RANDOM_IV)
Paul Bakker68884e32013-01-07 18:20:04 +01001325 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001326 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1327 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001328 if( ret != 0 )
1329 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001330
Paul Bakker68884e32013-01-07 18:20:04 +01001331 memcpy( ssl->out_iv,
1332 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1333 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001334#else
1335 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1336 {
1337 /* Reminder if we ever add an AEAD mode with a different size */
1338 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1339 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1340 }
1341
1342 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1343 ssl->out_ctr, 8 );
1344 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1345#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00001346
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001347 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001348 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001349
Paul Bakker68884e32013-01-07 18:20:04 +01001350 /*
1351 * Fix pointer positions and message length with added IV
1352 */
1353 enc_msg = ssl->out_msg;
1354 enc_msglen = ssl->out_msglen;
1355 ssl->out_msglen += ssl->transform_out->ivlen -
1356 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001357
Paul Bakker68884e32013-01-07 18:20:04 +01001358 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1359 "including %d bytes of padding",
1360 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001361
Paul Bakker68884e32013-01-07 18:20:04 +01001362 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001363 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001364 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001365 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1366 ssl->transform_out->iv_enc,
1367 ssl->transform_out->ivlen,
1368 add_data, 13,
1369 enc_msg, enc_msglen,
1370 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001371 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001372 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001373 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001374 return( ret );
1375 }
1376
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001377 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001378 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001379 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001380 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001381 }
1382
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001383 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001384 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001385
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001386 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001387 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001388 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001389#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001390#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1391 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001392 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001393 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001394 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001395 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001396 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001397
Paul Bakker48916f92012-09-16 19:57:18 +00001398 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1399 ssl->transform_out->ivlen;
1400 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001401 padlen = 0;
1402
1403 for( i = 0; i <= padlen; i++ )
1404 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1405
1406 ssl->out_msglen += padlen + 1;
1407
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001408 enc_msglen = ssl->out_msglen;
1409 enc_msg = ssl->out_msg;
1410
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001411#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001412 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001413 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1414 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001415 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001416 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001417 {
1418 /*
1419 * Generate IV
1420 */
Paul Bakker48916f92012-09-16 19:57:18 +00001421 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1422 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001423 if( ret != 0 )
1424 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001425
Paul Bakker92be97b2013-01-02 17:30:03 +01001426 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001427 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001428
1429 /*
1430 * Fix pointer positions and message length with added IV
1431 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001432 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001433 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001434 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001435 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001436#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001437
Paul Bakker5121ce52009-01-03 21:22:43 +00001438 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001439 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001440 ssl->out_msglen, ssl->transform_out->ivlen,
1441 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001442
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001443 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001444 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001445 ssl->transform_out->ivlen,
1446 enc_msg, enc_msglen,
1447 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001448 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001449 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001450 return( ret );
1451 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001452
Paul Bakkercca5b812013-08-31 17:40:26 +02001453 if( enc_msglen != olen )
1454 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001455 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001456 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001457 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001458
1459#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001460 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1461 {
1462 /*
1463 * Save IV in SSL3 and TLS1
1464 */
1465 memcpy( ssl->transform_out->iv_enc,
1466 ssl->transform_out->cipher_ctx_enc.iv,
1467 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001468 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001469#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001470
1471#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001472 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001473 {
1474 /*
1475 * MAC(MAC_write_key, seq_num +
1476 * TLSCipherText.type +
1477 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001478 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001479 * IV + // except for TLS 1.0
1480 * ENC(content + padding + padding_length));
1481 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001482 unsigned char pseudo_hdr[13];
1483
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001484 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1485
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001486 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1487 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001488 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1489 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1490
1491 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001492
1493 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1494 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1495 ssl->out_iv, ssl->out_msglen );
1496 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1497 ssl->out_iv + ssl->out_msglen );
1498 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1499
1500 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001501 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001502 }
1503#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001504 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001505 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001506#endif /* POLARSSL_CIPHER_MODE_CBC &&
1507 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001508 {
1509 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001510 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001511 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001512
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001513 /* Make extra sure authentication was performed, exactly once */
1514 if( auth_done != 1 )
1515 {
1516 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1517 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1518 }
1519
Paul Bakker5121ce52009-01-03 21:22:43 +00001520 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1521
1522 return( 0 );
1523}
1524
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001525#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001526
Paul Bakker5121ce52009-01-03 21:22:43 +00001527static int ssl_decrypt_buf( ssl_context *ssl )
1528{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001529 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001530 cipher_mode_t mode;
1531 int auth_done = 0;
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001532#if defined(POLARSSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001533 size_t padlen = 0, correct = 1;
1534#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001535
1536 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1537
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001538 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1539 {
1540 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1541 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1542 }
1543
1544 mode = cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
1545
Paul Bakker48916f92012-09-16 19:57:18 +00001546 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001547 {
1548 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001549 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001550 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001551 }
1552
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001553#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001554 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001555 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001556 int ret;
1557 size_t olen = 0;
1558
Paul Bakker68884e32013-01-07 18:20:04 +01001559 padlen = 0;
1560
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001561 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001562 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001563 ssl->transform_in->ivlen,
1564 ssl->in_msg, ssl->in_msglen,
1565 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001566 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001567 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001568 return( ret );
1569 }
1570
1571 if( ssl->in_msglen != olen )
1572 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001573 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001574 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001575 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001576 }
Paul Bakker68884e32013-01-07 18:20:04 +01001577 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001578#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001579#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1580 if( mode == POLARSSL_MODE_GCM ||
1581 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001582 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001583 int ret;
1584 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001585 unsigned char *dec_msg;
1586 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001587 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001588 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1589 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001590 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1591 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001592
Manuel Pégourié-Gonnard06d75192015-02-11 14:54:11 +00001593 if( ssl->in_msglen < (size_t) explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001594 {
1595 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1596 "+ taglen (%d)", ssl->in_msglen,
1597 explicit_iv_len, taglen ) );
1598 return( POLARSSL_ERR_SSL_INVALID_MAC );
1599 }
1600 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1601
Paul Bakker68884e32013-01-07 18:20:04 +01001602 dec_msg = ssl->in_msg;
1603 dec_msg_result = ssl->in_msg;
1604 ssl->in_msglen = dec_msglen;
1605
1606 memcpy( add_data, ssl->in_ctr, 8 );
1607 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001608 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1609 ssl->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001610 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1611 add_data[12] = ssl->in_msglen & 0xFF;
1612
1613 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1614 add_data, 13 );
1615
1616 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1617 ssl->in_iv,
1618 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1619
1620 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1621 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001622 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001623
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001624 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001625 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001626 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001627 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1628 ssl->transform_in->iv_dec,
1629 ssl->transform_in->ivlen,
1630 add_data, 13,
1631 dec_msg, dec_msglen,
1632 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001633 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001634 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001635 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1636
1637 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1638 return( POLARSSL_ERR_SSL_INVALID_MAC );
1639
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001640 return( ret );
1641 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001642 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001643
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001644 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001645 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001646 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001647 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001648 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001649 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001650 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001651#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001652#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1653 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001654 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001655 {
Paul Bakker45829992013-01-03 14:52:21 +01001656 /*
1657 * Decrypt and check the padding
1658 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001659 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001660 unsigned char *dec_msg;
1661 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001662 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001663 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001664 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001665
Paul Bakker5121ce52009-01-03 21:22:43 +00001666 /*
Paul Bakker45829992013-01-03 14:52:21 +01001667 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001668 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001669#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001670 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1671 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001672#endif
Paul Bakker45829992013-01-03 14:52:21 +01001673
1674 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1675 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1676 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001677 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1678 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1679 ssl->transform_in->ivlen,
1680 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001681 return( POLARSSL_ERR_SSL_INVALID_MAC );
1682 }
1683
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001684 dec_msglen = ssl->in_msglen;
1685 dec_msg = ssl->in_msg;
1686 dec_msg_result = ssl->in_msg;
1687
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001688 /*
1689 * Authenticate before decrypt if enabled
1690 */
1691#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001692 if( ssl->session_in->encrypt_then_mac == SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001693 {
1694 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001695 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001696
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001697 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1698
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001699 dec_msglen -= ssl->transform_in->maclen;
1700 ssl->in_msglen -= ssl->transform_in->maclen;
1701
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001702 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1703 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1704 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1705 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1706
1707 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1708
1709 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001710 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1711 ssl->in_iv, ssl->in_msglen );
1712 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1713 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1714
1715 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1716 ssl->transform_in->maclen );
1717 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1718 ssl->transform_in->maclen );
1719
1720 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1721 ssl->transform_in->maclen ) != 0 )
1722 {
1723 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1724
1725 return( POLARSSL_ERR_SSL_INVALID_MAC );
1726 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001727 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001728 }
1729#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1730
1731 /*
1732 * Check length sanity
1733 */
1734 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1735 {
1736 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1737 ssl->in_msglen, ssl->transform_in->ivlen ) );
1738 return( POLARSSL_ERR_SSL_INVALID_MAC );
1739 }
1740
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001741#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001742 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001743 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001744 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001745 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001746 {
Paul Bakker48916f92012-09-16 19:57:18 +00001747 dec_msglen -= ssl->transform_in->ivlen;
1748 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001749
Paul Bakker48916f92012-09-16 19:57:18 +00001750 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001751 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001752 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001753#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001754
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001755 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001756 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001757 ssl->transform_in->ivlen,
1758 dec_msg, dec_msglen,
1759 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001760 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001761 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001762 return( ret );
1763 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001764
Paul Bakkercca5b812013-08-31 17:40:26 +02001765 if( dec_msglen != olen )
1766 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001767 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001768 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001769 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001770
1771#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001772 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1773 {
1774 /*
1775 * Save IV in SSL3 and TLS1
1776 */
1777 memcpy( ssl->transform_in->iv_dec,
1778 ssl->transform_in->cipher_ctx_dec.iv,
1779 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001780 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001781#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001782
1783 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001784
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001785 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001786 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001787 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001788#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001789 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1790 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001791#endif
Paul Bakker45829992013-01-03 14:52:21 +01001792 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001793 correct = 0;
1794 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001795
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001796#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001797 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1798 {
Paul Bakker48916f92012-09-16 19:57:18 +00001799 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001800 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001801#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001802 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1803 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001804 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001805#endif
Paul Bakker45829992013-01-03 14:52:21 +01001806 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001807 }
1808 }
1809 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001810#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001811#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1812 defined(POLARSSL_SSL_PROTO_TLS1_2)
1813 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001814 {
1815 /*
Paul Bakker45829992013-01-03 14:52:21 +01001816 * TLSv1+: always check the padding up to the first failure
1817 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001818 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001819 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001820 size_t padding_idx = ssl->in_msglen - padlen - 1;
1821
Paul Bakker956c9e02013-12-19 14:42:28 +01001822 /*
1823 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001824 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001825 *
Paul Bakker61885c72014-04-25 12:59:03 +02001826 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1827 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001828 *
1829 * In both cases we reset padding_idx to a safe value (0) to
1830 * prevent out-of-buffer reads.
1831 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001832 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001833 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1834 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001835
1836 padding_idx *= correct;
1837
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001838 for( i = 1; i <= 256; i++ )
1839 {
1840 real_count &= ( i <= padlen );
1841 pad_count += real_count *
1842 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1843 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001844
1845 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001846
Paul Bakkerd66f0702013-01-31 16:57:45 +01001847#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001848 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001849 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001850#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001851 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001852 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001853 else
1854#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1855 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001856 {
1857 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001858 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001859 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001860
1861 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001862 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001863 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001864#endif /* POLARSSL_CIPHER_MODE_CBC &&
1865 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001866 {
1867 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001868 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001869 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001870
1871 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1872 ssl->in_msg, ssl->in_msglen );
1873
1874 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001875 * Authenticate if not done yet.
1876 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001877 */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001878#if defined(POLARSSL_SOME_MODES_USE_MAC)
1879 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001880 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001881 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1882
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001883 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001884
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001885 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1886 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001887
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001888 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001889
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001890#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001891 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1892 {
1893 ssl_mac( &ssl->transform_in->md_ctx_dec,
1894 ssl->transform_in->mac_dec,
1895 ssl->in_msg, ssl->in_msglen,
1896 ssl->in_ctr, ssl->in_msgtype );
1897 }
1898 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001899#endif /* POLARSSL_SSL_PROTO_SSL3 */
1900#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001901 defined(POLARSSL_SSL_PROTO_TLS1_2)
1902 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1903 {
1904 /*
1905 * Process MAC and always update for padlen afterwards to make
1906 * total time independent of padlen
1907 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001908 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001909 *
1910 * Known timing attacks:
1911 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1912 *
1913 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1914 * correctly. (We round down instead of up, so -56 is the correct
1915 * value for our calculations instead of -55)
1916 */
1917 size_t j, extra_run = 0;
1918 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1919 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001920
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001921 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001922
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001923 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
1924 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
1925 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001926 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1927 ssl->in_msglen );
1928 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1929 ssl->in_msg + ssl->in_msglen );
1930 for( j = 0; j < extra_run; j++ )
1931 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001932
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001933 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1934 }
1935 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001936#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001937 POLARSSL_SSL_PROTO_TLS1_2 */
1938 {
1939 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001940 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001941 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001942
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001943 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1944 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1945 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001946
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001947 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001948 ssl->transform_in->maclen ) != 0 )
1949 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001950#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001951 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001952#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001953 correct = 0;
1954 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001955 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001956
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001957 /*
1958 * Finally check the correct flag
1959 */
1960 if( correct == 0 )
1961 return( POLARSSL_ERR_SSL_INVALID_MAC );
1962 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001963#endif /* POLARSSL_SOME_MODES_USE_MAC */
1964
1965 /* Make extra sure authentication was performed, exactly once */
1966 if( auth_done != 1 )
1967 {
1968 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1969 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1970 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001971
1972 if( ssl->in_msglen == 0 )
1973 {
1974 ssl->nb_zero++;
1975
1976 /*
1977 * Three or more empty messages may be a DoS attack
1978 * (excessive CPU consumption).
1979 */
1980 if( ssl->nb_zero > 3 )
1981 {
1982 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1983 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001984 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001985 }
1986 }
1987 else
1988 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001989
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001990#if defined(POLARSSL_SSL_PROTO_DTLS)
1991 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001992 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02001993 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001994 }
1995 else
1996#endif
1997 {
1998 for( i = 8; i > ssl_ep_len( ssl ); i-- )
1999 if( ++ssl->in_ctr[i - 1] != 0 )
2000 break;
2001
2002 /* The loop goes to its end iff the counter is wrapping */
2003 if( i == ssl_ep_len( ssl ) )
2004 {
2005 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2006 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
2007 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002008 }
2009
Paul Bakker5121ce52009-01-03 21:22:43 +00002010 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
2011
2012 return( 0 );
2013}
2014
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002015#undef MAC_NONE
2016#undef MAC_PLAINTEXT
2017#undef MAC_CIPHERTEXT
2018
Paul Bakker2770fbd2012-07-03 13:30:23 +00002019#if defined(POLARSSL_ZLIB_SUPPORT)
2020/*
2021 * Compression/decompression functions
2022 */
2023static int ssl_compress_buf( ssl_context *ssl )
2024{
2025 int ret;
2026 unsigned char *msg_post = ssl->out_msg;
2027 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002028 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002029
2030 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
2031
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002032 if( len_pre == 0 )
2033 return( 0 );
2034
Paul Bakker2770fbd2012-07-03 13:30:23 +00002035 memcpy( msg_pre, ssl->out_msg, len_pre );
2036
2037 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
2038 ssl->out_msglen ) );
2039
2040 SSL_DEBUG_BUF( 4, "before compression: output payload",
2041 ssl->out_msg, ssl->out_msglen );
2042
Paul Bakker48916f92012-09-16 19:57:18 +00002043 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2044 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2045 ssl->transform_out->ctx_deflate.next_out = msg_post;
2046 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002047
Paul Bakker48916f92012-09-16 19:57:18 +00002048 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002049 if( ret != Z_OK )
2050 {
2051 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2052 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
2053 }
2054
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002055 ssl->out_msglen = SSL_BUFFER_LEN -
2056 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002057
Paul Bakker2770fbd2012-07-03 13:30:23 +00002058 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
2059 ssl->out_msglen ) );
2060
2061 SSL_DEBUG_BUF( 4, "after compression: output payload",
2062 ssl->out_msg, ssl->out_msglen );
2063
2064 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
2065
2066 return( 0 );
2067}
2068
2069static int ssl_decompress_buf( ssl_context *ssl )
2070{
2071 int ret;
2072 unsigned char *msg_post = ssl->in_msg;
2073 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002074 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002075
2076 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
2077
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002078 if( len_pre == 0 )
2079 return( 0 );
2080
Paul Bakker2770fbd2012-07-03 13:30:23 +00002081 memcpy( msg_pre, ssl->in_msg, len_pre );
2082
2083 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
2084 ssl->in_msglen ) );
2085
2086 SSL_DEBUG_BUF( 4, "before decompression: input payload",
2087 ssl->in_msg, ssl->in_msglen );
2088
Paul Bakker48916f92012-09-16 19:57:18 +00002089 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2090 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2091 ssl->transform_in->ctx_inflate.next_out = msg_post;
2092 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002093
Paul Bakker48916f92012-09-16 19:57:18 +00002094 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002095 if( ret != Z_OK )
2096 {
2097 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2098 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
2099 }
2100
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002101 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
2102 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002103
Paul Bakker2770fbd2012-07-03 13:30:23 +00002104 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
2105 ssl->in_msglen ) );
2106
2107 SSL_DEBUG_BUF( 4, "after decompression: input payload",
2108 ssl->in_msg, ssl->in_msglen );
2109
2110 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
2111
2112 return( 0 );
2113}
2114#endif /* POLARSSL_ZLIB_SUPPORT */
2115
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002116#if defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002117static int ssl_write_hello_request( ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002118
2119#if defined(POLARSSL_SSL_PROTO_DTLS)
2120static int ssl_resend_hello_request( ssl_context *ssl )
2121{
2122 /* If renegotiation is not enforced, retransmit until we would reach max
2123 * timeout if we were using the usual handshake doubling scheme */
2124 if( ssl->renego_max_records < 0 )
2125 {
2126 uint32_t ratio = ssl->hs_timeout_max / ssl->hs_timeout_min + 1;
2127 unsigned char doublings = 1;
2128
2129 while( ratio != 0 )
2130 {
2131 ++doublings;
2132 ratio >>= 1;
2133 }
2134
2135 if( ++ssl->renego_records_seen > doublings )
2136 {
2137 SSL_DEBUG_MSG( 0, ( "no longer retransmitting hello request" ) );
2138 return( 0 );
2139 }
2140 }
2141
2142 return( ssl_write_hello_request( ssl ) );
2143}
2144#endif
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002145#endif /* POLARSSL_SSL_SRV_C && POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002146
Paul Bakker5121ce52009-01-03 21:22:43 +00002147/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002148 * Fill the input message buffer by appending data to it.
2149 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002150 *
2151 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2152 * available (from this read and/or a previous one). Otherwise, an error code
2153 * is returned (possibly EOF or WANT_READ).
2154 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002155 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2156 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2157 * since we always read a whole datagram at once.
2158 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002159 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002160 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002161 */
Paul Bakker23986e52011-04-24 08:57:21 +00002162int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002163{
Paul Bakker23986e52011-04-24 08:57:21 +00002164 int ret;
2165 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002166
2167 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
2168
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002169 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2170 {
2171 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2172 "or ssl_set_bio_timeout()" ) );
2173 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2174 }
2175
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002176 if( nb_want > SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002177 {
2178 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2179 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2180 }
2181
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002182#if defined(POLARSSL_SSL_PROTO_DTLS)
2183 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002184 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002185 uint32_t timeout;
2186
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002187 /*
2188 * The point is, we need to always read a full datagram at once, so we
2189 * sometimes read more then requested, and handle the additional data.
2190 * It could be the rest of the current record (while fetching the
2191 * header) and/or some other records in the same datagram.
2192 */
2193
2194 /*
2195 * Move to the next record in the already read datagram if applicable
2196 */
2197 if( ssl->next_record_offset != 0 )
2198 {
2199 if( ssl->in_left < ssl->next_record_offset )
2200 {
2201 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2202 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2203 }
2204
2205 ssl->in_left -= ssl->next_record_offset;
2206
2207 if( ssl->in_left != 0 )
2208 {
2209 SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
2210 ssl->next_record_offset ) );
2211 memmove( ssl->in_hdr,
2212 ssl->in_hdr + ssl->next_record_offset,
2213 ssl->in_left );
2214 }
2215
2216 ssl->next_record_offset = 0;
2217 }
2218
Paul Bakker5121ce52009-01-03 21:22:43 +00002219 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2220 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002221
2222 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002223 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002224 */
2225 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002226 {
2227 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002228 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002229 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002230
2231 /*
2232 * A record can't be split accross datagrams. If we need to read but
2233 * are not at the beginning of a new record, the caller did something
2234 * wrong.
2235 */
2236 if( ssl->in_left != 0 )
2237 {
2238 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2239 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2240 }
2241
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002242 SSL_DEBUG_MSG( 3, ( "current timer: %u", ssl->time_limit ) );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002243
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002244 /*
2245 * Don't even try to read if time's out already.
2246 * This avoids by-passing the timer when repeatedly receiving messages
2247 * that will end up being dropped.
2248 */
2249 if( ssl_check_timer( ssl ) != 0 )
2250 ret = POLARSSL_ERR_NET_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002251 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002252 {
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002253 len = SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
2254
2255 if( ssl->state != SSL_HANDSHAKE_OVER )
2256 timeout = ssl->handshake->retransmit_timeout;
2257 else
2258 timeout = ssl->read_timeout;
2259
2260 SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
2261
2262 if( ssl->f_recv_timeout != NULL && timeout != 0 )
2263 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2264 timeout );
2265 else
2266 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2267
2268 SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
2269
2270 if( ret == 0 )
2271 return( POLARSSL_ERR_SSL_CONN_EOF );
2272 }
2273
2274 if( ret == POLARSSL_ERR_NET_TIMEOUT )
2275 {
2276 SSL_DEBUG_MSG( 2, ( "timeout" ) );
2277 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002278
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002279 if( ssl->state != SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002280 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002281 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2282 {
2283 SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
2284 return( POLARSSL_ERR_NET_TIMEOUT );
2285 }
2286
2287 if( ( ret = ssl_resend( ssl ) ) != 0 )
2288 {
2289 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2290 return( ret );
2291 }
2292
2293 return( POLARSSL_ERR_NET_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002294 }
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002295#if defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002296 else if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00002297 ssl->renego_status == SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002298 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002299 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002300 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002301 SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002302 return( ret );
2303 }
2304
2305 return( POLARSSL_ERR_NET_WANT_READ );
2306 }
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002307#endif /* POLARSSL_SSL_SRV_C && POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002308 }
2309
Paul Bakker5121ce52009-01-03 21:22:43 +00002310 if( ret < 0 )
2311 return( ret );
2312
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002313 ssl->in_left = ret;
2314 }
2315 else
2316#endif
2317 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002318 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2319 ssl->in_left, nb_want ) );
2320
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002321 while( ssl->in_left < nb_want )
2322 {
2323 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002324 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr + ssl->in_left, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002325
2326 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2327 ssl->in_left, nb_want ) );
2328 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2329
2330 if( ret == 0 )
2331 return( POLARSSL_ERR_SSL_CONN_EOF );
2332
2333 if( ret < 0 )
2334 return( ret );
2335
2336 ssl->in_left += ret;
2337 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002338 }
2339
2340 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2341
2342 return( 0 );
2343}
2344
2345/*
2346 * Flush any data not yet written
2347 */
2348int ssl_flush_output( ssl_context *ssl )
2349{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002350 int ret;
2351 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002352
2353 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2354
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002355 if( ssl->f_send == NULL )
2356 {
2357 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2358 "or ssl_set_bio_timeout()" ) );
2359 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2360 }
2361
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002362 /* Avoid incrementing counter if data is flushed */
2363 if( ssl->out_left == 0 )
2364 {
2365 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2366 return( 0 );
2367 }
2368
Paul Bakker5121ce52009-01-03 21:22:43 +00002369 while( ssl->out_left > 0 )
2370 {
2371 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002372 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002373
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002374 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
2375 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002376 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002377
Paul Bakker5121ce52009-01-03 21:22:43 +00002378 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2379
2380 if( ret <= 0 )
2381 return( ret );
2382
2383 ssl->out_left -= ret;
2384 }
2385
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002386 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002387 if( ++ssl->out_ctr[i - 1] != 0 )
2388 break;
2389
2390 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002391 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002392 {
2393 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2394 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
2395 }
2396
Paul Bakker5121ce52009-01-03 21:22:43 +00002397 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2398
2399 return( 0 );
2400}
2401
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002402/*
2403 * Functions to handle the DTLS retransmission state machine
2404 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002405#if defined(POLARSSL_SSL_PROTO_DTLS)
2406/*
2407 * Append current handshake message to current outgoing flight
2408 */
2409static int ssl_flight_append( ssl_context *ssl )
2410{
2411 ssl_flight_item *msg;
2412
2413 /* Allocate space for current message */
2414 if( ( msg = polarssl_malloc( sizeof( ssl_flight_item ) ) ) == NULL )
2415 {
2416 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed",
2417 sizeof( ssl_flight_item ) ) );
2418 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2419 }
2420
2421 if( ( msg->p = polarssl_malloc( ssl->out_msglen ) ) == NULL )
2422 {
2423 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard6b875fc2014-10-17 14:02:33 +02002424 polarssl_free( msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002425 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2426 }
2427
2428 /* Copy current handshake message with headers */
2429 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2430 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002431 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002432 msg->next = NULL;
2433
2434 /* Append to the current flight */
2435 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002436 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002437 else
2438 {
2439 ssl_flight_item *cur = ssl->handshake->flight;
2440 while( cur->next != NULL )
2441 cur = cur->next;
2442 cur->next = msg;
2443 }
2444
2445 return( 0 );
2446}
2447
2448/*
2449 * Free the current flight of handshake messages
2450 */
2451static void ssl_flight_free( ssl_flight_item *flight )
2452{
2453 ssl_flight_item *cur = flight;
2454 ssl_flight_item *next;
2455
2456 while( cur != NULL )
2457 {
2458 next = cur->next;
2459
2460 polarssl_free( cur->p );
2461 polarssl_free( cur );
2462
2463 cur = next;
2464 }
2465}
2466
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002467#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2468static void ssl_dtls_replay_reset( ssl_context *ssl );
2469#endif
2470
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002471/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002472 * Swap transform_out and out_ctr with the alternative ones
2473 */
2474static void ssl_swap_epochs( ssl_context *ssl )
2475{
2476 ssl_transform *tmp_transform;
2477 unsigned char tmp_out_ctr[8];
2478
2479 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2480 {
2481 SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
2482 return;
2483 }
2484
2485 SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
2486
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002487 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002488 tmp_transform = ssl->transform_out;
2489 ssl->transform_out = ssl->handshake->alt_transform_out;
2490 ssl->handshake->alt_transform_out = tmp_transform;
2491
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002492 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002493 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2494 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2495 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002496
2497 /* Adjust to the newly activated transform */
2498 if( ssl->transform_out != NULL &&
2499 ssl->minor_ver >= SSL_MINOR_VERSION_2 )
2500 {
2501 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2502 ssl->transform_out->fixed_ivlen;
2503 }
2504 else
2505 ssl->out_msg = ssl->out_iv;
2506
2507#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2508 if( ssl_hw_record_activate != NULL )
2509 {
2510 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
2511 {
2512 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
2513 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
2514 }
2515 }
2516#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002517}
2518
2519/*
2520 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002521 *
2522 * Need to remember the current message in case flush_output returns
2523 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002524 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002525 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002526int ssl_resend( ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002527{
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002528 SSL_DEBUG_MSG( 2, ( "=> ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002529
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002530 if( ssl->handshake->retransmit_state != SSL_RETRANS_SENDING )
2531 {
2532 SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
2533
2534 ssl->handshake->cur_msg = ssl->handshake->flight;
2535 ssl_swap_epochs( ssl );
2536
2537 ssl->handshake->retransmit_state = SSL_RETRANS_SENDING;
2538 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002539
2540 while( ssl->handshake->cur_msg != NULL )
2541 {
2542 int ret;
2543 ssl_flight_item *cur = ssl->handshake->cur_msg;
2544
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002545 /* Swap epochs before sending Finished: we can't do it after
2546 * sending ChangeCipherSpec, in case write returns WANT_READ.
2547 * Must be done before copying, may change out_msg pointer */
2548 if( cur->type == SSL_MSG_HANDSHAKE &&
2549 cur->p[0] == SSL_HS_FINISHED )
2550 {
2551 ssl_swap_epochs( ssl );
2552 }
2553
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002554 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002555 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002556 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002557
2558 ssl->handshake->cur_msg = cur->next;
2559
2560 SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
2561
2562 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2563 {
2564 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2565 return( ret );
2566 }
2567 }
2568
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002569 if( ssl->state == SSL_HANDSHAKE_OVER )
2570 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2571 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002572 {
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002573 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002574 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2575 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002576
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002577 SSL_DEBUG_MSG( 2, ( "<= ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002578
2579 return( 0 );
2580}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002581
2582/*
2583 * To be called when the last message of an incoming flight is received.
2584 */
2585void ssl_recv_flight_completed( ssl_context *ssl )
2586{
2587 /* We won't need to resend that one any more */
2588 ssl_flight_free( ssl->handshake->flight );
2589 ssl->handshake->flight = NULL;
2590 ssl->handshake->cur_msg = NULL;
2591
2592 /* The next incoming flight will start with this msg_seq */
2593 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2594
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002595 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002596 ssl_set_timer( ssl, 0 );
2597
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002598 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2599 ssl->in_msg[0] == SSL_HS_FINISHED )
2600 {
2601 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2602 }
2603 else
2604 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
2605}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002606
2607/*
2608 * To be called when the last message of an outgoing flight is send.
2609 */
2610void ssl_send_flight_completed( ssl_context *ssl )
2611{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002612 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002613 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002614
2615 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2616 ssl->in_msg[0] == SSL_HS_FINISHED )
2617 {
2618 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2619 }
2620 else
2621 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
2622}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002623#endif /* POLARSSL_SSL_PROTO_DTLS */
2624
Paul Bakker5121ce52009-01-03 21:22:43 +00002625/*
2626 * Record layer functions
2627 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002628
2629/*
2630 * Write current record.
2631 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2632 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002633int ssl_write_record( ssl_context *ssl )
2634{
Paul Bakker05ef8352012-05-08 09:17:57 +00002635 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002636 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002637
2638 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2639
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002640#if defined(POLARSSL_SSL_PROTO_DTLS)
2641 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2642 ssl->handshake != NULL &&
2643 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2644 {
2645 ; /* Skip special handshake treatment when resending */
2646 }
2647 else
2648#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002649 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2650 {
2651 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2652 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2653 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2654
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002655 /*
2656 * DTLS has additional fields in the Handshake layer,
2657 * between the length field and the actual payload:
2658 * uint16 message_seq;
2659 * uint24 fragment_offset;
2660 * uint24 fragment_length;
2661 */
2662#if defined(POLARSSL_SSL_PROTO_DTLS)
2663 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2664 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002665 /* Make room for the additional DTLS fields */
2666 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002667 ssl->out_msglen += 8;
2668 len += 8;
2669
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002670 /* Write message_seq and update it, except for HelloRequest */
2671 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2672 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002673 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2674 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2675 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002676 }
2677 else
2678 {
2679 ssl->out_msg[4] = 0;
2680 ssl->out_msg[5] = 0;
2681 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002682
2683 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2684 memset( ssl->out_msg + 6, 0x00, 3 );
2685 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002686 }
2687#endif /* POLARSSL_SSL_PROTO_DTLS */
2688
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002689 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2690 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 }
2692
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002693 /* Save handshake and CCS messages for resending */
2694#if defined(POLARSSL_SSL_PROTO_DTLS)
2695 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2696 ssl->handshake != NULL &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02002697 ssl->handshake->retransmit_state != SSL_RETRANS_SENDING &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002698 ( ssl->out_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC ||
2699 ssl->out_msgtype == SSL_MSG_HANDSHAKE ) )
2700 {
2701 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2702 {
2703 SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
2704 return( ret );
2705 }
2706 }
2707#endif
2708
Paul Bakker2770fbd2012-07-03 13:30:23 +00002709#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002710 if( ssl->transform_out != NULL &&
2711 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002712 {
2713 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2714 {
2715 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2716 return( ret );
2717 }
2718
2719 len = ssl->out_msglen;
2720 }
2721#endif /*POLARSSL_ZLIB_SUPPORT */
2722
Paul Bakker05ef8352012-05-08 09:17:57 +00002723#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002724 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002725 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002726 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002727
Paul Bakker05ef8352012-05-08 09:17:57 +00002728 ret = ssl_hw_record_write( ssl );
2729 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2730 {
2731 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002732 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002733 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002734
2735 if( ret == 0 )
2736 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002737 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002738#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002739 if( !done )
2740 {
2741 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002742 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2743 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002744
2745 ssl->out_len[0] = (unsigned char)( len >> 8 );
2746 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002747
Paul Bakker48916f92012-09-16 19:57:18 +00002748 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002749 {
2750 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2751 {
2752 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2753 return( ret );
2754 }
2755
2756 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002757 ssl->out_len[0] = (unsigned char)( len >> 8 );
2758 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002759 }
2760
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002761 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002762
2763 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2764 "version = [%d:%d], msglen = %d",
2765 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002766 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002767
2768 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002769 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002770 }
2771
Paul Bakker5121ce52009-01-03 21:22:43 +00002772 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2773 {
2774 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2775 return( ret );
2776 }
2777
2778 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2779
2780 return( 0 );
2781}
2782
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002783#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002784/*
2785 * Mark bits in bitmask (used for DTLS HS reassembly)
2786 */
2787static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2788{
2789 unsigned int start_bits, end_bits;
2790
2791 start_bits = 8 - ( offset % 8 );
2792 if( start_bits != 8 )
2793 {
2794 size_t first_byte_idx = offset / 8;
2795
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002796 /* Special case */
2797 if( len <= start_bits )
2798 {
2799 for( ; len != 0; len-- )
2800 mask[first_byte_idx] |= 1 << ( start_bits - len );
2801
2802 /* Avoid potential issues with offset or len becoming invalid */
2803 return;
2804 }
2805
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002806 offset += start_bits; /* Now offset % 8 == 0 */
2807 len -= start_bits;
2808
2809 for( ; start_bits != 0; start_bits-- )
2810 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2811 }
2812
2813 end_bits = len % 8;
2814 if( end_bits != 0 )
2815 {
2816 size_t last_byte_idx = ( offset + len ) / 8;
2817
2818 len -= end_bits; /* Now len % 8 == 0 */
2819
2820 for( ; end_bits != 0; end_bits-- )
2821 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2822 }
2823
2824 memset( mask + offset / 8, 0xFF, len / 8 );
2825}
2826
2827/*
2828 * Check that bitmask is full
2829 */
2830static int ssl_bitmask_check( unsigned char *mask, size_t len )
2831{
2832 size_t i;
2833
2834 for( i = 0; i < len / 8; i++ )
2835 if( mask[i] != 0xFF )
2836 return( -1 );
2837
2838 for( i = 0; i < len % 8; i++ )
2839 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2840 return( -1 );
2841
2842 return( 0 );
2843}
2844
2845/*
2846 * Reassemble fragmented DTLS handshake messages.
2847 *
2848 * Use a temporary buffer for reassembly, divided in two parts:
2849 * - the first holds the reassembled message (including handshake header),
2850 * - the second holds a bitmask indicating which parts of the message
2851 * (excluding headers) have been received so far.
2852 */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002853static int ssl_reassemble_dtls_handshake( ssl_context *ssl )
2854{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002855 unsigned char *msg, *bitmask;
2856 size_t frag_len, frag_off;
2857 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
2858
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002859 if( ssl->handshake == NULL )
2860 {
2861 SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
2862 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2863 }
2864
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002865 /*
2866 * For first fragment, check size and allocate buffer
2867 */
2868 if( ssl->handshake->hs_msg == NULL )
2869 {
2870 size_t alloc_len;
2871
2872 SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
2873 msg_len ) );
2874
2875 if( ssl->in_hslen > SSL_MAX_CONTENT_LEN )
2876 {
2877 SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
2878 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2879 }
2880
2881 /* The bitmask needs one bit per byte of message excluding header */
2882 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
2883
2884 ssl->handshake->hs_msg = polarssl_malloc( alloc_len );
2885 if( ssl->handshake->hs_msg == NULL )
2886 {
2887 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", alloc_len ) );
2888 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2889 }
2890
2891 memset( ssl->handshake->hs_msg, 0, alloc_len );
2892
2893 /* Prepare final header: copy msg_type, length and message_seq,
2894 * then add standardised fragment_offset and fragment_length */
2895 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
2896 memset( ssl->handshake->hs_msg + 6, 0, 3 );
2897 memcpy( ssl->handshake->hs_msg + 9,
2898 ssl->handshake->hs_msg + 1, 3 );
2899 }
2900 else
2901 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002902 /* Make sure msg_type and length are consistent */
2903 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002904 {
2905 SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
2906 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2907 }
2908 }
2909
2910 msg = ssl->handshake->hs_msg + 12;
2911 bitmask = msg + msg_len;
2912
2913 /*
2914 * Check and copy current fragment
2915 */
2916 frag_off = ( ssl->in_msg[6] << 16 ) |
2917 ( ssl->in_msg[7] << 8 ) |
2918 ssl->in_msg[8];
2919 frag_len = ( ssl->in_msg[9] << 16 ) |
2920 ( ssl->in_msg[10] << 8 ) |
2921 ssl->in_msg[11];
2922
2923 if( frag_off + frag_len > msg_len )
2924 {
2925 SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
2926 frag_off, frag_len, msg_len ) );
2927 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2928 }
2929
2930 if( frag_len + 12 > ssl->in_msglen )
2931 {
2932 SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
2933 frag_len, ssl->in_msglen ) );
2934 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2935 }
2936
2937 SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
2938 frag_off, frag_len ) );
2939
2940 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
2941 ssl_bitmask_set( bitmask, frag_off, frag_len );
2942
2943 /*
2944 * Do we have the complete message by now?
2945 * If yes, finalize it, else ask to read the next record.
2946 */
2947 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
2948 {
2949 SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
2950 return( POLARSSL_ERR_NET_WANT_READ );
2951 }
2952
2953 SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
2954
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02002955 if( frag_len + 12 < ssl->in_msglen )
2956 {
2957 /*
2958 * We'got more handshake messages in the same record.
2959 * This case is not handled now because no know implementation does
2960 * that and it's hard to test, so we prefer to fail cleanly for now.
2961 */
2962 SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
2963 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2964 }
2965
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002966 if( ssl->in_left > ssl->next_record_offset )
2967 {
2968 /*
2969 * We've got more data in the buffer after the current record,
2970 * that we don't want to overwrite. Move it before writing the
2971 * reassembled message, and adjust in_left and next_record_offset.
2972 */
2973 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
2974 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
2975 size_t remain_len = ssl->in_left - ssl->next_record_offset;
2976
2977 /* First compute and check new lengths */
2978 ssl->next_record_offset = new_remain - ssl->in_hdr;
2979 ssl->in_left = ssl->next_record_offset + remain_len;
2980
2981 if( ssl->in_left > SSL_BUFFER_LEN -
2982 (size_t)( ssl->in_hdr - ssl->in_buf ) )
2983 {
2984 SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
2985 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
2986 }
2987
2988 memmove( new_remain, cur_remain, remain_len );
2989 }
2990
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002991 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
2992
2993 polarssl_free( ssl->handshake->hs_msg );
2994 ssl->handshake->hs_msg = NULL;
2995
2996 SSL_DEBUG_BUF( 3, "reassembled handshake message",
2997 ssl->in_msg, ssl->in_hslen );
2998
2999 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003000}
3001#endif /* POLARSSL_SSL_PROTO_DTLS */
3002
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003003static int ssl_prepare_handshake_record( ssl_context *ssl )
3004{
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003005 if( ssl->in_msglen < ssl_hs_hdr_len( ssl ) )
3006 {
3007 SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
3008 ssl->in_msglen ) );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003009 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003010 }
3011
3012 ssl->in_hslen = ssl_hs_hdr_len( ssl ) + (
3013 ( ssl->in_msg[1] << 16 ) |
3014 ( ssl->in_msg[2] << 8 ) |
3015 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003016
3017 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
3018 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003019 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003020
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003021#if defined(POLARSSL_SSL_PROTO_DTLS)
3022 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003023 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003024 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003025 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003026
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003027 /* ssl->handshake is NULL when receiving ClientHello for renego */
3028 if( ssl->handshake != NULL &&
3029 recv_msg_seq != ssl->handshake->in_msg_seq )
3030 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003031 /* Retransmit only on last message from previous flight, to avoid
3032 * too many retransmissions.
3033 * Besides, No sane server ever retransmits HelloVerifyRequest */
3034 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard93017de2014-09-19 22:42:40 +02003035 ssl->in_msg[0] != SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003036 {
3037 SSL_DEBUG_MSG( 2, ( "received message from last flight, "
3038 "message_seq = %d, start_of_flight = %d",
3039 recv_msg_seq,
3040 ssl->handshake->in_flight_start_seq ) );
3041
3042 if( ( ret = ssl_resend( ssl ) ) != 0 )
3043 {
3044 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3045 return( ret );
3046 }
3047 }
3048 else
3049 {
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02003050 SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003051 "message_seq = %d, expected = %d",
3052 recv_msg_seq,
3053 ssl->handshake->in_msg_seq ) );
3054 }
3055
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003056 return( POLARSSL_ERR_NET_WANT_READ );
3057 }
3058 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003059
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003060 /* Reassemble if current message is fragmented or reassembly is
3061 * already in progress */
3062 if( ssl->in_msglen < ssl->in_hslen ||
3063 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3064 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3065 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003066 {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003067 SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
3068
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003069 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3070 {
3071 SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
3072 return( ret );
3073 }
3074 }
3075 }
3076 else
3077#endif /* POLARSSL_SSL_PROTO_DTLS */
3078 /* With TLS we don't handle fragmentation (for now) */
3079 if( ssl->in_msglen < ssl->in_hslen )
3080 {
3081 SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02003082 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003083 }
3084
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003085 if( ssl->state != SSL_HANDSHAKE_OVER )
3086 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
3087
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003088 /* Handshake message is complete, increment counter */
3089#if defined(POLARSSL_SSL_PROTO_DTLS)
3090 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3091 ssl->handshake != NULL )
3092 {
3093 ssl->handshake->in_msg_seq++;
3094 }
3095#endif
3096
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003097 return( 0 );
3098}
3099
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003100/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003101 * DTLS anti-replay: RFC 6347 4.1.2.6
3102 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003103 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3104 * Bit n is set iff record number in_window_top - n has been seen.
3105 *
3106 * Usually, in_window_top is the last record number seen and the lsb of
3107 * in_window is set. The only exception is the initial state (record number 0
3108 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003109 */
3110#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3111static void ssl_dtls_replay_reset( ssl_context *ssl )
3112{
3113 ssl->in_window_top = 0;
3114 ssl->in_window = 0;
3115}
3116
3117static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3118{
3119 return( ( (uint64_t) buf[0] << 40 ) |
3120 ( (uint64_t) buf[1] << 32 ) |
3121 ( (uint64_t) buf[2] << 24 ) |
3122 ( (uint64_t) buf[3] << 16 ) |
3123 ( (uint64_t) buf[4] << 8 ) |
3124 ( (uint64_t) buf[5] ) );
3125}
3126
3127/*
3128 * Return 0 if sequence number is acceptable, -1 otherwise
3129 */
3130int ssl_dtls_replay_check( ssl_context *ssl )
3131{
3132 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3133 uint64_t bit;
3134
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003135 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
3136 return( 0 );
3137
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003138 if( rec_seqnum > ssl->in_window_top )
3139 return( 0 );
3140
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003141 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003142
3143 if( bit >= 64 )
3144 return( -1 );
3145
3146 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3147 return( -1 );
3148
3149 return( 0 );
3150}
3151
3152/*
3153 * Update replay window on new validated record
3154 */
3155void ssl_dtls_replay_update( ssl_context *ssl )
3156{
3157 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3158
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003159 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
3160 return;
3161
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003162 if( rec_seqnum > ssl->in_window_top )
3163 {
3164 /* Update window_top and the contents of the window */
3165 uint64_t shift = rec_seqnum - ssl->in_window_top;
3166
3167 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003168 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003169 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003170 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003171 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003172 ssl->in_window |= 1;
3173 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003174
3175 ssl->in_window_top = rec_seqnum;
3176 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003177 else
3178 {
3179 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003180 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003181
3182 if( bit < 64 ) /* Always true, but be extra sure */
3183 ssl->in_window |= (uint64_t) 1 << bit;
3184 }
3185}
3186#endif /* POLARSSL_SSL_DTLS_ANTI_REPLAY */
3187
3188/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003189 * ContentType type;
3190 * ProtocolVersion version;
3191 * uint16 epoch; // DTLS only
3192 * uint48 sequence_number; // DTLS only
3193 * uint16 length;
3194 */
3195static int ssl_parse_record_header( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003196{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003197 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003198 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003199
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003200 SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, ssl_hdr_len( ssl ) );
3201
Paul Bakker5121ce52009-01-03 21:22:43 +00003202 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003203 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003204 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003205
3206 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
3207 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003208 ssl->in_msgtype,
3209 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003210
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003211 /* Check record type */
3212 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
3213 ssl->in_msgtype != SSL_MSG_ALERT &&
3214 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
3215 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
3216 {
3217 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003218
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003219 if( ( ret = ssl_send_alert_message( ssl,
3220 SSL_ALERT_LEVEL_FATAL,
3221 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
3222 {
3223 return( ret );
3224 }
3225
3226 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3227 }
3228
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003229#if defined(POLARSSL_SSL_PROTO_DTLS)
3230 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3231 {
3232 /* Drop unexpected ChangeCipherSpec messages */
3233 if( ssl->in_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC &&
3234 ssl->state != SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3235 ssl->state != SSL_SERVER_CHANGE_CIPHER_SPEC )
3236 {
3237 SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3238 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3239 }
3240
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02003241 /* Drop unexpected ApplicationData records,
3242 * except at the beginning of renegotiations */
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003243 if( ssl->in_msgtype == SSL_MSG_APPLICATION_DATA &&
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00003244 ssl->state != SSL_HANDSHAKE_OVER
3245#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00003246 && ! ( ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS &&
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00003247 ssl->state == SSL_SERVER_HELLO )
3248#endif
3249 )
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003250 {
3251 SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
3252 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3253 }
3254 }
3255#endif
3256
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003257 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003258 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003259 {
3260 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003261 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003262 }
3263
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003264 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003265 {
3266 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003267 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003268 }
3269
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003270 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003271#if defined(POLARSSL_SSL_PROTO_DTLS)
3272 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3273 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003274 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003275
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003276 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003277 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003278 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003279 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003280 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003281 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003282 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003283
3284#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3285 if( ssl_dtls_replay_check( ssl ) != 0 )
3286 {
3287 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3288 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3289 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003290#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003291 }
3292#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003293
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003294 /* Check length against the size of our buffer */
3295 if( ssl->in_msglen > SSL_BUFFER_LEN
3296 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003297 {
3298 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3299 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3300 }
3301
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003302 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003303 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003304 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003305 if( ssl->in_msglen < 1 ||
3306 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003307 {
3308 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003309 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003310 }
3311 }
3312 else
3313 {
Paul Bakker48916f92012-09-16 19:57:18 +00003314 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003315 {
3316 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003317 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003318 }
3319
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003320#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003321 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00003322 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003323 {
3324 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003325 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003326 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003327#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003328#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3329 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003330 /*
3331 * TLS encrypted messages can have up to 256 bytes of padding
3332 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003333 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003334 ssl->in_msglen > ssl->transform_in->minlen +
3335 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003336 {
3337 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003338 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003339 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003340#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003341 }
3342
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003343 return( 0 );
3344}
Paul Bakker5121ce52009-01-03 21:22:43 +00003345
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003346/*
3347 * If applicable, decrypt (and decompress) record content
3348 */
3349static int ssl_prepare_record_content( ssl_context *ssl )
3350{
3351 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003352
Paul Bakker5121ce52009-01-03 21:22:43 +00003353 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003354 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003355
Paul Bakker05ef8352012-05-08 09:17:57 +00003356#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003357 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003358 {
3359 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
3360
3361 ret = ssl_hw_record_read( ssl );
3362 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
3363 {
3364 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003365 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003366 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003367
3368 if( ret == 0 )
3369 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003370 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003371#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003372 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003373 {
3374 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3375 {
3376 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
3377 return( ret );
3378 }
3379
3380 SSL_DEBUG_BUF( 4, "input payload after decrypt",
3381 ssl->in_msg, ssl->in_msglen );
3382
3383 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
3384 {
3385 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003386 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003387 }
3388 }
3389
Paul Bakker2770fbd2012-07-03 13:30:23 +00003390#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003391 if( ssl->transform_in != NULL &&
3392 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003393 {
3394 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3395 {
3396 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
3397 return( ret );
3398 }
3399
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003400 // TODO: what's the purpose of these lines? is in_len used?
3401 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3402 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003403 }
3404#endif /* POLARSSL_ZLIB_SUPPORT */
3405
Manuel Pégourié-Gonnard8464a462014-09-24 14:05:32 +02003406#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003407 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3408 {
3409 ssl_dtls_replay_update( ssl );
3410 }
3411#endif
3412
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003413 return( 0 );
3414}
3415
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003416static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
3417
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003418/*
3419 * Read a record.
3420 *
3421 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
3422 * and continue reading until a valid record is found.
3423 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003424int ssl_read_record( ssl_context *ssl )
3425{
3426 int ret;
3427
3428 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
3429
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02003430 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003431 {
3432 /*
3433 * Get next Handshake message in the current record
3434 */
3435 ssl->in_msglen -= ssl->in_hslen;
3436
3437 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3438 ssl->in_msglen );
3439
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003440 SSL_DEBUG_BUF( 4, "remaining content in record",
3441 ssl->in_msg, ssl->in_msglen );
3442
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003443 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3444 return( ret );
3445
3446 return( 0 );
3447 }
3448
3449 ssl->in_hslen = 0;
3450
3451 /*
3452 * Read the record header and parse it
3453 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003454#if defined(POLARSSL_SSL_PROTO_DTLS)
3455read_record_header:
3456#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003457 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3458 {
3459 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3460 return( ret );
3461 }
3462
3463 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003464 {
3465#if defined(POLARSSL_SSL_PROTO_DTLS)
3466 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3467 {
3468 /* Ignore bad record and get next one; drop the whole datagram
3469 * since current header cannot be trusted to find the next record
3470 * in current datagram */
3471 ssl->next_record_offset = 0;
3472 ssl->in_left = 0;
3473
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003474 SSL_DEBUG_MSG( 1, ( "discarding invalid record (header)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003475 goto read_record_header;
3476 }
3477#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003478 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003479 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003480
3481 /*
3482 * Read and optionally decrypt the message contents
3483 */
3484 if( ( ret = ssl_fetch_input( ssl,
3485 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3486 {
3487 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3488 return( ret );
3489 }
3490
3491 /* Done reading this record, get ready for the next one */
3492#if defined(POLARSSL_SSL_PROTO_DTLS)
3493 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3494 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3495 else
3496#endif
3497 ssl->in_left = 0;
3498
3499 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003500 {
3501#if defined(POLARSSL_SSL_PROTO_DTLS)
3502 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3503 {
3504 /* Silently discard invalid records */
3505 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3506 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3507 {
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02003508#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
3509 if( ssl->badmac_limit != 0 &&
3510 ++ssl->badmac_seen >= ssl->badmac_limit )
3511 {
3512 SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
3513 return( POLARSSL_ERR_SSL_INVALID_MAC );
3514 }
3515#endif
3516
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003517 SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003518 goto read_record_header;
3519 }
3520
3521 return( ret );
3522 }
3523 else
3524#endif
3525 {
3526 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard9b669902015-03-06 16:52:46 +00003527#if defined(POLARSSL_SSL_ALL_ALERT_MESSAGES)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003528 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3529 {
3530 ssl_send_alert_message( ssl,
3531 SSL_ALERT_LEVEL_FATAL,
3532 SSL_ALERT_MSG_BAD_RECORD_MAC );
3533 }
3534#endif
3535 return( ret );
3536 }
3537 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003538
3539 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003540 * When we sent the last flight of the handshake, we MUST respond to a
3541 * retransmit of the peer's previous flight with a retransmit. (In
3542 * practice, only the Finished message will make it, other messages
3543 * including CCS use the old transform so they're dropped as invalid.)
3544 *
3545 * If the record we received is not a handshake message, however, it
3546 * means the peer received our last flight so we can clean up
3547 * handshake info.
3548 *
3549 * This check needs to be done before prepare_handshake() due to an edge
3550 * case: if the client immediately requests renegotiation, this
3551 * finishes the current handshake first, avoiding the new ClientHello
3552 * being mistaken for an ancient message in the current handshake.
3553 */
3554#if defined(POLARSSL_SSL_PROTO_DTLS)
3555 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3556 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003557 ssl->state == SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003558 {
3559 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3560 ssl->in_msg[0] == SSL_HS_FINISHED )
3561 {
3562 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3563
3564 if( ( ret = ssl_resend( ssl ) ) != 0 )
3565 {
3566 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3567 return( ret );
3568 }
3569
3570 return( POLARSSL_ERR_NET_WANT_READ );
3571 }
3572 else
3573 {
3574 ssl_handshake_wrapup_free_hs_transform( ssl );
3575 }
3576 }
3577#endif
3578
3579 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003580 * Handle particular types of records
3581 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003582 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3583 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003584 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3585 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003586 }
3587
3588 if( ssl->in_msgtype == SSL_MSG_ALERT )
3589 {
3590 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3591 ssl->in_msg[0], ssl->in_msg[1] ) );
3592
3593 /*
3594 * Ignore non-fatal alerts, except close_notify
3595 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003596 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003597 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003598 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3599 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003600 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003601 }
3602
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003603 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3604 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003605 {
3606 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003607 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003608 }
3609 }
3610
Paul Bakker5121ce52009-01-03 21:22:43 +00003611 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3612
3613 return( 0 );
3614}
3615
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003616int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3617{
3618 int ret;
3619
3620 if( ( ret = ssl_send_alert_message( ssl,
3621 SSL_ALERT_LEVEL_FATAL,
3622 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3623 {
3624 return( ret );
3625 }
3626
3627 return( 0 );
3628}
3629
Paul Bakker0a925182012-04-16 06:46:41 +00003630int ssl_send_alert_message( ssl_context *ssl,
3631 unsigned char level,
3632 unsigned char message )
3633{
3634 int ret;
3635
3636 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3637
3638 ssl->out_msgtype = SSL_MSG_ALERT;
3639 ssl->out_msglen = 2;
3640 ssl->out_msg[0] = level;
3641 ssl->out_msg[1] = message;
3642
3643 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3644 {
3645 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3646 return( ret );
3647 }
3648
3649 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3650
3651 return( 0 );
3652}
3653
Paul Bakker5121ce52009-01-03 21:22:43 +00003654/*
3655 * Handshake functions
3656 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003657#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3658 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3659 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3660 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3661 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3662 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3663 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003664int ssl_write_certificate( ssl_context *ssl )
3665{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003666 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003667
3668 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3669
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003670 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003671 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3672 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003673 {
3674 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3675 ssl->state++;
3676 return( 0 );
3677 }
3678
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003679 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3680 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003681}
3682
3683int ssl_parse_certificate( ssl_context *ssl )
3684{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003685 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3686
3687 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3688
3689 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003690 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3691 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003692 {
3693 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3694 ssl->state++;
3695 return( 0 );
3696 }
3697
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003698 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3699 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003700}
3701#else
3702int ssl_write_certificate( ssl_context *ssl )
3703{
3704 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3705 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003706 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003707 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3708
3709 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3710
3711 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003712 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3713 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003714 {
3715 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3716 ssl->state++;
3717 return( 0 );
3718 }
3719
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003720#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003721 if( ssl->endpoint == SSL_IS_CLIENT )
3722 {
3723 if( ssl->client_auth == 0 )
3724 {
3725 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3726 ssl->state++;
3727 return( 0 );
3728 }
3729
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003730#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003731 /*
3732 * If using SSLv3 and got no cert, send an Alert message
3733 * (otherwise an empty Certificate message will be sent).
3734 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003735 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003736 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3737 {
3738 ssl->out_msglen = 2;
3739 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003740 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3741 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003742
3743 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3744 goto write_msg;
3745 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003746#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003747 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003748#endif /* POLARSSL_SSL_CLI_C */
3749#if defined(POLARSSL_SSL_SRV_C)
3750 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003751 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003752 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003753 {
3754 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003755 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003756 }
3757 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003758#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003759
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003760 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003761
3762 /*
3763 * 0 . 0 handshake type
3764 * 1 . 3 handshake length
3765 * 4 . 6 length of all certs
3766 * 7 . 9 length of cert. 1
3767 * 10 . n-1 peer certificate
3768 * n . n+2 length of cert. 2
3769 * n+3 . ... upper level cert, etc.
3770 */
3771 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003772 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003773
Paul Bakker29087132010-03-21 21:03:34 +00003774 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003775 {
3776 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003777 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003778 {
3779 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3780 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003781 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003782 }
3783
3784 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3785 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3786 ssl->out_msg[i + 2] = (unsigned char)( n );
3787
3788 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3789 i += n; crt = crt->next;
3790 }
3791
3792 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3793 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3794 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3795
3796 ssl->out_msglen = i;
3797 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3798 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3799
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003800#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003801write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003802#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003803
3804 ssl->state++;
3805
3806 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3807 {
3808 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3809 return( ret );
3810 }
3811
3812 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3813
Paul Bakkered27a042013-04-18 22:46:23 +02003814 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003815}
3816
3817int ssl_parse_certificate( ssl_context *ssl )
3818{
Paul Bakkered27a042013-04-18 22:46:23 +02003819 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003820 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003821 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003822
3823 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3824
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003825 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003826 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3827 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003828 {
3829 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3830 ssl->state++;
3831 return( 0 );
3832 }
3833
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003834#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003835 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003836 ( ssl->authmode == SSL_VERIFY_NONE ||
3837 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003838 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003839 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003840 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3841 ssl->state++;
3842 return( 0 );
3843 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003844#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003845
3846 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3847 {
3848 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3849 return( ret );
3850 }
3851
3852 ssl->state++;
3853
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003854#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003855#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003856 /*
3857 * Check if the client sent an empty certificate
3858 */
3859 if( ssl->endpoint == SSL_IS_SERVER &&
3860 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3861 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003862 if( ssl->in_msglen == 2 &&
3863 ssl->in_msgtype == SSL_MSG_ALERT &&
3864 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3865 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003866 {
3867 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3868
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003869 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003870 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3871 return( 0 );
3872 else
Paul Bakker40e46942009-01-03 21:51:57 +00003873 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003874 }
3875 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003876#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003877
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003878#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3879 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003880 if( ssl->endpoint == SSL_IS_SERVER &&
3881 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3882 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003883 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003884 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3885 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003886 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003887 {
3888 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3889
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003890 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003891 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003892 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003893 else
3894 return( 0 );
3895 }
3896 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003897#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3898 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003899#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003900
3901 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3902 {
3903 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003904 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003905 }
3906
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003907 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3908 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003909 {
3910 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003911 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003912 }
3913
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003914 i = ssl_hs_hdr_len( ssl );
3915
Paul Bakker5121ce52009-01-03 21:22:43 +00003916 /*
3917 * Same message structure as in ssl_write_certificate()
3918 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003919 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003920
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003921 if( ssl->in_msg[i] != 0 ||
3922 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003923 {
3924 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003925 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003926 }
3927
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003928 /* In case we tried to reuse a session but it failed */
3929 if( ssl->session_negotiate->peer_cert != NULL )
3930 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003931 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003932 polarssl_free( ssl->session_negotiate->peer_cert );
3933 }
3934
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003935 if( ( ssl->session_negotiate->peer_cert = polarssl_malloc(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003936 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003937 {
3938 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003939 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003940 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003941 }
3942
Paul Bakkerb6b09562013-09-18 14:17:41 +02003943 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003944
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003945 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003946
3947 while( i < ssl->in_hslen )
3948 {
3949 if( ssl->in_msg[i] != 0 )
3950 {
3951 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003952 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003953 }
3954
3955 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3956 | (unsigned int) ssl->in_msg[i + 2];
3957 i += 3;
3958
3959 if( n < 128 || i + n > ssl->in_hslen )
3960 {
3961 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003962 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003963 }
3964
Paul Bakkerddf26b42013-09-18 13:46:23 +02003965 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3966 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003967 if( ret != 0 )
3968 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003969 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003970 return( ret );
3971 }
3972
3973 i += n;
3974 }
3975
Paul Bakker48916f92012-09-16 19:57:18 +00003976 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003977
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003978 /*
3979 * On client, make sure the server cert doesn't change during renego to
3980 * avoid "triple handshake" attack: https://secure-resumption.com/
3981 */
Paul Bakkerf6080b82015-01-13 16:18:23 +01003982#if defined(POLARSSL_SSL_RENEGOTIATION) && defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003983 if( ssl->endpoint == SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00003984 ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003985 {
3986 if( ssl->session->peer_cert == NULL )
3987 {
3988 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3989 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3990 }
3991
3992 if( ssl->session->peer_cert->raw.len !=
3993 ssl->session_negotiate->peer_cert->raw.len ||
3994 memcmp( ssl->session->peer_cert->raw.p,
3995 ssl->session_negotiate->peer_cert->raw.p,
3996 ssl->session->peer_cert->raw.len ) != 0 )
3997 {
3998 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3999 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
4000 }
4001 }
Paul Bakkerf6080b82015-01-13 16:18:23 +01004002#endif /* POLARSSL_SSL_RENEGOTIATION && POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004003
Paul Bakker5121ce52009-01-03 21:22:43 +00004004 if( ssl->authmode != SSL_VERIFY_NONE )
4005 {
4006 if( ssl->ca_chain == NULL )
4007 {
4008 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004009 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004010 }
4011
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004012 /*
4013 * Main check: verify certificate
4014 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02004015 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
4016 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
4017 &ssl->session_negotiate->verify_result,
4018 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00004019
4020 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004021 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004022 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004023 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004024
4025 /*
4026 * Secondary checks: always done, but change 'ret' only if it was 0
4027 */
4028
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004029#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004030 {
Manuel Pégourié-Gonnard0db107e2015-03-19 14:01:57 +00004031 const pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004032
4033 /* If certificate uses an EC key, make sure the curve is OK */
4034 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
4035 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
4036 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02004037 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004038 if( ret == 0 )
4039 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004040 }
4041 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004042#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00004043
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004044 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
4045 ciphersuite_info,
4046 ! ssl->endpoint ) != 0 )
4047 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02004048 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004049 if( ret == 0 )
4050 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
4051 }
4052
Paul Bakker5121ce52009-01-03 21:22:43 +00004053 if( ssl->authmode != SSL_VERIFY_REQUIRED )
4054 ret = 0;
4055 }
4056
4057 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
4058
4059 return( ret );
4060}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01004061#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
4062 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
4063 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
4064 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
4065 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
4066 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
4067 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004068
4069int ssl_write_change_cipher_spec( ssl_context *ssl )
4070{
4071 int ret;
4072
4073 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
4074
4075 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
4076 ssl->out_msglen = 1;
4077 ssl->out_msg[0] = 1;
4078
Paul Bakker5121ce52009-01-03 21:22:43 +00004079 ssl->state++;
4080
4081 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4082 {
4083 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4084 return( ret );
4085 }
4086
4087 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
4088
4089 return( 0 );
4090}
4091
4092int ssl_parse_change_cipher_spec( ssl_context *ssl )
4093{
4094 int ret;
4095
4096 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
4097
Paul Bakker5121ce52009-01-03 21:22:43 +00004098 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4099 {
4100 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4101 return( ret );
4102 }
4103
4104 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
4105 {
4106 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004107 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004108 }
4109
4110 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
4111 {
4112 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004113 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00004114 }
4115
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004116 /*
4117 * Switch to our negotiated transform and session parameters for inbound
4118 * data.
4119 */
4120 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
4121 ssl->transform_in = ssl->transform_negotiate;
4122 ssl->session_in = ssl->session_negotiate;
4123
4124#if defined(POLARSSL_SSL_PROTO_DTLS)
4125 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4126 {
4127#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4128 ssl_dtls_replay_reset( ssl );
4129#endif
4130
4131 /* Increment epoch */
4132 if( ++ssl->in_epoch == 0 )
4133 {
4134 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4135 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4136 }
4137 }
4138 else
4139#endif /* POLARSSL_SSL_PROTO_DTLS */
4140 memset( ssl->in_ctr, 0, 8 );
4141
4142 /*
4143 * Set the in_msg pointer to the correct location based on IV length
4144 */
4145 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4146 {
4147 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
4148 ssl->transform_negotiate->fixed_ivlen;
4149 }
4150 else
4151 ssl->in_msg = ssl->in_iv;
4152
4153#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4154 if( ssl_hw_record_activate != NULL )
4155 {
4156 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
4157 {
4158 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4159 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4160 }
4161 }
4162#endif
4163
Paul Bakker5121ce52009-01-03 21:22:43 +00004164 ssl->state++;
4165
4166 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
4167
4168 return( 0 );
4169}
4170
Paul Bakker41c83d32013-03-20 14:39:14 +01004171void ssl_optimize_checksum( ssl_context *ssl,
4172 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00004173{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02004174 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01004175
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 Bakker380da532012-04-18 16:10:25 +00004178 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00004179 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00004180 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004181#endif
4182#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4183#if defined(POLARSSL_SHA512_C)
4184 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
4185 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
4186 else
4187#endif
4188#if defined(POLARSSL_SHA256_C)
4189 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00004190 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004191 else
4192#endif
4193#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004194 {
4195 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004196 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004197 }
Paul Bakker380da532012-04-18 16:10:25 +00004198}
Paul Bakkerf7abd422013-04-16 13:15:56 +02004199
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004200void ssl_reset_checksum( ssl_context *ssl )
4201{
4202#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4203 defined(POLARSSL_SSL_PROTO_TLS1_1)
4204 md5_starts( &ssl->handshake->fin_md5 );
4205 sha1_starts( &ssl->handshake->fin_sha1 );
4206#endif
4207#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4208#if defined(POLARSSL_SHA256_C)
4209 sha256_starts( &ssl->handshake->fin_sha256, 0 );
4210#endif
4211#if defined(POLARSSL_SHA512_C)
4212 sha512_starts( &ssl->handshake->fin_sha512, 1 );
4213#endif
4214#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4215}
4216
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004217static void ssl_update_checksum_start( ssl_context *ssl,
4218 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004219{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004220#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4221 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00004222 md5_update( &ssl->handshake->fin_md5 , buf, len );
4223 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004224#endif
4225#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4226#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02004227 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004228#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02004229#if defined(POLARSSL_SHA512_C)
4230 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01004231#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004232#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004233}
4234
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004235#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4236 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004237static void ssl_update_checksum_md5sha1( ssl_context *ssl,
4238 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004239{
Paul Bakker48916f92012-09-16 19:57:18 +00004240 md5_update( &ssl->handshake->fin_md5 , buf, len );
4241 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004242}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004243#endif
Paul Bakker380da532012-04-18 16:10:25 +00004244
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004245#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4246#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004247static void ssl_update_checksum_sha256( ssl_context *ssl,
4248 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004249{
Paul Bakker9e36f042013-06-30 14:34:05 +02004250 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004251}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004252#endif
Paul Bakker380da532012-04-18 16:10:25 +00004253
Paul Bakker9e36f042013-06-30 14:34:05 +02004254#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004255static void ssl_update_checksum_sha384( ssl_context *ssl,
4256 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004257{
Paul Bakker9e36f042013-06-30 14:34:05 +02004258 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004259}
Paul Bakker769075d2012-11-24 11:26:46 +01004260#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004261#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004262
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004263#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004264static void ssl_calc_finished_ssl(
4265 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004266{
Paul Bakker3c2122f2013-06-24 19:03:14 +02004267 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004268 md5_context md5;
4269 sha1_context sha1;
4270
Paul Bakker5121ce52009-01-03 21:22:43 +00004271 unsigned char padbuf[48];
4272 unsigned char md5sum[16];
4273 unsigned char sha1sum[20];
4274
Paul Bakker48916f92012-09-16 19:57:18 +00004275 ssl_session *session = ssl->session_negotiate;
4276 if( !session )
4277 session = ssl->session;
4278
Paul Bakker1ef83d62012-04-11 12:09:53 +00004279 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
4280
Paul Bakker48916f92012-09-16 19:57:18 +00004281 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4282 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004283
4284 /*
4285 * SSLv3:
4286 * hash =
4287 * MD5( master + pad2 +
4288 * MD5( handshake + sender + master + pad1 ) )
4289 * + SHA1( master + pad2 +
4290 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004291 */
4292
Paul Bakker90995b52013-06-24 19:20:35 +02004293#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004294 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004295 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004296#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004297
Paul Bakker90995b52013-06-24 19:20:35 +02004298#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004299 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004300 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004301#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004302
Paul Bakker3c2122f2013-06-24 19:03:14 +02004303 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
4304 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004305
Paul Bakker1ef83d62012-04-11 12:09:53 +00004306 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004307
Paul Bakker3c2122f2013-06-24 19:03:14 +02004308 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004309 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004310 md5_update( &md5, padbuf, 48 );
4311 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004312
Paul Bakker3c2122f2013-06-24 19:03:14 +02004313 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004314 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004315 sha1_update( &sha1, padbuf, 40 );
4316 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004317
Paul Bakker1ef83d62012-04-11 12:09:53 +00004318 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004319
Paul Bakker1ef83d62012-04-11 12:09:53 +00004320 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00004321 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004322 md5_update( &md5, padbuf, 48 );
4323 md5_update( &md5, md5sum, 16 );
4324 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004325
Paul Bakker1ef83d62012-04-11 12:09:53 +00004326 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00004327 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004328 sha1_update( &sha1, padbuf , 40 );
4329 sha1_update( &sha1, sha1sum, 20 );
4330 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004331
Paul Bakker1ef83d62012-04-11 12:09:53 +00004332 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004333
Paul Bakker5b4af392014-06-26 12:09:34 +02004334 md5_free( &md5 );
4335 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004336
Paul Bakker34617722014-06-13 17:20:13 +02004337 polarssl_zeroize( padbuf, sizeof( padbuf ) );
4338 polarssl_zeroize( md5sum, sizeof( md5sum ) );
4339 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004340
4341 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4342}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004343#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004344
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004345#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004346static void ssl_calc_finished_tls(
4347 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004348{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004349 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004350 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004351 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00004352 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004353 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004354
Paul Bakker48916f92012-09-16 19:57:18 +00004355 ssl_session *session = ssl->session_negotiate;
4356 if( !session )
4357 session = ssl->session;
4358
Paul Bakker1ef83d62012-04-11 12:09:53 +00004359 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004360
Paul Bakker48916f92012-09-16 19:57:18 +00004361 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4362 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004363
Paul Bakker1ef83d62012-04-11 12:09:53 +00004364 /*
4365 * TLSv1:
4366 * hash = PRF( master, finished_label,
4367 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4368 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004369
Paul Bakker90995b52013-06-24 19:20:35 +02004370#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004371 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4372 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004373#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004374
Paul Bakker90995b52013-06-24 19:20:35 +02004375#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004376 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4377 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004378#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004379
4380 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004381 ? "client finished"
4382 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004383
4384 md5_finish( &md5, padbuf );
4385 sha1_finish( &sha1, padbuf + 16 );
4386
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004387 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004388 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004389
4390 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4391
Paul Bakker5b4af392014-06-26 12:09:34 +02004392 md5_free( &md5 );
4393 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004394
Paul Bakker34617722014-06-13 17:20:13 +02004395 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004396
4397 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4398}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004399#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004400
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004401#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4402#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004403static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004404 ssl_context *ssl, unsigned char *buf, int from )
4405{
4406 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004407 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004408 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004409 unsigned char padbuf[32];
4410
Paul Bakker48916f92012-09-16 19:57:18 +00004411 ssl_session *session = ssl->session_negotiate;
4412 if( !session )
4413 session = ssl->session;
4414
Paul Bakker380da532012-04-18 16:10:25 +00004415 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004416
Paul Bakker9e36f042013-06-30 14:34:05 +02004417 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004418
4419 /*
4420 * TLSv1.2:
4421 * hash = PRF( master, finished_label,
4422 * Hash( handshake ) )[0.11]
4423 */
4424
Paul Bakker9e36f042013-06-30 14:34:05 +02004425#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004426 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004427 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004428#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004429
4430 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004431 ? "client finished"
4432 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004433
Paul Bakker9e36f042013-06-30 14:34:05 +02004434 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004435
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004436 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004437 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004438
4439 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4440
Paul Bakker5b4af392014-06-26 12:09:34 +02004441 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004442
Paul Bakker34617722014-06-13 17:20:13 +02004443 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004444
4445 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4446}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004447#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004448
Paul Bakker9e36f042013-06-30 14:34:05 +02004449#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004450static void ssl_calc_finished_tls_sha384(
4451 ssl_context *ssl, unsigned char *buf, int from )
4452{
4453 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004454 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004455 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004456 unsigned char padbuf[48];
4457
Paul Bakker48916f92012-09-16 19:57:18 +00004458 ssl_session *session = ssl->session_negotiate;
4459 if( !session )
4460 session = ssl->session;
4461
Paul Bakker380da532012-04-18 16:10:25 +00004462 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004463
Paul Bakker9e36f042013-06-30 14:34:05 +02004464 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004465
4466 /*
4467 * TLSv1.2:
4468 * hash = PRF( master, finished_label,
4469 * Hash( handshake ) )[0.11]
4470 */
4471
Paul Bakker9e36f042013-06-30 14:34:05 +02004472#if !defined(POLARSSL_SHA512_ALT)
4473 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4474 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004475#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004476
4477 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004478 ? "client finished"
4479 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004480
Paul Bakker9e36f042013-06-30 14:34:05 +02004481 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004482
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004483 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004484 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004485
4486 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4487
Paul Bakker5b4af392014-06-26 12:09:34 +02004488 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004489
Paul Bakker34617722014-06-13 17:20:13 +02004490 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004491
4492 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4493}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004494#endif /* POLARSSL_SHA512_C */
4495#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004496
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004497static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004498{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004499 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004500
4501 /*
4502 * Free our handshake params
4503 */
4504 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004505 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004506 ssl->handshake = NULL;
4507
4508 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004509 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004510 */
4511 if( ssl->transform )
4512 {
4513 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004514 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004515 }
4516 ssl->transform = ssl->transform_negotiate;
4517 ssl->transform_negotiate = NULL;
4518
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004519 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4520}
4521
4522void ssl_handshake_wrapup( ssl_context *ssl )
4523{
4524 int resume = ssl->handshake->resume;
4525
4526 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4527
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004528#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00004529 if( ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004530 {
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00004531 ssl->renego_status = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004532 ssl->renego_records_seen = 0;
4533 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004534#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004535
4536 /*
4537 * Free the previous session and switch in the current one
4538 */
Paul Bakker0a597072012-09-25 21:55:46 +00004539 if( ssl->session )
4540 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01004541#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4542 /* RFC 7366 3.1: keep the EtM state */
4543 ssl->session_negotiate->encrypt_then_mac =
4544 ssl->session->encrypt_then_mac;
4545#endif
4546
Paul Bakker0a597072012-09-25 21:55:46 +00004547 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004548 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004549 }
4550 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004551 ssl->session_negotiate = NULL;
4552
Paul Bakker0a597072012-09-25 21:55:46 +00004553 /*
4554 * Add cache entry
4555 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004556 if( ssl->f_set_cache != NULL &&
4557 ssl->session->length != 0 &&
4558 resume == 0 )
4559 {
Paul Bakker0a597072012-09-25 21:55:46 +00004560 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4561 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004562 }
Paul Bakker0a597072012-09-25 21:55:46 +00004563
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004564#if defined(POLARSSL_SSL_PROTO_DTLS)
4565 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4566 ssl->handshake->flight != NULL )
4567 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02004568 /* Cancel handshake timer */
4569 ssl_set_timer( ssl, 0 );
4570
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004571 /* Keep last flight around in case we need to resend it:
4572 * we need the handshake and transform structures for that */
4573 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4574 }
4575 else
4576#endif
4577 ssl_handshake_wrapup_free_hs_transform( ssl );
4578
Paul Bakker48916f92012-09-16 19:57:18 +00004579 ssl->state++;
4580
4581 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4582}
4583
Paul Bakker1ef83d62012-04-11 12:09:53 +00004584int ssl_write_finished( ssl_context *ssl )
4585{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004586 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004587
4588 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4589
Paul Bakker92be97b2013-01-02 17:30:03 +01004590 /*
4591 * Set the out_msg pointer to the correct location based on IV length
4592 */
4593 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4594 {
4595 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4596 ssl->transform_negotiate->fixed_ivlen;
4597 }
4598 else
4599 ssl->out_msg = ssl->out_iv;
4600
Paul Bakker48916f92012-09-16 19:57:18 +00004601 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004602
4603 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004604 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4605
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004606#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004607 ssl->verify_data_len = hash_len;
4608 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004609#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004610
Paul Bakker5121ce52009-01-03 21:22:43 +00004611 ssl->out_msglen = 4 + hash_len;
4612 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4613 ssl->out_msg[0] = SSL_HS_FINISHED;
4614
4615 /*
4616 * In case of session resuming, invert the client and server
4617 * ChangeCipherSpec messages order.
4618 */
Paul Bakker0a597072012-09-25 21:55:46 +00004619 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004620 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004621#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004622 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004623 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004624#endif
4625#if defined(POLARSSL_SSL_SRV_C)
4626 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004627 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004628#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004629 }
4630 else
4631 ssl->state++;
4632
Paul Bakker48916f92012-09-16 19:57:18 +00004633 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004634 * Switch to our negotiated transform and session parameters for outbound
4635 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004636 */
4637 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004638
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004639#if defined(POLARSSL_SSL_PROTO_DTLS)
4640 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4641 {
4642 unsigned char i;
4643
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004644 /* Remember current epoch settings for resending */
4645 ssl->handshake->alt_transform_out = ssl->transform_out;
4646 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4647
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004648 /* Set sequence_number to zero */
4649 memset( ssl->out_ctr + 2, 0, 6 );
4650
4651 /* Increment epoch */
4652 for( i = 2; i > 0; i-- )
4653 if( ++ssl->out_ctr[i - 1] != 0 )
4654 break;
4655
4656 /* The loop goes to its end iff the counter is wrapping */
4657 if( i == 0 )
4658 {
4659 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4660 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4661 }
4662 }
4663 else
4664#endif /* POLARSSL_SSL_PROTO_DTLS */
4665 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004666
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004667 ssl->transform_out = ssl->transform_negotiate;
4668 ssl->session_out = ssl->session_negotiate;
4669
Paul Bakker07eb38b2012-12-19 14:42:06 +01004670#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004671 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004672 {
4673 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4674 {
4675 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4676 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4677 }
4678 }
4679#endif
4680
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004681#if defined(POLARSSL_SSL_PROTO_DTLS)
4682 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4683 ssl_send_flight_completed( ssl );
4684#endif
4685
Paul Bakker5121ce52009-01-03 21:22:43 +00004686 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4687 {
4688 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4689 return( ret );
4690 }
4691
4692 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4693
4694 return( 0 );
4695}
4696
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004697#if defined(POLARSSL_SSL_PROTO_SSL3)
4698#define SSL_MAX_HASH_LEN 36
4699#else
4700#define SSL_MAX_HASH_LEN 12
4701#endif
4702
Paul Bakker5121ce52009-01-03 21:22:43 +00004703int ssl_parse_finished( ssl_context *ssl )
4704{
Paul Bakker23986e52011-04-24 08:57:21 +00004705 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004706 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004707 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004708
4709 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4710
Paul Bakker48916f92012-09-16 19:57:18 +00004711 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004712
Paul Bakker5121ce52009-01-03 21:22:43 +00004713 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4714 {
4715 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4716 return( ret );
4717 }
4718
4719 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4720 {
4721 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004722 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004723 }
4724
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004725 /* There is currently no ciphersuite using another length with TLS 1.2 */
4726#if defined(POLARSSL_SSL_PROTO_SSL3)
4727 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4728 hash_len = 36;
4729 else
4730#endif
4731 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004732
4733 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004734 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004735 {
4736 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004737 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004738 }
4739
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004740 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4741 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004742 {
4743 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004744 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004745 }
4746
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004747#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004748 ssl->verify_data_len = hash_len;
4749 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004750#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004751
Paul Bakker0a597072012-09-25 21:55:46 +00004752 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004753 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004754#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004755 if( ssl->endpoint == SSL_IS_CLIENT )
4756 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004757#endif
4758#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004759 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004760 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004761#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004762 }
4763 else
4764 ssl->state++;
4765
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004766#if defined(POLARSSL_SSL_PROTO_DTLS)
4767 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4768 ssl_recv_flight_completed( ssl );
4769#endif
4770
Paul Bakker5121ce52009-01-03 21:22:43 +00004771 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4772
4773 return( 0 );
4774}
4775
Paul Bakker968afaa2014-07-09 11:09:24 +02004776static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004777{
4778 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4779
4780#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4781 defined(POLARSSL_SSL_PROTO_TLS1_1)
4782 md5_init( &handshake->fin_md5 );
4783 sha1_init( &handshake->fin_sha1 );
4784 md5_starts( &handshake->fin_md5 );
4785 sha1_starts( &handshake->fin_sha1 );
4786#endif
4787#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4788#if defined(POLARSSL_SHA256_C)
4789 sha256_init( &handshake->fin_sha256 );
4790 sha256_starts( &handshake->fin_sha256, 0 );
4791#endif
4792#if defined(POLARSSL_SHA512_C)
4793 sha512_init( &handshake->fin_sha512 );
4794 sha512_starts( &handshake->fin_sha512, 1 );
4795#endif
4796#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4797
4798 handshake->update_checksum = ssl_update_checksum_start;
4799 handshake->sig_alg = SSL_HASH_SHA1;
4800
4801#if defined(POLARSSL_DHM_C)
4802 dhm_init( &handshake->dhm_ctx );
4803#endif
4804#if defined(POLARSSL_ECDH_C)
4805 ecdh_init( &handshake->ecdh_ctx );
4806#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004807}
4808
4809static void ssl_transform_init( ssl_transform *transform )
4810{
4811 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004812
4813 cipher_init( &transform->cipher_ctx_enc );
4814 cipher_init( &transform->cipher_ctx_dec );
4815
4816 md_init( &transform->md_ctx_enc );
4817 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004818}
4819
4820void ssl_session_init( ssl_session *session )
4821{
4822 memset( session, 0, sizeof(ssl_session) );
4823}
4824
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004825static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004826{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004827 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004828 if( ssl->transform_negotiate )
4829 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004830 if( ssl->session_negotiate )
4831 ssl_session_free( ssl->session_negotiate );
4832 if( ssl->handshake )
4833 ssl_handshake_free( ssl->handshake );
4834
4835 /*
4836 * Either the pointers are now NULL or cleared properly and can be freed.
4837 * Now allocate missing structures.
4838 */
4839 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004840 {
Mansour Moufid99b92592015-02-15 17:46:32 -05004841 ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004842 }
Paul Bakker48916f92012-09-16 19:57:18 +00004843
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004844 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004845 {
Mansour Moufid99b92592015-02-15 17:46:32 -05004846 ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004847 }
Paul Bakker48916f92012-09-16 19:57:18 +00004848
Paul Bakker82788fb2014-10-20 13:59:19 +02004849 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004850 {
Mansour Moufid99b92592015-02-15 17:46:32 -05004851 ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004852 }
Paul Bakker48916f92012-09-16 19:57:18 +00004853
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004854 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004855 if( ssl->handshake == NULL ||
4856 ssl->transform_negotiate == NULL ||
4857 ssl->session_negotiate == NULL )
4858 {
4859 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004860
4861 polarssl_free( ssl->handshake );
4862 polarssl_free( ssl->transform_negotiate );
4863 polarssl_free( ssl->session_negotiate );
4864
4865 ssl->handshake = NULL;
4866 ssl->transform_negotiate = NULL;
4867 ssl->session_negotiate = NULL;
4868
Paul Bakker48916f92012-09-16 19:57:18 +00004869 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4870 }
4871
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004872 /* Initialize structures */
4873 ssl_session_init( ssl->session_negotiate );
4874 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004875 ssl_handshake_params_init( ssl->handshake );
4876
4877#if defined(POLARSSL_X509_CRT_PARSE_C)
4878 ssl->handshake->key_cert = ssl->key_cert;
4879#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004880
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004881 /*
4882 * We may not know yet if we're using DTLS,
4883 * so always initiliase DTLS-specific fields.
4884 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004885#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004886 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004887
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004888 // TODO: not the right place, we may not know endpoint yet
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004889 if( ssl->endpoint == SSL_IS_CLIENT )
4890 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4891 else
4892 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004893#endif
4894
Paul Bakker48916f92012-09-16 19:57:18 +00004895 return( 0 );
4896}
4897
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004898#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4899/* Dummy cookie callbacks for defaults */
4900static int ssl_cookie_write_dummy( void *ctx,
4901 unsigned char **p, unsigned char *end,
4902 const unsigned char *cli_id, size_t cli_id_len )
4903{
4904 ((void) ctx);
4905 ((void) p);
4906 ((void) end);
4907 ((void) cli_id);
4908 ((void) cli_id_len);
4909
4910 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4911}
4912
4913static int ssl_cookie_check_dummy( void *ctx,
4914 const unsigned char *cookie, size_t cookie_len,
4915 const unsigned char *cli_id, size_t cli_id_len )
4916{
4917 ((void) ctx);
4918 ((void) cookie);
4919 ((void) cookie_len);
4920 ((void) cli_id);
4921 ((void) cli_id_len);
4922
4923 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4924}
4925#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4926
Paul Bakker5121ce52009-01-03 21:22:43 +00004927/*
4928 * Initialize an SSL context
4929 */
4930int ssl_init( ssl_context *ssl )
4931{
Paul Bakker48916f92012-09-16 19:57:18 +00004932 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004933 int len = SSL_BUFFER_LEN;
4934
4935 memset( ssl, 0, sizeof( ssl_context ) );
4936
Paul Bakker62f2dee2012-09-28 07:31:51 +00004937 /*
4938 * Sane defaults
4939 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004940 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4941 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4942 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4943 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004944
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004945 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004946
Manuel Pégourié-Gonnard849b1742015-03-20 19:13:22 +00004947 ssl_set_arc4_support( ssl, SSL_ARC4_DISABLED );
4948
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004949#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004950 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004951 memset( ssl->renego_period, 0xFF, 7 );
4952 ssl->renego_period[7] = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004953#endif
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004954
Paul Bakker62f2dee2012-09-28 07:31:51 +00004955#if defined(POLARSSL_DHM_C)
4956 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4957 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4958 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4959 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4960 {
4961 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4962 return( ret );
4963 }
4964#endif
4965
4966 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004967 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004968 */
Manuel Pégourié-Gonnard4e41c992015-02-18 10:39:49 +00004969 if( ( ssl->in_buf = polarssl_malloc( len ) ) == NULL ||
4970 ( ssl->out_buf = polarssl_malloc( len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004971 {
4972 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004973 polarssl_free( ssl->in_buf );
4974 ssl->in_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004975 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004976 }
4977
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004978 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4979 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004980
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004981 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4982 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4983
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004984#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4985 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
4986#endif
4987
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004988#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4989 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
4990#endif
4991
Paul Bakker606b4ba2013-08-14 16:52:14 +02004992#if defined(POLARSSL_SSL_SESSION_TICKETS)
4993 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4994#endif
4995
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004996#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004997 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004998#endif
4999
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005000#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
5001 ssl->f_cookie_write = ssl_cookie_write_dummy;
5002 ssl->f_cookie_check = ssl_cookie_check_dummy;
5003#endif
5004
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005005#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
5006 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
5007#endif
5008
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02005009#if defined(POLARSSL_SSL_PROTO_DTLS)
5010 ssl->hs_timeout_min = SSL_DTLS_TIMEOUT_DFL_MIN;
5011 ssl->hs_timeout_max = SSL_DTLS_TIMEOUT_DFL_MAX;
5012#endif
5013
Paul Bakker48916f92012-09-16 19:57:18 +00005014 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5015 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005016
5017 return( 0 );
5018}
5019
5020/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00005021 * Reset an initialized and used SSL context for re-use while retaining
5022 * all application-set variables, function pointers and data.
5023 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005024int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00005025{
Paul Bakker48916f92012-09-16 19:57:18 +00005026 int ret;
5027
Paul Bakker7eb013f2011-10-06 12:37:39 +00005028 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005029
5030#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00005031 ssl->renego_status = SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005032 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00005033
5034 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard61860192014-11-04 13:05:42 +01005035 memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
5036 memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005037#endif
5038 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00005039
Paul Bakker7eb013f2011-10-06 12:37:39 +00005040 ssl->in_offt = NULL;
5041
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005042 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005043 ssl->in_msgtype = 0;
5044 ssl->in_msglen = 0;
5045 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005046#if defined(POLARSSL_SSL_PROTO_DTLS)
5047 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005048 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005049#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005050#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
5051 ssl_dtls_replay_reset( ssl );
5052#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005053
5054 ssl->in_hslen = 0;
5055 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005056 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005057
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005058 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005059 ssl->out_msgtype = 0;
5060 ssl->out_msglen = 0;
5061 ssl->out_left = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005062#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005063 if( ssl->split_done != SSL_CBC_RECORD_SPLITTING_DISABLED )
5064 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005065#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005066
Paul Bakker48916f92012-09-16 19:57:18 +00005067 ssl->transform_in = NULL;
5068 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005069
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005070 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
5071 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00005072
5073#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02005074 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005075 {
5076 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005077 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005078 {
5079 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
5080 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
5081 }
Paul Bakker05ef8352012-05-08 09:17:57 +00005082 }
5083#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00005084
Paul Bakker48916f92012-09-16 19:57:18 +00005085 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005086 {
Paul Bakker48916f92012-09-16 19:57:18 +00005087 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005088 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005089 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00005090 }
Paul Bakker48916f92012-09-16 19:57:18 +00005091
Paul Bakkerc0463502013-02-14 11:19:38 +01005092 if( ssl->session )
5093 {
5094 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005095 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005096 ssl->session = NULL;
5097 }
5098
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005099#if defined(POLARSSL_SSL_ALPN)
5100 ssl->alpn_chosen = NULL;
5101#endif
5102
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02005103#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005104 polarssl_free( ssl->cli_id );
5105 ssl->cli_id = NULL;
5106 ssl->cli_id_len = 0;
5107#endif
5108
Paul Bakker48916f92012-09-16 19:57:18 +00005109 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5110 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005111
5112 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00005113}
5114
Paul Bakkera503a632013-08-14 13:48:06 +02005115#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005116static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
5117{
5118 aes_free( &tkeys->enc );
5119 aes_free( &tkeys->dec );
5120
5121 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
5122}
5123
Paul Bakker7eb013f2011-10-06 12:37:39 +00005124/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005125 * Allocate and initialize ticket keys
5126 */
5127static int ssl_ticket_keys_init( ssl_context *ssl )
5128{
5129 int ret;
5130 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005131 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005132
5133 if( ssl->ticket_keys != NULL )
5134 return( 0 );
5135
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005136 tkeys = polarssl_malloc( sizeof(ssl_ticket_keys) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005137 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005138 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5139
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005140 aes_init( &tkeys->enc );
5141 aes_init( &tkeys->dec );
5142
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005143 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01005144 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005145 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005146 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005147 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005148 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005149
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02005150 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
5151 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
5152 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
5153 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005154 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005155 polarssl_free( tkeys );
5156 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02005157 }
5158
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005159 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01005160 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005161 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005162 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005163 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005164 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005165
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005166 ssl->ticket_keys = tkeys;
5167
5168 return( 0 );
5169}
Paul Bakkera503a632013-08-14 13:48:06 +02005170#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005171
5172/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005173 * SSL set accessors
5174 */
5175void ssl_set_endpoint( ssl_context *ssl, int endpoint )
5176{
5177 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005178
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005179#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
5180 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005181 if( endpoint == SSL_IS_CLIENT )
5182 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02005183#endif
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01005184
5185#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
5186 if( endpoint == SSL_IS_SERVER )
5187 ssl->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
5188#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005189}
5190
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005191int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005192{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005193#if defined(POLARSSL_SSL_PROTO_DTLS)
5194 if( transport == SSL_TRANSPORT_DATAGRAM )
5195 {
5196 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005197
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005198 ssl->out_hdr = ssl->out_buf;
5199 ssl->out_ctr = ssl->out_buf + 3;
5200 ssl->out_len = ssl->out_buf + 11;
5201 ssl->out_iv = ssl->out_buf + 13;
5202 ssl->out_msg = ssl->out_buf + 13;
5203
5204 ssl->in_hdr = ssl->in_buf;
5205 ssl->in_ctr = ssl->in_buf + 3;
5206 ssl->in_len = ssl->in_buf + 11;
5207 ssl->in_iv = ssl->in_buf + 13;
5208 ssl->in_msg = ssl->in_buf + 13;
5209
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005210 /* DTLS starts with TLS1.1 */
5211 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
5212 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005213
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005214 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
5215 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
5216
5217 return( 0 );
5218 }
5219#endif
5220
5221 if( transport == SSL_TRANSPORT_STREAM )
5222 {
5223 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005224
5225 ssl->out_ctr = ssl->out_buf;
5226 ssl->out_hdr = ssl->out_buf + 8;
5227 ssl->out_len = ssl->out_buf + 11;
5228 ssl->out_iv = ssl->out_buf + 13;
5229 ssl->out_msg = ssl->out_buf + 13;
5230
5231 ssl->in_ctr = ssl->in_buf;
5232 ssl->in_hdr = ssl->in_buf + 8;
5233 ssl->in_len = ssl->in_buf + 11;
5234 ssl->in_iv = ssl->in_buf + 13;
5235 ssl->in_msg = ssl->in_buf + 13;
5236
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005237 return( 0 );
5238 }
5239
5240 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005241}
5242
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005243#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
5244void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
5245{
5246 ssl->anti_replay = mode;
5247}
5248#endif
5249
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005250#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
5251void ssl_set_dtls_badmac_limit( ssl_context *ssl, unsigned limit )
5252{
5253 ssl->badmac_limit = limit;
5254}
5255#endif
5256
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02005257#if defined(POLARSSL_SSL_PROTO_DTLS)
5258void ssl_set_handshake_timeout( ssl_context *ssl, uint32_t min, uint32_t max )
5259{
5260 ssl->hs_timeout_min = min;
5261 ssl->hs_timeout_max = max;
5262}
5263#endif
5264
Paul Bakker5121ce52009-01-03 21:22:43 +00005265void ssl_set_authmode( ssl_context *ssl, int authmode )
5266{
5267 ssl->authmode = authmode;
5268}
5269
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005270#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005271void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005272 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005273 void *p_vrfy )
5274{
5275 ssl->f_vrfy = f_vrfy;
5276 ssl->p_vrfy = p_vrfy;
5277}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005278#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005279
Paul Bakker5121ce52009-01-03 21:22:43 +00005280void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00005281 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00005282 void *p_rng )
5283{
5284 ssl->f_rng = f_rng;
5285 ssl->p_rng = p_rng;
5286}
5287
5288void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00005289 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00005290 void *p_dbg )
5291{
5292 ssl->f_dbg = f_dbg;
5293 ssl->p_dbg = p_dbg;
5294}
5295
Manuel Pégourié-Gonnard9a65e802015-03-25 17:19:12 +01005296#if ! defined(POLARSSL_DEPRECATED_REMOVED)
Paul Bakker5121ce52009-01-03 21:22:43 +00005297void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00005298 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00005299 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00005300{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02005301 if( p_recv != p_send )
5302 {
5303 ssl->f_recv = NULL;
5304 ssl->f_send = NULL;
5305 ssl->p_bio = NULL;
5306 return;
5307 }
5308
Paul Bakker5121ce52009-01-03 21:22:43 +00005309 ssl->f_recv = f_recv;
5310 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02005311 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00005312}
Manuel Pégourié-Gonnard9a65e802015-03-25 17:19:12 +01005313#endif /* POLARSSL_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005314
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005315void ssl_set_bio_timeout( ssl_context *ssl,
5316 void *p_bio,
5317 int (*f_send)(void *, const unsigned char *, size_t),
5318 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +02005319 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t),
5320 uint32_t timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005321{
5322 ssl->p_bio = p_bio;
5323 ssl->f_send = f_send;
5324 ssl->f_recv = f_recv;
5325 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard27074302014-10-01 17:35:50 +02005326 ssl->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005327}
5328
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005329#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00005330void ssl_set_session_cache( ssl_context *ssl,
5331 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
5332 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00005333{
Paul Bakker0a597072012-09-25 21:55:46 +00005334 ssl->f_get_cache = f_get_cache;
5335 ssl->p_get_cache = p_get_cache;
5336 ssl->f_set_cache = f_set_cache;
5337 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00005338}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005339#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005340
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005341#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005342int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00005343{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005344 int ret;
5345
5346 if( ssl == NULL ||
5347 session == NULL ||
5348 ssl->session_negotiate == NULL ||
5349 ssl->endpoint != SSL_IS_CLIENT )
5350 {
5351 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5352 }
5353
5354 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5355 return( ret );
5356
Paul Bakker0a597072012-09-25 21:55:46 +00005357 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005358
5359 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005360}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005361#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005362
Paul Bakkerb68cad62012-08-23 08:34:18 +00005363void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005364{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005365 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
5366 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
5367 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
5368 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
5369}
5370
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005371void ssl_set_ciphersuites_for_version( ssl_context *ssl,
5372 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005373 int major, int minor )
5374{
5375 if( major != SSL_MAJOR_VERSION_3 )
5376 return;
5377
5378 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
5379 return;
5380
5381 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005382}
5383
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005384#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005385/* Add a new (empty) key_cert entry an return a pointer to it */
5386static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
5387{
5388 ssl_key_cert *key_cert, *last;
5389
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005390 key_cert = polarssl_malloc( sizeof(ssl_key_cert) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005391 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005392 return( NULL );
5393
5394 memset( key_cert, 0, sizeof( ssl_key_cert ) );
5395
5396 /* Append the new key_cert to the (possibly empty) current list */
5397 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005398 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005399 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01005400 if( ssl->handshake != NULL )
5401 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01005402 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005403 else
5404 {
5405 last = ssl->key_cert;
5406 while( last->next != NULL )
5407 last = last->next;
5408 last->next = key_cert;
5409 }
5410
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005411 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005412}
5413
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005414void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00005415 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00005416{
5417 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00005418 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005419 ssl->peer_cn = peer_cn;
5420}
5421
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005422int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005423 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00005424{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005425 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
5426
5427 if( key_cert == NULL )
5428 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5429
5430 key_cert->cert = own_cert;
5431 key_cert->key = pk_key;
5432
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00005433 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005434}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005435#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005436
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005437#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005438int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5439 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005440{
Paul Bakker6db455e2013-09-18 17:29:31 +02005441 if( psk == NULL || psk_identity == NULL )
5442 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5443
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005444 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005445 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5446
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00005447 if( ssl->psk != NULL || ssl->psk_identity != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02005448 {
5449 polarssl_free( ssl->psk );
5450 polarssl_free( ssl->psk_identity );
5451 }
5452
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00005453 if( ( ssl->psk = polarssl_malloc( psk_len ) ) == NULL ||
5454 ( ssl->psk_identity = polarssl_malloc( psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05005455 {
5456 polarssl_free( ssl->psk );
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00005457 ssl->psk = NULL;
Mansour Moufidf81088b2015-02-17 13:10:21 -05005458 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5459 }
Paul Bakker6db455e2013-09-18 17:29:31 +02005460
Paul Bakker6db455e2013-09-18 17:29:31 +02005461 ssl->psk_len = psk_len;
5462 ssl->psk_identity_len = psk_identity_len;
5463
Paul Bakker6db455e2013-09-18 17:29:31 +02005464 memcpy( ssl->psk, psk, ssl->psk_len );
5465 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
5466
5467 return( 0 );
5468}
5469
5470void ssl_set_psk_cb( ssl_context *ssl,
5471 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5472 size_t),
5473 void *p_psk )
5474{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005475 ssl->f_psk = f_psk;
5476 ssl->p_psk = p_psk;
Paul Bakker43b7e352011-01-18 15:27:19 +00005477}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005478#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005479
Paul Bakker48916f92012-09-16 19:57:18 +00005480#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005481int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
5482{
5483 int ret;
5484
Paul Bakker48916f92012-09-16 19:57:18 +00005485 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005486 {
5487 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5488 return( ret );
5489 }
5490
Paul Bakker48916f92012-09-16 19:57:18 +00005491 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005492 {
5493 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5494 return( ret );
5495 }
5496
5497 return( 0 );
5498}
5499
Paul Bakker1b57b062011-01-06 15:48:19 +00005500int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5501{
5502 int ret;
5503
Paul Bakker66d5d072014-06-17 16:39:18 +02005504 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005505 {
5506 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5507 return( ret );
5508 }
5509
Paul Bakker66d5d072014-06-17 16:39:18 +02005510 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005511 {
5512 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5513 return( ret );
5514 }
5515
5516 return( 0 );
5517}
Paul Bakker48916f92012-09-16 19:57:18 +00005518#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005519
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005520#if defined(POLARSSL_SSL_SET_CURVES)
5521/*
5522 * Set the allowed elliptic curves
5523 */
5524void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5525{
5526 ssl->curve_list = curve_list;
5527}
5528#endif
5529
Paul Bakker0be444a2013-08-27 21:55:01 +02005530#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005531int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005532{
5533 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005534 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005535
5536 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005537
5538 if( ssl->hostname_len + 1 == 0 )
5539 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5540
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005541 ssl->hostname = polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005542
Paul Bakkerb15b8512012-01-13 13:44:06 +00005543 if( ssl->hostname == NULL )
5544 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5545
Paul Bakker3c2122f2013-06-24 19:03:14 +02005546 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005547 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005548
Paul Bakker40ea7de2009-05-03 10:18:48 +00005549 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005550
5551 return( 0 );
5552}
5553
Paul Bakker5701cdc2012-09-27 21:49:42 +00005554void ssl_set_sni( ssl_context *ssl,
5555 int (*f_sni)(void *, ssl_context *,
5556 const unsigned char *, size_t),
5557 void *p_sni )
5558{
5559 ssl->f_sni = f_sni;
5560 ssl->p_sni = p_sni;
5561}
Paul Bakker0be444a2013-08-27 21:55:01 +02005562#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005563
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005564#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005565int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005566{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005567 size_t cur_len, tot_len;
5568 const char **p;
5569
5570 /*
5571 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5572 * truncated". Check lengths now rather than later.
5573 */
5574 tot_len = 0;
5575 for( p = protos; *p != NULL; p++ )
5576 {
5577 cur_len = strlen( *p );
5578 tot_len += cur_len;
5579
5580 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5581 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5582 }
5583
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005584 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005585
5586 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005587}
5588
5589const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5590{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005591 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005592}
5593#endif /* POLARSSL_SSL_ALPN */
5594
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005595static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005596{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005597 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005598 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005599 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005600 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005601 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005602
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005603#if defined(POLARSSL_SSL_PROTO_DTLS)
5604 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5605 minor < SSL_MINOR_VERSION_2 )
5606 {
5607 return( -1 );
5608 }
5609#else
5610 ((void) ssl);
5611#endif
5612
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005613 return( 0 );
5614}
5615
5616int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5617{
5618 if( ssl_check_version( ssl, major, minor ) != 0 )
5619 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5620
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005621 ssl->max_major_ver = major;
5622 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005623
5624 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005625}
5626
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005627int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005628{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005629 if( ssl_check_version( ssl, major, minor ) != 0 )
5630 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005631
5632 ssl->min_major_ver = major;
5633 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005634
5635 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005636}
5637
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02005638#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
5639void ssl_set_fallback( ssl_context *ssl, char fallback )
5640{
5641 ssl->fallback = fallback;
5642}
5643#endif
5644
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01005645#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
5646void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
5647{
5648 ssl->encrypt_then_mac = etm;
5649}
5650#endif
5651
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02005652#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
5653void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
5654{
5655 ssl->extended_ms = ems;
5656}
5657#endif
5658
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01005659void ssl_set_arc4_support( ssl_context *ssl, char arc4 )
5660{
5661 ssl->arc4_disabled = arc4;
5662}
5663
Paul Bakker05decb22013-08-15 13:33:48 +02005664#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005665int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5666{
Paul Bakker77e257e2013-12-16 15:29:52 +01005667 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005668 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005669 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005670 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005671 }
5672
5673 ssl->mfl_code = mfl_code;
5674
5675 return( 0 );
5676}
Paul Bakker05decb22013-08-15 13:33:48 +02005677#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005678
Paul Bakker1f2bc622013-08-15 13:45:55 +02005679#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005680int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005681{
Paul Bakker8c1ede62013-07-19 14:14:37 +02005682 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005683
5684 return( 0 );
5685}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005686#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005687
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005688#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
5689void ssl_set_cbc_record_splitting( ssl_context *ssl, char split )
5690{
5691 ssl->split_done = split;
5692}
5693#endif
5694
Paul Bakker48916f92012-09-16 19:57:18 +00005695void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5696{
5697 ssl->allow_legacy_renegotiation = allow_legacy;
5698}
5699
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005700#if defined(POLARSSL_SSL_RENEGOTIATION)
5701void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5702{
5703 ssl->disable_renegotiation = renegotiation;
5704}
5705
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005706void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5707{
5708 ssl->renego_max_records = max_records;
5709}
5710
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01005711void ssl_set_renegotiation_period( ssl_context *ssl,
5712 const unsigned char period[8] )
5713{
5714 memcpy( ssl->renego_period, period, 8 );
5715}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005716#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00005717
Paul Bakkera503a632013-08-14 13:48:06 +02005718#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005719int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5720{
5721 ssl->session_tickets = use_tickets;
5722
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005723#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005724 if( ssl->endpoint == SSL_IS_CLIENT )
5725 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005726#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005727
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01005728 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
5729 return( 0 );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005730
5731 if( ssl->f_rng == NULL )
5732 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5733
5734 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005735}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005736
5737void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5738{
5739 ssl->ticket_lifetime = lifetime;
5740}
Paul Bakkera503a632013-08-14 13:48:06 +02005741#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005742
Paul Bakker5121ce52009-01-03 21:22:43 +00005743/*
5744 * SSL get accessors
5745 */
5746size_t ssl_get_bytes_avail( const ssl_context *ssl )
5747{
5748 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5749}
5750
Paul Bakkerff60ee62010-03-16 21:09:09 +00005751int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005752{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00005753 if( ssl->session != NULL )
5754 return( ssl->session->verify_result );
5755
5756 if( ssl->session_negotiate != NULL )
5757 return( ssl->session_negotiate->verify_result );
5758
5759 return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005760}
5761
Paul Bakkere3166ce2011-01-27 17:40:50 +00005762const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005763{
Paul Bakker926c8e42013-03-06 10:23:34 +01005764 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005765 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005766
Paul Bakkere3166ce2011-01-27 17:40:50 +00005767 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005768}
5769
Paul Bakker43ca69c2011-01-15 17:35:19 +00005770const char *ssl_get_version( const ssl_context *ssl )
5771{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005772#if defined(POLARSSL_SSL_PROTO_DTLS)
5773 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5774 {
5775 switch( ssl->minor_ver )
5776 {
5777 case SSL_MINOR_VERSION_2:
5778 return( "DTLSv1.0" );
5779
5780 case SSL_MINOR_VERSION_3:
5781 return( "DTLSv1.2" );
5782
5783 default:
5784 return( "unknown (DTLS)" );
5785 }
5786 }
5787#endif
5788
Paul Bakker43ca69c2011-01-15 17:35:19 +00005789 switch( ssl->minor_ver )
5790 {
5791 case SSL_MINOR_VERSION_0:
5792 return( "SSLv3.0" );
5793
5794 case SSL_MINOR_VERSION_1:
5795 return( "TLSv1.0" );
5796
5797 case SSL_MINOR_VERSION_2:
5798 return( "TLSv1.1" );
5799
Paul Bakker1ef83d62012-04-11 12:09:53 +00005800 case SSL_MINOR_VERSION_3:
5801 return( "TLSv1.2" );
5802
Paul Bakker43ca69c2011-01-15 17:35:19 +00005803 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005804 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005805 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005806}
5807
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005808int ssl_get_record_expansion( const ssl_context *ssl )
5809{
5810 int transform_expansion;
5811 const ssl_transform *transform = ssl->transform_out;
5812
5813#if defined(POLARSSL_ZLIB_SUPPORT)
5814 if( ssl->session_out->compression != SSL_COMPRESS_NULL )
5815 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
5816#endif
5817
5818 if( transform == NULL )
5819 return( ssl_hdr_len( ssl ) );
5820
5821 switch( cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
5822 {
5823 case POLARSSL_MODE_GCM:
5824 case POLARSSL_MODE_CCM:
5825 case POLARSSL_MODE_STREAM:
5826 transform_expansion = transform->minlen;
5827 break;
5828
5829 case POLARSSL_MODE_CBC:
5830 transform_expansion = transform->maclen
5831 + cipher_get_block_size( &transform->cipher_ctx_enc );
5832 break;
5833
5834 default:
5835 SSL_DEBUG_MSG( 0, ( "should never happen" ) );
5836 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
5837 }
5838
5839 return( ssl_hdr_len( ssl ) + transform_expansion );
5840}
5841
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005842#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005843const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005844{
5845 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005846 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005847
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005848 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005849}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005850#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005851
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005852#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005853int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5854{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005855 if( ssl == NULL ||
5856 dst == NULL ||
5857 ssl->session == NULL ||
5858 ssl->endpoint != SSL_IS_CLIENT )
5859 {
5860 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5861 }
5862
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005863 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005864}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005865#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005866
Paul Bakker5121ce52009-01-03 21:22:43 +00005867/*
Paul Bakker1961b702013-01-25 14:49:24 +01005868 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005869 */
Paul Bakker1961b702013-01-25 14:49:24 +01005870int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005871{
Paul Bakker40e46942009-01-03 21:51:57 +00005872 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005873
Paul Bakker40e46942009-01-03 21:51:57 +00005874#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005875 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005876 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005877#endif
Paul Bakker40e46942009-01-03 21:51:57 +00005878#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005879 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005880 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005881#endif
5882
Paul Bakker1961b702013-01-25 14:49:24 +01005883 return( ret );
5884}
5885
5886/*
5887 * Perform the SSL handshake
5888 */
5889int ssl_handshake( ssl_context *ssl )
5890{
5891 int ret = 0;
5892
5893 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5894
5895 while( ssl->state != SSL_HANDSHAKE_OVER )
5896 {
5897 ret = ssl_handshake_step( ssl );
5898
5899 if( ret != 0 )
5900 break;
5901 }
5902
Paul Bakker5121ce52009-01-03 21:22:43 +00005903 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5904
5905 return( ret );
5906}
5907
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005908#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005909#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005910/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005911 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005912 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005913static int ssl_write_hello_request( ssl_context *ssl )
5914{
5915 int ret;
5916
5917 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5918
5919 ssl->out_msglen = 4;
5920 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5921 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5922
5923 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5924 {
5925 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5926 return( ret );
5927 }
5928
5929 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5930
5931 return( 0 );
5932}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005933#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005934
5935/*
5936 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005937 * - any side: calling ssl_renegotiate(),
5938 * - client: receiving a HelloRequest during ssl_read(),
5939 * - server: receiving any handshake message on server during ssl_read() after
5940 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005941 * If the handshake doesn't complete due to waiting for I/O, it will continue
5942 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005943 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005944static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005945{
5946 int ret;
5947
5948 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5949
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005950 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5951 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005952
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005953 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5954 * the ServerHello will have message_seq = 1" */
5955#if defined(POLARSSL_SSL_PROTO_DTLS)
5956 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00005957 ssl->renego_status == SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005958 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005959 if( ssl->endpoint == SSL_IS_SERVER )
5960 ssl->handshake->out_msg_seq = 1;
5961 else
5962 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005963 }
5964#endif
5965
Paul Bakker48916f92012-09-16 19:57:18 +00005966 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00005967 ssl->renego_status = SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00005968
Paul Bakker48916f92012-09-16 19:57:18 +00005969 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5970 {
5971 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5972 return( ret );
5973 }
5974
5975 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5976
5977 return( 0 );
5978}
5979
5980/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005981 * Renegotiate current connection on client,
5982 * or request renegotiation on server
5983 */
5984int ssl_renegotiate( ssl_context *ssl )
5985{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005986 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005987
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005988#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005989 /* On server, just send the request */
5990 if( ssl->endpoint == SSL_IS_SERVER )
5991 {
5992 if( ssl->state != SSL_HANDSHAKE_OVER )
5993 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5994
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00005995 ssl->renego_status = SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005996
5997 /* Did we already try/start sending HelloRequest? */
5998 if( ssl->out_left != 0 )
5999 return( ssl_flush_output( ssl ) );
6000
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006001 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006002 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006003#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006004
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006005#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006006 /*
6007 * On client, either start the renegotiation process or,
6008 * if already in progress, continue the handshake
6009 */
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00006010 if( ssl->renego_status != SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006011 {
6012 if( ssl->state != SSL_HANDSHAKE_OVER )
6013 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
6014
6015 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
6016 {
6017 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
6018 return( ret );
6019 }
6020 }
6021 else
6022 {
6023 if( ( ret = ssl_handshake( ssl ) ) != 0 )
6024 {
6025 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6026 return( ret );
6027 }
6028 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006029#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006030
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006031 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006032}
6033
6034/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006035 * Check record counters and renegotiate if they're above the limit.
6036 */
6037static int ssl_check_ctr_renegotiate( ssl_context *ssl )
6038{
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006039 if( ssl->state != SSL_HANDSHAKE_OVER ||
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00006040 ssl->renego_status == SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006041 ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED )
6042 {
6043 return( 0 );
6044 }
6045
6046 // TODO: adapt for DTLS
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006047 if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 &&
6048 memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006049 {
6050 return( 0 );
6051 }
6052
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006053 SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006054 return( ssl_renegotiate( ssl ) );
6055}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006056#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006057
6058/*
6059 * Receive application data decrypted from the SSL layer
6060 */
Paul Bakker23986e52011-04-24 08:57:21 +00006061int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006062{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006063 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00006064 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00006065
6066 SSL_DEBUG_MSG( 2, ( "=> read" ) );
6067
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006068#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006069 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006070 {
6071 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
6072 return( ret );
6073
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006074 if( ssl->handshake != NULL &&
6075 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
6076 {
6077 if( ( ret = ssl_resend( ssl ) ) != 0 )
6078 return( ret );
6079 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006080 }
6081#endif
6082
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006083#if defined(POLARSSL_SSL_RENEGOTIATION)
6084 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
6085 {
6086 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
6087 return( ret );
6088 }
6089#endif
6090
Paul Bakker5121ce52009-01-03 21:22:43 +00006091 if( ssl->state != SSL_HANDSHAKE_OVER )
6092 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006093 ret = ssl_handshake( ssl );
6094 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
6095 {
6096 record_read = 1;
6097 }
6098 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006099 {
6100 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6101 return( ret );
6102 }
6103 }
6104
6105 if( ssl->in_offt == NULL )
6106 {
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02006107#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006108 /* Start timer if not already running */
6109 if( ssl->time_limit == 0 )
6110 ssl_set_timer( ssl, ssl->read_timeout );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02006111#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006112
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006113 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00006114 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006115 if( ( ret = ssl_read_record( ssl ) ) != 0 )
6116 {
6117 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
6118 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00006119
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006120 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
6121 return( ret );
6122 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006123 }
6124
6125 if( ssl->in_msglen == 0 &&
6126 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
6127 {
6128 /*
6129 * OpenSSL sends empty messages to randomize the IV
6130 */
6131 if( ( ret = ssl_read_record( ssl ) ) != 0 )
6132 {
Paul Bakker831a7552011-05-18 13:32:51 +00006133 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
6134 return( 0 );
6135
Paul Bakker5121ce52009-01-03 21:22:43 +00006136 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
6137 return( ret );
6138 }
6139 }
6140
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006141#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006142 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
6143 {
6144 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
6145
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006146#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006147 if( ssl->endpoint == SSL_IS_CLIENT &&
6148 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00006149 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006150 {
6151 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006152
6153 /* With DTLS, drop the packet (probably from last handshake) */
6154#if defined(POLARSSL_SSL_PROTO_DTLS)
6155 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6156 return( POLARSSL_ERR_NET_WANT_READ );
6157#endif
6158 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6159 }
6160
6161 if( ssl->endpoint == SSL_IS_SERVER &&
6162 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
6163 {
6164 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
6165
6166 /* With DTLS, drop the packet (probably from last handshake) */
6167#if defined(POLARSSL_SSL_PROTO_DTLS)
6168 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6169 return( POLARSSL_ERR_NET_WANT_READ );
6170#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006171 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6172 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006173#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006174
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006175 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
6176 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006177 ssl->allow_legacy_renegotiation ==
6178 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006179 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006180 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006181
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006182#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006183 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006184 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006185 /*
6186 * SSLv3 does not have a "no_renegotiation" alert
6187 */
6188 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
6189 return( ret );
6190 }
6191 else
Paul Bakker9af723c2014-05-01 13:03:14 +02006192#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006193#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
6194 defined(POLARSSL_SSL_PROTO_TLS1_2)
6195 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006196 {
6197 if( ( ret = ssl_send_alert_message( ssl,
6198 SSL_ALERT_LEVEL_WARNING,
6199 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
6200 {
6201 return( ret );
6202 }
Paul Bakker48916f92012-09-16 19:57:18 +00006203 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006204 else
Paul Bakker9af723c2014-05-01 13:03:14 +02006205#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
6206 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02006207 {
6208 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006209 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02006210 }
Paul Bakker48916f92012-09-16 19:57:18 +00006211 }
6212 else
6213 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006214 /* DTLS clients need to know renego is server-initiated */
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02006215#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006216 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
6217 ssl->endpoint == SSL_IS_CLIENT )
6218 {
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00006219 ssl->renego_status = SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006220 }
6221#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006222 ret = ssl_start_renegotiation( ssl );
6223 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
6224 {
6225 record_read = 1;
6226 }
6227 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006228 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006229 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00006230 return( ret );
6231 }
Paul Bakker48916f92012-09-16 19:57:18 +00006232 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006233
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006234 /* If a non-handshake record was read during renego, fallthrough,
6235 * else tell the user they should call ssl_read() again */
6236 if( ! record_read )
6237 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00006238 }
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00006239 else if( ssl->renego_status == SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006240 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006241
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006242 if( ssl->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006243 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006244 if( ++ssl->renego_records_seen > ssl->renego_max_records )
6245 {
6246 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
6247 "but not honored by client" ) );
6248 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6249 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006250 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006251 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006252#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006253
6254 /* Fatal and closure alerts handled by ssl_read_record() */
6255 if( ssl->in_msgtype == SSL_MSG_ALERT )
6256 {
6257 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
6258 return( POLARSSL_ERR_NET_WANT_READ );
6259 }
6260
6261 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00006262 {
6263 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00006264 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006265 }
6266
6267 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006268
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02006269#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02006270 /* We're going to return something now, cancel timer,
6271 * except if handshake (renegotiation) is in progress */
6272 if( ssl->state == SSL_HANDSHAKE_OVER )
6273 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006274
6275 /* If we requested renego but received AppData, resend HelloRequest.
6276 * Do it now, after setting in_offt, to avoid taking this branch
6277 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00006278#if defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006279 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00006280 ssl->renego_status == SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006281 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006282 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006283 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006284 SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006285 return( ret );
6286 }
6287 }
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00006288#endif /* POLARSSL_SSL_SRV_C && POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02006289#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006290 }
6291
6292 n = ( len < ssl->in_msglen )
6293 ? len : ssl->in_msglen;
6294
6295 memcpy( buf, ssl->in_offt, n );
6296 ssl->in_msglen -= n;
6297
6298 if( ssl->in_msglen == 0 )
6299 /* all bytes consumed */
6300 ssl->in_offt = NULL;
6301 else
6302 /* more data available */
6303 ssl->in_offt += n;
6304
6305 SSL_DEBUG_MSG( 2, ( "<= read" ) );
6306
Paul Bakker23986e52011-04-24 08:57:21 +00006307 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006308}
6309
6310/*
6311 * Send application data to be encrypted by the SSL layer
6312 */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006313#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
6314static int ssl_write_real( ssl_context *ssl, const unsigned char *buf, size_t len )
6315#else
Paul Bakker23986e52011-04-24 08:57:21 +00006316int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006317#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006318{
Paul Bakker23986e52011-04-24 08:57:21 +00006319 int ret;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006320#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
6321 unsigned int max_len;
6322#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006323
6324 SSL_DEBUG_MSG( 2, ( "=> write" ) );
6325
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006326#if defined(POLARSSL_SSL_RENEGOTIATION)
6327 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
6328 {
6329 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
6330 return( ret );
6331 }
6332#endif
6333
Paul Bakker5121ce52009-01-03 21:22:43 +00006334 if( ssl->state != SSL_HANDSHAKE_OVER )
6335 {
6336 if( ( ret = ssl_handshake( ssl ) ) != 0 )
6337 {
6338 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6339 return( ret );
6340 }
6341 }
6342
Paul Bakker05decb22013-08-15 13:33:48 +02006343#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02006344 /*
6345 * Assume mfl_code is correct since it was checked when set
6346 */
6347 max_len = mfl_code_to_length[ssl->mfl_code];
6348
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006349 /*
Paul Bakker05decb22013-08-15 13:33:48 +02006350 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006351 */
6352 if( ssl->session_out != NULL &&
6353 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6354 {
6355 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6356 }
6357
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006358 if( len > max_len )
6359 {
6360#if defined(POLARSSL_SSL_PROTO_DTLS)
6361 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6362 {
6363 SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
6364 "maximum fragment length: %d > %d",
6365 len, max_len ) );
6366 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
6367 }
6368 else
6369#endif
6370 len = max_len;
6371 }
6372#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker887bd502011-06-08 13:10:54 +00006373
Paul Bakker5121ce52009-01-03 21:22:43 +00006374 if( ssl->out_left != 0 )
6375 {
6376 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
6377 {
6378 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
6379 return( ret );
6380 }
6381 }
Paul Bakker887bd502011-06-08 13:10:54 +00006382 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00006383 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006384 ssl->out_msglen = len;
Paul Bakker887bd502011-06-08 13:10:54 +00006385 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006386 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00006387
6388 if( ( ret = ssl_write_record( ssl ) ) != 0 )
6389 {
6390 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
6391 return( ret );
6392 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006393 }
6394
6395 SSL_DEBUG_MSG( 2, ( "<= write" ) );
6396
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006397 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006398}
6399
6400/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006401 * Write application data, doing 1/n-1 splitting if necessary.
6402 *
6403 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006404 * then the caller will call us again with the same arguments, so
6405 * remember wether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006406 */
6407#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
6408int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
6409{
6410 int ret;
6411
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006412 if( ssl->split_done == SSL_CBC_RECORD_SPLITTING_DISABLED ||
6413 len <= 1 ||
6414 ssl->minor_ver > SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006415 cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
6416 != POLARSSL_MODE_CBC )
6417 {
6418 return( ssl_write_real( ssl, buf, len ) );
6419 }
6420
6421 if( ssl->split_done == 0 )
6422 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006423 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006424 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006425 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006426 }
6427
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006428 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
6429 return( ret );
6430 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006431
6432 return( ret + 1 );
6433}
6434#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */
6435
6436/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006437 * Notify the peer that the connection is being closed
6438 */
6439int ssl_close_notify( ssl_context *ssl )
6440{
6441 int ret;
6442
6443 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
6444
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006445 if( ssl->out_left != 0 )
6446 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006447
6448 if( ssl->state == SSL_HANDSHAKE_OVER )
6449 {
Paul Bakker48916f92012-09-16 19:57:18 +00006450 if( ( ret = ssl_send_alert_message( ssl,
6451 SSL_ALERT_LEVEL_WARNING,
6452 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006453 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006454 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006455 return( ret );
6456 }
6457 }
6458
6459 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
6460
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006461 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006462}
6463
Paul Bakker48916f92012-09-16 19:57:18 +00006464void ssl_transform_free( ssl_transform *transform )
6465{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006466 if( transform == NULL )
6467 return;
6468
Paul Bakker48916f92012-09-16 19:57:18 +00006469#if defined(POLARSSL_ZLIB_SUPPORT)
6470 deflateEnd( &transform->ctx_deflate );
6471 inflateEnd( &transform->ctx_inflate );
6472#endif
6473
Paul Bakker84bbeb52014-07-01 14:53:22 +02006474 cipher_free( &transform->cipher_ctx_enc );
6475 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02006476
Paul Bakker84bbeb52014-07-01 14:53:22 +02006477 md_free( &transform->md_ctx_enc );
6478 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02006479
Paul Bakker34617722014-06-13 17:20:13 +02006480 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006481}
6482
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006483#if defined(POLARSSL_X509_CRT_PARSE_C)
6484static void ssl_key_cert_free( ssl_key_cert *key_cert )
6485{
6486 ssl_key_cert *cur = key_cert, *next;
6487
6488 while( cur != NULL )
6489 {
6490 next = cur->next;
6491
6492 if( cur->key_own_alloc )
6493 {
6494 pk_free( cur->key );
6495 polarssl_free( cur->key );
6496 }
6497 polarssl_free( cur );
6498
6499 cur = next;
6500 }
6501}
6502#endif /* POLARSSL_X509_CRT_PARSE_C */
6503
Paul Bakker48916f92012-09-16 19:57:18 +00006504void ssl_handshake_free( ssl_handshake_params *handshake )
6505{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006506 if( handshake == NULL )
6507 return;
6508
Paul Bakker48916f92012-09-16 19:57:18 +00006509#if defined(POLARSSL_DHM_C)
6510 dhm_free( &handshake->dhm_ctx );
6511#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006512#if defined(POLARSSL_ECDH_C)
6513 ecdh_free( &handshake->ecdh_ctx );
6514#endif
6515
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006516#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02006517 /* explicit void pointer cast for buggy MS compiler */
6518 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006519#endif
6520
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006521#if defined(POLARSSL_X509_CRT_PARSE_C) && \
6522 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
6523 /*
6524 * Free only the linked list wrapper, not the keys themselves
6525 * since the belong to the SNI callback
6526 */
6527 if( handshake->sni_key_cert != NULL )
6528 {
6529 ssl_key_cert *cur = handshake->sni_key_cert, *next;
6530
6531 while( cur != NULL )
6532 {
6533 next = cur->next;
6534 polarssl_free( cur );
6535 cur = next;
6536 }
6537 }
Paul Bakker9af723c2014-05-01 13:03:14 +02006538#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006539
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006540#if defined(POLARSSL_SSL_PROTO_DTLS)
6541 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02006542 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02006543 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006544#endif
6545
Paul Bakker34617722014-06-13 17:20:13 +02006546 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006547}
6548
6549void ssl_session_free( ssl_session *session )
6550{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006551 if( session == NULL )
6552 return;
6553
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006554#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00006555 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00006556 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006557 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02006558 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00006559 }
Paul Bakkered27a042013-04-18 22:46:23 +02006560#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006561
Paul Bakkera503a632013-08-14 13:48:06 +02006562#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006563 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02006564#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006565
Paul Bakker34617722014-06-13 17:20:13 +02006566 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006567}
6568
Paul Bakker5121ce52009-01-03 21:22:43 +00006569/*
6570 * Free an SSL context
6571 */
6572void ssl_free( ssl_context *ssl )
6573{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006574 if( ssl == NULL )
6575 return;
6576
Paul Bakker5121ce52009-01-03 21:22:43 +00006577 SSL_DEBUG_MSG( 2, ( "=> free" ) );
6578
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006579 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006580 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006581 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
6582 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006583 }
6584
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006585 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006586 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006587 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
6588 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006589 }
6590
Paul Bakker16770332013-10-11 09:59:44 +02006591#if defined(POLARSSL_ZLIB_SUPPORT)
6592 if( ssl->compress_buf != NULL )
6593 {
Paul Bakker34617722014-06-13 17:20:13 +02006594 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02006595 polarssl_free( ssl->compress_buf );
6596 }
6597#endif
6598
Paul Bakker40e46942009-01-03 21:51:57 +00006599#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006600 mpi_free( &ssl->dhm_P );
6601 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006602#endif
6603
Paul Bakker48916f92012-09-16 19:57:18 +00006604 if( ssl->transform )
6605 {
6606 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02006607 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006608 }
6609
6610 if( ssl->handshake )
6611 {
6612 ssl_handshake_free( ssl->handshake );
6613 ssl_transform_free( ssl->transform_negotiate );
6614 ssl_session_free( ssl->session_negotiate );
6615
Paul Bakker6e339b52013-07-03 13:37:05 +02006616 polarssl_free( ssl->handshake );
6617 polarssl_free( ssl->transform_negotiate );
6618 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00006619 }
6620
Paul Bakkerc0463502013-02-14 11:19:38 +01006621 if( ssl->session )
6622 {
6623 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02006624 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006625 }
6626
Paul Bakkera503a632013-08-14 13:48:06 +02006627#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02006628 if( ssl->ticket_keys )
6629 {
6630 ssl_ticket_keys_free( ssl->ticket_keys );
6631 polarssl_free( ssl->ticket_keys );
6632 }
Paul Bakkera503a632013-08-14 13:48:06 +02006633#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006634
Paul Bakker0be444a2013-08-27 21:55:01 +02006635#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02006636 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006637 {
Paul Bakker34617722014-06-13 17:20:13 +02006638 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006639 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006640 ssl->hostname_len = 0;
6641 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006642#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006643
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006644#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006645 if( ssl->psk != NULL )
6646 {
Paul Bakker34617722014-06-13 17:20:13 +02006647 polarssl_zeroize( ssl->psk, ssl->psk_len );
6648 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006649 polarssl_free( ssl->psk );
6650 polarssl_free( ssl->psk_identity );
6651 ssl->psk_len = 0;
6652 ssl->psk_identity_len = 0;
6653 }
6654#endif
6655
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006656#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006657 ssl_key_cert_free( ssl->key_cert );
6658#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006659
Paul Bakker05ef8352012-05-08 09:17:57 +00006660#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6661 if( ssl_hw_record_finish != NULL )
6662 {
6663 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6664 ssl_hw_record_finish( ssl );
6665 }
6666#endif
6667
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006668#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006669 polarssl_free( ssl->cli_id );
6670#endif
6671
Paul Bakker5121ce52009-01-03 21:22:43 +00006672 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006673
Paul Bakker86f04f42013-02-14 11:20:09 +01006674 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006675 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006676}
6677
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006678#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006679/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006680 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006681 */
6682unsigned char ssl_sig_from_pk( pk_context *pk )
6683{
6684#if defined(POLARSSL_RSA_C)
6685 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6686 return( SSL_SIG_RSA );
6687#endif
6688#if defined(POLARSSL_ECDSA_C)
6689 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6690 return( SSL_SIG_ECDSA );
6691#endif
6692 return( SSL_SIG_ANON );
6693}
6694
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006695pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6696{
6697 switch( sig )
6698 {
6699#if defined(POLARSSL_RSA_C)
6700 case SSL_SIG_RSA:
6701 return( POLARSSL_PK_RSA );
6702#endif
6703#if defined(POLARSSL_ECDSA_C)
6704 case SSL_SIG_ECDSA:
6705 return( POLARSSL_PK_ECDSA );
6706#endif
6707 default:
6708 return( POLARSSL_PK_NONE );
6709 }
6710}
Paul Bakker9af723c2014-05-01 13:03:14 +02006711#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006712
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006713/*
6714 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6715 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006716md_type_t ssl_md_alg_from_hash( unsigned char hash )
6717{
6718 switch( hash )
6719 {
6720#if defined(POLARSSL_MD5_C)
6721 case SSL_HASH_MD5:
6722 return( POLARSSL_MD_MD5 );
6723#endif
6724#if defined(POLARSSL_SHA1_C)
6725 case SSL_HASH_SHA1:
6726 return( POLARSSL_MD_SHA1 );
6727#endif
6728#if defined(POLARSSL_SHA256_C)
6729 case SSL_HASH_SHA224:
6730 return( POLARSSL_MD_SHA224 );
6731 case SSL_HASH_SHA256:
6732 return( POLARSSL_MD_SHA256 );
6733#endif
6734#if defined(POLARSSL_SHA512_C)
6735 case SSL_HASH_SHA384:
6736 return( POLARSSL_MD_SHA384 );
6737 case SSL_HASH_SHA512:
6738 return( POLARSSL_MD_SHA512 );
6739#endif
6740 default:
6741 return( POLARSSL_MD_NONE );
6742 }
6743}
6744
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006745#if defined(POLARSSL_SSL_SET_CURVES)
6746/*
6747 * Check is a curve proposed by the peer is in our list.
6748 * Return 1 if we're willing to use it, 0 otherwise.
6749 */
6750int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6751{
6752 const ecp_group_id *gid;
6753
6754 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6755 if( *gid == grp_id )
6756 return( 1 );
6757
6758 return( 0 );
6759}
Paul Bakker9af723c2014-05-01 13:03:14 +02006760#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006761
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006762#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006763int ssl_check_cert_usage( const x509_crt *cert,
6764 const ssl_ciphersuite_t *ciphersuite,
6765 int cert_endpoint )
6766{
6767#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6768 int usage = 0;
6769#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006770#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6771 const char *ext_oid;
6772 size_t ext_len;
6773#endif
6774
6775#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6776 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6777 ((void) cert);
6778 ((void) cert_endpoint);
6779#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006780
6781#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6782 if( cert_endpoint == SSL_IS_SERVER )
6783 {
6784 /* Server part of the key exchange */
6785 switch( ciphersuite->key_exchange )
6786 {
6787 case POLARSSL_KEY_EXCHANGE_RSA:
6788 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6789 usage = KU_KEY_ENCIPHERMENT;
6790 break;
6791
6792 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6793 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6794 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6795 usage = KU_DIGITAL_SIGNATURE;
6796 break;
6797
6798 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6799 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6800 usage = KU_KEY_AGREEMENT;
6801 break;
6802
6803 /* Don't use default: we want warnings when adding new values */
6804 case POLARSSL_KEY_EXCHANGE_NONE:
6805 case POLARSSL_KEY_EXCHANGE_PSK:
6806 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6807 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6808 usage = 0;
6809 }
6810 }
6811 else
6812 {
6813 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6814 usage = KU_DIGITAL_SIGNATURE;
6815 }
6816
6817 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6818 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006819#else
6820 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006821#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6822
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006823#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6824 if( cert_endpoint == SSL_IS_SERVER )
6825 {
6826 ext_oid = OID_SERVER_AUTH;
6827 ext_len = OID_SIZE( OID_SERVER_AUTH );
6828 }
6829 else
6830 {
6831 ext_oid = OID_CLIENT_AUTH;
6832 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6833 }
6834
6835 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6836 return( -1 );
6837#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6838
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006839 return( 0 );
6840}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006841#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006842
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006843/*
6844 * Convert version numbers to/from wire format
6845 * and, for DTLS, to/from TLS equivalent.
6846 *
6847 * For TLS this is the identity.
6848 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6849 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6850 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6851 */
6852void ssl_write_version( int major, int minor, int transport,
6853 unsigned char ver[2] )
6854{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006855#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006856 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006857 {
6858 if( minor == SSL_MINOR_VERSION_2 )
6859 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6860
6861 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6862 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6863 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006864 else
6865#else
6866 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006867#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006868 {
6869 ver[0] = (unsigned char) major;
6870 ver[1] = (unsigned char) minor;
6871 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006872}
6873
6874void ssl_read_version( int *major, int *minor, int transport,
6875 const unsigned char ver[2] )
6876{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006877#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006878 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006879 {
6880 *major = 255 - ver[0] + 2;
6881 *minor = 255 - ver[1] + 1;
6882
6883 if( *minor == SSL_MINOR_VERSION_1 )
6884 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6885 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006886 else
6887#else
6888 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006889#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006890 {
6891 *major = ver[0];
6892 *minor = ver[1];
6893 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006894}
6895
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006896#endif /* POLARSSL_SSL_TLS_C */