blob: e9baa8f8115951fa4a75e0a8e767b503b29f4e6d [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é-Gonnardba958b82014-10-09 16:13:44 +02003208 ssl->state != SSL_HANDSHAKE_OVER &&
Manuel Pégourié-Gonnard2f5a1b42015-03-09 11:12:32 +00003209 ! ( ssl->renegotiation == SSL_RENEGOTIATION_IN_PROGRESS &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02003210 ssl->state == SSL_SERVER_HELLO ) )
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003211 {
3212 SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
3213 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3214 }
3215 }
3216#endif
3217
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003218 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003219 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003220 {
3221 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003222 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003223 }
3224
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003225 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003226 {
3227 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003228 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003229 }
3230
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003231 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003232#if defined(POLARSSL_SSL_PROTO_DTLS)
3233 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3234 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003235 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003236
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003237 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003238 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003239 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003240 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003241 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003242 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003243 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003244
3245#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3246 if( ssl_dtls_replay_check( ssl ) != 0 )
3247 {
3248 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3249 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3250 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003251#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003252 }
3253#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003254
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003255 /* Check length against the size of our buffer */
3256 if( ssl->in_msglen > SSL_BUFFER_LEN
3257 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003258 {
3259 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3260 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3261 }
3262
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003263 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003264 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003265 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003266 if( ssl->in_msglen < 1 ||
3267 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003268 {
3269 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003270 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003271 }
3272 }
3273 else
3274 {
Paul Bakker48916f92012-09-16 19:57:18 +00003275 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003276 {
3277 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003278 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003279 }
3280
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003281#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003282 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00003283 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003284 {
3285 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003286 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003287 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003288#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003289#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3290 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003291 /*
3292 * TLS encrypted messages can have up to 256 bytes of padding
3293 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003294 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003295 ssl->in_msglen > ssl->transform_in->minlen +
3296 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003297 {
3298 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003299 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003300 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003301#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003302 }
3303
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003304 return( 0 );
3305}
Paul Bakker5121ce52009-01-03 21:22:43 +00003306
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003307/*
3308 * If applicable, decrypt (and decompress) record content
3309 */
3310static int ssl_prepare_record_content( ssl_context *ssl )
3311{
3312 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003313
Paul Bakker5121ce52009-01-03 21:22:43 +00003314 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003315 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003316
Paul Bakker05ef8352012-05-08 09:17:57 +00003317#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003318 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003319 {
3320 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
3321
3322 ret = ssl_hw_record_read( ssl );
3323 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
3324 {
3325 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003326 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003327 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003328
3329 if( ret == 0 )
3330 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003331 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003332#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003333 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003334 {
3335 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3336 {
3337 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
3338 return( ret );
3339 }
3340
3341 SSL_DEBUG_BUF( 4, "input payload after decrypt",
3342 ssl->in_msg, ssl->in_msglen );
3343
3344 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
3345 {
3346 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003347 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003348 }
3349 }
3350
Paul Bakker2770fbd2012-07-03 13:30:23 +00003351#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003352 if( ssl->transform_in != NULL &&
3353 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003354 {
3355 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3356 {
3357 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
3358 return( ret );
3359 }
3360
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003361 // TODO: what's the purpose of these lines? is in_len used?
3362 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3363 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003364 }
3365#endif /* POLARSSL_ZLIB_SUPPORT */
3366
Manuel Pégourié-Gonnard8464a462014-09-24 14:05:32 +02003367#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003368 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3369 {
3370 ssl_dtls_replay_update( ssl );
3371 }
3372#endif
3373
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003374 return( 0 );
3375}
3376
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003377static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
3378
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003379/*
3380 * Read a record.
3381 *
3382 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
3383 * and continue reading until a valid record is found.
3384 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003385int ssl_read_record( ssl_context *ssl )
3386{
3387 int ret;
3388
3389 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
3390
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02003391 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003392 {
3393 /*
3394 * Get next Handshake message in the current record
3395 */
3396 ssl->in_msglen -= ssl->in_hslen;
3397
3398 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3399 ssl->in_msglen );
3400
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003401 SSL_DEBUG_BUF( 4, "remaining content in record",
3402 ssl->in_msg, ssl->in_msglen );
3403
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003404 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3405 return( ret );
3406
3407 return( 0 );
3408 }
3409
3410 ssl->in_hslen = 0;
3411
3412 /*
3413 * Read the record header and parse it
3414 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003415#if defined(POLARSSL_SSL_PROTO_DTLS)
3416read_record_header:
3417#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003418 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3419 {
3420 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3421 return( ret );
3422 }
3423
3424 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003425 {
3426#if defined(POLARSSL_SSL_PROTO_DTLS)
3427 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3428 {
3429 /* Ignore bad record and get next one; drop the whole datagram
3430 * since current header cannot be trusted to find the next record
3431 * in current datagram */
3432 ssl->next_record_offset = 0;
3433 ssl->in_left = 0;
3434
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003435 SSL_DEBUG_MSG( 1, ( "discarding invalid record (header)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003436 goto read_record_header;
3437 }
3438#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003439 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003440 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003441
3442 /*
3443 * Read and optionally decrypt the message contents
3444 */
3445 if( ( ret = ssl_fetch_input( ssl,
3446 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3447 {
3448 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3449 return( ret );
3450 }
3451
3452 /* Done reading this record, get ready for the next one */
3453#if defined(POLARSSL_SSL_PROTO_DTLS)
3454 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3455 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3456 else
3457#endif
3458 ssl->in_left = 0;
3459
3460 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003461 {
3462#if defined(POLARSSL_SSL_PROTO_DTLS)
3463 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3464 {
3465 /* Silently discard invalid records */
3466 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3467 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3468 {
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02003469#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
3470 if( ssl->badmac_limit != 0 &&
3471 ++ssl->badmac_seen >= ssl->badmac_limit )
3472 {
3473 SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
3474 return( POLARSSL_ERR_SSL_INVALID_MAC );
3475 }
3476#endif
3477
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003478 SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003479 goto read_record_header;
3480 }
3481
3482 return( ret );
3483 }
3484 else
3485#endif
3486 {
3487 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard9b669902015-03-06 16:52:46 +00003488#if defined(POLARSSL_SSL_ALL_ALERT_MESSAGES)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003489 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3490 {
3491 ssl_send_alert_message( ssl,
3492 SSL_ALERT_LEVEL_FATAL,
3493 SSL_ALERT_MSG_BAD_RECORD_MAC );
3494 }
3495#endif
3496 return( ret );
3497 }
3498 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003499
3500 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003501 * When we sent the last flight of the handshake, we MUST respond to a
3502 * retransmit of the peer's previous flight with a retransmit. (In
3503 * practice, only the Finished message will make it, other messages
3504 * including CCS use the old transform so they're dropped as invalid.)
3505 *
3506 * If the record we received is not a handshake message, however, it
3507 * means the peer received our last flight so we can clean up
3508 * handshake info.
3509 *
3510 * This check needs to be done before prepare_handshake() due to an edge
3511 * case: if the client immediately requests renegotiation, this
3512 * finishes the current handshake first, avoiding the new ClientHello
3513 * being mistaken for an ancient message in the current handshake.
3514 */
3515#if defined(POLARSSL_SSL_PROTO_DTLS)
3516 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3517 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003518 ssl->state == SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003519 {
3520 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3521 ssl->in_msg[0] == SSL_HS_FINISHED )
3522 {
3523 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3524
3525 if( ( ret = ssl_resend( ssl ) ) != 0 )
3526 {
3527 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3528 return( ret );
3529 }
3530
3531 return( POLARSSL_ERR_NET_WANT_READ );
3532 }
3533 else
3534 {
3535 ssl_handshake_wrapup_free_hs_transform( ssl );
3536 }
3537 }
3538#endif
3539
3540 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003541 * Handle particular types of records
3542 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003543 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3544 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003545 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3546 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003547 }
3548
3549 if( ssl->in_msgtype == SSL_MSG_ALERT )
3550 {
3551 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3552 ssl->in_msg[0], ssl->in_msg[1] ) );
3553
3554 /*
3555 * Ignore non-fatal alerts, except close_notify
3556 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003557 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003558 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003559 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3560 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003561 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003562 }
3563
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003564 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3565 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003566 {
3567 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003568 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003569 }
3570 }
3571
Paul Bakker5121ce52009-01-03 21:22:43 +00003572 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3573
3574 return( 0 );
3575}
3576
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003577int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3578{
3579 int ret;
3580
3581 if( ( ret = ssl_send_alert_message( ssl,
3582 SSL_ALERT_LEVEL_FATAL,
3583 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3584 {
3585 return( ret );
3586 }
3587
3588 return( 0 );
3589}
3590
Paul Bakker0a925182012-04-16 06:46:41 +00003591int ssl_send_alert_message( ssl_context *ssl,
3592 unsigned char level,
3593 unsigned char message )
3594{
3595 int ret;
3596
3597 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3598
3599 ssl->out_msgtype = SSL_MSG_ALERT;
3600 ssl->out_msglen = 2;
3601 ssl->out_msg[0] = level;
3602 ssl->out_msg[1] = message;
3603
3604 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3605 {
3606 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3607 return( ret );
3608 }
3609
3610 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3611
3612 return( 0 );
3613}
3614
Paul Bakker5121ce52009-01-03 21:22:43 +00003615/*
3616 * Handshake functions
3617 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003618#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3619 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3620 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3621 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3622 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3623 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3624 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003625int ssl_write_certificate( ssl_context *ssl )
3626{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003627 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003628
3629 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3630
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003631 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003632 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3633 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003634 {
3635 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3636 ssl->state++;
3637 return( 0 );
3638 }
3639
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003640 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3641 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003642}
3643
3644int ssl_parse_certificate( ssl_context *ssl )
3645{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003646 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3647
3648 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3649
3650 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003651 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3652 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003653 {
3654 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3655 ssl->state++;
3656 return( 0 );
3657 }
3658
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003659 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3660 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003661}
3662#else
3663int ssl_write_certificate( ssl_context *ssl )
3664{
3665 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3666 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003667 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003668 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3669
3670 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3671
3672 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003673 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3674 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003675 {
3676 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3677 ssl->state++;
3678 return( 0 );
3679 }
3680
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003681#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003682 if( ssl->endpoint == SSL_IS_CLIENT )
3683 {
3684 if( ssl->client_auth == 0 )
3685 {
3686 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3687 ssl->state++;
3688 return( 0 );
3689 }
3690
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003691#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003692 /*
3693 * If using SSLv3 and got no cert, send an Alert message
3694 * (otherwise an empty Certificate message will be sent).
3695 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003696 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003697 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3698 {
3699 ssl->out_msglen = 2;
3700 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003701 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3702 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003703
3704 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3705 goto write_msg;
3706 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003707#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003708 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003709#endif /* POLARSSL_SSL_CLI_C */
3710#if defined(POLARSSL_SSL_SRV_C)
3711 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003712 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003713 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003714 {
3715 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003716 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003717 }
3718 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003719#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003720
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003721 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003722
3723 /*
3724 * 0 . 0 handshake type
3725 * 1 . 3 handshake length
3726 * 4 . 6 length of all certs
3727 * 7 . 9 length of cert. 1
3728 * 10 . n-1 peer certificate
3729 * n . n+2 length of cert. 2
3730 * n+3 . ... upper level cert, etc.
3731 */
3732 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003733 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003734
Paul Bakker29087132010-03-21 21:03:34 +00003735 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003736 {
3737 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003738 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003739 {
3740 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3741 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003742 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003743 }
3744
3745 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3746 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3747 ssl->out_msg[i + 2] = (unsigned char)( n );
3748
3749 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3750 i += n; crt = crt->next;
3751 }
3752
3753 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3754 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3755 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3756
3757 ssl->out_msglen = i;
3758 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3759 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3760
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003761#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003762write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003763#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003764
3765 ssl->state++;
3766
3767 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3768 {
3769 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3770 return( ret );
3771 }
3772
3773 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3774
Paul Bakkered27a042013-04-18 22:46:23 +02003775 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003776}
3777
3778int ssl_parse_certificate( ssl_context *ssl )
3779{
Paul Bakkered27a042013-04-18 22:46:23 +02003780 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003781 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003782 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003783
3784 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3785
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003786 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003787 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3788 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003789 {
3790 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3791 ssl->state++;
3792 return( 0 );
3793 }
3794
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003795#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003796 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003797 ( ssl->authmode == SSL_VERIFY_NONE ||
3798 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003799 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003800 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003801 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3802 ssl->state++;
3803 return( 0 );
3804 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003805#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003806
3807 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3808 {
3809 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3810 return( ret );
3811 }
3812
3813 ssl->state++;
3814
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003815#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003816#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003817 /*
3818 * Check if the client sent an empty certificate
3819 */
3820 if( ssl->endpoint == SSL_IS_SERVER &&
3821 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3822 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003823 if( ssl->in_msglen == 2 &&
3824 ssl->in_msgtype == SSL_MSG_ALERT &&
3825 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3826 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003827 {
3828 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3829
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003830 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003831 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3832 return( 0 );
3833 else
Paul Bakker40e46942009-01-03 21:51:57 +00003834 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003835 }
3836 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003837#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003838
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003839#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3840 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003841 if( ssl->endpoint == SSL_IS_SERVER &&
3842 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3843 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003844 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003845 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3846 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003847 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003848 {
3849 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3850
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003851 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003852 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003853 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003854 else
3855 return( 0 );
3856 }
3857 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003858#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3859 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003860#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003861
3862 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3863 {
3864 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003865 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003866 }
3867
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003868 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3869 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003870 {
3871 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003872 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003873 }
3874
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003875 i = ssl_hs_hdr_len( ssl );
3876
Paul Bakker5121ce52009-01-03 21:22:43 +00003877 /*
3878 * Same message structure as in ssl_write_certificate()
3879 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003880 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003881
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003882 if( ssl->in_msg[i] != 0 ||
3883 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003884 {
3885 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003886 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003887 }
3888
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003889 /* In case we tried to reuse a session but it failed */
3890 if( ssl->session_negotiate->peer_cert != NULL )
3891 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003892 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003893 polarssl_free( ssl->session_negotiate->peer_cert );
3894 }
3895
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003896 if( ( ssl->session_negotiate->peer_cert = polarssl_malloc(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003897 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003898 {
3899 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003900 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003901 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003902 }
3903
Paul Bakkerb6b09562013-09-18 14:17:41 +02003904 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003905
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003906 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003907
3908 while( i < ssl->in_hslen )
3909 {
3910 if( ssl->in_msg[i] != 0 )
3911 {
3912 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003913 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003914 }
3915
3916 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3917 | (unsigned int) ssl->in_msg[i + 2];
3918 i += 3;
3919
3920 if( n < 128 || i + n > ssl->in_hslen )
3921 {
3922 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003923 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003924 }
3925
Paul Bakkerddf26b42013-09-18 13:46:23 +02003926 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3927 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003928 if( ret != 0 )
3929 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003930 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003931 return( ret );
3932 }
3933
3934 i += n;
3935 }
3936
Paul Bakker48916f92012-09-16 19:57:18 +00003937 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003938
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003939 /*
3940 * On client, make sure the server cert doesn't change during renego to
3941 * avoid "triple handshake" attack: https://secure-resumption.com/
3942 */
Paul Bakkerf6080b82015-01-13 16:18:23 +01003943#if defined(POLARSSL_SSL_RENEGOTIATION) && defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003944 if( ssl->endpoint == SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2f5a1b42015-03-09 11:12:32 +00003945 ssl->renegotiation == SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003946 {
3947 if( ssl->session->peer_cert == NULL )
3948 {
3949 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3950 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3951 }
3952
3953 if( ssl->session->peer_cert->raw.len !=
3954 ssl->session_negotiate->peer_cert->raw.len ||
3955 memcmp( ssl->session->peer_cert->raw.p,
3956 ssl->session_negotiate->peer_cert->raw.p,
3957 ssl->session->peer_cert->raw.len ) != 0 )
3958 {
3959 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3960 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3961 }
3962 }
Paul Bakkerf6080b82015-01-13 16:18:23 +01003963#endif /* POLARSSL_SSL_RENEGOTIATION && POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003964
Paul Bakker5121ce52009-01-03 21:22:43 +00003965 if( ssl->authmode != SSL_VERIFY_NONE )
3966 {
3967 if( ssl->ca_chain == NULL )
3968 {
3969 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003970 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003971 }
3972
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003973 /*
3974 * Main check: verify certificate
3975 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003976 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3977 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3978 &ssl->session_negotiate->verify_result,
3979 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003980
3981 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003982 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003983 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003984 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003985
3986 /*
3987 * Secondary checks: always done, but change 'ret' only if it was 0
3988 */
3989
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003990#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003991 {
Paul Bakker93389cc2014-04-17 14:44:38 +02003992 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003993
3994 /* If certificate uses an EC key, make sure the curve is OK */
3995 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3996 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
3997 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003998 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003999 if( ret == 0 )
4000 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004001 }
4002 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004003#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00004004
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004005 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
4006 ciphersuite_info,
4007 ! ssl->endpoint ) != 0 )
4008 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02004009 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004010 if( ret == 0 )
4011 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
4012 }
4013
Paul Bakker5121ce52009-01-03 21:22:43 +00004014 if( ssl->authmode != SSL_VERIFY_REQUIRED )
4015 ret = 0;
4016 }
4017
4018 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
4019
4020 return( ret );
4021}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01004022#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
4023 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
4024 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
4025 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
4026 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
4027 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
4028 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004029
4030int ssl_write_change_cipher_spec( ssl_context *ssl )
4031{
4032 int ret;
4033
4034 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
4035
4036 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
4037 ssl->out_msglen = 1;
4038 ssl->out_msg[0] = 1;
4039
Paul Bakker5121ce52009-01-03 21:22:43 +00004040 ssl->state++;
4041
4042 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4043 {
4044 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4045 return( ret );
4046 }
4047
4048 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
4049
4050 return( 0 );
4051}
4052
4053int ssl_parse_change_cipher_spec( ssl_context *ssl )
4054{
4055 int ret;
4056
4057 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
4058
Paul Bakker5121ce52009-01-03 21:22:43 +00004059 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4060 {
4061 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4062 return( ret );
4063 }
4064
4065 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
4066 {
4067 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004068 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004069 }
4070
4071 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
4072 {
4073 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004074 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00004075 }
4076
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004077 /*
4078 * Switch to our negotiated transform and session parameters for inbound
4079 * data.
4080 */
4081 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
4082 ssl->transform_in = ssl->transform_negotiate;
4083 ssl->session_in = ssl->session_negotiate;
4084
4085#if defined(POLARSSL_SSL_PROTO_DTLS)
4086 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4087 {
4088#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4089 ssl_dtls_replay_reset( ssl );
4090#endif
4091
4092 /* Increment epoch */
4093 if( ++ssl->in_epoch == 0 )
4094 {
4095 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4096 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4097 }
4098 }
4099 else
4100#endif /* POLARSSL_SSL_PROTO_DTLS */
4101 memset( ssl->in_ctr, 0, 8 );
4102
4103 /*
4104 * Set the in_msg pointer to the correct location based on IV length
4105 */
4106 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4107 {
4108 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
4109 ssl->transform_negotiate->fixed_ivlen;
4110 }
4111 else
4112 ssl->in_msg = ssl->in_iv;
4113
4114#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4115 if( ssl_hw_record_activate != NULL )
4116 {
4117 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
4118 {
4119 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4120 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4121 }
4122 }
4123#endif
4124
Paul Bakker5121ce52009-01-03 21:22:43 +00004125 ssl->state++;
4126
4127 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
4128
4129 return( 0 );
4130}
4131
Paul Bakker41c83d32013-03-20 14:39:14 +01004132void ssl_optimize_checksum( ssl_context *ssl,
4133 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00004134{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02004135 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01004136
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004137#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4138 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00004139 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00004140 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00004141 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004142#endif
4143#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4144#if defined(POLARSSL_SHA512_C)
4145 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
4146 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
4147 else
4148#endif
4149#if defined(POLARSSL_SHA256_C)
4150 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00004151 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004152 else
4153#endif
4154#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004155 {
4156 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004157 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004158 }
Paul Bakker380da532012-04-18 16:10:25 +00004159}
Paul Bakkerf7abd422013-04-16 13:15:56 +02004160
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004161void ssl_reset_checksum( ssl_context *ssl )
4162{
4163#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4164 defined(POLARSSL_SSL_PROTO_TLS1_1)
4165 md5_starts( &ssl->handshake->fin_md5 );
4166 sha1_starts( &ssl->handshake->fin_sha1 );
4167#endif
4168#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4169#if defined(POLARSSL_SHA256_C)
4170 sha256_starts( &ssl->handshake->fin_sha256, 0 );
4171#endif
4172#if defined(POLARSSL_SHA512_C)
4173 sha512_starts( &ssl->handshake->fin_sha512, 1 );
4174#endif
4175#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4176}
4177
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004178static void ssl_update_checksum_start( ssl_context *ssl,
4179 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004180{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004181#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4182 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00004183 md5_update( &ssl->handshake->fin_md5 , buf, len );
4184 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004185#endif
4186#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4187#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02004188 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004189#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02004190#if defined(POLARSSL_SHA512_C)
4191 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01004192#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004193#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004194}
4195
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004196#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4197 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004198static void ssl_update_checksum_md5sha1( ssl_context *ssl,
4199 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004200{
Paul Bakker48916f92012-09-16 19:57:18 +00004201 md5_update( &ssl->handshake->fin_md5 , buf, len );
4202 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004203}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004204#endif
Paul Bakker380da532012-04-18 16:10:25 +00004205
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004206#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4207#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004208static void ssl_update_checksum_sha256( ssl_context *ssl,
4209 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004210{
Paul Bakker9e36f042013-06-30 14:34:05 +02004211 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004212}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004213#endif
Paul Bakker380da532012-04-18 16:10:25 +00004214
Paul Bakker9e36f042013-06-30 14:34:05 +02004215#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004216static void ssl_update_checksum_sha384( ssl_context *ssl,
4217 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004218{
Paul Bakker9e36f042013-06-30 14:34:05 +02004219 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004220}
Paul Bakker769075d2012-11-24 11:26:46 +01004221#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004222#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004223
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004224#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004225static void ssl_calc_finished_ssl(
4226 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004227{
Paul Bakker3c2122f2013-06-24 19:03:14 +02004228 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004229 md5_context md5;
4230 sha1_context sha1;
4231
Paul Bakker5121ce52009-01-03 21:22:43 +00004232 unsigned char padbuf[48];
4233 unsigned char md5sum[16];
4234 unsigned char sha1sum[20];
4235
Paul Bakker48916f92012-09-16 19:57:18 +00004236 ssl_session *session = ssl->session_negotiate;
4237 if( !session )
4238 session = ssl->session;
4239
Paul Bakker1ef83d62012-04-11 12:09:53 +00004240 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
4241
Paul Bakker48916f92012-09-16 19:57:18 +00004242 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4243 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004244
4245 /*
4246 * SSLv3:
4247 * hash =
4248 * MD5( master + pad2 +
4249 * MD5( handshake + sender + master + pad1 ) )
4250 * + SHA1( master + pad2 +
4251 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004252 */
4253
Paul Bakker90995b52013-06-24 19:20:35 +02004254#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004255 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004256 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004257#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004258
Paul Bakker90995b52013-06-24 19:20:35 +02004259#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004260 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004261 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004262#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004263
Paul Bakker3c2122f2013-06-24 19:03:14 +02004264 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
4265 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004266
Paul Bakker1ef83d62012-04-11 12:09:53 +00004267 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004268
Paul Bakker3c2122f2013-06-24 19:03:14 +02004269 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004270 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004271 md5_update( &md5, padbuf, 48 );
4272 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004273
Paul Bakker3c2122f2013-06-24 19:03:14 +02004274 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004275 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004276 sha1_update( &sha1, padbuf, 40 );
4277 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004278
Paul Bakker1ef83d62012-04-11 12:09:53 +00004279 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004280
Paul Bakker1ef83d62012-04-11 12:09:53 +00004281 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00004282 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004283 md5_update( &md5, padbuf, 48 );
4284 md5_update( &md5, md5sum, 16 );
4285 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004286
Paul Bakker1ef83d62012-04-11 12:09:53 +00004287 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00004288 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004289 sha1_update( &sha1, padbuf , 40 );
4290 sha1_update( &sha1, sha1sum, 20 );
4291 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004292
Paul Bakker1ef83d62012-04-11 12:09:53 +00004293 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004294
Paul Bakker5b4af392014-06-26 12:09:34 +02004295 md5_free( &md5 );
4296 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004297
Paul Bakker34617722014-06-13 17:20:13 +02004298 polarssl_zeroize( padbuf, sizeof( padbuf ) );
4299 polarssl_zeroize( md5sum, sizeof( md5sum ) );
4300 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004301
4302 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4303}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004304#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004305
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004306#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004307static void ssl_calc_finished_tls(
4308 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004309{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004310 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004311 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004312 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00004313 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004314 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004315
Paul Bakker48916f92012-09-16 19:57:18 +00004316 ssl_session *session = ssl->session_negotiate;
4317 if( !session )
4318 session = ssl->session;
4319
Paul Bakker1ef83d62012-04-11 12:09:53 +00004320 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004321
Paul Bakker48916f92012-09-16 19:57:18 +00004322 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4323 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004324
Paul Bakker1ef83d62012-04-11 12:09:53 +00004325 /*
4326 * TLSv1:
4327 * hash = PRF( master, finished_label,
4328 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4329 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004330
Paul Bakker90995b52013-06-24 19:20:35 +02004331#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004332 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4333 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004334#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004335
Paul Bakker90995b52013-06-24 19:20:35 +02004336#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004337 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4338 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004339#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004340
4341 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004342 ? "client finished"
4343 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004344
4345 md5_finish( &md5, padbuf );
4346 sha1_finish( &sha1, padbuf + 16 );
4347
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004348 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004349 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004350
4351 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4352
Paul Bakker5b4af392014-06-26 12:09:34 +02004353 md5_free( &md5 );
4354 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004355
Paul Bakker34617722014-06-13 17:20:13 +02004356 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004357
4358 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4359}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004360#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004361
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004362#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4363#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004364static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004365 ssl_context *ssl, unsigned char *buf, int from )
4366{
4367 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004368 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004369 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004370 unsigned char padbuf[32];
4371
Paul Bakker48916f92012-09-16 19:57:18 +00004372 ssl_session *session = ssl->session_negotiate;
4373 if( !session )
4374 session = ssl->session;
4375
Paul Bakker380da532012-04-18 16:10:25 +00004376 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004377
Paul Bakker9e36f042013-06-30 14:34:05 +02004378 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004379
4380 /*
4381 * TLSv1.2:
4382 * hash = PRF( master, finished_label,
4383 * Hash( handshake ) )[0.11]
4384 */
4385
Paul Bakker9e36f042013-06-30 14:34:05 +02004386#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004387 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004388 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004389#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004390
4391 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004392 ? "client finished"
4393 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004394
Paul Bakker9e36f042013-06-30 14:34:05 +02004395 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004396
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004397 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004398 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004399
4400 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4401
Paul Bakker5b4af392014-06-26 12:09:34 +02004402 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004403
Paul Bakker34617722014-06-13 17:20:13 +02004404 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004405
4406 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4407}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004408#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004409
Paul Bakker9e36f042013-06-30 14:34:05 +02004410#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004411static void ssl_calc_finished_tls_sha384(
4412 ssl_context *ssl, unsigned char *buf, int from )
4413{
4414 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004415 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004416 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004417 unsigned char padbuf[48];
4418
Paul Bakker48916f92012-09-16 19:57:18 +00004419 ssl_session *session = ssl->session_negotiate;
4420 if( !session )
4421 session = ssl->session;
4422
Paul Bakker380da532012-04-18 16:10:25 +00004423 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004424
Paul Bakker9e36f042013-06-30 14:34:05 +02004425 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004426
4427 /*
4428 * TLSv1.2:
4429 * hash = PRF( master, finished_label,
4430 * Hash( handshake ) )[0.11]
4431 */
4432
Paul Bakker9e36f042013-06-30 14:34:05 +02004433#if !defined(POLARSSL_SHA512_ALT)
4434 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4435 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004436#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004437
4438 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004439 ? "client finished"
4440 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004441
Paul Bakker9e36f042013-06-30 14:34:05 +02004442 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004443
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004444 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004445 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004446
4447 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4448
Paul Bakker5b4af392014-06-26 12:09:34 +02004449 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004450
Paul Bakker34617722014-06-13 17:20:13 +02004451 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004452
4453 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4454}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004455#endif /* POLARSSL_SHA512_C */
4456#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004457
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004458static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004459{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004460 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004461
4462 /*
4463 * Free our handshake params
4464 */
4465 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004466 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004467 ssl->handshake = NULL;
4468
4469 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004470 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004471 */
4472 if( ssl->transform )
4473 {
4474 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004475 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004476 }
4477 ssl->transform = ssl->transform_negotiate;
4478 ssl->transform_negotiate = NULL;
4479
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004480 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4481}
4482
4483void ssl_handshake_wrapup( ssl_context *ssl )
4484{
4485 int resume = ssl->handshake->resume;
4486
4487 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4488
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004489#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2f5a1b42015-03-09 11:12:32 +00004490 if( ssl->renegotiation == SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004491 {
4492 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
4493 ssl->renego_records_seen = 0;
4494 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004495#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004496
4497 /*
4498 * Free the previous session and switch in the current one
4499 */
Paul Bakker0a597072012-09-25 21:55:46 +00004500 if( ssl->session )
4501 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01004502#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4503 /* RFC 7366 3.1: keep the EtM state */
4504 ssl->session_negotiate->encrypt_then_mac =
4505 ssl->session->encrypt_then_mac;
4506#endif
4507
Paul Bakker0a597072012-09-25 21:55:46 +00004508 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004509 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004510 }
4511 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004512 ssl->session_negotiate = NULL;
4513
Paul Bakker0a597072012-09-25 21:55:46 +00004514 /*
4515 * Add cache entry
4516 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004517 if( ssl->f_set_cache != NULL &&
4518 ssl->session->length != 0 &&
4519 resume == 0 )
4520 {
Paul Bakker0a597072012-09-25 21:55:46 +00004521 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4522 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004523 }
Paul Bakker0a597072012-09-25 21:55:46 +00004524
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004525#if defined(POLARSSL_SSL_PROTO_DTLS)
4526 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4527 ssl->handshake->flight != NULL )
4528 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02004529 /* Cancel handshake timer */
4530 ssl_set_timer( ssl, 0 );
4531
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004532 /* Keep last flight around in case we need to resend it:
4533 * we need the handshake and transform structures for that */
4534 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4535 }
4536 else
4537#endif
4538 ssl_handshake_wrapup_free_hs_transform( ssl );
4539
Paul Bakker48916f92012-09-16 19:57:18 +00004540 ssl->state++;
4541
4542 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4543}
4544
Paul Bakker1ef83d62012-04-11 12:09:53 +00004545int ssl_write_finished( ssl_context *ssl )
4546{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004547 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004548
4549 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4550
Paul Bakker92be97b2013-01-02 17:30:03 +01004551 /*
4552 * Set the out_msg pointer to the correct location based on IV length
4553 */
4554 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4555 {
4556 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4557 ssl->transform_negotiate->fixed_ivlen;
4558 }
4559 else
4560 ssl->out_msg = ssl->out_iv;
4561
Paul Bakker48916f92012-09-16 19:57:18 +00004562 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004563
4564 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004565 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4566
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004567#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004568 ssl->verify_data_len = hash_len;
4569 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004570#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004571
Paul Bakker5121ce52009-01-03 21:22:43 +00004572 ssl->out_msglen = 4 + hash_len;
4573 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4574 ssl->out_msg[0] = SSL_HS_FINISHED;
4575
4576 /*
4577 * In case of session resuming, invert the client and server
4578 * ChangeCipherSpec messages order.
4579 */
Paul Bakker0a597072012-09-25 21:55:46 +00004580 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004581 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004582#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004583 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004584 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004585#endif
4586#if defined(POLARSSL_SSL_SRV_C)
4587 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004588 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004589#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004590 }
4591 else
4592 ssl->state++;
4593
Paul Bakker48916f92012-09-16 19:57:18 +00004594 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004595 * Switch to our negotiated transform and session parameters for outbound
4596 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004597 */
4598 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004599
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004600#if defined(POLARSSL_SSL_PROTO_DTLS)
4601 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4602 {
4603 unsigned char i;
4604
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004605 /* Remember current epoch settings for resending */
4606 ssl->handshake->alt_transform_out = ssl->transform_out;
4607 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4608
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004609 /* Set sequence_number to zero */
4610 memset( ssl->out_ctr + 2, 0, 6 );
4611
4612 /* Increment epoch */
4613 for( i = 2; i > 0; i-- )
4614 if( ++ssl->out_ctr[i - 1] != 0 )
4615 break;
4616
4617 /* The loop goes to its end iff the counter is wrapping */
4618 if( i == 0 )
4619 {
4620 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4621 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4622 }
4623 }
4624 else
4625#endif /* POLARSSL_SSL_PROTO_DTLS */
4626 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004627
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004628 ssl->transform_out = ssl->transform_negotiate;
4629 ssl->session_out = ssl->session_negotiate;
4630
Paul Bakker07eb38b2012-12-19 14:42:06 +01004631#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004632 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004633 {
4634 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4635 {
4636 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4637 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4638 }
4639 }
4640#endif
4641
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004642#if defined(POLARSSL_SSL_PROTO_DTLS)
4643 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4644 ssl_send_flight_completed( ssl );
4645#endif
4646
Paul Bakker5121ce52009-01-03 21:22:43 +00004647 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4648 {
4649 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4650 return( ret );
4651 }
4652
4653 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4654
4655 return( 0 );
4656}
4657
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004658#if defined(POLARSSL_SSL_PROTO_SSL3)
4659#define SSL_MAX_HASH_LEN 36
4660#else
4661#define SSL_MAX_HASH_LEN 12
4662#endif
4663
Paul Bakker5121ce52009-01-03 21:22:43 +00004664int ssl_parse_finished( ssl_context *ssl )
4665{
Paul Bakker23986e52011-04-24 08:57:21 +00004666 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004667 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004668 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004669
4670 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4671
Paul Bakker48916f92012-09-16 19:57:18 +00004672 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004673
Paul Bakker5121ce52009-01-03 21:22:43 +00004674 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4675 {
4676 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4677 return( ret );
4678 }
4679
4680 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4681 {
4682 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004683 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004684 }
4685
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004686 /* There is currently no ciphersuite using another length with TLS 1.2 */
4687#if defined(POLARSSL_SSL_PROTO_SSL3)
4688 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4689 hash_len = 36;
4690 else
4691#endif
4692 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004693
4694 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004695 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004696 {
4697 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004698 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004699 }
4700
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004701 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4702 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004703 {
4704 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004705 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004706 }
4707
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004708#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004709 ssl->verify_data_len = hash_len;
4710 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004711#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004712
Paul Bakker0a597072012-09-25 21:55:46 +00004713 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004714 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004715#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004716 if( ssl->endpoint == SSL_IS_CLIENT )
4717 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004718#endif
4719#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004720 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004721 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004722#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004723 }
4724 else
4725 ssl->state++;
4726
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004727#if defined(POLARSSL_SSL_PROTO_DTLS)
4728 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4729 ssl_recv_flight_completed( ssl );
4730#endif
4731
Paul Bakker5121ce52009-01-03 21:22:43 +00004732 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4733
4734 return( 0 );
4735}
4736
Paul Bakker968afaa2014-07-09 11:09:24 +02004737static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004738{
4739 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4740
4741#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4742 defined(POLARSSL_SSL_PROTO_TLS1_1)
4743 md5_init( &handshake->fin_md5 );
4744 sha1_init( &handshake->fin_sha1 );
4745 md5_starts( &handshake->fin_md5 );
4746 sha1_starts( &handshake->fin_sha1 );
4747#endif
4748#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4749#if defined(POLARSSL_SHA256_C)
4750 sha256_init( &handshake->fin_sha256 );
4751 sha256_starts( &handshake->fin_sha256, 0 );
4752#endif
4753#if defined(POLARSSL_SHA512_C)
4754 sha512_init( &handshake->fin_sha512 );
4755 sha512_starts( &handshake->fin_sha512, 1 );
4756#endif
4757#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4758
4759 handshake->update_checksum = ssl_update_checksum_start;
4760 handshake->sig_alg = SSL_HASH_SHA1;
4761
4762#if defined(POLARSSL_DHM_C)
4763 dhm_init( &handshake->dhm_ctx );
4764#endif
4765#if defined(POLARSSL_ECDH_C)
4766 ecdh_init( &handshake->ecdh_ctx );
4767#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004768}
4769
4770static void ssl_transform_init( ssl_transform *transform )
4771{
4772 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004773
4774 cipher_init( &transform->cipher_ctx_enc );
4775 cipher_init( &transform->cipher_ctx_dec );
4776
4777 md_init( &transform->md_ctx_enc );
4778 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004779}
4780
4781void ssl_session_init( ssl_session *session )
4782{
4783 memset( session, 0, sizeof(ssl_session) );
4784}
4785
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004786static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004787{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004788 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004789 if( ssl->transform_negotiate )
4790 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004791 if( ssl->session_negotiate )
4792 ssl_session_free( ssl->session_negotiate );
4793 if( ssl->handshake )
4794 ssl_handshake_free( ssl->handshake );
4795
4796 /*
4797 * Either the pointers are now NULL or cleared properly and can be freed.
4798 * Now allocate missing structures.
4799 */
4800 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004801 {
Mansour Moufid99b92592015-02-15 17:46:32 -05004802 ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004803 }
Paul Bakker48916f92012-09-16 19:57:18 +00004804
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004805 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004806 {
Mansour Moufid99b92592015-02-15 17:46:32 -05004807 ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004808 }
Paul Bakker48916f92012-09-16 19:57:18 +00004809
Paul Bakker82788fb2014-10-20 13:59:19 +02004810 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004811 {
Mansour Moufid99b92592015-02-15 17:46:32 -05004812 ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004813 }
Paul Bakker48916f92012-09-16 19:57:18 +00004814
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004815 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004816 if( ssl->handshake == NULL ||
4817 ssl->transform_negotiate == NULL ||
4818 ssl->session_negotiate == NULL )
4819 {
4820 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004821
4822 polarssl_free( ssl->handshake );
4823 polarssl_free( ssl->transform_negotiate );
4824 polarssl_free( ssl->session_negotiate );
4825
4826 ssl->handshake = NULL;
4827 ssl->transform_negotiate = NULL;
4828 ssl->session_negotiate = NULL;
4829
Paul Bakker48916f92012-09-16 19:57:18 +00004830 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4831 }
4832
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004833 /* Initialize structures */
4834 ssl_session_init( ssl->session_negotiate );
4835 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004836 ssl_handshake_params_init( ssl->handshake );
4837
4838#if defined(POLARSSL_X509_CRT_PARSE_C)
4839 ssl->handshake->key_cert = ssl->key_cert;
4840#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004841
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004842 /*
4843 * We may not know yet if we're using DTLS,
4844 * so always initiliase DTLS-specific fields.
4845 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004846#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004847 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004848
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004849 // TODO: not the right place, we may not know endpoint yet
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004850 if( ssl->endpoint == SSL_IS_CLIENT )
4851 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4852 else
4853 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004854#endif
4855
Paul Bakker48916f92012-09-16 19:57:18 +00004856 return( 0 );
4857}
4858
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004859#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4860/* Dummy cookie callbacks for defaults */
4861static int ssl_cookie_write_dummy( void *ctx,
4862 unsigned char **p, unsigned char *end,
4863 const unsigned char *cli_id, size_t cli_id_len )
4864{
4865 ((void) ctx);
4866 ((void) p);
4867 ((void) end);
4868 ((void) cli_id);
4869 ((void) cli_id_len);
4870
4871 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4872}
4873
4874static int ssl_cookie_check_dummy( void *ctx,
4875 const unsigned char *cookie, size_t cookie_len,
4876 const unsigned char *cli_id, size_t cli_id_len )
4877{
4878 ((void) ctx);
4879 ((void) cookie);
4880 ((void) cookie_len);
4881 ((void) cli_id);
4882 ((void) cli_id_len);
4883
4884 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4885}
4886#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4887
Paul Bakker5121ce52009-01-03 21:22:43 +00004888/*
4889 * Initialize an SSL context
4890 */
4891int ssl_init( ssl_context *ssl )
4892{
Paul Bakker48916f92012-09-16 19:57:18 +00004893 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004894 int len = SSL_BUFFER_LEN;
4895
4896 memset( ssl, 0, sizeof( ssl_context ) );
4897
Paul Bakker62f2dee2012-09-28 07:31:51 +00004898 /*
4899 * Sane defaults
4900 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004901 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4902 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4903 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4904 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004905
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004906 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004907
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004908#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004909 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004910 memset( ssl->renego_period, 0xFF, 7 );
4911 ssl->renego_period[7] = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004912#endif
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004913
Paul Bakker62f2dee2012-09-28 07:31:51 +00004914#if defined(POLARSSL_DHM_C)
4915 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4916 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4917 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4918 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4919 {
4920 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4921 return( ret );
4922 }
4923#endif
4924
4925 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004926 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004927 */
Manuel Pégourié-Gonnard4e41c992015-02-18 10:39:49 +00004928 if( ( ssl->in_buf = polarssl_malloc( len ) ) == NULL ||
4929 ( ssl->out_buf = polarssl_malloc( len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004930 {
4931 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004932 polarssl_free( ssl->in_buf );
4933 ssl->in_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004934 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004935 }
4936
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004937 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4938 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004939
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004940 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4941 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4942
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004943#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4944 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
4945#endif
4946
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004947#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4948 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
4949#endif
4950
Paul Bakker606b4ba2013-08-14 16:52:14 +02004951#if defined(POLARSSL_SSL_SESSION_TICKETS)
4952 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4953#endif
4954
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004955#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004956 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004957#endif
4958
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004959#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4960 ssl->f_cookie_write = ssl_cookie_write_dummy;
4961 ssl->f_cookie_check = ssl_cookie_check_dummy;
4962#endif
4963
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004964#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4965 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
4966#endif
4967
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004968#if defined(POLARSSL_SSL_PROTO_DTLS)
4969 ssl->hs_timeout_min = SSL_DTLS_TIMEOUT_DFL_MIN;
4970 ssl->hs_timeout_max = SSL_DTLS_TIMEOUT_DFL_MAX;
4971#endif
4972
Paul Bakker48916f92012-09-16 19:57:18 +00004973 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4974 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004975
4976 return( 0 );
4977}
4978
4979/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004980 * Reset an initialized and used SSL context for re-use while retaining
4981 * all application-set variables, function pointers and data.
4982 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004983int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004984{
Paul Bakker48916f92012-09-16 19:57:18 +00004985 int ret;
4986
Paul Bakker7eb013f2011-10-06 12:37:39 +00004987 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004988
4989#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004990 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004991 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004992
4993 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard61860192014-11-04 13:05:42 +01004994 memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
4995 memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004996#endif
4997 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00004998
Paul Bakker7eb013f2011-10-06 12:37:39 +00004999 ssl->in_offt = NULL;
5000
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005001 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005002 ssl->in_msgtype = 0;
5003 ssl->in_msglen = 0;
5004 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005005#if defined(POLARSSL_SSL_PROTO_DTLS)
5006 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005007 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005008#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005009#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
5010 ssl_dtls_replay_reset( ssl );
5011#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005012
5013 ssl->in_hslen = 0;
5014 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005015 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005016
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005017 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005018 ssl->out_msgtype = 0;
5019 ssl->out_msglen = 0;
5020 ssl->out_left = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005021#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005022 if( ssl->split_done != SSL_CBC_RECORD_SPLITTING_DISABLED )
5023 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005024#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005025
Paul Bakker48916f92012-09-16 19:57:18 +00005026 ssl->transform_in = NULL;
5027 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005028
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005029 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
5030 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00005031
5032#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02005033 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005034 {
5035 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005036 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005037 {
5038 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
5039 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
5040 }
Paul Bakker05ef8352012-05-08 09:17:57 +00005041 }
5042#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00005043
Paul Bakker48916f92012-09-16 19:57:18 +00005044 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005045 {
Paul Bakker48916f92012-09-16 19:57:18 +00005046 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005047 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005048 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00005049 }
Paul Bakker48916f92012-09-16 19:57:18 +00005050
Paul Bakkerc0463502013-02-14 11:19:38 +01005051 if( ssl->session )
5052 {
5053 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005054 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005055 ssl->session = NULL;
5056 }
5057
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005058#if defined(POLARSSL_SSL_ALPN)
5059 ssl->alpn_chosen = NULL;
5060#endif
5061
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02005062#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005063 polarssl_free( ssl->cli_id );
5064 ssl->cli_id = NULL;
5065 ssl->cli_id_len = 0;
5066#endif
5067
Paul Bakker48916f92012-09-16 19:57:18 +00005068 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5069 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005070
5071 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00005072}
5073
Paul Bakkera503a632013-08-14 13:48:06 +02005074#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005075static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
5076{
5077 aes_free( &tkeys->enc );
5078 aes_free( &tkeys->dec );
5079
5080 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
5081}
5082
Paul Bakker7eb013f2011-10-06 12:37:39 +00005083/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005084 * Allocate and initialize ticket keys
5085 */
5086static int ssl_ticket_keys_init( ssl_context *ssl )
5087{
5088 int ret;
5089 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005090 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005091
5092 if( ssl->ticket_keys != NULL )
5093 return( 0 );
5094
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005095 tkeys = polarssl_malloc( sizeof(ssl_ticket_keys) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005096 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005097 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5098
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005099 aes_init( &tkeys->enc );
5100 aes_init( &tkeys->dec );
5101
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005102 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01005103 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005104 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005105 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005106 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005107 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005108
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02005109 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
5110 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
5111 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
5112 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005113 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005114 polarssl_free( tkeys );
5115 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02005116 }
5117
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005118 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01005119 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005120 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005121 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005122 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005123 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005124
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005125 ssl->ticket_keys = tkeys;
5126
5127 return( 0 );
5128}
Paul Bakkera503a632013-08-14 13:48:06 +02005129#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005130
5131/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005132 * SSL set accessors
5133 */
5134void ssl_set_endpoint( ssl_context *ssl, int endpoint )
5135{
5136 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005137
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005138#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
5139 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005140 if( endpoint == SSL_IS_CLIENT )
5141 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02005142#endif
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01005143
5144#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
5145 if( endpoint == SSL_IS_SERVER )
5146 ssl->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
5147#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005148}
5149
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005150int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005151{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005152#if defined(POLARSSL_SSL_PROTO_DTLS)
5153 if( transport == SSL_TRANSPORT_DATAGRAM )
5154 {
5155 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005156
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005157 ssl->out_hdr = ssl->out_buf;
5158 ssl->out_ctr = ssl->out_buf + 3;
5159 ssl->out_len = ssl->out_buf + 11;
5160 ssl->out_iv = ssl->out_buf + 13;
5161 ssl->out_msg = ssl->out_buf + 13;
5162
5163 ssl->in_hdr = ssl->in_buf;
5164 ssl->in_ctr = ssl->in_buf + 3;
5165 ssl->in_len = ssl->in_buf + 11;
5166 ssl->in_iv = ssl->in_buf + 13;
5167 ssl->in_msg = ssl->in_buf + 13;
5168
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005169 /* DTLS starts with TLS1.1 */
5170 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
5171 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005172
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005173 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
5174 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
5175
5176 return( 0 );
5177 }
5178#endif
5179
5180 if( transport == SSL_TRANSPORT_STREAM )
5181 {
5182 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005183
5184 ssl->out_ctr = ssl->out_buf;
5185 ssl->out_hdr = ssl->out_buf + 8;
5186 ssl->out_len = ssl->out_buf + 11;
5187 ssl->out_iv = ssl->out_buf + 13;
5188 ssl->out_msg = ssl->out_buf + 13;
5189
5190 ssl->in_ctr = ssl->in_buf;
5191 ssl->in_hdr = ssl->in_buf + 8;
5192 ssl->in_len = ssl->in_buf + 11;
5193 ssl->in_iv = ssl->in_buf + 13;
5194 ssl->in_msg = ssl->in_buf + 13;
5195
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005196 return( 0 );
5197 }
5198
5199 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005200}
5201
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005202#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
5203void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
5204{
5205 ssl->anti_replay = mode;
5206}
5207#endif
5208
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005209#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
5210void ssl_set_dtls_badmac_limit( ssl_context *ssl, unsigned limit )
5211{
5212 ssl->badmac_limit = limit;
5213}
5214#endif
5215
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02005216#if defined(POLARSSL_SSL_PROTO_DTLS)
5217void ssl_set_handshake_timeout( ssl_context *ssl, uint32_t min, uint32_t max )
5218{
5219 ssl->hs_timeout_min = min;
5220 ssl->hs_timeout_max = max;
5221}
5222#endif
5223
Paul Bakker5121ce52009-01-03 21:22:43 +00005224void ssl_set_authmode( ssl_context *ssl, int authmode )
5225{
5226 ssl->authmode = authmode;
5227}
5228
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005229#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005230void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005231 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005232 void *p_vrfy )
5233{
5234 ssl->f_vrfy = f_vrfy;
5235 ssl->p_vrfy = p_vrfy;
5236}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005237#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005238
Paul Bakker5121ce52009-01-03 21:22:43 +00005239void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00005240 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00005241 void *p_rng )
5242{
5243 ssl->f_rng = f_rng;
5244 ssl->p_rng = p_rng;
5245}
5246
5247void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00005248 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00005249 void *p_dbg )
5250{
5251 ssl->f_dbg = f_dbg;
5252 ssl->p_dbg = p_dbg;
5253}
5254
5255void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00005256 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00005257 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00005258{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02005259 if( p_recv != p_send )
5260 {
5261 ssl->f_recv = NULL;
5262 ssl->f_send = NULL;
5263 ssl->p_bio = NULL;
5264 return;
5265 }
5266
Paul Bakker5121ce52009-01-03 21:22:43 +00005267 ssl->f_recv = f_recv;
5268 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02005269 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00005270}
5271
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005272void ssl_set_bio_timeout( ssl_context *ssl,
5273 void *p_bio,
5274 int (*f_send)(void *, const unsigned char *, size_t),
5275 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +02005276 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t),
5277 uint32_t timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005278{
5279 ssl->p_bio = p_bio;
5280 ssl->f_send = f_send;
5281 ssl->f_recv = f_recv;
5282 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard27074302014-10-01 17:35:50 +02005283 ssl->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005284}
5285
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005286#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00005287void ssl_set_session_cache( ssl_context *ssl,
5288 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
5289 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00005290{
Paul Bakker0a597072012-09-25 21:55:46 +00005291 ssl->f_get_cache = f_get_cache;
5292 ssl->p_get_cache = p_get_cache;
5293 ssl->f_set_cache = f_set_cache;
5294 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00005295}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005296#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005297
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005298#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005299int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00005300{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005301 int ret;
5302
5303 if( ssl == NULL ||
5304 session == NULL ||
5305 ssl->session_negotiate == NULL ||
5306 ssl->endpoint != SSL_IS_CLIENT )
5307 {
5308 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5309 }
5310
5311 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5312 return( ret );
5313
Paul Bakker0a597072012-09-25 21:55:46 +00005314 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005315
5316 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005317}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005318#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005319
Paul Bakkerb68cad62012-08-23 08:34:18 +00005320void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005321{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005322 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
5323 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
5324 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
5325 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
5326}
5327
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005328void ssl_set_ciphersuites_for_version( ssl_context *ssl,
5329 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005330 int major, int minor )
5331{
5332 if( major != SSL_MAJOR_VERSION_3 )
5333 return;
5334
5335 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
5336 return;
5337
5338 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005339}
5340
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005341#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005342/* Add a new (empty) key_cert entry an return a pointer to it */
5343static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
5344{
5345 ssl_key_cert *key_cert, *last;
5346
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005347 key_cert = polarssl_malloc( sizeof(ssl_key_cert) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005348 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005349 return( NULL );
5350
5351 memset( key_cert, 0, sizeof( ssl_key_cert ) );
5352
5353 /* Append the new key_cert to the (possibly empty) current list */
5354 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005355 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005356 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01005357 if( ssl->handshake != NULL )
5358 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01005359 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005360 else
5361 {
5362 last = ssl->key_cert;
5363 while( last->next != NULL )
5364 last = last->next;
5365 last->next = key_cert;
5366 }
5367
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005368 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005369}
5370
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005371void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00005372 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00005373{
5374 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00005375 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005376 ssl->peer_cn = peer_cn;
5377}
5378
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005379int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005380 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00005381{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005382 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
5383
5384 if( key_cert == NULL )
5385 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5386
5387 key_cert->cert = own_cert;
5388 key_cert->key = pk_key;
5389
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01005390 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005391}
5392
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005393#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005394int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005395 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00005396{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005397 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005398 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005399
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005400 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005401 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5402
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005403 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005404 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005405 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005406
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005407 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005408
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005409 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005410 if( ret != 0 )
5411 return( ret );
5412
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005413 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005414 return( ret );
5415
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005416 key_cert->cert = own_cert;
5417 key_cert->key_own_alloc = 1;
5418
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01005419 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005420}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005421#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005422
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005423int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02005424 void *rsa_key,
5425 rsa_decrypt_func rsa_decrypt,
5426 rsa_sign_func rsa_sign,
5427 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00005428{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005429 int ret;
5430 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005431
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005432 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005433 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5434
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005435 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005436 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005437 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005438
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005439 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005440
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005441 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
5442 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
5443 return( ret );
5444
5445 key_cert->cert = own_cert;
5446 key_cert->key_own_alloc = 1;
5447
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01005448 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker43b7e352011-01-18 15:27:19 +00005449}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005450#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005451
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005452#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005453int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5454 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005455{
Paul Bakker6db455e2013-09-18 17:29:31 +02005456 if( psk == NULL || psk_identity == NULL )
5457 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5458
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005459 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005460 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5461
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00005462 if( ssl->psk != NULL || ssl->psk_identity != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02005463 {
5464 polarssl_free( ssl->psk );
5465 polarssl_free( ssl->psk_identity );
5466 }
5467
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00005468 if( ( ssl->psk = polarssl_malloc( psk_len ) ) == NULL ||
5469 ( ssl->psk_identity = polarssl_malloc( psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05005470 {
5471 polarssl_free( ssl->psk );
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00005472 ssl->psk = NULL;
Mansour Moufidf81088b2015-02-17 13:10:21 -05005473 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5474 }
Paul Bakker6db455e2013-09-18 17:29:31 +02005475
Paul Bakker6db455e2013-09-18 17:29:31 +02005476 ssl->psk_len = psk_len;
5477 ssl->psk_identity_len = psk_identity_len;
5478
Paul Bakker6db455e2013-09-18 17:29:31 +02005479 memcpy( ssl->psk, psk, ssl->psk_len );
5480 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
5481
5482 return( 0 );
5483}
5484
5485void ssl_set_psk_cb( ssl_context *ssl,
5486 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5487 size_t),
5488 void *p_psk )
5489{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005490 ssl->f_psk = f_psk;
5491 ssl->p_psk = p_psk;
Paul Bakker43b7e352011-01-18 15:27:19 +00005492}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005493#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005494
Paul Bakker48916f92012-09-16 19:57:18 +00005495#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005496int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
5497{
5498 int ret;
5499
Paul Bakker48916f92012-09-16 19:57:18 +00005500 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005501 {
5502 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5503 return( ret );
5504 }
5505
Paul Bakker48916f92012-09-16 19:57:18 +00005506 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005507 {
5508 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5509 return( ret );
5510 }
5511
5512 return( 0 );
5513}
5514
Paul Bakker1b57b062011-01-06 15:48:19 +00005515int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5516{
5517 int ret;
5518
Paul Bakker66d5d072014-06-17 16:39:18 +02005519 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005520 {
5521 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5522 return( ret );
5523 }
5524
Paul Bakker66d5d072014-06-17 16:39:18 +02005525 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005526 {
5527 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5528 return( ret );
5529 }
5530
5531 return( 0 );
5532}
Paul Bakker48916f92012-09-16 19:57:18 +00005533#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005534
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005535#if defined(POLARSSL_SSL_SET_CURVES)
5536/*
5537 * Set the allowed elliptic curves
5538 */
5539void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5540{
5541 ssl->curve_list = curve_list;
5542}
5543#endif
5544
Paul Bakker0be444a2013-08-27 21:55:01 +02005545#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005546int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005547{
5548 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005549 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005550
5551 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005552
5553 if( ssl->hostname_len + 1 == 0 )
5554 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5555
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005556 ssl->hostname = polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005557
Paul Bakkerb15b8512012-01-13 13:44:06 +00005558 if( ssl->hostname == NULL )
5559 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5560
Paul Bakker3c2122f2013-06-24 19:03:14 +02005561 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005562 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005563
Paul Bakker40ea7de2009-05-03 10:18:48 +00005564 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005565
5566 return( 0 );
5567}
5568
Paul Bakker5701cdc2012-09-27 21:49:42 +00005569void ssl_set_sni( ssl_context *ssl,
5570 int (*f_sni)(void *, ssl_context *,
5571 const unsigned char *, size_t),
5572 void *p_sni )
5573{
5574 ssl->f_sni = f_sni;
5575 ssl->p_sni = p_sni;
5576}
Paul Bakker0be444a2013-08-27 21:55:01 +02005577#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005578
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005579#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005580int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005581{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005582 size_t cur_len, tot_len;
5583 const char **p;
5584
5585 /*
5586 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5587 * truncated". Check lengths now rather than later.
5588 */
5589 tot_len = 0;
5590 for( p = protos; *p != NULL; p++ )
5591 {
5592 cur_len = strlen( *p );
5593 tot_len += cur_len;
5594
5595 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5596 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5597 }
5598
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005599 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005600
5601 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005602}
5603
5604const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5605{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005606 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005607}
5608#endif /* POLARSSL_SSL_ALPN */
5609
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005610static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005611{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005612 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005613 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005614 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005615 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005616 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005617
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005618#if defined(POLARSSL_SSL_PROTO_DTLS)
5619 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5620 minor < SSL_MINOR_VERSION_2 )
5621 {
5622 return( -1 );
5623 }
5624#else
5625 ((void) ssl);
5626#endif
5627
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005628 return( 0 );
5629}
5630
5631int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5632{
5633 if( ssl_check_version( ssl, major, minor ) != 0 )
5634 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5635
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005636 ssl->max_major_ver = major;
5637 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005638
5639 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005640}
5641
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005642int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005643{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005644 if( ssl_check_version( ssl, major, minor ) != 0 )
5645 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005646
5647 ssl->min_major_ver = major;
5648 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005649
5650 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005651}
5652
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02005653#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
5654void ssl_set_fallback( ssl_context *ssl, char fallback )
5655{
5656 ssl->fallback = fallback;
5657}
5658#endif
5659
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01005660#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
5661void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
5662{
5663 ssl->encrypt_then_mac = etm;
5664}
5665#endif
5666
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02005667#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
5668void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
5669{
5670 ssl->extended_ms = ems;
5671}
5672#endif
5673
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01005674void ssl_set_arc4_support( ssl_context *ssl, char arc4 )
5675{
5676 ssl->arc4_disabled = arc4;
5677}
5678
Paul Bakker05decb22013-08-15 13:33:48 +02005679#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005680int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5681{
Paul Bakker77e257e2013-12-16 15:29:52 +01005682 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005683 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005684 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005685 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005686 }
5687
5688 ssl->mfl_code = mfl_code;
5689
5690 return( 0 );
5691}
Paul Bakker05decb22013-08-15 13:33:48 +02005692#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005693
Paul Bakker1f2bc622013-08-15 13:45:55 +02005694#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005695int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005696{
Paul Bakker8c1ede62013-07-19 14:14:37 +02005697 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005698
5699 return( 0 );
5700}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005701#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005702
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005703#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
5704void ssl_set_cbc_record_splitting( ssl_context *ssl, char split )
5705{
5706 ssl->split_done = split;
5707}
5708#endif
5709
Paul Bakker48916f92012-09-16 19:57:18 +00005710void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5711{
5712 ssl->allow_legacy_renegotiation = allow_legacy;
5713}
5714
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005715#if defined(POLARSSL_SSL_RENEGOTIATION)
5716void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5717{
5718 ssl->disable_renegotiation = renegotiation;
5719}
5720
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005721void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5722{
5723 ssl->renego_max_records = max_records;
5724}
5725
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01005726void ssl_set_renegotiation_period( ssl_context *ssl,
5727 const unsigned char period[8] )
5728{
5729 memcpy( ssl->renego_period, period, 8 );
5730}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005731#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00005732
Paul Bakkera503a632013-08-14 13:48:06 +02005733#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005734int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5735{
5736 ssl->session_tickets = use_tickets;
5737
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005738#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005739 if( ssl->endpoint == SSL_IS_CLIENT )
5740 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005741#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005742
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01005743 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
5744 return( 0 );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005745
5746 if( ssl->f_rng == NULL )
5747 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5748
5749 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005750}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005751
5752void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5753{
5754 ssl->ticket_lifetime = lifetime;
5755}
Paul Bakkera503a632013-08-14 13:48:06 +02005756#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005757
Paul Bakker5121ce52009-01-03 21:22:43 +00005758/*
5759 * SSL get accessors
5760 */
5761size_t ssl_get_bytes_avail( const ssl_context *ssl )
5762{
5763 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5764}
5765
Paul Bakkerff60ee62010-03-16 21:09:09 +00005766int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005767{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00005768 if( ssl->session != NULL )
5769 return( ssl->session->verify_result );
5770
5771 if( ssl->session_negotiate != NULL )
5772 return( ssl->session_negotiate->verify_result );
5773
5774 return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005775}
5776
Paul Bakkere3166ce2011-01-27 17:40:50 +00005777const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005778{
Paul Bakker926c8e42013-03-06 10:23:34 +01005779 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005780 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005781
Paul Bakkere3166ce2011-01-27 17:40:50 +00005782 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005783}
5784
Paul Bakker43ca69c2011-01-15 17:35:19 +00005785const char *ssl_get_version( const ssl_context *ssl )
5786{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005787#if defined(POLARSSL_SSL_PROTO_DTLS)
5788 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5789 {
5790 switch( ssl->minor_ver )
5791 {
5792 case SSL_MINOR_VERSION_2:
5793 return( "DTLSv1.0" );
5794
5795 case SSL_MINOR_VERSION_3:
5796 return( "DTLSv1.2" );
5797
5798 default:
5799 return( "unknown (DTLS)" );
5800 }
5801 }
5802#endif
5803
Paul Bakker43ca69c2011-01-15 17:35:19 +00005804 switch( ssl->minor_ver )
5805 {
5806 case SSL_MINOR_VERSION_0:
5807 return( "SSLv3.0" );
5808
5809 case SSL_MINOR_VERSION_1:
5810 return( "TLSv1.0" );
5811
5812 case SSL_MINOR_VERSION_2:
5813 return( "TLSv1.1" );
5814
Paul Bakker1ef83d62012-04-11 12:09:53 +00005815 case SSL_MINOR_VERSION_3:
5816 return( "TLSv1.2" );
5817
Paul Bakker43ca69c2011-01-15 17:35:19 +00005818 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005819 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005820 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005821}
5822
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005823int ssl_get_record_expansion( const ssl_context *ssl )
5824{
5825 int transform_expansion;
5826 const ssl_transform *transform = ssl->transform_out;
5827
5828#if defined(POLARSSL_ZLIB_SUPPORT)
5829 if( ssl->session_out->compression != SSL_COMPRESS_NULL )
5830 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
5831#endif
5832
5833 if( transform == NULL )
5834 return( ssl_hdr_len( ssl ) );
5835
5836 switch( cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
5837 {
5838 case POLARSSL_MODE_GCM:
5839 case POLARSSL_MODE_CCM:
5840 case POLARSSL_MODE_STREAM:
5841 transform_expansion = transform->minlen;
5842 break;
5843
5844 case POLARSSL_MODE_CBC:
5845 transform_expansion = transform->maclen
5846 + cipher_get_block_size( &transform->cipher_ctx_enc );
5847 break;
5848
5849 default:
5850 SSL_DEBUG_MSG( 0, ( "should never happen" ) );
5851 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
5852 }
5853
5854 return( ssl_hdr_len( ssl ) + transform_expansion );
5855}
5856
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005857#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005858const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005859{
5860 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005861 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005862
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005863 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005864}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005865#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005866
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005867#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005868int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5869{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005870 if( ssl == NULL ||
5871 dst == NULL ||
5872 ssl->session == NULL ||
5873 ssl->endpoint != SSL_IS_CLIENT )
5874 {
5875 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5876 }
5877
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005878 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005879}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005880#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005881
Paul Bakker5121ce52009-01-03 21:22:43 +00005882/*
Paul Bakker1961b702013-01-25 14:49:24 +01005883 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005884 */
Paul Bakker1961b702013-01-25 14:49:24 +01005885int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005886{
Paul Bakker40e46942009-01-03 21:51:57 +00005887 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005888
Paul Bakker40e46942009-01-03 21:51:57 +00005889#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005890 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005891 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005892#endif
Paul Bakker40e46942009-01-03 21:51:57 +00005893#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005894 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005895 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005896#endif
5897
Paul Bakker1961b702013-01-25 14:49:24 +01005898 return( ret );
5899}
5900
5901/*
5902 * Perform the SSL handshake
5903 */
5904int ssl_handshake( ssl_context *ssl )
5905{
5906 int ret = 0;
5907
5908 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5909
5910 while( ssl->state != SSL_HANDSHAKE_OVER )
5911 {
5912 ret = ssl_handshake_step( ssl );
5913
5914 if( ret != 0 )
5915 break;
5916 }
5917
Paul Bakker5121ce52009-01-03 21:22:43 +00005918 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5919
5920 return( ret );
5921}
5922
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005923#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005924#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005925/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005926 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005927 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005928static int ssl_write_hello_request( ssl_context *ssl )
5929{
5930 int ret;
5931
5932 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5933
5934 ssl->out_msglen = 4;
5935 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5936 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5937
5938 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5939 {
5940 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5941 return( ret );
5942 }
5943
5944 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5945
5946 return( 0 );
5947}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005948#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005949
5950/*
5951 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005952 * - any side: calling ssl_renegotiate(),
5953 * - client: receiving a HelloRequest during ssl_read(),
5954 * - server: receiving any handshake message on server during ssl_read() after
5955 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005956 * If the handshake doesn't complete due to waiting for I/O, it will continue
5957 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005958 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005959static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005960{
5961 int ret;
5962
5963 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5964
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005965 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5966 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005967
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005968 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5969 * the ServerHello will have message_seq = 1" */
5970#if defined(POLARSSL_SSL_PROTO_DTLS)
5971 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005972 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5973 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005974 if( ssl->endpoint == SSL_IS_SERVER )
5975 ssl->handshake->out_msg_seq = 1;
5976 else
5977 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005978 }
5979#endif
5980
Paul Bakker48916f92012-09-16 19:57:18 +00005981 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard2f5a1b42015-03-09 11:12:32 +00005982 ssl->renegotiation = SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00005983
Paul Bakker48916f92012-09-16 19:57:18 +00005984 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5985 {
5986 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5987 return( ret );
5988 }
5989
5990 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5991
5992 return( 0 );
5993}
5994
5995/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005996 * Renegotiate current connection on client,
5997 * or request renegotiation on server
5998 */
5999int ssl_renegotiate( ssl_context *ssl )
6000{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006001 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006002
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006003#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006004 /* On server, just send the request */
6005 if( ssl->endpoint == SSL_IS_SERVER )
6006 {
6007 if( ssl->state != SSL_HANDSHAKE_OVER )
6008 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
6009
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02006010 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
6011
6012 /* Did we already try/start sending HelloRequest? */
6013 if( ssl->out_left != 0 )
6014 return( ssl_flush_output( ssl ) );
6015
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006016 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006017 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006018#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006019
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006020#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006021 /*
6022 * On client, either start the renegotiation process or,
6023 * if already in progress, continue the handshake
6024 */
Manuel Pégourié-Gonnard2f5a1b42015-03-09 11:12:32 +00006025 if( ssl->renegotiation != SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006026 {
6027 if( ssl->state != SSL_HANDSHAKE_OVER )
6028 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
6029
6030 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
6031 {
6032 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
6033 return( ret );
6034 }
6035 }
6036 else
6037 {
6038 if( ( ret = ssl_handshake( ssl ) ) != 0 )
6039 {
6040 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6041 return( ret );
6042 }
6043 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006044#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006045
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006046 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006047}
6048
6049/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006050 * Check record counters and renegotiate if they're above the limit.
6051 */
6052static int ssl_check_ctr_renegotiate( ssl_context *ssl )
6053{
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006054 if( ssl->state != SSL_HANDSHAKE_OVER ||
6055 ssl->renegotiation == SSL_RENEGOTIATION_PENDING ||
6056 ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED )
6057 {
6058 return( 0 );
6059 }
6060
6061 // TODO: adapt for DTLS
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006062 if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 &&
6063 memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006064 {
6065 return( 0 );
6066 }
6067
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006068 SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006069 return( ssl_renegotiate( ssl ) );
6070}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006071#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006072
6073/*
6074 * Receive application data decrypted from the SSL layer
6075 */
Paul Bakker23986e52011-04-24 08:57:21 +00006076int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006077{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006078 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00006079 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00006080
6081 SSL_DEBUG_MSG( 2, ( "=> read" ) );
6082
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006083#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006084 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006085 {
6086 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
6087 return( ret );
6088
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006089 if( ssl->handshake != NULL &&
6090 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
6091 {
6092 if( ( ret = ssl_resend( ssl ) ) != 0 )
6093 return( ret );
6094 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006095 }
6096#endif
6097
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006098#if defined(POLARSSL_SSL_RENEGOTIATION)
6099 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
6100 {
6101 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
6102 return( ret );
6103 }
6104#endif
6105
Paul Bakker5121ce52009-01-03 21:22:43 +00006106 if( ssl->state != SSL_HANDSHAKE_OVER )
6107 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006108 ret = ssl_handshake( ssl );
6109 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
6110 {
6111 record_read = 1;
6112 }
6113 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006114 {
6115 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6116 return( ret );
6117 }
6118 }
6119
6120 if( ssl->in_offt == NULL )
6121 {
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02006122#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006123 /* Start timer if not already running */
6124 if( ssl->time_limit == 0 )
6125 ssl_set_timer( ssl, ssl->read_timeout );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02006126#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006127
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006128 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00006129 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006130 if( ( ret = ssl_read_record( ssl ) ) != 0 )
6131 {
6132 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
6133 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00006134
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006135 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
6136 return( ret );
6137 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006138 }
6139
6140 if( ssl->in_msglen == 0 &&
6141 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
6142 {
6143 /*
6144 * OpenSSL sends empty messages to randomize the IV
6145 */
6146 if( ( ret = ssl_read_record( ssl ) ) != 0 )
6147 {
Paul Bakker831a7552011-05-18 13:32:51 +00006148 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
6149 return( 0 );
6150
Paul Bakker5121ce52009-01-03 21:22:43 +00006151 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
6152 return( ret );
6153 }
6154 }
6155
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006156#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006157 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
6158 {
6159 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
6160
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006161#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006162 if( ssl->endpoint == SSL_IS_CLIENT &&
6163 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00006164 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006165 {
6166 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006167
6168 /* With DTLS, drop the packet (probably from last handshake) */
6169#if defined(POLARSSL_SSL_PROTO_DTLS)
6170 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6171 return( POLARSSL_ERR_NET_WANT_READ );
6172#endif
6173 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6174 }
6175
6176 if( ssl->endpoint == SSL_IS_SERVER &&
6177 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
6178 {
6179 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
6180
6181 /* With DTLS, drop the packet (probably from last handshake) */
6182#if defined(POLARSSL_SSL_PROTO_DTLS)
6183 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6184 return( POLARSSL_ERR_NET_WANT_READ );
6185#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006186 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6187 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006188#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006189
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006190 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
6191 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006192 ssl->allow_legacy_renegotiation ==
6193 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006194 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006195 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006196
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006197#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006198 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006199 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006200 /*
6201 * SSLv3 does not have a "no_renegotiation" alert
6202 */
6203 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
6204 return( ret );
6205 }
6206 else
Paul Bakker9af723c2014-05-01 13:03:14 +02006207#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006208#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
6209 defined(POLARSSL_SSL_PROTO_TLS1_2)
6210 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006211 {
6212 if( ( ret = ssl_send_alert_message( ssl,
6213 SSL_ALERT_LEVEL_WARNING,
6214 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
6215 {
6216 return( ret );
6217 }
Paul Bakker48916f92012-09-16 19:57:18 +00006218 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006219 else
Paul Bakker9af723c2014-05-01 13:03:14 +02006220#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
6221 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02006222 {
6223 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006224 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02006225 }
Paul Bakker48916f92012-09-16 19:57:18 +00006226 }
6227 else
6228 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006229 /* DTLS clients need to know renego is server-initiated */
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02006230#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006231 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
6232 ssl->endpoint == SSL_IS_CLIENT )
6233 {
6234 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
6235 }
6236#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006237 ret = ssl_start_renegotiation( ssl );
6238 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
6239 {
6240 record_read = 1;
6241 }
6242 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006243 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006244 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00006245 return( ret );
6246 }
Paul Bakker48916f92012-09-16 19:57:18 +00006247 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006248
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006249 /* If a non-handshake record was read during renego, fallthrough,
6250 * else tell the user they should call ssl_read() again */
6251 if( ! record_read )
6252 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00006253 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006254 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
6255 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006256
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006257 if( ssl->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006258 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006259 if( ++ssl->renego_records_seen > ssl->renego_max_records )
6260 {
6261 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
6262 "but not honored by client" ) );
6263 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6264 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006265 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006266 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006267#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006268
6269 /* Fatal and closure alerts handled by ssl_read_record() */
6270 if( ssl->in_msgtype == SSL_MSG_ALERT )
6271 {
6272 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
6273 return( POLARSSL_ERR_NET_WANT_READ );
6274 }
6275
6276 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00006277 {
6278 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00006279 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006280 }
6281
6282 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006283
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02006284#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02006285 /* We're going to return something now, cancel timer,
6286 * except if handshake (renegotiation) is in progress */
6287 if( ssl->state == SSL_HANDSHAKE_OVER )
6288 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006289
6290 /* If we requested renego but received AppData, resend HelloRequest.
6291 * Do it now, after setting in_offt, to avoid taking this branch
6292 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00006293#if defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006294 if( ssl->endpoint == SSL_IS_SERVER &&
6295 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
6296 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006297 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006298 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006299 SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006300 return( ret );
6301 }
6302 }
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00006303#endif /* POLARSSL_SSL_SRV_C && POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02006304#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006305 }
6306
6307 n = ( len < ssl->in_msglen )
6308 ? len : ssl->in_msglen;
6309
6310 memcpy( buf, ssl->in_offt, n );
6311 ssl->in_msglen -= n;
6312
6313 if( ssl->in_msglen == 0 )
6314 /* all bytes consumed */
6315 ssl->in_offt = NULL;
6316 else
6317 /* more data available */
6318 ssl->in_offt += n;
6319
6320 SSL_DEBUG_MSG( 2, ( "<= read" ) );
6321
Paul Bakker23986e52011-04-24 08:57:21 +00006322 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006323}
6324
6325/*
6326 * Send application data to be encrypted by the SSL layer
6327 */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006328#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
6329static int ssl_write_real( ssl_context *ssl, const unsigned char *buf, size_t len )
6330#else
Paul Bakker23986e52011-04-24 08:57:21 +00006331int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006332#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006333{
Paul Bakker23986e52011-04-24 08:57:21 +00006334 int ret;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006335#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
6336 unsigned int max_len;
6337#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006338
6339 SSL_DEBUG_MSG( 2, ( "=> write" ) );
6340
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006341#if defined(POLARSSL_SSL_RENEGOTIATION)
6342 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
6343 {
6344 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
6345 return( ret );
6346 }
6347#endif
6348
Paul Bakker5121ce52009-01-03 21:22:43 +00006349 if( ssl->state != SSL_HANDSHAKE_OVER )
6350 {
6351 if( ( ret = ssl_handshake( ssl ) ) != 0 )
6352 {
6353 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6354 return( ret );
6355 }
6356 }
6357
Paul Bakker05decb22013-08-15 13:33:48 +02006358#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02006359 /*
6360 * Assume mfl_code is correct since it was checked when set
6361 */
6362 max_len = mfl_code_to_length[ssl->mfl_code];
6363
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006364 /*
Paul Bakker05decb22013-08-15 13:33:48 +02006365 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006366 */
6367 if( ssl->session_out != NULL &&
6368 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6369 {
6370 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6371 }
6372
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006373 if( len > max_len )
6374 {
6375#if defined(POLARSSL_SSL_PROTO_DTLS)
6376 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6377 {
6378 SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
6379 "maximum fragment length: %d > %d",
6380 len, max_len ) );
6381 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
6382 }
6383 else
6384#endif
6385 len = max_len;
6386 }
6387#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker887bd502011-06-08 13:10:54 +00006388
Paul Bakker5121ce52009-01-03 21:22:43 +00006389 if( ssl->out_left != 0 )
6390 {
6391 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
6392 {
6393 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
6394 return( ret );
6395 }
6396 }
Paul Bakker887bd502011-06-08 13:10:54 +00006397 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00006398 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006399 ssl->out_msglen = len;
Paul Bakker887bd502011-06-08 13:10:54 +00006400 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006401 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00006402
6403 if( ( ret = ssl_write_record( ssl ) ) != 0 )
6404 {
6405 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
6406 return( ret );
6407 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006408 }
6409
6410 SSL_DEBUG_MSG( 2, ( "<= write" ) );
6411
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006412 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006413}
6414
6415/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006416 * Write application data, doing 1/n-1 splitting if necessary.
6417 *
6418 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006419 * then the caller will call us again with the same arguments, so
6420 * remember wether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006421 */
6422#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
6423int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
6424{
6425 int ret;
6426
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006427 if( ssl->split_done == SSL_CBC_RECORD_SPLITTING_DISABLED ||
6428 len <= 1 ||
6429 ssl->minor_ver > SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006430 cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
6431 != POLARSSL_MODE_CBC )
6432 {
6433 return( ssl_write_real( ssl, buf, len ) );
6434 }
6435
6436 if( ssl->split_done == 0 )
6437 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006438 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006439 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006440 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006441 }
6442
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006443 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
6444 return( ret );
6445 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006446
6447 return( ret + 1 );
6448}
6449#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */
6450
6451/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006452 * Notify the peer that the connection is being closed
6453 */
6454int ssl_close_notify( ssl_context *ssl )
6455{
6456 int ret;
6457
6458 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
6459
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006460 if( ssl->out_left != 0 )
6461 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006462
6463 if( ssl->state == SSL_HANDSHAKE_OVER )
6464 {
Paul Bakker48916f92012-09-16 19:57:18 +00006465 if( ( ret = ssl_send_alert_message( ssl,
6466 SSL_ALERT_LEVEL_WARNING,
6467 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006468 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006469 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006470 return( ret );
6471 }
6472 }
6473
6474 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
6475
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006476 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006477}
6478
Paul Bakker48916f92012-09-16 19:57:18 +00006479void ssl_transform_free( ssl_transform *transform )
6480{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006481 if( transform == NULL )
6482 return;
6483
Paul Bakker48916f92012-09-16 19:57:18 +00006484#if defined(POLARSSL_ZLIB_SUPPORT)
6485 deflateEnd( &transform->ctx_deflate );
6486 inflateEnd( &transform->ctx_inflate );
6487#endif
6488
Paul Bakker84bbeb52014-07-01 14:53:22 +02006489 cipher_free( &transform->cipher_ctx_enc );
6490 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02006491
Paul Bakker84bbeb52014-07-01 14:53:22 +02006492 md_free( &transform->md_ctx_enc );
6493 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02006494
Paul Bakker34617722014-06-13 17:20:13 +02006495 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006496}
6497
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006498#if defined(POLARSSL_X509_CRT_PARSE_C)
6499static void ssl_key_cert_free( ssl_key_cert *key_cert )
6500{
6501 ssl_key_cert *cur = key_cert, *next;
6502
6503 while( cur != NULL )
6504 {
6505 next = cur->next;
6506
6507 if( cur->key_own_alloc )
6508 {
6509 pk_free( cur->key );
6510 polarssl_free( cur->key );
6511 }
6512 polarssl_free( cur );
6513
6514 cur = next;
6515 }
6516}
6517#endif /* POLARSSL_X509_CRT_PARSE_C */
6518
Paul Bakker48916f92012-09-16 19:57:18 +00006519void ssl_handshake_free( ssl_handshake_params *handshake )
6520{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006521 if( handshake == NULL )
6522 return;
6523
Paul Bakker48916f92012-09-16 19:57:18 +00006524#if defined(POLARSSL_DHM_C)
6525 dhm_free( &handshake->dhm_ctx );
6526#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006527#if defined(POLARSSL_ECDH_C)
6528 ecdh_free( &handshake->ecdh_ctx );
6529#endif
6530
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006531#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02006532 /* explicit void pointer cast for buggy MS compiler */
6533 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006534#endif
6535
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006536#if defined(POLARSSL_X509_CRT_PARSE_C) && \
6537 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
6538 /*
6539 * Free only the linked list wrapper, not the keys themselves
6540 * since the belong to the SNI callback
6541 */
6542 if( handshake->sni_key_cert != NULL )
6543 {
6544 ssl_key_cert *cur = handshake->sni_key_cert, *next;
6545
6546 while( cur != NULL )
6547 {
6548 next = cur->next;
6549 polarssl_free( cur );
6550 cur = next;
6551 }
6552 }
Paul Bakker9af723c2014-05-01 13:03:14 +02006553#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006554
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006555#if defined(POLARSSL_SSL_PROTO_DTLS)
6556 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02006557 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02006558 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006559#endif
6560
Paul Bakker34617722014-06-13 17:20:13 +02006561 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006562}
6563
6564void ssl_session_free( ssl_session *session )
6565{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006566 if( session == NULL )
6567 return;
6568
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006569#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00006570 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00006571 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006572 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02006573 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00006574 }
Paul Bakkered27a042013-04-18 22:46:23 +02006575#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006576
Paul Bakkera503a632013-08-14 13:48:06 +02006577#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006578 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02006579#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006580
Paul Bakker34617722014-06-13 17:20:13 +02006581 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006582}
6583
Paul Bakker5121ce52009-01-03 21:22:43 +00006584/*
6585 * Free an SSL context
6586 */
6587void ssl_free( ssl_context *ssl )
6588{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006589 if( ssl == NULL )
6590 return;
6591
Paul Bakker5121ce52009-01-03 21:22:43 +00006592 SSL_DEBUG_MSG( 2, ( "=> free" ) );
6593
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006594 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006595 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006596 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
6597 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006598 }
6599
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006600 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006601 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006602 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
6603 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006604 }
6605
Paul Bakker16770332013-10-11 09:59:44 +02006606#if defined(POLARSSL_ZLIB_SUPPORT)
6607 if( ssl->compress_buf != NULL )
6608 {
Paul Bakker34617722014-06-13 17:20:13 +02006609 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02006610 polarssl_free( ssl->compress_buf );
6611 }
6612#endif
6613
Paul Bakker40e46942009-01-03 21:51:57 +00006614#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006615 mpi_free( &ssl->dhm_P );
6616 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006617#endif
6618
Paul Bakker48916f92012-09-16 19:57:18 +00006619 if( ssl->transform )
6620 {
6621 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02006622 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006623 }
6624
6625 if( ssl->handshake )
6626 {
6627 ssl_handshake_free( ssl->handshake );
6628 ssl_transform_free( ssl->transform_negotiate );
6629 ssl_session_free( ssl->session_negotiate );
6630
Paul Bakker6e339b52013-07-03 13:37:05 +02006631 polarssl_free( ssl->handshake );
6632 polarssl_free( ssl->transform_negotiate );
6633 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00006634 }
6635
Paul Bakkerc0463502013-02-14 11:19:38 +01006636 if( ssl->session )
6637 {
6638 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02006639 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006640 }
6641
Paul Bakkera503a632013-08-14 13:48:06 +02006642#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02006643 if( ssl->ticket_keys )
6644 {
6645 ssl_ticket_keys_free( ssl->ticket_keys );
6646 polarssl_free( ssl->ticket_keys );
6647 }
Paul Bakkera503a632013-08-14 13:48:06 +02006648#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006649
Paul Bakker0be444a2013-08-27 21:55:01 +02006650#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02006651 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006652 {
Paul Bakker34617722014-06-13 17:20:13 +02006653 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006654 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006655 ssl->hostname_len = 0;
6656 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006657#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006658
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006659#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006660 if( ssl->psk != NULL )
6661 {
Paul Bakker34617722014-06-13 17:20:13 +02006662 polarssl_zeroize( ssl->psk, ssl->psk_len );
6663 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006664 polarssl_free( ssl->psk );
6665 polarssl_free( ssl->psk_identity );
6666 ssl->psk_len = 0;
6667 ssl->psk_identity_len = 0;
6668 }
6669#endif
6670
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006671#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006672 ssl_key_cert_free( ssl->key_cert );
6673#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006674
Paul Bakker05ef8352012-05-08 09:17:57 +00006675#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6676 if( ssl_hw_record_finish != NULL )
6677 {
6678 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6679 ssl_hw_record_finish( ssl );
6680 }
6681#endif
6682
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006683#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006684 polarssl_free( ssl->cli_id );
6685#endif
6686
Paul Bakker5121ce52009-01-03 21:22:43 +00006687 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006688
Paul Bakker86f04f42013-02-14 11:20:09 +01006689 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006690 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006691}
6692
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006693#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006694/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006695 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006696 */
6697unsigned char ssl_sig_from_pk( pk_context *pk )
6698{
6699#if defined(POLARSSL_RSA_C)
6700 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6701 return( SSL_SIG_RSA );
6702#endif
6703#if defined(POLARSSL_ECDSA_C)
6704 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6705 return( SSL_SIG_ECDSA );
6706#endif
6707 return( SSL_SIG_ANON );
6708}
6709
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006710pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6711{
6712 switch( sig )
6713 {
6714#if defined(POLARSSL_RSA_C)
6715 case SSL_SIG_RSA:
6716 return( POLARSSL_PK_RSA );
6717#endif
6718#if defined(POLARSSL_ECDSA_C)
6719 case SSL_SIG_ECDSA:
6720 return( POLARSSL_PK_ECDSA );
6721#endif
6722 default:
6723 return( POLARSSL_PK_NONE );
6724 }
6725}
Paul Bakker9af723c2014-05-01 13:03:14 +02006726#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006727
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006728/*
6729 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6730 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006731md_type_t ssl_md_alg_from_hash( unsigned char hash )
6732{
6733 switch( hash )
6734 {
6735#if defined(POLARSSL_MD5_C)
6736 case SSL_HASH_MD5:
6737 return( POLARSSL_MD_MD5 );
6738#endif
6739#if defined(POLARSSL_SHA1_C)
6740 case SSL_HASH_SHA1:
6741 return( POLARSSL_MD_SHA1 );
6742#endif
6743#if defined(POLARSSL_SHA256_C)
6744 case SSL_HASH_SHA224:
6745 return( POLARSSL_MD_SHA224 );
6746 case SSL_HASH_SHA256:
6747 return( POLARSSL_MD_SHA256 );
6748#endif
6749#if defined(POLARSSL_SHA512_C)
6750 case SSL_HASH_SHA384:
6751 return( POLARSSL_MD_SHA384 );
6752 case SSL_HASH_SHA512:
6753 return( POLARSSL_MD_SHA512 );
6754#endif
6755 default:
6756 return( POLARSSL_MD_NONE );
6757 }
6758}
6759
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006760#if defined(POLARSSL_SSL_SET_CURVES)
6761/*
6762 * Check is a curve proposed by the peer is in our list.
6763 * Return 1 if we're willing to use it, 0 otherwise.
6764 */
6765int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6766{
6767 const ecp_group_id *gid;
6768
6769 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6770 if( *gid == grp_id )
6771 return( 1 );
6772
6773 return( 0 );
6774}
Paul Bakker9af723c2014-05-01 13:03:14 +02006775#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006776
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006777#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006778int ssl_check_cert_usage( const x509_crt *cert,
6779 const ssl_ciphersuite_t *ciphersuite,
6780 int cert_endpoint )
6781{
6782#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6783 int usage = 0;
6784#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006785#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6786 const char *ext_oid;
6787 size_t ext_len;
6788#endif
6789
6790#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6791 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6792 ((void) cert);
6793 ((void) cert_endpoint);
6794#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006795
6796#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6797 if( cert_endpoint == SSL_IS_SERVER )
6798 {
6799 /* Server part of the key exchange */
6800 switch( ciphersuite->key_exchange )
6801 {
6802 case POLARSSL_KEY_EXCHANGE_RSA:
6803 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6804 usage = KU_KEY_ENCIPHERMENT;
6805 break;
6806
6807 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6808 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6809 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6810 usage = KU_DIGITAL_SIGNATURE;
6811 break;
6812
6813 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6814 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6815 usage = KU_KEY_AGREEMENT;
6816 break;
6817
6818 /* Don't use default: we want warnings when adding new values */
6819 case POLARSSL_KEY_EXCHANGE_NONE:
6820 case POLARSSL_KEY_EXCHANGE_PSK:
6821 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6822 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6823 usage = 0;
6824 }
6825 }
6826 else
6827 {
6828 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6829 usage = KU_DIGITAL_SIGNATURE;
6830 }
6831
6832 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6833 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006834#else
6835 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006836#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6837
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006838#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6839 if( cert_endpoint == SSL_IS_SERVER )
6840 {
6841 ext_oid = OID_SERVER_AUTH;
6842 ext_len = OID_SIZE( OID_SERVER_AUTH );
6843 }
6844 else
6845 {
6846 ext_oid = OID_CLIENT_AUTH;
6847 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6848 }
6849
6850 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6851 return( -1 );
6852#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6853
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006854 return( 0 );
6855}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006856#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006857
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006858/*
6859 * Convert version numbers to/from wire format
6860 * and, for DTLS, to/from TLS equivalent.
6861 *
6862 * For TLS this is the identity.
6863 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6864 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6865 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6866 */
6867void ssl_write_version( int major, int minor, int transport,
6868 unsigned char ver[2] )
6869{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006870#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006871 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006872 {
6873 if( minor == SSL_MINOR_VERSION_2 )
6874 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6875
6876 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6877 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6878 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006879 else
6880#else
6881 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006882#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006883 {
6884 ver[0] = (unsigned char) major;
6885 ver[1] = (unsigned char) minor;
6886 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006887}
6888
6889void ssl_read_version( int *major, int *minor, int transport,
6890 const unsigned char ver[2] )
6891{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006892#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006893 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006894 {
6895 *major = 255 - ver[0] + 2;
6896 *minor = 255 - ver[1] + 1;
6897
6898 if( *minor == SSL_MINOR_VERSION_1 )
6899 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6900 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006901 else
6902#else
6903 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006904#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006905 {
6906 *major = ver[0];
6907 *minor = ver[1];
6908 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006909}
6910
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006911#endif /* POLARSSL_SSL_TLS_C */