blob: a20432b9c3ce978dd09f86ae1a4ec0284fb48eb8 [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];
279
280 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000281 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000282
283 hs = ( slen + 1 ) / 2;
284 S1 = secret;
285 S2 = secret + slen - hs;
286
287 nb = strlen( label );
288 memcpy( tmp + 20, label, nb );
289 memcpy( tmp + 20 + nb, random, rlen );
290 nb += rlen;
291
292 /*
293 * First compute P_md5(secret,label+random)[0..dlen]
294 */
295 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
296
297 for( i = 0; i < dlen; i += 16 )
298 {
299 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
300 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
301
302 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
303
304 for( j = 0; j < k; j++ )
305 dstbuf[i + j] = h_i[j];
306 }
307
308 /*
309 * XOR out with P_sha1(secret,label+random)[0..dlen]
310 */
311 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
312
313 for( i = 0; i < dlen; i += 20 )
314 {
315 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
316 sha1_hmac( S2, hs, tmp, 20, tmp );
317
318 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
319
320 for( j = 0; j < k; j++ )
321 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
322 }
323
Paul Bakker34617722014-06-13 17:20:13 +0200324 polarssl_zeroize( tmp, sizeof( tmp ) );
325 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000326
327 return( 0 );
328}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200329#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000330
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200331#if defined(POLARSSL_SSL_PROTO_TLS1_2)
332#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200333static int tls_prf_sha256( const unsigned char *secret, size_t slen,
334 const char *label,
335 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000336 unsigned char *dstbuf, size_t dlen )
337{
338 size_t nb;
339 size_t i, j, k;
340 unsigned char tmp[128];
341 unsigned char h_i[32];
342
343 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
344 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
345
346 nb = strlen( label );
347 memcpy( tmp + 32, label, nb );
348 memcpy( tmp + 32 + nb, random, rlen );
349 nb += rlen;
350
351 /*
352 * Compute P_<hash>(secret, label + random)[0..dlen]
353 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200354 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000355
356 for( i = 0; i < dlen; i += 32 )
357 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200358 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
359 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000360
361 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
362
363 for( j = 0; j < k; j++ )
364 dstbuf[i + j] = h_i[j];
365 }
366
Paul Bakker34617722014-06-13 17:20:13 +0200367 polarssl_zeroize( tmp, sizeof( tmp ) );
368 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000369
370 return( 0 );
371}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200372#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000373
Paul Bakker9e36f042013-06-30 14:34:05 +0200374#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200375static int tls_prf_sha384( const unsigned char *secret, size_t slen,
376 const char *label,
377 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000378 unsigned char *dstbuf, size_t dlen )
379{
380 size_t nb;
381 size_t i, j, k;
382 unsigned char tmp[128];
383 unsigned char h_i[48];
384
385 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
386 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
387
388 nb = strlen( label );
389 memcpy( tmp + 48, label, nb );
390 memcpy( tmp + 48 + nb, random, rlen );
391 nb += rlen;
392
393 /*
394 * Compute P_<hash>(secret, label + random)[0..dlen]
395 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200396 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000397
398 for( i = 0; i < dlen; i += 48 )
399 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200400 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
401 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000402
403 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
404
405 for( j = 0; j < k; j++ )
406 dstbuf[i + j] = h_i[j];
407 }
408
Paul Bakker34617722014-06-13 17:20:13 +0200409 polarssl_zeroize( tmp, sizeof( tmp ) );
410 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000411
412 return( 0 );
413}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200414#endif /* POLARSSL_SHA512_C */
415#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000416
Paul Bakker66d5d072014-06-17 16:39:18 +0200417static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200418
419#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
420 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200421static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200422#endif
Paul Bakker380da532012-04-18 16:10:25 +0000423
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200424#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200425static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
426static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200427#endif
428
429#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200430static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
431static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200432#endif
433
434#if defined(POLARSSL_SSL_PROTO_TLS1_2)
435#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200436static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
437static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
438static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200439#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100440
Paul Bakker9e36f042013-06-30 14:34:05 +0200441#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200442static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
443static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
444static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100445#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200446#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000447
Paul Bakker5121ce52009-01-03 21:22:43 +0000448int ssl_derive_keys( ssl_context *ssl )
449{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200450 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000452 unsigned char keyblk[256];
453 unsigned char *key1;
454 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100455 unsigned char *mac_enc;
456 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200457 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100458 const cipher_info_t *cipher_info;
459 const md_info_t *md_info;
460
Paul Bakker48916f92012-09-16 19:57:18 +0000461 ssl_session *session = ssl->session_negotiate;
462 ssl_transform *transform = ssl->transform_negotiate;
463 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
465 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
466
Paul Bakker68884e32013-01-07 18:20:04 +0100467 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
468 if( cipher_info == NULL )
469 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200470 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100471 transform->ciphersuite_info->cipher ) );
472 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
473 }
474
475 md_info = md_info_from_type( transform->ciphersuite_info->mac );
476 if( md_info == NULL )
477 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200478 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100479 transform->ciphersuite_info->mac ) );
480 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
481 }
482
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000484 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000485 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200486#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000487 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000488 {
Paul Bakker48916f92012-09-16 19:57:18 +0000489 handshake->tls_prf = ssl3_prf;
490 handshake->calc_verify = ssl_calc_verify_ssl;
491 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000492 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200493 else
494#endif
495#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
496 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000497 {
Paul Bakker48916f92012-09-16 19:57:18 +0000498 handshake->tls_prf = tls1_prf;
499 handshake->calc_verify = ssl_calc_verify_tls;
500 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000501 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200502 else
503#endif
504#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200505#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200506 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
507 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000508 {
Paul Bakker48916f92012-09-16 19:57:18 +0000509 handshake->tls_prf = tls_prf_sha384;
510 handshake->calc_verify = ssl_calc_verify_tls_sha384;
511 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000512 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000513 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200514#endif
515#if defined(POLARSSL_SHA256_C)
516 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000517 {
Paul Bakker48916f92012-09-16 19:57:18 +0000518 handshake->tls_prf = tls_prf_sha256;
519 handshake->calc_verify = ssl_calc_verify_tls_sha256;
520 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000521 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200522 else
523#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200524#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200525 {
526 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200527 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200528 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000529
530 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 * SSLv3:
532 * master =
533 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
534 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
535 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200536 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200537 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 * master = PRF( premaster, "master secret", randbytes )[0..47]
539 */
Paul Bakker0a597072012-09-25 21:55:46 +0000540 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000541 {
Paul Bakker48916f92012-09-16 19:57:18 +0000542 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
543 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000544
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200545#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
546 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200547 {
548 unsigned char session_hash[48];
549 size_t hash_len;
550
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200551 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200552
553 ssl->handshake->calc_verify( ssl, session_hash );
554
555#if defined(POLARSSL_SSL_PROTO_TLS1_2)
556 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
557 {
558#if defined(POLARSSL_SHA512_C)
559 if( ssl->transform_negotiate->ciphersuite_info->mac ==
560 POLARSSL_MD_SHA384 )
561 {
562 hash_len = 48;
563 }
564 else
565#endif
566 hash_len = 32;
567 }
568 else
569#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
570 hash_len = 36;
571
572 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
573
574 handshake->tls_prf( handshake->premaster, handshake->pmslen,
575 "extended master secret",
576 session_hash, hash_len, session->master, 48 );
577
578 }
579 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200580#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000581 handshake->tls_prf( handshake->premaster, handshake->pmslen,
582 "master secret",
583 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200585
Paul Bakker34617722014-06-13 17:20:13 +0200586 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000587 }
588 else
589 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
590
591 /*
592 * Swap the client and server random values.
593 */
Paul Bakker48916f92012-09-16 19:57:18 +0000594 memcpy( tmp, handshake->randbytes, 64 );
595 memcpy( handshake->randbytes, tmp + 32, 32 );
596 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200597 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
599 /*
600 * SSLv3:
601 * key block =
602 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
603 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
604 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
605 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
606 * ...
607 *
608 * TLSv1:
609 * key block = PRF( master, "key expansion", randbytes )
610 */
Paul Bakker48916f92012-09-16 19:57:18 +0000611 handshake->tls_prf( session->master, 48, "key expansion",
612 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000613
Paul Bakker48916f92012-09-16 19:57:18 +0000614 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
615 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
616 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
617 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000618 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
619
Paul Bakker34617722014-06-13 17:20:13 +0200620 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
622 /*
623 * Determine the appropriate key, IV and MAC length.
624 */
Paul Bakker68884e32013-01-07 18:20:04 +0100625
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200626 transform->keylen = cipher_info->key_length / 8;
627
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200628 if( cipher_info->mode == POLARSSL_MODE_GCM ||
629 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000630 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200631 transform->maclen = 0;
632
Paul Bakker68884e32013-01-07 18:20:04 +0100633 transform->ivlen = 12;
634 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200635
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200636 /* Minimum length is expicit IV + tag */
637 transform->minlen = transform->ivlen - transform->fixed_ivlen
638 + ( transform->ciphersuite_info->flags &
639 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100640 }
641 else
642 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200643 int ret;
644
645 /* Initialize HMAC contexts */
646 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
647 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100648 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200649 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
650 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100651 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000652
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200653 /* Get MAC length */
654 transform->maclen = md_get_size( md_info );
655
656#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
657 /*
658 * If HMAC is to be truncated, we shall keep the leftmost bytes,
659 * (rfc 6066 page 13 or rfc 2104 section 4),
660 * so we only need to adjust the length here.
661 */
662 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
663 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
664#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
665
666 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100667 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000668
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200669 /* Minimum length */
670 if( cipher_info->mode == POLARSSL_MODE_STREAM )
671 transform->minlen = transform->maclen;
672 else
Paul Bakker68884e32013-01-07 18:20:04 +0100673 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200674 /*
675 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100676 * 1. if EtM is in use: one block plus MAC
677 * otherwise: * first multiple of blocklen greater than maclen
678 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200679 */
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100680#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
681 if( session->encrypt_then_mac == SSL_ETM_ENABLED )
682 {
683 transform->minlen = transform->maclen
684 + cipher_info->block_size;
685 }
686 else
687#endif
688 {
689 transform->minlen = transform->maclen
690 + cipher_info->block_size
691 - transform->maclen % cipher_info->block_size;
692 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200693
694#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
695 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
696 ssl->minor_ver == SSL_MINOR_VERSION_1 )
697 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100698 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200699#endif
700#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
701 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
702 ssl->minor_ver == SSL_MINOR_VERSION_3 )
703 {
704 transform->minlen += transform->ivlen;
705 }
706 else
707#endif
708 {
709 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
710 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
711 }
Paul Bakker68884e32013-01-07 18:20:04 +0100712 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000713 }
714
715 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000716 transform->keylen, transform->minlen, transform->ivlen,
717 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000718
719 /*
720 * Finally setup the cipher contexts, IVs and MAC secrets.
721 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100722#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000723 if( ssl->endpoint == SSL_IS_CLIENT )
724 {
Paul Bakker48916f92012-09-16 19:57:18 +0000725 key1 = keyblk + transform->maclen * 2;
726 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000727
Paul Bakker68884e32013-01-07 18:20:04 +0100728 mac_enc = keyblk;
729 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000730
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000731 /*
732 * This is not used in TLS v1.1.
733 */
Paul Bakker48916f92012-09-16 19:57:18 +0000734 iv_copy_len = ( transform->fixed_ivlen ) ?
735 transform->fixed_ivlen : transform->ivlen;
736 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
737 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000738 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000739 }
740 else
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100741#endif /* POLARSSL_SSL_CLI_C */
742#if defined(POLARSSL_SSL_SRV_C)
743 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000744 {
Paul Bakker48916f92012-09-16 19:57:18 +0000745 key1 = keyblk + transform->maclen * 2 + transform->keylen;
746 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000747
Paul Bakker68884e32013-01-07 18:20:04 +0100748 mac_enc = keyblk + transform->maclen;
749 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000750
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000751 /*
752 * This is not used in TLS v1.1.
753 */
Paul Bakker48916f92012-09-16 19:57:18 +0000754 iv_copy_len = ( transform->fixed_ivlen ) ?
755 transform->fixed_ivlen : transform->ivlen;
756 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
757 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000758 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000759 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100760 else
761#endif /* POLARSSL_SSL_SRV_C */
762 {
763 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
764 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
765 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000766
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200767#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100768 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
769 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100770 if( transform->maclen > sizeof transform->mac_enc )
771 {
772 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200773 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100774 }
775
Paul Bakker68884e32013-01-07 18:20:04 +0100776 memcpy( transform->mac_enc, mac_enc, transform->maclen );
777 memcpy( transform->mac_dec, mac_dec, transform->maclen );
778 }
779 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200780#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200781#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
782 defined(POLARSSL_SSL_PROTO_TLS1_2)
783 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100784 {
785 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
786 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
787 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200788 else
789#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200790 {
791 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200792 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200793 }
Paul Bakker68884e32013-01-07 18:20:04 +0100794
Paul Bakker05ef8352012-05-08 09:17:57 +0000795#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200796 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000797 {
798 int ret = 0;
799
800 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
801
Paul Bakker07eb38b2012-12-19 14:42:06 +0100802 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
803 transform->iv_enc, transform->iv_dec,
804 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100805 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100806 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000807 {
808 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200809 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000810 }
811 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200812#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000813
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200814 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
815 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000816 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200817 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
818 return( ret );
819 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200820
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200821 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
822 cipher_info ) ) != 0 )
823 {
824 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
825 return( ret );
826 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200827
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200828 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
829 cipher_info->key_length,
830 POLARSSL_ENCRYPT ) ) != 0 )
831 {
832 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
833 return( ret );
834 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200835
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200836 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
837 cipher_info->key_length,
838 POLARSSL_DECRYPT ) ) != 0 )
839 {
840 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
841 return( ret );
842 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200843
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200844#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200845 if( cipher_info->mode == POLARSSL_MODE_CBC )
846 {
847 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
848 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200849 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200850 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
851 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200852 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200853
854 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
855 POLARSSL_PADDING_NONE ) ) != 0 )
856 {
857 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
858 return( ret );
859 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000860 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200861#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000862
Paul Bakker34617722014-06-13 17:20:13 +0200863 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000864
Paul Bakker2770fbd2012-07-03 13:30:23 +0000865#if defined(POLARSSL_ZLIB_SUPPORT)
866 // Initialize compression
867 //
Paul Bakker48916f92012-09-16 19:57:18 +0000868 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000869 {
Paul Bakker16770332013-10-11 09:59:44 +0200870 if( ssl->compress_buf == NULL )
871 {
872 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
873 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
874 if( ssl->compress_buf == NULL )
875 {
876 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
877 SSL_BUFFER_LEN ) );
878 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
879 }
880 }
881
Paul Bakker2770fbd2012-07-03 13:30:23 +0000882 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
883
Paul Bakker48916f92012-09-16 19:57:18 +0000884 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
885 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000886
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200887 if( deflateInit( &transform->ctx_deflate,
888 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000889 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000890 {
891 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
892 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
893 }
894 }
895#endif /* POLARSSL_ZLIB_SUPPORT */
896
Paul Bakker5121ce52009-01-03 21:22:43 +0000897 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
898
899 return( 0 );
900}
901
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200902#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000903void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000904{
905 md5_context md5;
906 sha1_context sha1;
907 unsigned char pad_1[48];
908 unsigned char pad_2[48];
909
Paul Bakker380da532012-04-18 16:10:25 +0000910 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000911
Paul Bakker48916f92012-09-16 19:57:18 +0000912 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
913 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000914
Paul Bakker380da532012-04-18 16:10:25 +0000915 memset( pad_1, 0x36, 48 );
916 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000917
Paul Bakker48916f92012-09-16 19:57:18 +0000918 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000919 md5_update( &md5, pad_1, 48 );
920 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000921
Paul Bakker380da532012-04-18 16:10:25 +0000922 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000923 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000924 md5_update( &md5, pad_2, 48 );
925 md5_update( &md5, hash, 16 );
926 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000927
Paul Bakker48916f92012-09-16 19:57:18 +0000928 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000929 sha1_update( &sha1, pad_1, 40 );
930 sha1_finish( &sha1, hash + 16 );
931
932 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000933 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000934 sha1_update( &sha1, pad_2, 40 );
935 sha1_update( &sha1, hash + 16, 20 );
936 sha1_finish( &sha1, hash + 16 );
937
938 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
939 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
940
Paul Bakker5b4af392014-06-26 12:09:34 +0200941 md5_free( &md5 );
942 sha1_free( &sha1 );
943
Paul Bakker380da532012-04-18 16:10:25 +0000944 return;
945}
Paul Bakker9af723c2014-05-01 13:03:14 +0200946#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000947
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200948#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000949void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
950{
951 md5_context md5;
952 sha1_context sha1;
953
954 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
955
Paul Bakker48916f92012-09-16 19:57:18 +0000956 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
957 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000958
Paul Bakker48916f92012-09-16 19:57:18 +0000959 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000960 sha1_finish( &sha1, hash + 16 );
961
962 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
963 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
964
Paul Bakker5b4af392014-06-26 12:09:34 +0200965 md5_free( &md5 );
966 sha1_free( &sha1 );
967
Paul Bakker380da532012-04-18 16:10:25 +0000968 return;
969}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200970#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000971
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200972#if defined(POLARSSL_SSL_PROTO_TLS1_2)
973#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000974void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
975{
Paul Bakker9e36f042013-06-30 14:34:05 +0200976 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000977
978 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
979
Paul Bakker9e36f042013-06-30 14:34:05 +0200980 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
981 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000982
983 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
984 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
985
Paul Bakker5b4af392014-06-26 12:09:34 +0200986 sha256_free( &sha256 );
987
Paul Bakker380da532012-04-18 16:10:25 +0000988 return;
989}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200990#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000991
Paul Bakker9e36f042013-06-30 14:34:05 +0200992#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000993void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
994{
Paul Bakker9e36f042013-06-30 14:34:05 +0200995 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000996
997 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
998
Paul Bakker9e36f042013-06-30 14:34:05 +0200999 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
1000 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
Paul Bakkerca4ab492012-04-18 14:23:57 +00001002 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001003 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1004
Paul Bakker5b4af392014-06-26 12:09:34 +02001005 sha512_free( &sha512 );
1006
Paul Bakker5121ce52009-01-03 21:22:43 +00001007 return;
1008}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001009#endif /* POLARSSL_SHA512_C */
1010#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001011
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001012#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001013int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
1014{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001015 unsigned char *p = ssl->handshake->premaster;
1016 unsigned char *end = p + sizeof( ssl->handshake->premaster );
1017
1018 /*
1019 * PMS = struct {
1020 * opaque other_secret<0..2^16-1>;
1021 * opaque psk<0..2^16-1>;
1022 * };
1023 * with "other_secret" depending on the particular key exchange
1024 */
1025#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
1026 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
1027 {
1028 if( end - p < 2 + (int) ssl->psk_len )
1029 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1030
1031 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1032 *(p++) = (unsigned char)( ssl->psk_len );
1033 p += ssl->psk_len;
1034 }
1035 else
1036#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001037#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1038 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1039 {
1040 /*
1041 * other_secret already set by the ClientKeyExchange message,
1042 * and is 48 bytes long
1043 */
1044 *p++ = 0;
1045 *p++ = 48;
1046 p += 48;
1047 }
1048 else
1049#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001050#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1051 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1052 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001053 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02001054 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001055
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001056 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001057 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001058 p + 2, &len,
1059 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001060 {
1061 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
1062 return( ret );
1063 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001064 *(p++) = (unsigned char)( len >> 8 );
1065 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001066 p += len;
1067
1068 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1069 }
1070 else
1071#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1072#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1073 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1074 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001075 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001076 size_t zlen;
1077
1078 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001079 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001080 ssl->f_rng, ssl->p_rng ) ) != 0 )
1081 {
1082 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1083 return( ret );
1084 }
1085
1086 *(p++) = (unsigned char)( zlen >> 8 );
1087 *(p++) = (unsigned char)( zlen );
1088 p += zlen;
1089
1090 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1091 }
1092 else
1093#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1094 {
1095 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001096 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001097 }
1098
1099 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001100 if( end - p < 2 + (int) ssl->psk_len )
1101 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1102
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001103 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1104 *(p++) = (unsigned char)( ssl->psk_len );
1105 memcpy( p, ssl->psk, ssl->psk_len );
1106 p += ssl->psk_len;
1107
1108 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1109
1110 return( 0 );
1111}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001112#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001113
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001114#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001115/*
1116 * SSLv3.0 MAC functions
1117 */
Paul Bakker68884e32013-01-07 18:20:04 +01001118static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1119 unsigned char *buf, size_t len,
1120 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001121{
1122 unsigned char header[11];
1123 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001124 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001125 int md_size = md_get_size( md_ctx->md_info );
1126 int md_type = md_get_type( md_ctx->md_info );
1127
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001128 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001129 if( md_type == POLARSSL_MD_MD5 )
1130 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001131 else
Paul Bakker68884e32013-01-07 18:20:04 +01001132 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001133
1134 memcpy( header, ctr, 8 );
1135 header[ 8] = (unsigned char) type;
1136 header[ 9] = (unsigned char)( len >> 8 );
1137 header[10] = (unsigned char)( len );
1138
Paul Bakker68884e32013-01-07 18:20:04 +01001139 memset( padding, 0x36, padlen );
1140 md_starts( md_ctx );
1141 md_update( md_ctx, secret, md_size );
1142 md_update( md_ctx, padding, padlen );
1143 md_update( md_ctx, header, 11 );
1144 md_update( md_ctx, buf, len );
1145 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001146
Paul Bakker68884e32013-01-07 18:20:04 +01001147 memset( padding, 0x5C, padlen );
1148 md_starts( md_ctx );
1149 md_update( md_ctx, secret, md_size );
1150 md_update( md_ctx, padding, padlen );
1151 md_update( md_ctx, buf + len, md_size );
1152 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001153}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001154#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001155
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001156#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1157 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1158 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1159#define POLARSSL_SOME_MODES_USE_MAC
1160#endif
1161
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001162/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001163 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001164 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001165static int ssl_encrypt_buf( ssl_context *ssl )
1166{
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001167 cipher_mode_t mode;
1168 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001169
1170 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1171
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001172 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1173 {
1174 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1175 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1176 }
1177
1178 mode = cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
1179
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001180 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1181 ssl->out_msg, ssl->out_msglen );
1182
Paul Bakker5121ce52009-01-03 21:22:43 +00001183 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001184 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001185 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001186#if defined(POLARSSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001187 if( mode == POLARSSL_MODE_STREAM ||
1188 ( mode == POLARSSL_MODE_CBC
1189#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1190 && ssl->session_out->encrypt_then_mac == SSL_ETM_DISABLED
1191#endif
1192 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001194#if defined(POLARSSL_SSL_PROTO_SSL3)
1195 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1196 {
1197 ssl_mac( &ssl->transform_out->md_ctx_enc,
1198 ssl->transform_out->mac_enc,
1199 ssl->out_msg, ssl->out_msglen,
1200 ssl->out_ctr, ssl->out_msgtype );
1201 }
1202 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001203#endif
1204#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001205 defined(POLARSSL_SSL_PROTO_TLS1_2)
1206 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1207 {
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001208 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1209 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1210 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001211 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1212 ssl->out_msg, ssl->out_msglen );
1213 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1214 ssl->out_msg + ssl->out_msglen );
1215 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1216 }
1217 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001218#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001219 {
1220 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001221 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001222 }
1223
1224 SSL_DEBUG_BUF( 4, "computed mac",
1225 ssl->out_msg + ssl->out_msglen,
1226 ssl->transform_out->maclen );
1227
1228 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001229 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001230 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001231#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001232
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001233 /*
1234 * Encrypt
1235 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001236#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001237 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001238 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001239 int ret;
1240 size_t olen = 0;
1241
Paul Bakker5121ce52009-01-03 21:22:43 +00001242 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1243 "including %d bytes of padding",
1244 ssl->out_msglen, 0 ) );
1245
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001246 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001247 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001248 ssl->transform_out->ivlen,
1249 ssl->out_msg, ssl->out_msglen,
1250 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001251 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001252 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001253 return( ret );
1254 }
1255
1256 if( ssl->out_msglen != olen )
1257 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001258 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001259 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001260 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 }
Paul Bakker68884e32013-01-07 18:20:04 +01001262 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001263#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001264#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1265 if( mode == POLARSSL_MODE_GCM ||
1266 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001267 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001268 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001269 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001270 unsigned char *enc_msg;
1271 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001272 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1273 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001274
Paul Bakkerca4ab492012-04-18 14:23:57 +00001275 memcpy( add_data, ssl->out_ctr, 8 );
1276 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001277 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1278 ssl->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001279 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1280 add_data[12] = ssl->out_msglen & 0xFF;
1281
1282 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1283 add_data, 13 );
1284
Paul Bakker68884e32013-01-07 18:20:04 +01001285 /*
1286 * Generate IV
1287 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001288#if defined(POLARSSL_SSL_AEAD_RANDOM_IV)
Paul Bakker68884e32013-01-07 18:20:04 +01001289 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001290 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1291 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001292 if( ret != 0 )
1293 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001294
Paul Bakker68884e32013-01-07 18:20:04 +01001295 memcpy( ssl->out_iv,
1296 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1297 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001298#else
1299 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1300 {
1301 /* Reminder if we ever add an AEAD mode with a different size */
1302 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1303 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1304 }
1305
1306 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1307 ssl->out_ctr, 8 );
1308 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1309#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00001310
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001311 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001312 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001313
Paul Bakker68884e32013-01-07 18:20:04 +01001314 /*
1315 * Fix pointer positions and message length with added IV
1316 */
1317 enc_msg = ssl->out_msg;
1318 enc_msglen = ssl->out_msglen;
1319 ssl->out_msglen += ssl->transform_out->ivlen -
1320 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001321
Paul Bakker68884e32013-01-07 18:20:04 +01001322 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1323 "including %d bytes of padding",
1324 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001325
Paul Bakker68884e32013-01-07 18:20:04 +01001326 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001327 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001328 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001329 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1330 ssl->transform_out->iv_enc,
1331 ssl->transform_out->ivlen,
1332 add_data, 13,
1333 enc_msg, enc_msglen,
1334 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001335 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001336 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001337 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001338 return( ret );
1339 }
1340
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001341 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001342 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001343 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001344 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001345 }
1346
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001347 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001348 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001349
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001350 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001351 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001352 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001353#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001354#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1355 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001356 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001358 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001359 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001360 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001361
Paul Bakker48916f92012-09-16 19:57:18 +00001362 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1363 ssl->transform_out->ivlen;
1364 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001365 padlen = 0;
1366
1367 for( i = 0; i <= padlen; i++ )
1368 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1369
1370 ssl->out_msglen += padlen + 1;
1371
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001372 enc_msglen = ssl->out_msglen;
1373 enc_msg = ssl->out_msg;
1374
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001375#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001376 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001377 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1378 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001379 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001380 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001381 {
1382 /*
1383 * Generate IV
1384 */
Paul Bakker48916f92012-09-16 19:57:18 +00001385 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1386 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001387 if( ret != 0 )
1388 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001389
Paul Bakker92be97b2013-01-02 17:30:03 +01001390 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001391 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001392
1393 /*
1394 * Fix pointer positions and message length with added IV
1395 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001396 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001397 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001398 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001399 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001400#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001401
Paul Bakker5121ce52009-01-03 21:22:43 +00001402 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001403 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001404 ssl->out_msglen, ssl->transform_out->ivlen,
1405 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001407 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001408 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001409 ssl->transform_out->ivlen,
1410 enc_msg, enc_msglen,
1411 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001412 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001413 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001414 return( ret );
1415 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001416
Paul Bakkercca5b812013-08-31 17:40:26 +02001417 if( enc_msglen != olen )
1418 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001419 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001420 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001421 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001422
1423#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001424 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1425 {
1426 /*
1427 * Save IV in SSL3 and TLS1
1428 */
1429 memcpy( ssl->transform_out->iv_enc,
1430 ssl->transform_out->cipher_ctx_enc.iv,
1431 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001432 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001433#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001434
1435#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001436 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001437 {
1438 /*
1439 * MAC(MAC_write_key, seq_num +
1440 * TLSCipherText.type +
1441 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001442 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001443 * IV + // except for TLS 1.0
1444 * ENC(content + padding + padding_length));
1445 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001446 unsigned char pseudo_hdr[13];
1447
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001448 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1449
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001450 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1451 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001452 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1453 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1454
1455 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001456
1457 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1458 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1459 ssl->out_iv, ssl->out_msglen );
1460 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1461 ssl->out_iv + ssl->out_msglen );
1462 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1463
1464 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001465 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001466 }
1467#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001468 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001469 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001470#endif /* POLARSSL_CIPHER_MODE_CBC &&
1471 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001472 {
1473 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001474 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001475 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001476
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001477 /* Make extra sure authentication was performed, exactly once */
1478 if( auth_done != 1 )
1479 {
1480 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1481 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1482 }
1483
Paul Bakker5121ce52009-01-03 21:22:43 +00001484 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1485
1486 return( 0 );
1487}
1488
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001489#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001490
Paul Bakker5121ce52009-01-03 21:22:43 +00001491static int ssl_decrypt_buf( ssl_context *ssl )
1492{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001493 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001494 cipher_mode_t mode;
1495 int auth_done = 0;
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001496#if defined(POLARSSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001497 size_t padlen = 0, correct = 1;
1498#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001499
1500 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1501
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001502 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1503 {
1504 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1505 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1506 }
1507
1508 mode = cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
1509
Paul Bakker48916f92012-09-16 19:57:18 +00001510 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001511 {
1512 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001513 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001514 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001515 }
1516
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001517#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001518 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001519 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001520 int ret;
1521 size_t olen = 0;
1522
Paul Bakker68884e32013-01-07 18:20:04 +01001523 padlen = 0;
1524
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001525 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001526 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001527 ssl->transform_in->ivlen,
1528 ssl->in_msg, ssl->in_msglen,
1529 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001530 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001531 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001532 return( ret );
1533 }
1534
1535 if( ssl->in_msglen != olen )
1536 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001537 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001538 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001539 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001540 }
Paul Bakker68884e32013-01-07 18:20:04 +01001541 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001542#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001543#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1544 if( mode == POLARSSL_MODE_GCM ||
1545 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001546 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001547 int ret;
1548 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001549 unsigned char *dec_msg;
1550 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001551 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001552 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1553 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001554 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1555 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001556
Manuel Pégourié-Gonnard06d75192015-02-11 14:54:11 +00001557 if( ssl->in_msglen < (size_t) explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001558 {
1559 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1560 "+ taglen (%d)", ssl->in_msglen,
1561 explicit_iv_len, taglen ) );
1562 return( POLARSSL_ERR_SSL_INVALID_MAC );
1563 }
1564 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1565
Paul Bakker68884e32013-01-07 18:20:04 +01001566 dec_msg = ssl->in_msg;
1567 dec_msg_result = ssl->in_msg;
1568 ssl->in_msglen = dec_msglen;
1569
1570 memcpy( add_data, ssl->in_ctr, 8 );
1571 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001572 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1573 ssl->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001574 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1575 add_data[12] = ssl->in_msglen & 0xFF;
1576
1577 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1578 add_data, 13 );
1579
1580 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1581 ssl->in_iv,
1582 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1583
1584 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1585 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001586 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001587
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001588 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001589 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001590 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001591 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1592 ssl->transform_in->iv_dec,
1593 ssl->transform_in->ivlen,
1594 add_data, 13,
1595 dec_msg, dec_msglen,
1596 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001597 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001598 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001599 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1600
1601 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1602 return( POLARSSL_ERR_SSL_INVALID_MAC );
1603
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001604 return( ret );
1605 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001606 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001607
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001608 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001609 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001610 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001611 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001612 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001613 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001614 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001615#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001616#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1617 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001618 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001619 {
Paul Bakker45829992013-01-03 14:52:21 +01001620 /*
1621 * Decrypt and check the padding
1622 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001623 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001624 unsigned char *dec_msg;
1625 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001626 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001627 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001628 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001629
Paul Bakker5121ce52009-01-03 21:22:43 +00001630 /*
Paul Bakker45829992013-01-03 14:52:21 +01001631 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001632 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001633#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001634 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1635 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001636#endif
Paul Bakker45829992013-01-03 14:52:21 +01001637
1638 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1639 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1640 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001641 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1642 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1643 ssl->transform_in->ivlen,
1644 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001645 return( POLARSSL_ERR_SSL_INVALID_MAC );
1646 }
1647
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001648 dec_msglen = ssl->in_msglen;
1649 dec_msg = ssl->in_msg;
1650 dec_msg_result = ssl->in_msg;
1651
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001652 /*
1653 * Authenticate before decrypt if enabled
1654 */
1655#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001656 if( ssl->session_in->encrypt_then_mac == SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001657 {
1658 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001659 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001660
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001661 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1662
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001663 dec_msglen -= ssl->transform_in->maclen;
1664 ssl->in_msglen -= ssl->transform_in->maclen;
1665
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001666 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1667 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1668 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1669 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1670
1671 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1672
1673 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001674 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1675 ssl->in_iv, ssl->in_msglen );
1676 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1677 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1678
1679 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1680 ssl->transform_in->maclen );
1681 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1682 ssl->transform_in->maclen );
1683
1684 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1685 ssl->transform_in->maclen ) != 0 )
1686 {
1687 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1688
1689 return( POLARSSL_ERR_SSL_INVALID_MAC );
1690 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001691 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001692 }
1693#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1694
1695 /*
1696 * Check length sanity
1697 */
1698 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1699 {
1700 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1701 ssl->in_msglen, ssl->transform_in->ivlen ) );
1702 return( POLARSSL_ERR_SSL_INVALID_MAC );
1703 }
1704
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001705#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001706 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001707 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001708 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001709 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001710 {
Paul Bakker48916f92012-09-16 19:57:18 +00001711 dec_msglen -= ssl->transform_in->ivlen;
1712 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001713
Paul Bakker48916f92012-09-16 19:57:18 +00001714 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001715 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001716 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001717#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001718
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001719 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001720 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001721 ssl->transform_in->ivlen,
1722 dec_msg, dec_msglen,
1723 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001724 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001725 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001726 return( ret );
1727 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001728
Paul Bakkercca5b812013-08-31 17:40:26 +02001729 if( dec_msglen != olen )
1730 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001731 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001732 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001733 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001734
1735#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001736 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1737 {
1738 /*
1739 * Save IV in SSL3 and TLS1
1740 */
1741 memcpy( ssl->transform_in->iv_dec,
1742 ssl->transform_in->cipher_ctx_dec.iv,
1743 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001744 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001745#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001746
1747 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001748
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001749 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001750 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001751 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001752#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001753 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1754 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001755#endif
Paul Bakker45829992013-01-03 14:52:21 +01001756 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001757 correct = 0;
1758 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001759
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001760#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001761 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1762 {
Paul Bakker48916f92012-09-16 19:57:18 +00001763 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001764 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001765#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001766 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1767 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001768 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001769#endif
Paul Bakker45829992013-01-03 14:52:21 +01001770 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001771 }
1772 }
1773 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001774#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001775#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1776 defined(POLARSSL_SSL_PROTO_TLS1_2)
1777 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001778 {
1779 /*
Paul Bakker45829992013-01-03 14:52:21 +01001780 * TLSv1+: always check the padding up to the first failure
1781 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001782 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001783 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001784 size_t padding_idx = ssl->in_msglen - padlen - 1;
1785
Paul Bakker956c9e02013-12-19 14:42:28 +01001786 /*
1787 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001788 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001789 *
Paul Bakker61885c72014-04-25 12:59:03 +02001790 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1791 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001792 *
1793 * In both cases we reset padding_idx to a safe value (0) to
1794 * prevent out-of-buffer reads.
1795 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001796 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001797 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1798 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001799
1800 padding_idx *= correct;
1801
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001802 for( i = 1; i <= 256; i++ )
1803 {
1804 real_count &= ( i <= padlen );
1805 pad_count += real_count *
1806 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1807 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001808
1809 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001810
Paul Bakkerd66f0702013-01-31 16:57:45 +01001811#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001812 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001813 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001814#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001815 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001816 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001817 else
1818#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1819 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001820 {
1821 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001822 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001823 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001824
1825 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001826 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001827 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001828#endif /* POLARSSL_CIPHER_MODE_CBC &&
1829 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001830 {
1831 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001832 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001833 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001834
1835 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1836 ssl->in_msg, ssl->in_msglen );
1837
1838 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001839 * Authenticate if not done yet.
1840 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001841 */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001842#if defined(POLARSSL_SOME_MODES_USE_MAC)
1843 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001844 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001845 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1846
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001847 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001848
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001849 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1850 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001851
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001852 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001853
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001854#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001855 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1856 {
1857 ssl_mac( &ssl->transform_in->md_ctx_dec,
1858 ssl->transform_in->mac_dec,
1859 ssl->in_msg, ssl->in_msglen,
1860 ssl->in_ctr, ssl->in_msgtype );
1861 }
1862 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001863#endif /* POLARSSL_SSL_PROTO_SSL3 */
1864#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001865 defined(POLARSSL_SSL_PROTO_TLS1_2)
1866 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1867 {
1868 /*
1869 * Process MAC and always update for padlen afterwards to make
1870 * total time independent of padlen
1871 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001872 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001873 *
1874 * Known timing attacks:
1875 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1876 *
1877 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1878 * correctly. (We round down instead of up, so -56 is the correct
1879 * value for our calculations instead of -55)
1880 */
1881 size_t j, extra_run = 0;
1882 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1883 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001884
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001885 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001886
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001887 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
1888 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
1889 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001890 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1891 ssl->in_msglen );
1892 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1893 ssl->in_msg + ssl->in_msglen );
1894 for( j = 0; j < extra_run; j++ )
1895 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001896
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001897 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1898 }
1899 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001900#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001901 POLARSSL_SSL_PROTO_TLS1_2 */
1902 {
1903 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001904 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001905 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001906
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001907 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1908 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1909 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001910
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001911 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001912 ssl->transform_in->maclen ) != 0 )
1913 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001914#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001915 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001916#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001917 correct = 0;
1918 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001919 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001920
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001921 /*
1922 * Finally check the correct flag
1923 */
1924 if( correct == 0 )
1925 return( POLARSSL_ERR_SSL_INVALID_MAC );
1926 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001927#endif /* POLARSSL_SOME_MODES_USE_MAC */
1928
1929 /* Make extra sure authentication was performed, exactly once */
1930 if( auth_done != 1 )
1931 {
1932 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1933 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1934 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001935
1936 if( ssl->in_msglen == 0 )
1937 {
1938 ssl->nb_zero++;
1939
1940 /*
1941 * Three or more empty messages may be a DoS attack
1942 * (excessive CPU consumption).
1943 */
1944 if( ssl->nb_zero > 3 )
1945 {
1946 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1947 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001948 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001949 }
1950 }
1951 else
1952 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001953
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001954#if defined(POLARSSL_SSL_PROTO_DTLS)
1955 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001956 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02001957 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001958 }
1959 else
1960#endif
1961 {
1962 for( i = 8; i > ssl_ep_len( ssl ); i-- )
1963 if( ++ssl->in_ctr[i - 1] != 0 )
1964 break;
1965
1966 /* The loop goes to its end iff the counter is wrapping */
1967 if( i == ssl_ep_len( ssl ) )
1968 {
1969 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1970 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1971 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001972 }
1973
Paul Bakker5121ce52009-01-03 21:22:43 +00001974 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1975
1976 return( 0 );
1977}
1978
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001979#undef MAC_NONE
1980#undef MAC_PLAINTEXT
1981#undef MAC_CIPHERTEXT
1982
Paul Bakker2770fbd2012-07-03 13:30:23 +00001983#if defined(POLARSSL_ZLIB_SUPPORT)
1984/*
1985 * Compression/decompression functions
1986 */
1987static int ssl_compress_buf( ssl_context *ssl )
1988{
1989 int ret;
1990 unsigned char *msg_post = ssl->out_msg;
1991 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001992 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001993
1994 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1995
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001996 if( len_pre == 0 )
1997 return( 0 );
1998
Paul Bakker2770fbd2012-07-03 13:30:23 +00001999 memcpy( msg_pre, ssl->out_msg, len_pre );
2000
2001 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
2002 ssl->out_msglen ) );
2003
2004 SSL_DEBUG_BUF( 4, "before compression: output payload",
2005 ssl->out_msg, ssl->out_msglen );
2006
Paul Bakker48916f92012-09-16 19:57:18 +00002007 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2008 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2009 ssl->transform_out->ctx_deflate.next_out = msg_post;
2010 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002011
Paul Bakker48916f92012-09-16 19:57:18 +00002012 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002013 if( ret != Z_OK )
2014 {
2015 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2016 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
2017 }
2018
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002019 ssl->out_msglen = SSL_BUFFER_LEN -
2020 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002021
Paul Bakker2770fbd2012-07-03 13:30:23 +00002022 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
2023 ssl->out_msglen ) );
2024
2025 SSL_DEBUG_BUF( 4, "after compression: output payload",
2026 ssl->out_msg, ssl->out_msglen );
2027
2028 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
2029
2030 return( 0 );
2031}
2032
2033static int ssl_decompress_buf( ssl_context *ssl )
2034{
2035 int ret;
2036 unsigned char *msg_post = ssl->in_msg;
2037 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002038 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002039
2040 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
2041
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002042 if( len_pre == 0 )
2043 return( 0 );
2044
Paul Bakker2770fbd2012-07-03 13:30:23 +00002045 memcpy( msg_pre, ssl->in_msg, len_pre );
2046
2047 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
2048 ssl->in_msglen ) );
2049
2050 SSL_DEBUG_BUF( 4, "before decompression: input payload",
2051 ssl->in_msg, ssl->in_msglen );
2052
Paul Bakker48916f92012-09-16 19:57:18 +00002053 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2054 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2055 ssl->transform_in->ctx_inflate.next_out = msg_post;
2056 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002057
Paul Bakker48916f92012-09-16 19:57:18 +00002058 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002059 if( ret != Z_OK )
2060 {
2061 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2062 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
2063 }
2064
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002065 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
2066 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002067
Paul Bakker2770fbd2012-07-03 13:30:23 +00002068 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
2069 ssl->in_msglen ) );
2070
2071 SSL_DEBUG_BUF( 4, "after decompression: input payload",
2072 ssl->in_msg, ssl->in_msglen );
2073
2074 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
2075
2076 return( 0 );
2077}
2078#endif /* POLARSSL_ZLIB_SUPPORT */
2079
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002080#if defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002081static int ssl_write_hello_request( ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002082
2083#if defined(POLARSSL_SSL_PROTO_DTLS)
2084static int ssl_resend_hello_request( ssl_context *ssl )
2085{
2086 /* If renegotiation is not enforced, retransmit until we would reach max
2087 * timeout if we were using the usual handshake doubling scheme */
2088 if( ssl->renego_max_records < 0 )
2089 {
2090 uint32_t ratio = ssl->hs_timeout_max / ssl->hs_timeout_min + 1;
2091 unsigned char doublings = 1;
2092
2093 while( ratio != 0 )
2094 {
2095 ++doublings;
2096 ratio >>= 1;
2097 }
2098
2099 if( ++ssl->renego_records_seen > doublings )
2100 {
2101 SSL_DEBUG_MSG( 0, ( "no longer retransmitting hello request" ) );
2102 return( 0 );
2103 }
2104 }
2105
2106 return( ssl_write_hello_request( ssl ) );
2107}
2108#endif
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002109#endif /* POLARSSL_SSL_SRV_C && POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002110
Paul Bakker5121ce52009-01-03 21:22:43 +00002111/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002112 * Fill the input message buffer by appending data to it.
2113 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002114 *
2115 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2116 * available (from this read and/or a previous one). Otherwise, an error code
2117 * is returned (possibly EOF or WANT_READ).
2118 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002119 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2120 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2121 * since we always read a whole datagram at once.
2122 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002123 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002124 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 */
Paul Bakker23986e52011-04-24 08:57:21 +00002126int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002127{
Paul Bakker23986e52011-04-24 08:57:21 +00002128 int ret;
2129 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002130
2131 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
2132
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002133 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2134 {
2135 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2136 "or ssl_set_bio_timeout()" ) );
2137 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2138 }
2139
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002140 if( nb_want > SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002141 {
2142 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2143 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2144 }
2145
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002146#if defined(POLARSSL_SSL_PROTO_DTLS)
2147 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002148 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002149 uint32_t timeout;
2150
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002151 /*
2152 * The point is, we need to always read a full datagram at once, so we
2153 * sometimes read more then requested, and handle the additional data.
2154 * It could be the rest of the current record (while fetching the
2155 * header) and/or some other records in the same datagram.
2156 */
2157
2158 /*
2159 * Move to the next record in the already read datagram if applicable
2160 */
2161 if( ssl->next_record_offset != 0 )
2162 {
2163 if( ssl->in_left < ssl->next_record_offset )
2164 {
2165 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2166 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2167 }
2168
2169 ssl->in_left -= ssl->next_record_offset;
2170
2171 if( ssl->in_left != 0 )
2172 {
2173 SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
2174 ssl->next_record_offset ) );
2175 memmove( ssl->in_hdr,
2176 ssl->in_hdr + ssl->next_record_offset,
2177 ssl->in_left );
2178 }
2179
2180 ssl->next_record_offset = 0;
2181 }
2182
Paul Bakker5121ce52009-01-03 21:22:43 +00002183 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2184 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002185
2186 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002187 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002188 */
2189 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002190 {
2191 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002192 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002193 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002194
2195 /*
2196 * A record can't be split accross datagrams. If we need to read but
2197 * are not at the beginning of a new record, the caller did something
2198 * wrong.
2199 */
2200 if( ssl->in_left != 0 )
2201 {
2202 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2203 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2204 }
2205
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002206 SSL_DEBUG_MSG( 3, ( "current timer: %u", ssl->time_limit ) );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002207
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002208 /*
2209 * Don't even try to read if time's out already.
2210 * This avoids by-passing the timer when repeatedly receiving messages
2211 * that will end up being dropped.
2212 */
2213 if( ssl_check_timer( ssl ) != 0 )
2214 ret = POLARSSL_ERR_NET_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002215 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002216 {
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002217 len = SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
2218
2219 if( ssl->state != SSL_HANDSHAKE_OVER )
2220 timeout = ssl->handshake->retransmit_timeout;
2221 else
2222 timeout = ssl->read_timeout;
2223
2224 SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
2225
2226 if( ssl->f_recv_timeout != NULL && timeout != 0 )
2227 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2228 timeout );
2229 else
2230 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2231
2232 SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
2233
2234 if( ret == 0 )
2235 return( POLARSSL_ERR_SSL_CONN_EOF );
2236 }
2237
2238 if( ret == POLARSSL_ERR_NET_TIMEOUT )
2239 {
2240 SSL_DEBUG_MSG( 2, ( "timeout" ) );
2241 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002242
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002243 if( ssl->state != SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002244 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002245 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2246 {
2247 SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
2248 return( POLARSSL_ERR_NET_TIMEOUT );
2249 }
2250
2251 if( ( ret = ssl_resend( ssl ) ) != 0 )
2252 {
2253 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2254 return( ret );
2255 }
2256
2257 return( POLARSSL_ERR_NET_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002258 }
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002259#if defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002260 else if( ssl->endpoint == SSL_IS_SERVER &&
2261 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
2262 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002263 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002264 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002265 SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002266 return( ret );
2267 }
2268
2269 return( POLARSSL_ERR_NET_WANT_READ );
2270 }
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002271#endif /* POLARSSL_SSL_SRV_C && POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002272 }
2273
Paul Bakker5121ce52009-01-03 21:22:43 +00002274 if( ret < 0 )
2275 return( ret );
2276
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002277 ssl->in_left = ret;
2278 }
2279 else
2280#endif
2281 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002282 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2283 ssl->in_left, nb_want ) );
2284
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002285 while( ssl->in_left < nb_want )
2286 {
2287 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002288 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr + ssl->in_left, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002289
2290 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2291 ssl->in_left, nb_want ) );
2292 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2293
2294 if( ret == 0 )
2295 return( POLARSSL_ERR_SSL_CONN_EOF );
2296
2297 if( ret < 0 )
2298 return( ret );
2299
2300 ssl->in_left += ret;
2301 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002302 }
2303
2304 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2305
2306 return( 0 );
2307}
2308
2309/*
2310 * Flush any data not yet written
2311 */
2312int ssl_flush_output( ssl_context *ssl )
2313{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002314 int ret;
2315 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002316
2317 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2318
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002319 if( ssl->f_send == NULL )
2320 {
2321 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2322 "or ssl_set_bio_timeout()" ) );
2323 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2324 }
2325
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002326 /* Avoid incrementing counter if data is flushed */
2327 if( ssl->out_left == 0 )
2328 {
2329 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2330 return( 0 );
2331 }
2332
Paul Bakker5121ce52009-01-03 21:22:43 +00002333 while( ssl->out_left > 0 )
2334 {
2335 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002336 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002337
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002338 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
2339 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002340 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002341
Paul Bakker5121ce52009-01-03 21:22:43 +00002342 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2343
2344 if( ret <= 0 )
2345 return( ret );
2346
2347 ssl->out_left -= ret;
2348 }
2349
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002350 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002351 if( ++ssl->out_ctr[i - 1] != 0 )
2352 break;
2353
2354 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002355 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002356 {
2357 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2358 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
2359 }
2360
Paul Bakker5121ce52009-01-03 21:22:43 +00002361 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2362
2363 return( 0 );
2364}
2365
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002366/*
2367 * Functions to handle the DTLS retransmission state machine
2368 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002369#if defined(POLARSSL_SSL_PROTO_DTLS)
2370/*
2371 * Append current handshake message to current outgoing flight
2372 */
2373static int ssl_flight_append( ssl_context *ssl )
2374{
2375 ssl_flight_item *msg;
2376
2377 /* Allocate space for current message */
2378 if( ( msg = polarssl_malloc( sizeof( ssl_flight_item ) ) ) == NULL )
2379 {
2380 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed",
2381 sizeof( ssl_flight_item ) ) );
2382 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2383 }
2384
2385 if( ( msg->p = polarssl_malloc( ssl->out_msglen ) ) == NULL )
2386 {
2387 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard6b875fc2014-10-17 14:02:33 +02002388 polarssl_free( msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002389 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2390 }
2391
2392 /* Copy current handshake message with headers */
2393 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2394 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002395 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002396 msg->next = NULL;
2397
2398 /* Append to the current flight */
2399 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002400 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002401 else
2402 {
2403 ssl_flight_item *cur = ssl->handshake->flight;
2404 while( cur->next != NULL )
2405 cur = cur->next;
2406 cur->next = msg;
2407 }
2408
2409 return( 0 );
2410}
2411
2412/*
2413 * Free the current flight of handshake messages
2414 */
2415static void ssl_flight_free( ssl_flight_item *flight )
2416{
2417 ssl_flight_item *cur = flight;
2418 ssl_flight_item *next;
2419
2420 while( cur != NULL )
2421 {
2422 next = cur->next;
2423
2424 polarssl_free( cur->p );
2425 polarssl_free( cur );
2426
2427 cur = next;
2428 }
2429}
2430
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002431#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2432static void ssl_dtls_replay_reset( ssl_context *ssl );
2433#endif
2434
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002435/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002436 * Swap transform_out and out_ctr with the alternative ones
2437 */
2438static void ssl_swap_epochs( ssl_context *ssl )
2439{
2440 ssl_transform *tmp_transform;
2441 unsigned char tmp_out_ctr[8];
2442
2443 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2444 {
2445 SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
2446 return;
2447 }
2448
2449 SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
2450
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002451 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002452 tmp_transform = ssl->transform_out;
2453 ssl->transform_out = ssl->handshake->alt_transform_out;
2454 ssl->handshake->alt_transform_out = tmp_transform;
2455
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002456 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002457 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2458 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2459 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002460
2461 /* Adjust to the newly activated transform */
2462 if( ssl->transform_out != NULL &&
2463 ssl->minor_ver >= SSL_MINOR_VERSION_2 )
2464 {
2465 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2466 ssl->transform_out->fixed_ivlen;
2467 }
2468 else
2469 ssl->out_msg = ssl->out_iv;
2470
2471#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2472 if( ssl_hw_record_activate != NULL )
2473 {
2474 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
2475 {
2476 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
2477 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
2478 }
2479 }
2480#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002481}
2482
2483/*
2484 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002485 *
2486 * Need to remember the current message in case flush_output returns
2487 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002488 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002489 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002490int ssl_resend( ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002491{
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002492 SSL_DEBUG_MSG( 2, ( "=> ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002493
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002494 if( ssl->handshake->retransmit_state != SSL_RETRANS_SENDING )
2495 {
2496 SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
2497
2498 ssl->handshake->cur_msg = ssl->handshake->flight;
2499 ssl_swap_epochs( ssl );
2500
2501 ssl->handshake->retransmit_state = SSL_RETRANS_SENDING;
2502 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002503
2504 while( ssl->handshake->cur_msg != NULL )
2505 {
2506 int ret;
2507 ssl_flight_item *cur = ssl->handshake->cur_msg;
2508
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002509 /* Swap epochs before sending Finished: we can't do it after
2510 * sending ChangeCipherSpec, in case write returns WANT_READ.
2511 * Must be done before copying, may change out_msg pointer */
2512 if( cur->type == SSL_MSG_HANDSHAKE &&
2513 cur->p[0] == SSL_HS_FINISHED )
2514 {
2515 ssl_swap_epochs( ssl );
2516 }
2517
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002518 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002519 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002520 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002521
2522 ssl->handshake->cur_msg = cur->next;
2523
2524 SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
2525
2526 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2527 {
2528 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2529 return( ret );
2530 }
2531 }
2532
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002533 if( ssl->state == SSL_HANDSHAKE_OVER )
2534 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2535 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002536 {
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002537 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002538 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2539 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002540
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002541 SSL_DEBUG_MSG( 2, ( "<= ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002542
2543 return( 0 );
2544}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002545
2546/*
2547 * To be called when the last message of an incoming flight is received.
2548 */
2549void ssl_recv_flight_completed( ssl_context *ssl )
2550{
2551 /* We won't need to resend that one any more */
2552 ssl_flight_free( ssl->handshake->flight );
2553 ssl->handshake->flight = NULL;
2554 ssl->handshake->cur_msg = NULL;
2555
2556 /* The next incoming flight will start with this msg_seq */
2557 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2558
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002559 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002560 ssl_set_timer( ssl, 0 );
2561
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002562 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2563 ssl->in_msg[0] == SSL_HS_FINISHED )
2564 {
2565 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2566 }
2567 else
2568 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
2569}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002570
2571/*
2572 * To be called when the last message of an outgoing flight is send.
2573 */
2574void ssl_send_flight_completed( ssl_context *ssl )
2575{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002576 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002577 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002578
2579 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2580 ssl->in_msg[0] == SSL_HS_FINISHED )
2581 {
2582 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2583 }
2584 else
2585 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
2586}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002587#endif /* POLARSSL_SSL_PROTO_DTLS */
2588
Paul Bakker5121ce52009-01-03 21:22:43 +00002589/*
2590 * Record layer functions
2591 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002592
2593/*
2594 * Write current record.
2595 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2596 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002597int ssl_write_record( ssl_context *ssl )
2598{
Paul Bakker05ef8352012-05-08 09:17:57 +00002599 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002600 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002601
2602 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2603
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002604#if defined(POLARSSL_SSL_PROTO_DTLS)
2605 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2606 ssl->handshake != NULL &&
2607 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2608 {
2609 ; /* Skip special handshake treatment when resending */
2610 }
2611 else
2612#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002613 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2614 {
2615 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2616 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2617 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2618
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002619 /*
2620 * DTLS has additional fields in the Handshake layer,
2621 * between the length field and the actual payload:
2622 * uint16 message_seq;
2623 * uint24 fragment_offset;
2624 * uint24 fragment_length;
2625 */
2626#if defined(POLARSSL_SSL_PROTO_DTLS)
2627 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2628 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002629 /* Make room for the additional DTLS fields */
2630 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002631 ssl->out_msglen += 8;
2632 len += 8;
2633
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002634 /* Write message_seq and update it, except for HelloRequest */
2635 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2636 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002637 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2638 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2639 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002640 }
2641 else
2642 {
2643 ssl->out_msg[4] = 0;
2644 ssl->out_msg[5] = 0;
2645 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002646
2647 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2648 memset( ssl->out_msg + 6, 0x00, 3 );
2649 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002650 }
2651#endif /* POLARSSL_SSL_PROTO_DTLS */
2652
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002653 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2654 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 }
2656
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002657 /* Save handshake and CCS messages for resending */
2658#if defined(POLARSSL_SSL_PROTO_DTLS)
2659 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2660 ssl->handshake != NULL &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02002661 ssl->handshake->retransmit_state != SSL_RETRANS_SENDING &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002662 ( ssl->out_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC ||
2663 ssl->out_msgtype == SSL_MSG_HANDSHAKE ) )
2664 {
2665 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2666 {
2667 SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
2668 return( ret );
2669 }
2670 }
2671#endif
2672
Paul Bakker2770fbd2012-07-03 13:30:23 +00002673#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002674 if( ssl->transform_out != NULL &&
2675 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002676 {
2677 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2678 {
2679 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2680 return( ret );
2681 }
2682
2683 len = ssl->out_msglen;
2684 }
2685#endif /*POLARSSL_ZLIB_SUPPORT */
2686
Paul Bakker05ef8352012-05-08 09:17:57 +00002687#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002688 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002690 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002691
Paul Bakker05ef8352012-05-08 09:17:57 +00002692 ret = ssl_hw_record_write( ssl );
2693 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2694 {
2695 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002696 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002697 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002698
2699 if( ret == 0 )
2700 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002701 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002702#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002703 if( !done )
2704 {
2705 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002706 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2707 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002708
2709 ssl->out_len[0] = (unsigned char)( len >> 8 );
2710 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002711
Paul Bakker48916f92012-09-16 19:57:18 +00002712 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002713 {
2714 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2715 {
2716 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2717 return( ret );
2718 }
2719
2720 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002721 ssl->out_len[0] = (unsigned char)( len >> 8 );
2722 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002723 }
2724
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002725 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002726
2727 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2728 "version = [%d:%d], msglen = %d",
2729 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002730 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002731
2732 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002733 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002734 }
2735
Paul Bakker5121ce52009-01-03 21:22:43 +00002736 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2737 {
2738 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2739 return( ret );
2740 }
2741
2742 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2743
2744 return( 0 );
2745}
2746
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002747#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002748/*
2749 * Mark bits in bitmask (used for DTLS HS reassembly)
2750 */
2751static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2752{
2753 unsigned int start_bits, end_bits;
2754
2755 start_bits = 8 - ( offset % 8 );
2756 if( start_bits != 8 )
2757 {
2758 size_t first_byte_idx = offset / 8;
2759
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002760 /* Special case */
2761 if( len <= start_bits )
2762 {
2763 for( ; len != 0; len-- )
2764 mask[first_byte_idx] |= 1 << ( start_bits - len );
2765
2766 /* Avoid potential issues with offset or len becoming invalid */
2767 return;
2768 }
2769
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002770 offset += start_bits; /* Now offset % 8 == 0 */
2771 len -= start_bits;
2772
2773 for( ; start_bits != 0; start_bits-- )
2774 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2775 }
2776
2777 end_bits = len % 8;
2778 if( end_bits != 0 )
2779 {
2780 size_t last_byte_idx = ( offset + len ) / 8;
2781
2782 len -= end_bits; /* Now len % 8 == 0 */
2783
2784 for( ; end_bits != 0; end_bits-- )
2785 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2786 }
2787
2788 memset( mask + offset / 8, 0xFF, len / 8 );
2789}
2790
2791/*
2792 * Check that bitmask is full
2793 */
2794static int ssl_bitmask_check( unsigned char *mask, size_t len )
2795{
2796 size_t i;
2797
2798 for( i = 0; i < len / 8; i++ )
2799 if( mask[i] != 0xFF )
2800 return( -1 );
2801
2802 for( i = 0; i < len % 8; i++ )
2803 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2804 return( -1 );
2805
2806 return( 0 );
2807}
2808
2809/*
2810 * Reassemble fragmented DTLS handshake messages.
2811 *
2812 * Use a temporary buffer for reassembly, divided in two parts:
2813 * - the first holds the reassembled message (including handshake header),
2814 * - the second holds a bitmask indicating which parts of the message
2815 * (excluding headers) have been received so far.
2816 */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002817static int ssl_reassemble_dtls_handshake( ssl_context *ssl )
2818{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002819 unsigned char *msg, *bitmask;
2820 size_t frag_len, frag_off;
2821 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
2822
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002823 if( ssl->handshake == NULL )
2824 {
2825 SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
2826 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2827 }
2828
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002829 /*
2830 * For first fragment, check size and allocate buffer
2831 */
2832 if( ssl->handshake->hs_msg == NULL )
2833 {
2834 size_t alloc_len;
2835
2836 SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
2837 msg_len ) );
2838
2839 if( ssl->in_hslen > SSL_MAX_CONTENT_LEN )
2840 {
2841 SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
2842 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2843 }
2844
2845 /* The bitmask needs one bit per byte of message excluding header */
2846 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
2847
2848 ssl->handshake->hs_msg = polarssl_malloc( alloc_len );
2849 if( ssl->handshake->hs_msg == NULL )
2850 {
2851 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", alloc_len ) );
2852 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2853 }
2854
2855 memset( ssl->handshake->hs_msg, 0, alloc_len );
2856
2857 /* Prepare final header: copy msg_type, length and message_seq,
2858 * then add standardised fragment_offset and fragment_length */
2859 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
2860 memset( ssl->handshake->hs_msg + 6, 0, 3 );
2861 memcpy( ssl->handshake->hs_msg + 9,
2862 ssl->handshake->hs_msg + 1, 3 );
2863 }
2864 else
2865 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002866 /* Make sure msg_type and length are consistent */
2867 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002868 {
2869 SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
2870 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2871 }
2872 }
2873
2874 msg = ssl->handshake->hs_msg + 12;
2875 bitmask = msg + msg_len;
2876
2877 /*
2878 * Check and copy current fragment
2879 */
2880 frag_off = ( ssl->in_msg[6] << 16 ) |
2881 ( ssl->in_msg[7] << 8 ) |
2882 ssl->in_msg[8];
2883 frag_len = ( ssl->in_msg[9] << 16 ) |
2884 ( ssl->in_msg[10] << 8 ) |
2885 ssl->in_msg[11];
2886
2887 if( frag_off + frag_len > msg_len )
2888 {
2889 SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
2890 frag_off, frag_len, msg_len ) );
2891 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2892 }
2893
2894 if( frag_len + 12 > ssl->in_msglen )
2895 {
2896 SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
2897 frag_len, ssl->in_msglen ) );
2898 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2899 }
2900
2901 SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
2902 frag_off, frag_len ) );
2903
2904 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
2905 ssl_bitmask_set( bitmask, frag_off, frag_len );
2906
2907 /*
2908 * Do we have the complete message by now?
2909 * If yes, finalize it, else ask to read the next record.
2910 */
2911 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
2912 {
2913 SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
2914 return( POLARSSL_ERR_NET_WANT_READ );
2915 }
2916
2917 SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
2918
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02002919 if( frag_len + 12 < ssl->in_msglen )
2920 {
2921 /*
2922 * We'got more handshake messages in the same record.
2923 * This case is not handled now because no know implementation does
2924 * that and it's hard to test, so we prefer to fail cleanly for now.
2925 */
2926 SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
2927 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2928 }
2929
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002930 if( ssl->in_left > ssl->next_record_offset )
2931 {
2932 /*
2933 * We've got more data in the buffer after the current record,
2934 * that we don't want to overwrite. Move it before writing the
2935 * reassembled message, and adjust in_left and next_record_offset.
2936 */
2937 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
2938 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
2939 size_t remain_len = ssl->in_left - ssl->next_record_offset;
2940
2941 /* First compute and check new lengths */
2942 ssl->next_record_offset = new_remain - ssl->in_hdr;
2943 ssl->in_left = ssl->next_record_offset + remain_len;
2944
2945 if( ssl->in_left > SSL_BUFFER_LEN -
2946 (size_t)( ssl->in_hdr - ssl->in_buf ) )
2947 {
2948 SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
2949 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
2950 }
2951
2952 memmove( new_remain, cur_remain, remain_len );
2953 }
2954
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002955 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
2956
2957 polarssl_free( ssl->handshake->hs_msg );
2958 ssl->handshake->hs_msg = NULL;
2959
2960 SSL_DEBUG_BUF( 3, "reassembled handshake message",
2961 ssl->in_msg, ssl->in_hslen );
2962
2963 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002964}
2965#endif /* POLARSSL_SSL_PROTO_DTLS */
2966
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002967static int ssl_prepare_handshake_record( ssl_context *ssl )
2968{
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002969 if( ssl->in_msglen < ssl_hs_hdr_len( ssl ) )
2970 {
2971 SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
2972 ssl->in_msglen ) );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02002973 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002974 }
2975
2976 ssl->in_hslen = ssl_hs_hdr_len( ssl ) + (
2977 ( ssl->in_msg[1] << 16 ) |
2978 ( ssl->in_msg[2] << 8 ) |
2979 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002980
2981 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2982 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002983 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002984
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002985#if defined(POLARSSL_SSL_PROTO_DTLS)
2986 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002987 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002988 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002989 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002990
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002991 /* ssl->handshake is NULL when receiving ClientHello for renego */
2992 if( ssl->handshake != NULL &&
2993 recv_msg_seq != ssl->handshake->in_msg_seq )
2994 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002995 /* Retransmit only on last message from previous flight, to avoid
2996 * too many retransmissions.
2997 * Besides, No sane server ever retransmits HelloVerifyRequest */
2998 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard93017de2014-09-19 22:42:40 +02002999 ssl->in_msg[0] != SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003000 {
3001 SSL_DEBUG_MSG( 2, ( "received message from last flight, "
3002 "message_seq = %d, start_of_flight = %d",
3003 recv_msg_seq,
3004 ssl->handshake->in_flight_start_seq ) );
3005
3006 if( ( ret = ssl_resend( ssl ) ) != 0 )
3007 {
3008 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3009 return( ret );
3010 }
3011 }
3012 else
3013 {
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02003014 SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003015 "message_seq = %d, expected = %d",
3016 recv_msg_seq,
3017 ssl->handshake->in_msg_seq ) );
3018 }
3019
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003020 return( POLARSSL_ERR_NET_WANT_READ );
3021 }
3022 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003023
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003024 /* Reassemble if current message is fragmented or reassembly is
3025 * already in progress */
3026 if( ssl->in_msglen < ssl->in_hslen ||
3027 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3028 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3029 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003030 {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003031 SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
3032
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003033 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3034 {
3035 SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
3036 return( ret );
3037 }
3038 }
3039 }
3040 else
3041#endif /* POLARSSL_SSL_PROTO_DTLS */
3042 /* With TLS we don't handle fragmentation (for now) */
3043 if( ssl->in_msglen < ssl->in_hslen )
3044 {
3045 SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02003046 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003047 }
3048
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003049 if( ssl->state != SSL_HANDSHAKE_OVER )
3050 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
3051
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003052 /* Handshake message is complete, increment counter */
3053#if defined(POLARSSL_SSL_PROTO_DTLS)
3054 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3055 ssl->handshake != NULL )
3056 {
3057 ssl->handshake->in_msg_seq++;
3058 }
3059#endif
3060
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003061 return( 0 );
3062}
3063
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003064/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003065 * DTLS anti-replay: RFC 6347 4.1.2.6
3066 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003067 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3068 * Bit n is set iff record number in_window_top - n has been seen.
3069 *
3070 * Usually, in_window_top is the last record number seen and the lsb of
3071 * in_window is set. The only exception is the initial state (record number 0
3072 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003073 */
3074#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3075static void ssl_dtls_replay_reset( ssl_context *ssl )
3076{
3077 ssl->in_window_top = 0;
3078 ssl->in_window = 0;
3079}
3080
3081static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3082{
3083 return( ( (uint64_t) buf[0] << 40 ) |
3084 ( (uint64_t) buf[1] << 32 ) |
3085 ( (uint64_t) buf[2] << 24 ) |
3086 ( (uint64_t) buf[3] << 16 ) |
3087 ( (uint64_t) buf[4] << 8 ) |
3088 ( (uint64_t) buf[5] ) );
3089}
3090
3091/*
3092 * Return 0 if sequence number is acceptable, -1 otherwise
3093 */
3094int ssl_dtls_replay_check( ssl_context *ssl )
3095{
3096 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3097 uint64_t bit;
3098
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003099 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
3100 return( 0 );
3101
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003102 if( rec_seqnum > ssl->in_window_top )
3103 return( 0 );
3104
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003105 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003106
3107 if( bit >= 64 )
3108 return( -1 );
3109
3110 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3111 return( -1 );
3112
3113 return( 0 );
3114}
3115
3116/*
3117 * Update replay window on new validated record
3118 */
3119void ssl_dtls_replay_update( ssl_context *ssl )
3120{
3121 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3122
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003123 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
3124 return;
3125
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003126 if( rec_seqnum > ssl->in_window_top )
3127 {
3128 /* Update window_top and the contents of the window */
3129 uint64_t shift = rec_seqnum - ssl->in_window_top;
3130
3131 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003132 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003133 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003134 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003135 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003136 ssl->in_window |= 1;
3137 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003138
3139 ssl->in_window_top = rec_seqnum;
3140 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003141 else
3142 {
3143 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003144 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003145
3146 if( bit < 64 ) /* Always true, but be extra sure */
3147 ssl->in_window |= (uint64_t) 1 << bit;
3148 }
3149}
3150#endif /* POLARSSL_SSL_DTLS_ANTI_REPLAY */
3151
3152/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003153 * ContentType type;
3154 * ProtocolVersion version;
3155 * uint16 epoch; // DTLS only
3156 * uint48 sequence_number; // DTLS only
3157 * uint16 length;
3158 */
3159static int ssl_parse_record_header( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003160{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003161 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003162 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003163
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003164 SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, ssl_hdr_len( ssl ) );
3165
Paul Bakker5121ce52009-01-03 21:22:43 +00003166 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003167 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003168 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003169
3170 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
3171 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003172 ssl->in_msgtype,
3173 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003174
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003175 /* Check record type */
3176 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
3177 ssl->in_msgtype != SSL_MSG_ALERT &&
3178 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
3179 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
3180 {
3181 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003182
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003183 if( ( ret = ssl_send_alert_message( ssl,
3184 SSL_ALERT_LEVEL_FATAL,
3185 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
3186 {
3187 return( ret );
3188 }
3189
3190 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3191 }
3192
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003193#if defined(POLARSSL_SSL_PROTO_DTLS)
3194 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3195 {
3196 /* Drop unexpected ChangeCipherSpec messages */
3197 if( ssl->in_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC &&
3198 ssl->state != SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3199 ssl->state != SSL_SERVER_CHANGE_CIPHER_SPEC )
3200 {
3201 SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3202 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3203 }
3204
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02003205 /* Drop unexpected ApplicationData records,
3206 * except at the beginning of renegotiations */
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003207 if( ssl->in_msgtype == SSL_MSG_APPLICATION_DATA &&
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00003208 ssl->state != SSL_HANDSHAKE_OVER
3209#if defined(POLARSSL_SSL_RENEGOTIATION)
3210 && ! ( ssl->renegotiation == SSL_RENEGOTIATION_IN_PROGRESS &&
3211 ssl->state == SSL_SERVER_HELLO )
3212#endif
3213 )
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003214 {
3215 SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
3216 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3217 }
3218 }
3219#endif
3220
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003221 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003222 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003223 {
3224 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003225 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003226 }
3227
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003228 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003229 {
3230 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003231 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003232 }
3233
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003234 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003235#if defined(POLARSSL_SSL_PROTO_DTLS)
3236 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3237 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003238 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003239
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003240 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003241 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003242 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003243 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003244 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003245 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003246 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003247
3248#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3249 if( ssl_dtls_replay_check( ssl ) != 0 )
3250 {
3251 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3252 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3253 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003254#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003255 }
3256#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003257
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003258 /* Check length against the size of our buffer */
3259 if( ssl->in_msglen > SSL_BUFFER_LEN
3260 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003261 {
3262 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3263 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3264 }
3265
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003266 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003267 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003268 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003269 if( ssl->in_msglen < 1 ||
3270 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003271 {
3272 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003273 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003274 }
3275 }
3276 else
3277 {
Paul Bakker48916f92012-09-16 19:57:18 +00003278 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003279 {
3280 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003281 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003282 }
3283
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003284#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003285 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00003286 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003287 {
3288 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003289 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003290 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003291#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003292#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3293 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003294 /*
3295 * TLS encrypted messages can have up to 256 bytes of padding
3296 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003297 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003298 ssl->in_msglen > ssl->transform_in->minlen +
3299 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003300 {
3301 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003302 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003303 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003304#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003305 }
3306
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003307 return( 0 );
3308}
Paul Bakker5121ce52009-01-03 21:22:43 +00003309
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003310/*
3311 * If applicable, decrypt (and decompress) record content
3312 */
3313static int ssl_prepare_record_content( ssl_context *ssl )
3314{
3315 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003316
Paul Bakker5121ce52009-01-03 21:22:43 +00003317 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003318 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003319
Paul Bakker05ef8352012-05-08 09:17:57 +00003320#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003321 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003322 {
3323 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
3324
3325 ret = ssl_hw_record_read( ssl );
3326 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
3327 {
3328 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003329 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003330 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003331
3332 if( ret == 0 )
3333 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003334 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003335#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003336 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003337 {
3338 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3339 {
3340 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
3341 return( ret );
3342 }
3343
3344 SSL_DEBUG_BUF( 4, "input payload after decrypt",
3345 ssl->in_msg, ssl->in_msglen );
3346
3347 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
3348 {
3349 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003350 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003351 }
3352 }
3353
Paul Bakker2770fbd2012-07-03 13:30:23 +00003354#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003355 if( ssl->transform_in != NULL &&
3356 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003357 {
3358 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3359 {
3360 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
3361 return( ret );
3362 }
3363
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003364 // TODO: what's the purpose of these lines? is in_len used?
3365 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3366 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003367 }
3368#endif /* POLARSSL_ZLIB_SUPPORT */
3369
Manuel Pégourié-Gonnard8464a462014-09-24 14:05:32 +02003370#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003371 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3372 {
3373 ssl_dtls_replay_update( ssl );
3374 }
3375#endif
3376
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003377 return( 0 );
3378}
3379
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003380static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
3381
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003382/*
3383 * Read a record.
3384 *
3385 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
3386 * and continue reading until a valid record is found.
3387 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003388int ssl_read_record( ssl_context *ssl )
3389{
3390 int ret;
3391
3392 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
3393
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02003394 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003395 {
3396 /*
3397 * Get next Handshake message in the current record
3398 */
3399 ssl->in_msglen -= ssl->in_hslen;
3400
3401 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3402 ssl->in_msglen );
3403
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003404 SSL_DEBUG_BUF( 4, "remaining content in record",
3405 ssl->in_msg, ssl->in_msglen );
3406
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003407 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3408 return( ret );
3409
3410 return( 0 );
3411 }
3412
3413 ssl->in_hslen = 0;
3414
3415 /*
3416 * Read the record header and parse it
3417 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003418#if defined(POLARSSL_SSL_PROTO_DTLS)
3419read_record_header:
3420#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003421 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3422 {
3423 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3424 return( ret );
3425 }
3426
3427 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003428 {
3429#if defined(POLARSSL_SSL_PROTO_DTLS)
3430 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3431 {
3432 /* Ignore bad record and get next one; drop the whole datagram
3433 * since current header cannot be trusted to find the next record
3434 * in current datagram */
3435 ssl->next_record_offset = 0;
3436 ssl->in_left = 0;
3437
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003438 SSL_DEBUG_MSG( 1, ( "discarding invalid record (header)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003439 goto read_record_header;
3440 }
3441#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003442 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003443 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003444
3445 /*
3446 * Read and optionally decrypt the message contents
3447 */
3448 if( ( ret = ssl_fetch_input( ssl,
3449 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3450 {
3451 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3452 return( ret );
3453 }
3454
3455 /* Done reading this record, get ready for the next one */
3456#if defined(POLARSSL_SSL_PROTO_DTLS)
3457 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3458 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3459 else
3460#endif
3461 ssl->in_left = 0;
3462
3463 if( ( ret = ssl_prepare_record_content( 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 /* Silently discard invalid records */
3469 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3470 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3471 {
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02003472#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
3473 if( ssl->badmac_limit != 0 &&
3474 ++ssl->badmac_seen >= ssl->badmac_limit )
3475 {
3476 SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
3477 return( POLARSSL_ERR_SSL_INVALID_MAC );
3478 }
3479#endif
3480
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003481 SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003482 goto read_record_header;
3483 }
3484
3485 return( ret );
3486 }
3487 else
3488#endif
3489 {
3490 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard9b669902015-03-06 16:52:46 +00003491#if defined(POLARSSL_SSL_ALL_ALERT_MESSAGES)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003492 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3493 {
3494 ssl_send_alert_message( ssl,
3495 SSL_ALERT_LEVEL_FATAL,
3496 SSL_ALERT_MSG_BAD_RECORD_MAC );
3497 }
3498#endif
3499 return( ret );
3500 }
3501 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003502
3503 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003504 * When we sent the last flight of the handshake, we MUST respond to a
3505 * retransmit of the peer's previous flight with a retransmit. (In
3506 * practice, only the Finished message will make it, other messages
3507 * including CCS use the old transform so they're dropped as invalid.)
3508 *
3509 * If the record we received is not a handshake message, however, it
3510 * means the peer received our last flight so we can clean up
3511 * handshake info.
3512 *
3513 * This check needs to be done before prepare_handshake() due to an edge
3514 * case: if the client immediately requests renegotiation, this
3515 * finishes the current handshake first, avoiding the new ClientHello
3516 * being mistaken for an ancient message in the current handshake.
3517 */
3518#if defined(POLARSSL_SSL_PROTO_DTLS)
3519 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3520 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003521 ssl->state == SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003522 {
3523 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3524 ssl->in_msg[0] == SSL_HS_FINISHED )
3525 {
3526 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3527
3528 if( ( ret = ssl_resend( ssl ) ) != 0 )
3529 {
3530 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3531 return( ret );
3532 }
3533
3534 return( POLARSSL_ERR_NET_WANT_READ );
3535 }
3536 else
3537 {
3538 ssl_handshake_wrapup_free_hs_transform( ssl );
3539 }
3540 }
3541#endif
3542
3543 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003544 * Handle particular types of records
3545 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003546 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3547 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003548 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3549 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003550 }
3551
3552 if( ssl->in_msgtype == SSL_MSG_ALERT )
3553 {
3554 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3555 ssl->in_msg[0], ssl->in_msg[1] ) );
3556
3557 /*
3558 * Ignore non-fatal alerts, except close_notify
3559 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003560 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003561 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003562 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3563 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003564 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003565 }
3566
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003567 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3568 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003569 {
3570 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003571 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003572 }
3573 }
3574
Paul Bakker5121ce52009-01-03 21:22:43 +00003575 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3576
3577 return( 0 );
3578}
3579
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003580int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3581{
3582 int ret;
3583
3584 if( ( ret = ssl_send_alert_message( ssl,
3585 SSL_ALERT_LEVEL_FATAL,
3586 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3587 {
3588 return( ret );
3589 }
3590
3591 return( 0 );
3592}
3593
Paul Bakker0a925182012-04-16 06:46:41 +00003594int ssl_send_alert_message( ssl_context *ssl,
3595 unsigned char level,
3596 unsigned char message )
3597{
3598 int ret;
3599
3600 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3601
3602 ssl->out_msgtype = SSL_MSG_ALERT;
3603 ssl->out_msglen = 2;
3604 ssl->out_msg[0] = level;
3605 ssl->out_msg[1] = message;
3606
3607 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3608 {
3609 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3610 return( ret );
3611 }
3612
3613 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3614
3615 return( 0 );
3616}
3617
Paul Bakker5121ce52009-01-03 21:22:43 +00003618/*
3619 * Handshake functions
3620 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003621#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3622 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3623 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3624 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3625 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3626 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3627 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003628int ssl_write_certificate( ssl_context *ssl )
3629{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003630 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003631
3632 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3633
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003634 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003635 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3636 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003637 {
3638 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3639 ssl->state++;
3640 return( 0 );
3641 }
3642
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003643 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3644 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003645}
3646
3647int ssl_parse_certificate( ssl_context *ssl )
3648{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003649 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3650
3651 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3652
3653 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003654 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3655 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003656 {
3657 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3658 ssl->state++;
3659 return( 0 );
3660 }
3661
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003662 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3663 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003664}
3665#else
3666int ssl_write_certificate( ssl_context *ssl )
3667{
3668 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3669 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003670 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003671 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3672
3673 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3674
3675 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003676 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3677 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003678 {
3679 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3680 ssl->state++;
3681 return( 0 );
3682 }
3683
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003684#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003685 if( ssl->endpoint == SSL_IS_CLIENT )
3686 {
3687 if( ssl->client_auth == 0 )
3688 {
3689 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3690 ssl->state++;
3691 return( 0 );
3692 }
3693
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003694#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003695 /*
3696 * If using SSLv3 and got no cert, send an Alert message
3697 * (otherwise an empty Certificate message will be sent).
3698 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003699 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003700 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3701 {
3702 ssl->out_msglen = 2;
3703 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003704 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3705 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003706
3707 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3708 goto write_msg;
3709 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003710#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003711 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003712#endif /* POLARSSL_SSL_CLI_C */
3713#if defined(POLARSSL_SSL_SRV_C)
3714 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003715 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003716 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003717 {
3718 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003719 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003720 }
3721 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003722#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003723
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003724 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003725
3726 /*
3727 * 0 . 0 handshake type
3728 * 1 . 3 handshake length
3729 * 4 . 6 length of all certs
3730 * 7 . 9 length of cert. 1
3731 * 10 . n-1 peer certificate
3732 * n . n+2 length of cert. 2
3733 * n+3 . ... upper level cert, etc.
3734 */
3735 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003736 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003737
Paul Bakker29087132010-03-21 21:03:34 +00003738 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003739 {
3740 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003741 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003742 {
3743 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3744 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003745 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003746 }
3747
3748 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3749 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3750 ssl->out_msg[i + 2] = (unsigned char)( n );
3751
3752 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3753 i += n; crt = crt->next;
3754 }
3755
3756 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3757 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3758 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3759
3760 ssl->out_msglen = i;
3761 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3762 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3763
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003764#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003765write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003766#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003767
3768 ssl->state++;
3769
3770 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3771 {
3772 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3773 return( ret );
3774 }
3775
3776 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3777
Paul Bakkered27a042013-04-18 22:46:23 +02003778 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003779}
3780
3781int ssl_parse_certificate( ssl_context *ssl )
3782{
Paul Bakkered27a042013-04-18 22:46:23 +02003783 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003784 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003785 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003786
3787 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3788
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003789 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003790 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3791 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003792 {
3793 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3794 ssl->state++;
3795 return( 0 );
3796 }
3797
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003798#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003799 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003800 ( ssl->authmode == SSL_VERIFY_NONE ||
3801 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003802 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003803 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003804 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3805 ssl->state++;
3806 return( 0 );
3807 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003808#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003809
3810 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3811 {
3812 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3813 return( ret );
3814 }
3815
3816 ssl->state++;
3817
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003818#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003819#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003820 /*
3821 * Check if the client sent an empty certificate
3822 */
3823 if( ssl->endpoint == SSL_IS_SERVER &&
3824 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3825 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003826 if( ssl->in_msglen == 2 &&
3827 ssl->in_msgtype == SSL_MSG_ALERT &&
3828 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3829 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003830 {
3831 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3832
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003833 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003834 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3835 return( 0 );
3836 else
Paul Bakker40e46942009-01-03 21:51:57 +00003837 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003838 }
3839 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003840#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003841
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003842#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3843 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003844 if( ssl->endpoint == SSL_IS_SERVER &&
3845 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3846 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003847 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003848 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3849 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003850 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003851 {
3852 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3853
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003854 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003855 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003856 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003857 else
3858 return( 0 );
3859 }
3860 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003861#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3862 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003863#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003864
3865 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3866 {
3867 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003868 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003869 }
3870
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003871 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3872 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003873 {
3874 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003875 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003876 }
3877
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003878 i = ssl_hs_hdr_len( ssl );
3879
Paul Bakker5121ce52009-01-03 21:22:43 +00003880 /*
3881 * Same message structure as in ssl_write_certificate()
3882 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003883 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003884
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003885 if( ssl->in_msg[i] != 0 ||
3886 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003887 {
3888 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003889 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003890 }
3891
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003892 /* In case we tried to reuse a session but it failed */
3893 if( ssl->session_negotiate->peer_cert != NULL )
3894 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003895 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003896 polarssl_free( ssl->session_negotiate->peer_cert );
3897 }
3898
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003899 if( ( ssl->session_negotiate->peer_cert = polarssl_malloc(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003900 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003901 {
3902 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003903 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003904 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003905 }
3906
Paul Bakkerb6b09562013-09-18 14:17:41 +02003907 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003908
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003909 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003910
3911 while( i < ssl->in_hslen )
3912 {
3913 if( ssl->in_msg[i] != 0 )
3914 {
3915 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003916 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003917 }
3918
3919 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3920 | (unsigned int) ssl->in_msg[i + 2];
3921 i += 3;
3922
3923 if( n < 128 || i + n > ssl->in_hslen )
3924 {
3925 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003926 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003927 }
3928
Paul Bakkerddf26b42013-09-18 13:46:23 +02003929 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3930 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003931 if( ret != 0 )
3932 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003933 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003934 return( ret );
3935 }
3936
3937 i += n;
3938 }
3939
Paul Bakker48916f92012-09-16 19:57:18 +00003940 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003941
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003942 /*
3943 * On client, make sure the server cert doesn't change during renego to
3944 * avoid "triple handshake" attack: https://secure-resumption.com/
3945 */
Paul Bakkerf6080b82015-01-13 16:18:23 +01003946#if defined(POLARSSL_SSL_RENEGOTIATION) && defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003947 if( ssl->endpoint == SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2f5a1b42015-03-09 11:12:32 +00003948 ssl->renegotiation == SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003949 {
3950 if( ssl->session->peer_cert == NULL )
3951 {
3952 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3953 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3954 }
3955
3956 if( ssl->session->peer_cert->raw.len !=
3957 ssl->session_negotiate->peer_cert->raw.len ||
3958 memcmp( ssl->session->peer_cert->raw.p,
3959 ssl->session_negotiate->peer_cert->raw.p,
3960 ssl->session->peer_cert->raw.len ) != 0 )
3961 {
3962 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3963 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3964 }
3965 }
Paul Bakkerf6080b82015-01-13 16:18:23 +01003966#endif /* POLARSSL_SSL_RENEGOTIATION && POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003967
Paul Bakker5121ce52009-01-03 21:22:43 +00003968 if( ssl->authmode != SSL_VERIFY_NONE )
3969 {
3970 if( ssl->ca_chain == NULL )
3971 {
3972 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003973 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003974 }
3975
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003976 /*
3977 * Main check: verify certificate
3978 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003979 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3980 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3981 &ssl->session_negotiate->verify_result,
3982 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003983
3984 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003985 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003986 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003987 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003988
3989 /*
3990 * Secondary checks: always done, but change 'ret' only if it was 0
3991 */
3992
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003993#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003994 {
Paul Bakker93389cc2014-04-17 14:44:38 +02003995 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003996
3997 /* If certificate uses an EC key, make sure the curve is OK */
3998 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3999 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
4000 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02004001 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004002 if( ret == 0 )
4003 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004004 }
4005 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004006#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00004007
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004008 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
4009 ciphersuite_info,
4010 ! ssl->endpoint ) != 0 )
4011 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02004012 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004013 if( ret == 0 )
4014 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
4015 }
4016
Paul Bakker5121ce52009-01-03 21:22:43 +00004017 if( ssl->authmode != SSL_VERIFY_REQUIRED )
4018 ret = 0;
4019 }
4020
4021 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
4022
4023 return( ret );
4024}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01004025#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
4026 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
4027 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
4028 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
4029 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
4030 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
4031 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004032
4033int ssl_write_change_cipher_spec( ssl_context *ssl )
4034{
4035 int ret;
4036
4037 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
4038
4039 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
4040 ssl->out_msglen = 1;
4041 ssl->out_msg[0] = 1;
4042
Paul Bakker5121ce52009-01-03 21:22:43 +00004043 ssl->state++;
4044
4045 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4046 {
4047 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4048 return( ret );
4049 }
4050
4051 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
4052
4053 return( 0 );
4054}
4055
4056int ssl_parse_change_cipher_spec( ssl_context *ssl )
4057{
4058 int ret;
4059
4060 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
4061
Paul Bakker5121ce52009-01-03 21:22:43 +00004062 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4063 {
4064 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4065 return( ret );
4066 }
4067
4068 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
4069 {
4070 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004071 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004072 }
4073
4074 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
4075 {
4076 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004077 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00004078 }
4079
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004080 /*
4081 * Switch to our negotiated transform and session parameters for inbound
4082 * data.
4083 */
4084 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
4085 ssl->transform_in = ssl->transform_negotiate;
4086 ssl->session_in = ssl->session_negotiate;
4087
4088#if defined(POLARSSL_SSL_PROTO_DTLS)
4089 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4090 {
4091#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4092 ssl_dtls_replay_reset( ssl );
4093#endif
4094
4095 /* Increment epoch */
4096 if( ++ssl->in_epoch == 0 )
4097 {
4098 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4099 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4100 }
4101 }
4102 else
4103#endif /* POLARSSL_SSL_PROTO_DTLS */
4104 memset( ssl->in_ctr, 0, 8 );
4105
4106 /*
4107 * Set the in_msg pointer to the correct location based on IV length
4108 */
4109 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4110 {
4111 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
4112 ssl->transform_negotiate->fixed_ivlen;
4113 }
4114 else
4115 ssl->in_msg = ssl->in_iv;
4116
4117#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4118 if( ssl_hw_record_activate != NULL )
4119 {
4120 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
4121 {
4122 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4123 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4124 }
4125 }
4126#endif
4127
Paul Bakker5121ce52009-01-03 21:22:43 +00004128 ssl->state++;
4129
4130 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
4131
4132 return( 0 );
4133}
4134
Paul Bakker41c83d32013-03-20 14:39:14 +01004135void ssl_optimize_checksum( ssl_context *ssl,
4136 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00004137{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02004138 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01004139
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004140#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4141 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00004142 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00004143 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00004144 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004145#endif
4146#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4147#if defined(POLARSSL_SHA512_C)
4148 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
4149 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
4150 else
4151#endif
4152#if defined(POLARSSL_SHA256_C)
4153 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00004154 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004155 else
4156#endif
4157#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004158 {
4159 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004160 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004161 }
Paul Bakker380da532012-04-18 16:10:25 +00004162}
Paul Bakkerf7abd422013-04-16 13:15:56 +02004163
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004164void ssl_reset_checksum( ssl_context *ssl )
4165{
4166#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4167 defined(POLARSSL_SSL_PROTO_TLS1_1)
4168 md5_starts( &ssl->handshake->fin_md5 );
4169 sha1_starts( &ssl->handshake->fin_sha1 );
4170#endif
4171#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4172#if defined(POLARSSL_SHA256_C)
4173 sha256_starts( &ssl->handshake->fin_sha256, 0 );
4174#endif
4175#if defined(POLARSSL_SHA512_C)
4176 sha512_starts( &ssl->handshake->fin_sha512, 1 );
4177#endif
4178#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4179}
4180
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004181static void ssl_update_checksum_start( ssl_context *ssl,
4182 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004183{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004184#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4185 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00004186 md5_update( &ssl->handshake->fin_md5 , buf, len );
4187 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004188#endif
4189#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4190#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02004191 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004192#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02004193#if defined(POLARSSL_SHA512_C)
4194 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01004195#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004196#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004197}
4198
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004199#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4200 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004201static void ssl_update_checksum_md5sha1( ssl_context *ssl,
4202 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004203{
Paul Bakker48916f92012-09-16 19:57:18 +00004204 md5_update( &ssl->handshake->fin_md5 , buf, len );
4205 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004206}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004207#endif
Paul Bakker380da532012-04-18 16:10:25 +00004208
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004209#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4210#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004211static void ssl_update_checksum_sha256( ssl_context *ssl,
4212 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004213{
Paul Bakker9e36f042013-06-30 14:34:05 +02004214 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004215}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004216#endif
Paul Bakker380da532012-04-18 16:10:25 +00004217
Paul Bakker9e36f042013-06-30 14:34:05 +02004218#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004219static void ssl_update_checksum_sha384( ssl_context *ssl,
4220 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004221{
Paul Bakker9e36f042013-06-30 14:34:05 +02004222 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004223}
Paul Bakker769075d2012-11-24 11:26:46 +01004224#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004225#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004226
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004227#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004228static void ssl_calc_finished_ssl(
4229 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004230{
Paul Bakker3c2122f2013-06-24 19:03:14 +02004231 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004232 md5_context md5;
4233 sha1_context sha1;
4234
Paul Bakker5121ce52009-01-03 21:22:43 +00004235 unsigned char padbuf[48];
4236 unsigned char md5sum[16];
4237 unsigned char sha1sum[20];
4238
Paul Bakker48916f92012-09-16 19:57:18 +00004239 ssl_session *session = ssl->session_negotiate;
4240 if( !session )
4241 session = ssl->session;
4242
Paul Bakker1ef83d62012-04-11 12:09:53 +00004243 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
4244
Paul Bakker48916f92012-09-16 19:57:18 +00004245 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4246 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004247
4248 /*
4249 * SSLv3:
4250 * hash =
4251 * MD5( master + pad2 +
4252 * MD5( handshake + sender + master + pad1 ) )
4253 * + SHA1( master + pad2 +
4254 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004255 */
4256
Paul Bakker90995b52013-06-24 19:20:35 +02004257#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004258 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004259 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004260#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004261
Paul Bakker90995b52013-06-24 19:20:35 +02004262#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004263 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004264 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004265#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004266
Paul Bakker3c2122f2013-06-24 19:03:14 +02004267 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
4268 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004269
Paul Bakker1ef83d62012-04-11 12:09:53 +00004270 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004271
Paul Bakker3c2122f2013-06-24 19:03:14 +02004272 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004273 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004274 md5_update( &md5, padbuf, 48 );
4275 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004276
Paul Bakker3c2122f2013-06-24 19:03:14 +02004277 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004278 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004279 sha1_update( &sha1, padbuf, 40 );
4280 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004281
Paul Bakker1ef83d62012-04-11 12:09:53 +00004282 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004283
Paul Bakker1ef83d62012-04-11 12:09:53 +00004284 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00004285 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004286 md5_update( &md5, padbuf, 48 );
4287 md5_update( &md5, md5sum, 16 );
4288 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004289
Paul Bakker1ef83d62012-04-11 12:09:53 +00004290 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00004291 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004292 sha1_update( &sha1, padbuf , 40 );
4293 sha1_update( &sha1, sha1sum, 20 );
4294 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004295
Paul Bakker1ef83d62012-04-11 12:09:53 +00004296 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004297
Paul Bakker5b4af392014-06-26 12:09:34 +02004298 md5_free( &md5 );
4299 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004300
Paul Bakker34617722014-06-13 17:20:13 +02004301 polarssl_zeroize( padbuf, sizeof( padbuf ) );
4302 polarssl_zeroize( md5sum, sizeof( md5sum ) );
4303 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004304
4305 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4306}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004307#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004308
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004309#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004310static void ssl_calc_finished_tls(
4311 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004312{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004313 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004314 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004315 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00004316 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004317 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004318
Paul Bakker48916f92012-09-16 19:57:18 +00004319 ssl_session *session = ssl->session_negotiate;
4320 if( !session )
4321 session = ssl->session;
4322
Paul Bakker1ef83d62012-04-11 12:09:53 +00004323 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004324
Paul Bakker48916f92012-09-16 19:57:18 +00004325 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4326 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004327
Paul Bakker1ef83d62012-04-11 12:09:53 +00004328 /*
4329 * TLSv1:
4330 * hash = PRF( master, finished_label,
4331 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4332 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004333
Paul Bakker90995b52013-06-24 19:20:35 +02004334#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004335 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4336 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004337#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004338
Paul Bakker90995b52013-06-24 19:20:35 +02004339#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004340 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4341 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004342#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004343
4344 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004345 ? "client finished"
4346 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004347
4348 md5_finish( &md5, padbuf );
4349 sha1_finish( &sha1, padbuf + 16 );
4350
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004351 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004352 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004353
4354 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4355
Paul Bakker5b4af392014-06-26 12:09:34 +02004356 md5_free( &md5 );
4357 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004358
Paul Bakker34617722014-06-13 17:20:13 +02004359 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004360
4361 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4362}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004363#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004364
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004365#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4366#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004367static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004368 ssl_context *ssl, unsigned char *buf, int from )
4369{
4370 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004371 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004372 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004373 unsigned char padbuf[32];
4374
Paul Bakker48916f92012-09-16 19:57:18 +00004375 ssl_session *session = ssl->session_negotiate;
4376 if( !session )
4377 session = ssl->session;
4378
Paul Bakker380da532012-04-18 16:10:25 +00004379 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004380
Paul Bakker9e36f042013-06-30 14:34:05 +02004381 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004382
4383 /*
4384 * TLSv1.2:
4385 * hash = PRF( master, finished_label,
4386 * Hash( handshake ) )[0.11]
4387 */
4388
Paul Bakker9e36f042013-06-30 14:34:05 +02004389#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004390 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004391 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004392#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004393
4394 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004395 ? "client finished"
4396 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004397
Paul Bakker9e36f042013-06-30 14:34:05 +02004398 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004399
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004400 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004401 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004402
4403 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4404
Paul Bakker5b4af392014-06-26 12:09:34 +02004405 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004406
Paul Bakker34617722014-06-13 17:20:13 +02004407 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004408
4409 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4410}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004411#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004412
Paul Bakker9e36f042013-06-30 14:34:05 +02004413#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004414static void ssl_calc_finished_tls_sha384(
4415 ssl_context *ssl, unsigned char *buf, int from )
4416{
4417 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004418 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004419 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004420 unsigned char padbuf[48];
4421
Paul Bakker48916f92012-09-16 19:57:18 +00004422 ssl_session *session = ssl->session_negotiate;
4423 if( !session )
4424 session = ssl->session;
4425
Paul Bakker380da532012-04-18 16:10:25 +00004426 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004427
Paul Bakker9e36f042013-06-30 14:34:05 +02004428 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004429
4430 /*
4431 * TLSv1.2:
4432 * hash = PRF( master, finished_label,
4433 * Hash( handshake ) )[0.11]
4434 */
4435
Paul Bakker9e36f042013-06-30 14:34:05 +02004436#if !defined(POLARSSL_SHA512_ALT)
4437 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4438 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004439#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004440
4441 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004442 ? "client finished"
4443 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004444
Paul Bakker9e36f042013-06-30 14:34:05 +02004445 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004446
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004447 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004448 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004449
4450 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4451
Paul Bakker5b4af392014-06-26 12:09:34 +02004452 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004453
Paul Bakker34617722014-06-13 17:20:13 +02004454 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004455
4456 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4457}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004458#endif /* POLARSSL_SHA512_C */
4459#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004460
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004461static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004462{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004463 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004464
4465 /*
4466 * Free our handshake params
4467 */
4468 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004469 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004470 ssl->handshake = NULL;
4471
4472 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004473 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004474 */
4475 if( ssl->transform )
4476 {
4477 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004478 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004479 }
4480 ssl->transform = ssl->transform_negotiate;
4481 ssl->transform_negotiate = NULL;
4482
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004483 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4484}
4485
4486void ssl_handshake_wrapup( ssl_context *ssl )
4487{
4488 int resume = ssl->handshake->resume;
4489
4490 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4491
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004492#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2f5a1b42015-03-09 11:12:32 +00004493 if( ssl->renegotiation == SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004494 {
4495 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
4496 ssl->renego_records_seen = 0;
4497 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004498#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004499
4500 /*
4501 * Free the previous session and switch in the current one
4502 */
Paul Bakker0a597072012-09-25 21:55:46 +00004503 if( ssl->session )
4504 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01004505#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4506 /* RFC 7366 3.1: keep the EtM state */
4507 ssl->session_negotiate->encrypt_then_mac =
4508 ssl->session->encrypt_then_mac;
4509#endif
4510
Paul Bakker0a597072012-09-25 21:55:46 +00004511 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004512 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004513 }
4514 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004515 ssl->session_negotiate = NULL;
4516
Paul Bakker0a597072012-09-25 21:55:46 +00004517 /*
4518 * Add cache entry
4519 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004520 if( ssl->f_set_cache != NULL &&
4521 ssl->session->length != 0 &&
4522 resume == 0 )
4523 {
Paul Bakker0a597072012-09-25 21:55:46 +00004524 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4525 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004526 }
Paul Bakker0a597072012-09-25 21:55:46 +00004527
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004528#if defined(POLARSSL_SSL_PROTO_DTLS)
4529 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4530 ssl->handshake->flight != NULL )
4531 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02004532 /* Cancel handshake timer */
4533 ssl_set_timer( ssl, 0 );
4534
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004535 /* Keep last flight around in case we need to resend it:
4536 * we need the handshake and transform structures for that */
4537 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4538 }
4539 else
4540#endif
4541 ssl_handshake_wrapup_free_hs_transform( ssl );
4542
Paul Bakker48916f92012-09-16 19:57:18 +00004543 ssl->state++;
4544
4545 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4546}
4547
Paul Bakker1ef83d62012-04-11 12:09:53 +00004548int ssl_write_finished( ssl_context *ssl )
4549{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004550 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004551
4552 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4553
Paul Bakker92be97b2013-01-02 17:30:03 +01004554 /*
4555 * Set the out_msg pointer to the correct location based on IV length
4556 */
4557 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4558 {
4559 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4560 ssl->transform_negotiate->fixed_ivlen;
4561 }
4562 else
4563 ssl->out_msg = ssl->out_iv;
4564
Paul Bakker48916f92012-09-16 19:57:18 +00004565 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004566
4567 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004568 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4569
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004570#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004571 ssl->verify_data_len = hash_len;
4572 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004573#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004574
Paul Bakker5121ce52009-01-03 21:22:43 +00004575 ssl->out_msglen = 4 + hash_len;
4576 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4577 ssl->out_msg[0] = SSL_HS_FINISHED;
4578
4579 /*
4580 * In case of session resuming, invert the client and server
4581 * ChangeCipherSpec messages order.
4582 */
Paul Bakker0a597072012-09-25 21:55:46 +00004583 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004584 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004585#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004586 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004587 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004588#endif
4589#if defined(POLARSSL_SSL_SRV_C)
4590 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004591 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004592#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004593 }
4594 else
4595 ssl->state++;
4596
Paul Bakker48916f92012-09-16 19:57:18 +00004597 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004598 * Switch to our negotiated transform and session parameters for outbound
4599 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004600 */
4601 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004602
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004603#if defined(POLARSSL_SSL_PROTO_DTLS)
4604 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4605 {
4606 unsigned char i;
4607
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004608 /* Remember current epoch settings for resending */
4609 ssl->handshake->alt_transform_out = ssl->transform_out;
4610 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4611
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004612 /* Set sequence_number to zero */
4613 memset( ssl->out_ctr + 2, 0, 6 );
4614
4615 /* Increment epoch */
4616 for( i = 2; i > 0; i-- )
4617 if( ++ssl->out_ctr[i - 1] != 0 )
4618 break;
4619
4620 /* The loop goes to its end iff the counter is wrapping */
4621 if( i == 0 )
4622 {
4623 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4624 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4625 }
4626 }
4627 else
4628#endif /* POLARSSL_SSL_PROTO_DTLS */
4629 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004630
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004631 ssl->transform_out = ssl->transform_negotiate;
4632 ssl->session_out = ssl->session_negotiate;
4633
Paul Bakker07eb38b2012-12-19 14:42:06 +01004634#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004635 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004636 {
4637 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4638 {
4639 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4640 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4641 }
4642 }
4643#endif
4644
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004645#if defined(POLARSSL_SSL_PROTO_DTLS)
4646 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4647 ssl_send_flight_completed( ssl );
4648#endif
4649
Paul Bakker5121ce52009-01-03 21:22:43 +00004650 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4651 {
4652 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4653 return( ret );
4654 }
4655
4656 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4657
4658 return( 0 );
4659}
4660
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004661#if defined(POLARSSL_SSL_PROTO_SSL3)
4662#define SSL_MAX_HASH_LEN 36
4663#else
4664#define SSL_MAX_HASH_LEN 12
4665#endif
4666
Paul Bakker5121ce52009-01-03 21:22:43 +00004667int ssl_parse_finished( ssl_context *ssl )
4668{
Paul Bakker23986e52011-04-24 08:57:21 +00004669 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004670 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004671 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004672
4673 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4674
Paul Bakker48916f92012-09-16 19:57:18 +00004675 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004676
Paul Bakker5121ce52009-01-03 21:22:43 +00004677 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4678 {
4679 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4680 return( ret );
4681 }
4682
4683 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4684 {
4685 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004686 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004687 }
4688
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004689 /* There is currently no ciphersuite using another length with TLS 1.2 */
4690#if defined(POLARSSL_SSL_PROTO_SSL3)
4691 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4692 hash_len = 36;
4693 else
4694#endif
4695 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004696
4697 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004698 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004699 {
4700 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004701 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004702 }
4703
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004704 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4705 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004706 {
4707 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004708 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004709 }
4710
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004711#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004712 ssl->verify_data_len = hash_len;
4713 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004714#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004715
Paul Bakker0a597072012-09-25 21:55:46 +00004716 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004717 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004718#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004719 if( ssl->endpoint == SSL_IS_CLIENT )
4720 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004721#endif
4722#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004723 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004724 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004725#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004726 }
4727 else
4728 ssl->state++;
4729
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004730#if defined(POLARSSL_SSL_PROTO_DTLS)
4731 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4732 ssl_recv_flight_completed( ssl );
4733#endif
4734
Paul Bakker5121ce52009-01-03 21:22:43 +00004735 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4736
4737 return( 0 );
4738}
4739
Paul Bakker968afaa2014-07-09 11:09:24 +02004740static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004741{
4742 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4743
4744#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4745 defined(POLARSSL_SSL_PROTO_TLS1_1)
4746 md5_init( &handshake->fin_md5 );
4747 sha1_init( &handshake->fin_sha1 );
4748 md5_starts( &handshake->fin_md5 );
4749 sha1_starts( &handshake->fin_sha1 );
4750#endif
4751#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4752#if defined(POLARSSL_SHA256_C)
4753 sha256_init( &handshake->fin_sha256 );
4754 sha256_starts( &handshake->fin_sha256, 0 );
4755#endif
4756#if defined(POLARSSL_SHA512_C)
4757 sha512_init( &handshake->fin_sha512 );
4758 sha512_starts( &handshake->fin_sha512, 1 );
4759#endif
4760#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4761
4762 handshake->update_checksum = ssl_update_checksum_start;
4763 handshake->sig_alg = SSL_HASH_SHA1;
4764
4765#if defined(POLARSSL_DHM_C)
4766 dhm_init( &handshake->dhm_ctx );
4767#endif
4768#if defined(POLARSSL_ECDH_C)
4769 ecdh_init( &handshake->ecdh_ctx );
4770#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004771}
4772
4773static void ssl_transform_init( ssl_transform *transform )
4774{
4775 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004776
4777 cipher_init( &transform->cipher_ctx_enc );
4778 cipher_init( &transform->cipher_ctx_dec );
4779
4780 md_init( &transform->md_ctx_enc );
4781 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004782}
4783
4784void ssl_session_init( ssl_session *session )
4785{
4786 memset( session, 0, sizeof(ssl_session) );
4787}
4788
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004789static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004790{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004791 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004792 if( ssl->transform_negotiate )
4793 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004794 if( ssl->session_negotiate )
4795 ssl_session_free( ssl->session_negotiate );
4796 if( ssl->handshake )
4797 ssl_handshake_free( ssl->handshake );
4798
4799 /*
4800 * Either the pointers are now NULL or cleared properly and can be freed.
4801 * Now allocate missing structures.
4802 */
4803 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004804 {
Mansour Moufid99b92592015-02-15 17:46:32 -05004805 ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004806 }
Paul Bakker48916f92012-09-16 19:57:18 +00004807
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004808 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004809 {
Mansour Moufid99b92592015-02-15 17:46:32 -05004810 ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004811 }
Paul Bakker48916f92012-09-16 19:57:18 +00004812
Paul Bakker82788fb2014-10-20 13:59:19 +02004813 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004814 {
Mansour Moufid99b92592015-02-15 17:46:32 -05004815 ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004816 }
Paul Bakker48916f92012-09-16 19:57:18 +00004817
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004818 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004819 if( ssl->handshake == NULL ||
4820 ssl->transform_negotiate == NULL ||
4821 ssl->session_negotiate == NULL )
4822 {
4823 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004824
4825 polarssl_free( ssl->handshake );
4826 polarssl_free( ssl->transform_negotiate );
4827 polarssl_free( ssl->session_negotiate );
4828
4829 ssl->handshake = NULL;
4830 ssl->transform_negotiate = NULL;
4831 ssl->session_negotiate = NULL;
4832
Paul Bakker48916f92012-09-16 19:57:18 +00004833 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4834 }
4835
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004836 /* Initialize structures */
4837 ssl_session_init( ssl->session_negotiate );
4838 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004839 ssl_handshake_params_init( ssl->handshake );
4840
4841#if defined(POLARSSL_X509_CRT_PARSE_C)
4842 ssl->handshake->key_cert = ssl->key_cert;
4843#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004844
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004845 /*
4846 * We may not know yet if we're using DTLS,
4847 * so always initiliase DTLS-specific fields.
4848 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004849#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004850 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004851
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004852 // TODO: not the right place, we may not know endpoint yet
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004853 if( ssl->endpoint == SSL_IS_CLIENT )
4854 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4855 else
4856 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004857#endif
4858
Paul Bakker48916f92012-09-16 19:57:18 +00004859 return( 0 );
4860}
4861
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004862#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4863/* Dummy cookie callbacks for defaults */
4864static int ssl_cookie_write_dummy( void *ctx,
4865 unsigned char **p, unsigned char *end,
4866 const unsigned char *cli_id, size_t cli_id_len )
4867{
4868 ((void) ctx);
4869 ((void) p);
4870 ((void) end);
4871 ((void) cli_id);
4872 ((void) cli_id_len);
4873
4874 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4875}
4876
4877static int ssl_cookie_check_dummy( void *ctx,
4878 const unsigned char *cookie, size_t cookie_len,
4879 const unsigned char *cli_id, size_t cli_id_len )
4880{
4881 ((void) ctx);
4882 ((void) cookie);
4883 ((void) cookie_len);
4884 ((void) cli_id);
4885 ((void) cli_id_len);
4886
4887 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4888}
4889#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4890
Paul Bakker5121ce52009-01-03 21:22:43 +00004891/*
4892 * Initialize an SSL context
4893 */
4894int ssl_init( ssl_context *ssl )
4895{
Paul Bakker48916f92012-09-16 19:57:18 +00004896 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004897 int len = SSL_BUFFER_LEN;
4898
4899 memset( ssl, 0, sizeof( ssl_context ) );
4900
Paul Bakker62f2dee2012-09-28 07:31:51 +00004901 /*
4902 * Sane defaults
4903 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004904 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4905 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4906 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4907 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004908
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004909 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004910
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004911#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004912 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004913 memset( ssl->renego_period, 0xFF, 7 );
4914 ssl->renego_period[7] = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004915#endif
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004916
Paul Bakker62f2dee2012-09-28 07:31:51 +00004917#if defined(POLARSSL_DHM_C)
4918 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4919 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4920 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4921 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4922 {
4923 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4924 return( ret );
4925 }
4926#endif
4927
4928 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004929 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004930 */
Manuel Pégourié-Gonnard4e41c992015-02-18 10:39:49 +00004931 if( ( ssl->in_buf = polarssl_malloc( len ) ) == NULL ||
4932 ( ssl->out_buf = polarssl_malloc( len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004933 {
4934 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004935 polarssl_free( ssl->in_buf );
4936 ssl->in_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004937 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004938 }
4939
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004940 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4941 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004942
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004943 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4944 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4945
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004946#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4947 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
4948#endif
4949
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004950#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4951 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
4952#endif
4953
Paul Bakker606b4ba2013-08-14 16:52:14 +02004954#if defined(POLARSSL_SSL_SESSION_TICKETS)
4955 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4956#endif
4957
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004958#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004959 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004960#endif
4961
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004962#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4963 ssl->f_cookie_write = ssl_cookie_write_dummy;
4964 ssl->f_cookie_check = ssl_cookie_check_dummy;
4965#endif
4966
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004967#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4968 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
4969#endif
4970
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004971#if defined(POLARSSL_SSL_PROTO_DTLS)
4972 ssl->hs_timeout_min = SSL_DTLS_TIMEOUT_DFL_MIN;
4973 ssl->hs_timeout_max = SSL_DTLS_TIMEOUT_DFL_MAX;
4974#endif
4975
Paul Bakker48916f92012-09-16 19:57:18 +00004976 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4977 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004978
4979 return( 0 );
4980}
4981
4982/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004983 * Reset an initialized and used SSL context for re-use while retaining
4984 * all application-set variables, function pointers and data.
4985 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004986int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004987{
Paul Bakker48916f92012-09-16 19:57:18 +00004988 int ret;
4989
Paul Bakker7eb013f2011-10-06 12:37:39 +00004990 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004991
4992#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004993 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004994 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004995
4996 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard61860192014-11-04 13:05:42 +01004997 memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
4998 memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004999#endif
5000 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00005001
Paul Bakker7eb013f2011-10-06 12:37:39 +00005002 ssl->in_offt = NULL;
5003
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005004 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005005 ssl->in_msgtype = 0;
5006 ssl->in_msglen = 0;
5007 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005008#if defined(POLARSSL_SSL_PROTO_DTLS)
5009 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005010 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005011#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005012#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
5013 ssl_dtls_replay_reset( ssl );
5014#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005015
5016 ssl->in_hslen = 0;
5017 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005018 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005019
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005020 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005021 ssl->out_msgtype = 0;
5022 ssl->out_msglen = 0;
5023 ssl->out_left = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005024#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005025 if( ssl->split_done != SSL_CBC_RECORD_SPLITTING_DISABLED )
5026 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005027#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005028
Paul Bakker48916f92012-09-16 19:57:18 +00005029 ssl->transform_in = NULL;
5030 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005031
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005032 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
5033 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00005034
5035#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02005036 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005037 {
5038 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005039 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005040 {
5041 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
5042 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
5043 }
Paul Bakker05ef8352012-05-08 09:17:57 +00005044 }
5045#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00005046
Paul Bakker48916f92012-09-16 19:57:18 +00005047 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005048 {
Paul Bakker48916f92012-09-16 19:57:18 +00005049 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005050 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005051 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00005052 }
Paul Bakker48916f92012-09-16 19:57:18 +00005053
Paul Bakkerc0463502013-02-14 11:19:38 +01005054 if( ssl->session )
5055 {
5056 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005057 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005058 ssl->session = NULL;
5059 }
5060
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005061#if defined(POLARSSL_SSL_ALPN)
5062 ssl->alpn_chosen = NULL;
5063#endif
5064
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02005065#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005066 polarssl_free( ssl->cli_id );
5067 ssl->cli_id = NULL;
5068 ssl->cli_id_len = 0;
5069#endif
5070
Paul Bakker48916f92012-09-16 19:57:18 +00005071 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5072 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005073
5074 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00005075}
5076
Paul Bakkera503a632013-08-14 13:48:06 +02005077#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005078static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
5079{
5080 aes_free( &tkeys->enc );
5081 aes_free( &tkeys->dec );
5082
5083 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
5084}
5085
Paul Bakker7eb013f2011-10-06 12:37:39 +00005086/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005087 * Allocate and initialize ticket keys
5088 */
5089static int ssl_ticket_keys_init( ssl_context *ssl )
5090{
5091 int ret;
5092 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005093 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005094
5095 if( ssl->ticket_keys != NULL )
5096 return( 0 );
5097
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005098 tkeys = polarssl_malloc( sizeof(ssl_ticket_keys) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005099 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005100 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5101
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005102 aes_init( &tkeys->enc );
5103 aes_init( &tkeys->dec );
5104
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005105 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01005106 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005107 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005108 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005109 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005110 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005111
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02005112 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
5113 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
5114 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
5115 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005116 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005117 polarssl_free( tkeys );
5118 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02005119 }
5120
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005121 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01005122 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005123 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005124 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005125 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005126 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005127
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005128 ssl->ticket_keys = tkeys;
5129
5130 return( 0 );
5131}
Paul Bakkera503a632013-08-14 13:48:06 +02005132#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005133
5134/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005135 * SSL set accessors
5136 */
5137void ssl_set_endpoint( ssl_context *ssl, int endpoint )
5138{
5139 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005140
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005141#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
5142 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005143 if( endpoint == SSL_IS_CLIENT )
5144 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02005145#endif
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01005146
5147#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
5148 if( endpoint == SSL_IS_SERVER )
5149 ssl->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
5150#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005151}
5152
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005153int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005154{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005155#if defined(POLARSSL_SSL_PROTO_DTLS)
5156 if( transport == SSL_TRANSPORT_DATAGRAM )
5157 {
5158 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005159
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005160 ssl->out_hdr = ssl->out_buf;
5161 ssl->out_ctr = ssl->out_buf + 3;
5162 ssl->out_len = ssl->out_buf + 11;
5163 ssl->out_iv = ssl->out_buf + 13;
5164 ssl->out_msg = ssl->out_buf + 13;
5165
5166 ssl->in_hdr = ssl->in_buf;
5167 ssl->in_ctr = ssl->in_buf + 3;
5168 ssl->in_len = ssl->in_buf + 11;
5169 ssl->in_iv = ssl->in_buf + 13;
5170 ssl->in_msg = ssl->in_buf + 13;
5171
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005172 /* DTLS starts with TLS1.1 */
5173 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
5174 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005175
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005176 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
5177 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
5178
5179 return( 0 );
5180 }
5181#endif
5182
5183 if( transport == SSL_TRANSPORT_STREAM )
5184 {
5185 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005186
5187 ssl->out_ctr = ssl->out_buf;
5188 ssl->out_hdr = ssl->out_buf + 8;
5189 ssl->out_len = ssl->out_buf + 11;
5190 ssl->out_iv = ssl->out_buf + 13;
5191 ssl->out_msg = ssl->out_buf + 13;
5192
5193 ssl->in_ctr = ssl->in_buf;
5194 ssl->in_hdr = ssl->in_buf + 8;
5195 ssl->in_len = ssl->in_buf + 11;
5196 ssl->in_iv = ssl->in_buf + 13;
5197 ssl->in_msg = ssl->in_buf + 13;
5198
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005199 return( 0 );
5200 }
5201
5202 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005203}
5204
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005205#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
5206void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
5207{
5208 ssl->anti_replay = mode;
5209}
5210#endif
5211
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005212#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
5213void ssl_set_dtls_badmac_limit( ssl_context *ssl, unsigned limit )
5214{
5215 ssl->badmac_limit = limit;
5216}
5217#endif
5218
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02005219#if defined(POLARSSL_SSL_PROTO_DTLS)
5220void ssl_set_handshake_timeout( ssl_context *ssl, uint32_t min, uint32_t max )
5221{
5222 ssl->hs_timeout_min = min;
5223 ssl->hs_timeout_max = max;
5224}
5225#endif
5226
Paul Bakker5121ce52009-01-03 21:22:43 +00005227void ssl_set_authmode( ssl_context *ssl, int authmode )
5228{
5229 ssl->authmode = authmode;
5230}
5231
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005232#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005233void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005234 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005235 void *p_vrfy )
5236{
5237 ssl->f_vrfy = f_vrfy;
5238 ssl->p_vrfy = p_vrfy;
5239}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005240#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005241
Paul Bakker5121ce52009-01-03 21:22:43 +00005242void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00005243 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00005244 void *p_rng )
5245{
5246 ssl->f_rng = f_rng;
5247 ssl->p_rng = p_rng;
5248}
5249
5250void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00005251 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00005252 void *p_dbg )
5253{
5254 ssl->f_dbg = f_dbg;
5255 ssl->p_dbg = p_dbg;
5256}
5257
5258void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00005259 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00005260 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00005261{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02005262 if( p_recv != p_send )
5263 {
5264 ssl->f_recv = NULL;
5265 ssl->f_send = NULL;
5266 ssl->p_bio = NULL;
5267 return;
5268 }
5269
Paul Bakker5121ce52009-01-03 21:22:43 +00005270 ssl->f_recv = f_recv;
5271 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02005272 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00005273}
5274
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005275void ssl_set_bio_timeout( ssl_context *ssl,
5276 void *p_bio,
5277 int (*f_send)(void *, const unsigned char *, size_t),
5278 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +02005279 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t),
5280 uint32_t timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005281{
5282 ssl->p_bio = p_bio;
5283 ssl->f_send = f_send;
5284 ssl->f_recv = f_recv;
5285 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard27074302014-10-01 17:35:50 +02005286 ssl->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005287}
5288
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005289#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00005290void ssl_set_session_cache( ssl_context *ssl,
5291 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
5292 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00005293{
Paul Bakker0a597072012-09-25 21:55:46 +00005294 ssl->f_get_cache = f_get_cache;
5295 ssl->p_get_cache = p_get_cache;
5296 ssl->f_set_cache = f_set_cache;
5297 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00005298}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005299#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005300
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005301#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005302int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00005303{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005304 int ret;
5305
5306 if( ssl == NULL ||
5307 session == NULL ||
5308 ssl->session_negotiate == NULL ||
5309 ssl->endpoint != SSL_IS_CLIENT )
5310 {
5311 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5312 }
5313
5314 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5315 return( ret );
5316
Paul Bakker0a597072012-09-25 21:55:46 +00005317 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005318
5319 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005320}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005321#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005322
Paul Bakkerb68cad62012-08-23 08:34:18 +00005323void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005324{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005325 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
5326 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
5327 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
5328 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
5329}
5330
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005331void ssl_set_ciphersuites_for_version( ssl_context *ssl,
5332 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005333 int major, int minor )
5334{
5335 if( major != SSL_MAJOR_VERSION_3 )
5336 return;
5337
5338 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
5339 return;
5340
5341 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005342}
5343
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005344#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005345/* Add a new (empty) key_cert entry an return a pointer to it */
5346static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
5347{
5348 ssl_key_cert *key_cert, *last;
5349
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005350 key_cert = polarssl_malloc( sizeof(ssl_key_cert) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005351 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005352 return( NULL );
5353
5354 memset( key_cert, 0, sizeof( ssl_key_cert ) );
5355
5356 /* Append the new key_cert to the (possibly empty) current list */
5357 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005358 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005359 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01005360 if( ssl->handshake != NULL )
5361 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01005362 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005363 else
5364 {
5365 last = ssl->key_cert;
5366 while( last->next != NULL )
5367 last = last->next;
5368 last->next = key_cert;
5369 }
5370
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005371 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005372}
5373
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005374void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00005375 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00005376{
5377 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00005378 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005379 ssl->peer_cn = peer_cn;
5380}
5381
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005382int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005383 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00005384{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005385 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
5386
5387 if( key_cert == NULL )
5388 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5389
5390 key_cert->cert = own_cert;
5391 key_cert->key = pk_key;
5392
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01005393 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005394}
5395
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005396#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005397int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005398 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00005399{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005400 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005401 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005402
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005403 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005404 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5405
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005406 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005407 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005408 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005409
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005410 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005411
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005412 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005413 if( ret != 0 )
5414 return( ret );
5415
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005416 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005417 return( ret );
5418
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005419 key_cert->cert = own_cert;
5420 key_cert->key_own_alloc = 1;
5421
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01005422 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005423}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005424#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005425
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005426int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02005427 void *rsa_key,
5428 rsa_decrypt_func rsa_decrypt,
5429 rsa_sign_func rsa_sign,
5430 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00005431{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005432 int ret;
5433 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005434
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005435 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005436 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5437
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005438 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005439 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005440 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005441
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005442 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005443
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005444 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
5445 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
5446 return( ret );
5447
5448 key_cert->cert = own_cert;
5449 key_cert->key_own_alloc = 1;
5450
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01005451 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker43b7e352011-01-18 15:27:19 +00005452}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005453#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005454
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005455#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005456int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5457 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005458{
Paul Bakker6db455e2013-09-18 17:29:31 +02005459 if( psk == NULL || psk_identity == NULL )
5460 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5461
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005462 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005463 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5464
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00005465 if( ssl->psk != NULL || ssl->psk_identity != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02005466 {
5467 polarssl_free( ssl->psk );
5468 polarssl_free( ssl->psk_identity );
5469 }
5470
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00005471 if( ( ssl->psk = polarssl_malloc( psk_len ) ) == NULL ||
5472 ( ssl->psk_identity = polarssl_malloc( psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05005473 {
5474 polarssl_free( ssl->psk );
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00005475 ssl->psk = NULL;
Mansour Moufidf81088b2015-02-17 13:10:21 -05005476 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5477 }
Paul Bakker6db455e2013-09-18 17:29:31 +02005478
Paul Bakker6db455e2013-09-18 17:29:31 +02005479 ssl->psk_len = psk_len;
5480 ssl->psk_identity_len = psk_identity_len;
5481
Paul Bakker6db455e2013-09-18 17:29:31 +02005482 memcpy( ssl->psk, psk, ssl->psk_len );
5483 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
5484
5485 return( 0 );
5486}
5487
5488void ssl_set_psk_cb( ssl_context *ssl,
5489 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5490 size_t),
5491 void *p_psk )
5492{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005493 ssl->f_psk = f_psk;
5494 ssl->p_psk = p_psk;
Paul Bakker43b7e352011-01-18 15:27:19 +00005495}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005496#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005497
Paul Bakker48916f92012-09-16 19:57:18 +00005498#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005499int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
5500{
5501 int ret;
5502
Paul Bakker48916f92012-09-16 19:57:18 +00005503 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005504 {
5505 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5506 return( ret );
5507 }
5508
Paul Bakker48916f92012-09-16 19:57:18 +00005509 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005510 {
5511 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5512 return( ret );
5513 }
5514
5515 return( 0 );
5516}
5517
Paul Bakker1b57b062011-01-06 15:48:19 +00005518int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5519{
5520 int ret;
5521
Paul Bakker66d5d072014-06-17 16:39:18 +02005522 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005523 {
5524 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5525 return( ret );
5526 }
5527
Paul Bakker66d5d072014-06-17 16:39:18 +02005528 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005529 {
5530 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5531 return( ret );
5532 }
5533
5534 return( 0 );
5535}
Paul Bakker48916f92012-09-16 19:57:18 +00005536#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005537
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005538#if defined(POLARSSL_SSL_SET_CURVES)
5539/*
5540 * Set the allowed elliptic curves
5541 */
5542void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5543{
5544 ssl->curve_list = curve_list;
5545}
5546#endif
5547
Paul Bakker0be444a2013-08-27 21:55:01 +02005548#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005549int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005550{
5551 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005552 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005553
5554 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005555
5556 if( ssl->hostname_len + 1 == 0 )
5557 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5558
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005559 ssl->hostname = polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005560
Paul Bakkerb15b8512012-01-13 13:44:06 +00005561 if( ssl->hostname == NULL )
5562 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5563
Paul Bakker3c2122f2013-06-24 19:03:14 +02005564 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005565 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005566
Paul Bakker40ea7de2009-05-03 10:18:48 +00005567 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005568
5569 return( 0 );
5570}
5571
Paul Bakker5701cdc2012-09-27 21:49:42 +00005572void ssl_set_sni( ssl_context *ssl,
5573 int (*f_sni)(void *, ssl_context *,
5574 const unsigned char *, size_t),
5575 void *p_sni )
5576{
5577 ssl->f_sni = f_sni;
5578 ssl->p_sni = p_sni;
5579}
Paul Bakker0be444a2013-08-27 21:55:01 +02005580#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005581
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005582#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005583int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005584{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005585 size_t cur_len, tot_len;
5586 const char **p;
5587
5588 /*
5589 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5590 * truncated". Check lengths now rather than later.
5591 */
5592 tot_len = 0;
5593 for( p = protos; *p != NULL; p++ )
5594 {
5595 cur_len = strlen( *p );
5596 tot_len += cur_len;
5597
5598 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5599 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5600 }
5601
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005602 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005603
5604 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005605}
5606
5607const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5608{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005609 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005610}
5611#endif /* POLARSSL_SSL_ALPN */
5612
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005613static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005614{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005615 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005616 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005617 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005618 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005619 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005620
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005621#if defined(POLARSSL_SSL_PROTO_DTLS)
5622 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5623 minor < SSL_MINOR_VERSION_2 )
5624 {
5625 return( -1 );
5626 }
5627#else
5628 ((void) ssl);
5629#endif
5630
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005631 return( 0 );
5632}
5633
5634int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5635{
5636 if( ssl_check_version( ssl, major, minor ) != 0 )
5637 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5638
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005639 ssl->max_major_ver = major;
5640 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005641
5642 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005643}
5644
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005645int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005646{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005647 if( ssl_check_version( ssl, major, minor ) != 0 )
5648 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005649
5650 ssl->min_major_ver = major;
5651 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005652
5653 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005654}
5655
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02005656#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
5657void ssl_set_fallback( ssl_context *ssl, char fallback )
5658{
5659 ssl->fallback = fallback;
5660}
5661#endif
5662
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01005663#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
5664void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
5665{
5666 ssl->encrypt_then_mac = etm;
5667}
5668#endif
5669
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02005670#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
5671void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
5672{
5673 ssl->extended_ms = ems;
5674}
5675#endif
5676
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01005677void ssl_set_arc4_support( ssl_context *ssl, char arc4 )
5678{
5679 ssl->arc4_disabled = arc4;
5680}
5681
Paul Bakker05decb22013-08-15 13:33:48 +02005682#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005683int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5684{
Paul Bakker77e257e2013-12-16 15:29:52 +01005685 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005686 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005687 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005688 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005689 }
5690
5691 ssl->mfl_code = mfl_code;
5692
5693 return( 0 );
5694}
Paul Bakker05decb22013-08-15 13:33:48 +02005695#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005696
Paul Bakker1f2bc622013-08-15 13:45:55 +02005697#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005698int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005699{
Paul Bakker8c1ede62013-07-19 14:14:37 +02005700 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005701
5702 return( 0 );
5703}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005704#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005705
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005706#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
5707void ssl_set_cbc_record_splitting( ssl_context *ssl, char split )
5708{
5709 ssl->split_done = split;
5710}
5711#endif
5712
Paul Bakker48916f92012-09-16 19:57:18 +00005713void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5714{
5715 ssl->allow_legacy_renegotiation = allow_legacy;
5716}
5717
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005718#if defined(POLARSSL_SSL_RENEGOTIATION)
5719void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5720{
5721 ssl->disable_renegotiation = renegotiation;
5722}
5723
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005724void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5725{
5726 ssl->renego_max_records = max_records;
5727}
5728
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01005729void ssl_set_renegotiation_period( ssl_context *ssl,
5730 const unsigned char period[8] )
5731{
5732 memcpy( ssl->renego_period, period, 8 );
5733}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005734#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00005735
Paul Bakkera503a632013-08-14 13:48:06 +02005736#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005737int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5738{
5739 ssl->session_tickets = use_tickets;
5740
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005741#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005742 if( ssl->endpoint == SSL_IS_CLIENT )
5743 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005744#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005745
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01005746 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
5747 return( 0 );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005748
5749 if( ssl->f_rng == NULL )
5750 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5751
5752 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005753}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005754
5755void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5756{
5757 ssl->ticket_lifetime = lifetime;
5758}
Paul Bakkera503a632013-08-14 13:48:06 +02005759#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005760
Paul Bakker5121ce52009-01-03 21:22:43 +00005761/*
5762 * SSL get accessors
5763 */
5764size_t ssl_get_bytes_avail( const ssl_context *ssl )
5765{
5766 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5767}
5768
Paul Bakkerff60ee62010-03-16 21:09:09 +00005769int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005770{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00005771 if( ssl->session != NULL )
5772 return( ssl->session->verify_result );
5773
5774 if( ssl->session_negotiate != NULL )
5775 return( ssl->session_negotiate->verify_result );
5776
5777 return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005778}
5779
Paul Bakkere3166ce2011-01-27 17:40:50 +00005780const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005781{
Paul Bakker926c8e42013-03-06 10:23:34 +01005782 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005783 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005784
Paul Bakkere3166ce2011-01-27 17:40:50 +00005785 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005786}
5787
Paul Bakker43ca69c2011-01-15 17:35:19 +00005788const char *ssl_get_version( const ssl_context *ssl )
5789{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005790#if defined(POLARSSL_SSL_PROTO_DTLS)
5791 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5792 {
5793 switch( ssl->minor_ver )
5794 {
5795 case SSL_MINOR_VERSION_2:
5796 return( "DTLSv1.0" );
5797
5798 case SSL_MINOR_VERSION_3:
5799 return( "DTLSv1.2" );
5800
5801 default:
5802 return( "unknown (DTLS)" );
5803 }
5804 }
5805#endif
5806
Paul Bakker43ca69c2011-01-15 17:35:19 +00005807 switch( ssl->minor_ver )
5808 {
5809 case SSL_MINOR_VERSION_0:
5810 return( "SSLv3.0" );
5811
5812 case SSL_MINOR_VERSION_1:
5813 return( "TLSv1.0" );
5814
5815 case SSL_MINOR_VERSION_2:
5816 return( "TLSv1.1" );
5817
Paul Bakker1ef83d62012-04-11 12:09:53 +00005818 case SSL_MINOR_VERSION_3:
5819 return( "TLSv1.2" );
5820
Paul Bakker43ca69c2011-01-15 17:35:19 +00005821 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005822 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005823 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005824}
5825
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005826int ssl_get_record_expansion( const ssl_context *ssl )
5827{
5828 int transform_expansion;
5829 const ssl_transform *transform = ssl->transform_out;
5830
5831#if defined(POLARSSL_ZLIB_SUPPORT)
5832 if( ssl->session_out->compression != SSL_COMPRESS_NULL )
5833 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
5834#endif
5835
5836 if( transform == NULL )
5837 return( ssl_hdr_len( ssl ) );
5838
5839 switch( cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
5840 {
5841 case POLARSSL_MODE_GCM:
5842 case POLARSSL_MODE_CCM:
5843 case POLARSSL_MODE_STREAM:
5844 transform_expansion = transform->minlen;
5845 break;
5846
5847 case POLARSSL_MODE_CBC:
5848 transform_expansion = transform->maclen
5849 + cipher_get_block_size( &transform->cipher_ctx_enc );
5850 break;
5851
5852 default:
5853 SSL_DEBUG_MSG( 0, ( "should never happen" ) );
5854 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
5855 }
5856
5857 return( ssl_hdr_len( ssl ) + transform_expansion );
5858}
5859
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005860#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005861const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005862{
5863 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005864 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005865
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005866 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005867}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005868#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005869
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005870#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005871int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5872{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005873 if( ssl == NULL ||
5874 dst == NULL ||
5875 ssl->session == NULL ||
5876 ssl->endpoint != SSL_IS_CLIENT )
5877 {
5878 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5879 }
5880
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005881 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005882}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005883#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005884
Paul Bakker5121ce52009-01-03 21:22:43 +00005885/*
Paul Bakker1961b702013-01-25 14:49:24 +01005886 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005887 */
Paul Bakker1961b702013-01-25 14:49:24 +01005888int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005889{
Paul Bakker40e46942009-01-03 21:51:57 +00005890 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005891
Paul Bakker40e46942009-01-03 21:51:57 +00005892#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005893 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005894 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005895#endif
Paul Bakker40e46942009-01-03 21:51:57 +00005896#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005897 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005898 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005899#endif
5900
Paul Bakker1961b702013-01-25 14:49:24 +01005901 return( ret );
5902}
5903
5904/*
5905 * Perform the SSL handshake
5906 */
5907int ssl_handshake( ssl_context *ssl )
5908{
5909 int ret = 0;
5910
5911 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5912
5913 while( ssl->state != SSL_HANDSHAKE_OVER )
5914 {
5915 ret = ssl_handshake_step( ssl );
5916
5917 if( ret != 0 )
5918 break;
5919 }
5920
Paul Bakker5121ce52009-01-03 21:22:43 +00005921 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5922
5923 return( ret );
5924}
5925
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005926#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005927#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005928/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005929 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005930 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005931static int ssl_write_hello_request( ssl_context *ssl )
5932{
5933 int ret;
5934
5935 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5936
5937 ssl->out_msglen = 4;
5938 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5939 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5940
5941 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5942 {
5943 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5944 return( ret );
5945 }
5946
5947 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5948
5949 return( 0 );
5950}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005951#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005952
5953/*
5954 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005955 * - any side: calling ssl_renegotiate(),
5956 * - client: receiving a HelloRequest during ssl_read(),
5957 * - server: receiving any handshake message on server during ssl_read() after
5958 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005959 * If the handshake doesn't complete due to waiting for I/O, it will continue
5960 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005961 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005962static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005963{
5964 int ret;
5965
5966 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5967
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005968 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5969 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005970
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005971 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5972 * the ServerHello will have message_seq = 1" */
5973#if defined(POLARSSL_SSL_PROTO_DTLS)
5974 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005975 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5976 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005977 if( ssl->endpoint == SSL_IS_SERVER )
5978 ssl->handshake->out_msg_seq = 1;
5979 else
5980 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005981 }
5982#endif
5983
Paul Bakker48916f92012-09-16 19:57:18 +00005984 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard2f5a1b42015-03-09 11:12:32 +00005985 ssl->renegotiation = SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00005986
Paul Bakker48916f92012-09-16 19:57:18 +00005987 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5988 {
5989 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5990 return( ret );
5991 }
5992
5993 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5994
5995 return( 0 );
5996}
5997
5998/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005999 * Renegotiate current connection on client,
6000 * or request renegotiation on server
6001 */
6002int ssl_renegotiate( ssl_context *ssl )
6003{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006004 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006005
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006006#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006007 /* On server, just send the request */
6008 if( ssl->endpoint == SSL_IS_SERVER )
6009 {
6010 if( ssl->state != SSL_HANDSHAKE_OVER )
6011 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
6012
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02006013 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
6014
6015 /* Did we already try/start sending HelloRequest? */
6016 if( ssl->out_left != 0 )
6017 return( ssl_flush_output( ssl ) );
6018
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006019 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006020 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006021#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006022
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006023#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006024 /*
6025 * On client, either start the renegotiation process or,
6026 * if already in progress, continue the handshake
6027 */
Manuel Pégourié-Gonnard2f5a1b42015-03-09 11:12:32 +00006028 if( ssl->renegotiation != SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006029 {
6030 if( ssl->state != SSL_HANDSHAKE_OVER )
6031 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
6032
6033 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
6034 {
6035 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
6036 return( ret );
6037 }
6038 }
6039 else
6040 {
6041 if( ( ret = ssl_handshake( ssl ) ) != 0 )
6042 {
6043 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6044 return( ret );
6045 }
6046 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006047#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006048
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006049 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006050}
6051
6052/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006053 * Check record counters and renegotiate if they're above the limit.
6054 */
6055static int ssl_check_ctr_renegotiate( ssl_context *ssl )
6056{
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006057 if( ssl->state != SSL_HANDSHAKE_OVER ||
6058 ssl->renegotiation == SSL_RENEGOTIATION_PENDING ||
6059 ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED )
6060 {
6061 return( 0 );
6062 }
6063
6064 // TODO: adapt for DTLS
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006065 if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 &&
6066 memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006067 {
6068 return( 0 );
6069 }
6070
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006071 SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006072 return( ssl_renegotiate( ssl ) );
6073}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006074#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006075
6076/*
6077 * Receive application data decrypted from the SSL layer
6078 */
Paul Bakker23986e52011-04-24 08:57:21 +00006079int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006080{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006081 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00006082 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00006083
6084 SSL_DEBUG_MSG( 2, ( "=> read" ) );
6085
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006086#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006087 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006088 {
6089 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
6090 return( ret );
6091
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006092 if( ssl->handshake != NULL &&
6093 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
6094 {
6095 if( ( ret = ssl_resend( ssl ) ) != 0 )
6096 return( ret );
6097 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006098 }
6099#endif
6100
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006101#if defined(POLARSSL_SSL_RENEGOTIATION)
6102 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
6103 {
6104 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
6105 return( ret );
6106 }
6107#endif
6108
Paul Bakker5121ce52009-01-03 21:22:43 +00006109 if( ssl->state != SSL_HANDSHAKE_OVER )
6110 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006111 ret = ssl_handshake( ssl );
6112 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
6113 {
6114 record_read = 1;
6115 }
6116 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006117 {
6118 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6119 return( ret );
6120 }
6121 }
6122
6123 if( ssl->in_offt == NULL )
6124 {
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02006125#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006126 /* Start timer if not already running */
6127 if( ssl->time_limit == 0 )
6128 ssl_set_timer( ssl, ssl->read_timeout );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02006129#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006130
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006131 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00006132 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006133 if( ( ret = ssl_read_record( ssl ) ) != 0 )
6134 {
6135 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
6136 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00006137
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006138 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
6139 return( ret );
6140 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006141 }
6142
6143 if( ssl->in_msglen == 0 &&
6144 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
6145 {
6146 /*
6147 * OpenSSL sends empty messages to randomize the IV
6148 */
6149 if( ( ret = ssl_read_record( ssl ) ) != 0 )
6150 {
Paul Bakker831a7552011-05-18 13:32:51 +00006151 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
6152 return( 0 );
6153
Paul Bakker5121ce52009-01-03 21:22:43 +00006154 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
6155 return( ret );
6156 }
6157 }
6158
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006159#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006160 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
6161 {
6162 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
6163
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006164#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006165 if( ssl->endpoint == SSL_IS_CLIENT &&
6166 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00006167 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006168 {
6169 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006170
6171 /* With DTLS, drop the packet (probably from last handshake) */
6172#if defined(POLARSSL_SSL_PROTO_DTLS)
6173 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6174 return( POLARSSL_ERR_NET_WANT_READ );
6175#endif
6176 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6177 }
6178
6179 if( ssl->endpoint == SSL_IS_SERVER &&
6180 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
6181 {
6182 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
6183
6184 /* With DTLS, drop the packet (probably from last handshake) */
6185#if defined(POLARSSL_SSL_PROTO_DTLS)
6186 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6187 return( POLARSSL_ERR_NET_WANT_READ );
6188#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006189 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6190 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006191#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006192
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006193 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
6194 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006195 ssl->allow_legacy_renegotiation ==
6196 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006197 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006198 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006199
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006200#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006201 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006202 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006203 /*
6204 * SSLv3 does not have a "no_renegotiation" alert
6205 */
6206 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
6207 return( ret );
6208 }
6209 else
Paul Bakker9af723c2014-05-01 13:03:14 +02006210#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006211#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
6212 defined(POLARSSL_SSL_PROTO_TLS1_2)
6213 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006214 {
6215 if( ( ret = ssl_send_alert_message( ssl,
6216 SSL_ALERT_LEVEL_WARNING,
6217 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
6218 {
6219 return( ret );
6220 }
Paul Bakker48916f92012-09-16 19:57:18 +00006221 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006222 else
Paul Bakker9af723c2014-05-01 13:03:14 +02006223#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
6224 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02006225 {
6226 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006227 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02006228 }
Paul Bakker48916f92012-09-16 19:57:18 +00006229 }
6230 else
6231 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006232 /* DTLS clients need to know renego is server-initiated */
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02006233#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006234 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
6235 ssl->endpoint == SSL_IS_CLIENT )
6236 {
6237 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
6238 }
6239#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006240 ret = ssl_start_renegotiation( ssl );
6241 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
6242 {
6243 record_read = 1;
6244 }
6245 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006246 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006247 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00006248 return( ret );
6249 }
Paul Bakker48916f92012-09-16 19:57:18 +00006250 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006251
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006252 /* If a non-handshake record was read during renego, fallthrough,
6253 * else tell the user they should call ssl_read() again */
6254 if( ! record_read )
6255 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00006256 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006257 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
6258 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006259
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006260 if( ssl->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006261 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006262 if( ++ssl->renego_records_seen > ssl->renego_max_records )
6263 {
6264 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
6265 "but not honored by client" ) );
6266 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6267 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006268 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006269 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006270#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006271
6272 /* Fatal and closure alerts handled by ssl_read_record() */
6273 if( ssl->in_msgtype == SSL_MSG_ALERT )
6274 {
6275 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
6276 return( POLARSSL_ERR_NET_WANT_READ );
6277 }
6278
6279 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00006280 {
6281 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00006282 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006283 }
6284
6285 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006286
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02006287#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02006288 /* We're going to return something now, cancel timer,
6289 * except if handshake (renegotiation) is in progress */
6290 if( ssl->state == SSL_HANDSHAKE_OVER )
6291 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006292
6293 /* If we requested renego but received AppData, resend HelloRequest.
6294 * Do it now, after setting in_offt, to avoid taking this branch
6295 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00006296#if defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006297 if( ssl->endpoint == SSL_IS_SERVER &&
6298 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
6299 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006300 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006301 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006302 SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006303 return( ret );
6304 }
6305 }
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00006306#endif /* POLARSSL_SSL_SRV_C && POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02006307#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006308 }
6309
6310 n = ( len < ssl->in_msglen )
6311 ? len : ssl->in_msglen;
6312
6313 memcpy( buf, ssl->in_offt, n );
6314 ssl->in_msglen -= n;
6315
6316 if( ssl->in_msglen == 0 )
6317 /* all bytes consumed */
6318 ssl->in_offt = NULL;
6319 else
6320 /* more data available */
6321 ssl->in_offt += n;
6322
6323 SSL_DEBUG_MSG( 2, ( "<= read" ) );
6324
Paul Bakker23986e52011-04-24 08:57:21 +00006325 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006326}
6327
6328/*
6329 * Send application data to be encrypted by the SSL layer
6330 */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006331#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
6332static int ssl_write_real( ssl_context *ssl, const unsigned char *buf, size_t len )
6333#else
Paul Bakker23986e52011-04-24 08:57:21 +00006334int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006335#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006336{
Paul Bakker23986e52011-04-24 08:57:21 +00006337 int ret;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006338#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
6339 unsigned int max_len;
6340#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006341
6342 SSL_DEBUG_MSG( 2, ( "=> write" ) );
6343
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006344#if defined(POLARSSL_SSL_RENEGOTIATION)
6345 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
6346 {
6347 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
6348 return( ret );
6349 }
6350#endif
6351
Paul Bakker5121ce52009-01-03 21:22:43 +00006352 if( ssl->state != SSL_HANDSHAKE_OVER )
6353 {
6354 if( ( ret = ssl_handshake( ssl ) ) != 0 )
6355 {
6356 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6357 return( ret );
6358 }
6359 }
6360
Paul Bakker05decb22013-08-15 13:33:48 +02006361#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02006362 /*
6363 * Assume mfl_code is correct since it was checked when set
6364 */
6365 max_len = mfl_code_to_length[ssl->mfl_code];
6366
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006367 /*
Paul Bakker05decb22013-08-15 13:33:48 +02006368 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006369 */
6370 if( ssl->session_out != NULL &&
6371 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6372 {
6373 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6374 }
6375
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006376 if( len > max_len )
6377 {
6378#if defined(POLARSSL_SSL_PROTO_DTLS)
6379 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6380 {
6381 SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
6382 "maximum fragment length: %d > %d",
6383 len, max_len ) );
6384 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
6385 }
6386 else
6387#endif
6388 len = max_len;
6389 }
6390#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker887bd502011-06-08 13:10:54 +00006391
Paul Bakker5121ce52009-01-03 21:22:43 +00006392 if( ssl->out_left != 0 )
6393 {
6394 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
6395 {
6396 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
6397 return( ret );
6398 }
6399 }
Paul Bakker887bd502011-06-08 13:10:54 +00006400 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00006401 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006402 ssl->out_msglen = len;
Paul Bakker887bd502011-06-08 13:10:54 +00006403 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006404 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00006405
6406 if( ( ret = ssl_write_record( ssl ) ) != 0 )
6407 {
6408 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
6409 return( ret );
6410 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006411 }
6412
6413 SSL_DEBUG_MSG( 2, ( "<= write" ) );
6414
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006415 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006416}
6417
6418/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006419 * Write application data, doing 1/n-1 splitting if necessary.
6420 *
6421 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006422 * then the caller will call us again with the same arguments, so
6423 * remember wether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006424 */
6425#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
6426int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
6427{
6428 int ret;
6429
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006430 if( ssl->split_done == SSL_CBC_RECORD_SPLITTING_DISABLED ||
6431 len <= 1 ||
6432 ssl->minor_ver > SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006433 cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
6434 != POLARSSL_MODE_CBC )
6435 {
6436 return( ssl_write_real( ssl, buf, len ) );
6437 }
6438
6439 if( ssl->split_done == 0 )
6440 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006441 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006442 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006443 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006444 }
6445
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006446 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
6447 return( ret );
6448 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006449
6450 return( ret + 1 );
6451}
6452#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */
6453
6454/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006455 * Notify the peer that the connection is being closed
6456 */
6457int ssl_close_notify( ssl_context *ssl )
6458{
6459 int ret;
6460
6461 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
6462
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006463 if( ssl->out_left != 0 )
6464 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006465
6466 if( ssl->state == SSL_HANDSHAKE_OVER )
6467 {
Paul Bakker48916f92012-09-16 19:57:18 +00006468 if( ( ret = ssl_send_alert_message( ssl,
6469 SSL_ALERT_LEVEL_WARNING,
6470 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006471 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006472 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006473 return( ret );
6474 }
6475 }
6476
6477 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
6478
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006479 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006480}
6481
Paul Bakker48916f92012-09-16 19:57:18 +00006482void ssl_transform_free( ssl_transform *transform )
6483{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006484 if( transform == NULL )
6485 return;
6486
Paul Bakker48916f92012-09-16 19:57:18 +00006487#if defined(POLARSSL_ZLIB_SUPPORT)
6488 deflateEnd( &transform->ctx_deflate );
6489 inflateEnd( &transform->ctx_inflate );
6490#endif
6491
Paul Bakker84bbeb52014-07-01 14:53:22 +02006492 cipher_free( &transform->cipher_ctx_enc );
6493 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02006494
Paul Bakker84bbeb52014-07-01 14:53:22 +02006495 md_free( &transform->md_ctx_enc );
6496 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02006497
Paul Bakker34617722014-06-13 17:20:13 +02006498 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006499}
6500
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006501#if defined(POLARSSL_X509_CRT_PARSE_C)
6502static void ssl_key_cert_free( ssl_key_cert *key_cert )
6503{
6504 ssl_key_cert *cur = key_cert, *next;
6505
6506 while( cur != NULL )
6507 {
6508 next = cur->next;
6509
6510 if( cur->key_own_alloc )
6511 {
6512 pk_free( cur->key );
6513 polarssl_free( cur->key );
6514 }
6515 polarssl_free( cur );
6516
6517 cur = next;
6518 }
6519}
6520#endif /* POLARSSL_X509_CRT_PARSE_C */
6521
Paul Bakker48916f92012-09-16 19:57:18 +00006522void ssl_handshake_free( ssl_handshake_params *handshake )
6523{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006524 if( handshake == NULL )
6525 return;
6526
Paul Bakker48916f92012-09-16 19:57:18 +00006527#if defined(POLARSSL_DHM_C)
6528 dhm_free( &handshake->dhm_ctx );
6529#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006530#if defined(POLARSSL_ECDH_C)
6531 ecdh_free( &handshake->ecdh_ctx );
6532#endif
6533
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006534#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02006535 /* explicit void pointer cast for buggy MS compiler */
6536 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006537#endif
6538
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006539#if defined(POLARSSL_X509_CRT_PARSE_C) && \
6540 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
6541 /*
6542 * Free only the linked list wrapper, not the keys themselves
6543 * since the belong to the SNI callback
6544 */
6545 if( handshake->sni_key_cert != NULL )
6546 {
6547 ssl_key_cert *cur = handshake->sni_key_cert, *next;
6548
6549 while( cur != NULL )
6550 {
6551 next = cur->next;
6552 polarssl_free( cur );
6553 cur = next;
6554 }
6555 }
Paul Bakker9af723c2014-05-01 13:03:14 +02006556#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006557
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006558#if defined(POLARSSL_SSL_PROTO_DTLS)
6559 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02006560 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02006561 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006562#endif
6563
Paul Bakker34617722014-06-13 17:20:13 +02006564 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006565}
6566
6567void ssl_session_free( ssl_session *session )
6568{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006569 if( session == NULL )
6570 return;
6571
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006572#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00006573 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00006574 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006575 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02006576 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00006577 }
Paul Bakkered27a042013-04-18 22:46:23 +02006578#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006579
Paul Bakkera503a632013-08-14 13:48:06 +02006580#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006581 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02006582#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006583
Paul Bakker34617722014-06-13 17:20:13 +02006584 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006585}
6586
Paul Bakker5121ce52009-01-03 21:22:43 +00006587/*
6588 * Free an SSL context
6589 */
6590void ssl_free( ssl_context *ssl )
6591{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006592 if( ssl == NULL )
6593 return;
6594
Paul Bakker5121ce52009-01-03 21:22:43 +00006595 SSL_DEBUG_MSG( 2, ( "=> free" ) );
6596
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006597 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006598 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006599 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
6600 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006601 }
6602
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006603 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006604 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006605 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
6606 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006607 }
6608
Paul Bakker16770332013-10-11 09:59:44 +02006609#if defined(POLARSSL_ZLIB_SUPPORT)
6610 if( ssl->compress_buf != NULL )
6611 {
Paul Bakker34617722014-06-13 17:20:13 +02006612 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02006613 polarssl_free( ssl->compress_buf );
6614 }
6615#endif
6616
Paul Bakker40e46942009-01-03 21:51:57 +00006617#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006618 mpi_free( &ssl->dhm_P );
6619 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006620#endif
6621
Paul Bakker48916f92012-09-16 19:57:18 +00006622 if( ssl->transform )
6623 {
6624 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02006625 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006626 }
6627
6628 if( ssl->handshake )
6629 {
6630 ssl_handshake_free( ssl->handshake );
6631 ssl_transform_free( ssl->transform_negotiate );
6632 ssl_session_free( ssl->session_negotiate );
6633
Paul Bakker6e339b52013-07-03 13:37:05 +02006634 polarssl_free( ssl->handshake );
6635 polarssl_free( ssl->transform_negotiate );
6636 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00006637 }
6638
Paul Bakkerc0463502013-02-14 11:19:38 +01006639 if( ssl->session )
6640 {
6641 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02006642 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006643 }
6644
Paul Bakkera503a632013-08-14 13:48:06 +02006645#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02006646 if( ssl->ticket_keys )
6647 {
6648 ssl_ticket_keys_free( ssl->ticket_keys );
6649 polarssl_free( ssl->ticket_keys );
6650 }
Paul Bakkera503a632013-08-14 13:48:06 +02006651#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006652
Paul Bakker0be444a2013-08-27 21:55:01 +02006653#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02006654 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006655 {
Paul Bakker34617722014-06-13 17:20:13 +02006656 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006657 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006658 ssl->hostname_len = 0;
6659 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006660#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006661
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006662#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006663 if( ssl->psk != NULL )
6664 {
Paul Bakker34617722014-06-13 17:20:13 +02006665 polarssl_zeroize( ssl->psk, ssl->psk_len );
6666 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006667 polarssl_free( ssl->psk );
6668 polarssl_free( ssl->psk_identity );
6669 ssl->psk_len = 0;
6670 ssl->psk_identity_len = 0;
6671 }
6672#endif
6673
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006674#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006675 ssl_key_cert_free( ssl->key_cert );
6676#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006677
Paul Bakker05ef8352012-05-08 09:17:57 +00006678#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6679 if( ssl_hw_record_finish != NULL )
6680 {
6681 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6682 ssl_hw_record_finish( ssl );
6683 }
6684#endif
6685
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006686#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006687 polarssl_free( ssl->cli_id );
6688#endif
6689
Paul Bakker5121ce52009-01-03 21:22:43 +00006690 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006691
Paul Bakker86f04f42013-02-14 11:20:09 +01006692 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006693 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006694}
6695
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006696#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006697/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006698 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006699 */
6700unsigned char ssl_sig_from_pk( pk_context *pk )
6701{
6702#if defined(POLARSSL_RSA_C)
6703 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6704 return( SSL_SIG_RSA );
6705#endif
6706#if defined(POLARSSL_ECDSA_C)
6707 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6708 return( SSL_SIG_ECDSA );
6709#endif
6710 return( SSL_SIG_ANON );
6711}
6712
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006713pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6714{
6715 switch( sig )
6716 {
6717#if defined(POLARSSL_RSA_C)
6718 case SSL_SIG_RSA:
6719 return( POLARSSL_PK_RSA );
6720#endif
6721#if defined(POLARSSL_ECDSA_C)
6722 case SSL_SIG_ECDSA:
6723 return( POLARSSL_PK_ECDSA );
6724#endif
6725 default:
6726 return( POLARSSL_PK_NONE );
6727 }
6728}
Paul Bakker9af723c2014-05-01 13:03:14 +02006729#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006730
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006731/*
6732 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6733 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006734md_type_t ssl_md_alg_from_hash( unsigned char hash )
6735{
6736 switch( hash )
6737 {
6738#if defined(POLARSSL_MD5_C)
6739 case SSL_HASH_MD5:
6740 return( POLARSSL_MD_MD5 );
6741#endif
6742#if defined(POLARSSL_SHA1_C)
6743 case SSL_HASH_SHA1:
6744 return( POLARSSL_MD_SHA1 );
6745#endif
6746#if defined(POLARSSL_SHA256_C)
6747 case SSL_HASH_SHA224:
6748 return( POLARSSL_MD_SHA224 );
6749 case SSL_HASH_SHA256:
6750 return( POLARSSL_MD_SHA256 );
6751#endif
6752#if defined(POLARSSL_SHA512_C)
6753 case SSL_HASH_SHA384:
6754 return( POLARSSL_MD_SHA384 );
6755 case SSL_HASH_SHA512:
6756 return( POLARSSL_MD_SHA512 );
6757#endif
6758 default:
6759 return( POLARSSL_MD_NONE );
6760 }
6761}
6762
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006763#if defined(POLARSSL_SSL_SET_CURVES)
6764/*
6765 * Check is a curve proposed by the peer is in our list.
6766 * Return 1 if we're willing to use it, 0 otherwise.
6767 */
6768int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6769{
6770 const ecp_group_id *gid;
6771
6772 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6773 if( *gid == grp_id )
6774 return( 1 );
6775
6776 return( 0 );
6777}
Paul Bakker9af723c2014-05-01 13:03:14 +02006778#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006779
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006780#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006781int ssl_check_cert_usage( const x509_crt *cert,
6782 const ssl_ciphersuite_t *ciphersuite,
6783 int cert_endpoint )
6784{
6785#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6786 int usage = 0;
6787#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006788#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6789 const char *ext_oid;
6790 size_t ext_len;
6791#endif
6792
6793#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6794 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6795 ((void) cert);
6796 ((void) cert_endpoint);
6797#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006798
6799#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6800 if( cert_endpoint == SSL_IS_SERVER )
6801 {
6802 /* Server part of the key exchange */
6803 switch( ciphersuite->key_exchange )
6804 {
6805 case POLARSSL_KEY_EXCHANGE_RSA:
6806 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6807 usage = KU_KEY_ENCIPHERMENT;
6808 break;
6809
6810 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6811 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6812 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6813 usage = KU_DIGITAL_SIGNATURE;
6814 break;
6815
6816 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6817 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6818 usage = KU_KEY_AGREEMENT;
6819 break;
6820
6821 /* Don't use default: we want warnings when adding new values */
6822 case POLARSSL_KEY_EXCHANGE_NONE:
6823 case POLARSSL_KEY_EXCHANGE_PSK:
6824 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6825 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6826 usage = 0;
6827 }
6828 }
6829 else
6830 {
6831 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6832 usage = KU_DIGITAL_SIGNATURE;
6833 }
6834
6835 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6836 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006837#else
6838 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006839#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6840
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006841#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6842 if( cert_endpoint == SSL_IS_SERVER )
6843 {
6844 ext_oid = OID_SERVER_AUTH;
6845 ext_len = OID_SIZE( OID_SERVER_AUTH );
6846 }
6847 else
6848 {
6849 ext_oid = OID_CLIENT_AUTH;
6850 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6851 }
6852
6853 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6854 return( -1 );
6855#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6856
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006857 return( 0 );
6858}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006859#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006860
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006861/*
6862 * Convert version numbers to/from wire format
6863 * and, for DTLS, to/from TLS equivalent.
6864 *
6865 * For TLS this is the identity.
6866 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6867 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6868 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6869 */
6870void ssl_write_version( int major, int minor, int transport,
6871 unsigned char ver[2] )
6872{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006873#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006874 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006875 {
6876 if( minor == SSL_MINOR_VERSION_2 )
6877 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6878
6879 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6880 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6881 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006882 else
6883#else
6884 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006885#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006886 {
6887 ver[0] = (unsigned char) major;
6888 ver[1] = (unsigned char) minor;
6889 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006890}
6891
6892void ssl_read_version( int *major, int *minor, int transport,
6893 const unsigned char ver[2] )
6894{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006895#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006896 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006897 {
6898 *major = 255 - ver[0] + 2;
6899 *minor = 255 - ver[1] + 1;
6900
6901 if( *minor == SSL_MINOR_VERSION_1 )
6902 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6903 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006904 else
6905#else
6906 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006907#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006908 {
6909 *major = ver[0];
6910 *minor = ver[1];
6911 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006912}
6913
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006914#endif /* POLARSSL_SSL_TLS_C */