2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* SSLv3/TLSv1 shared functions
|
|
|
|
*
|
2013-01-07 17:20:04 +00:00
|
|
|
* Copyright (C) 2006-2013, Brainspark B.V.
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
|
|
|
* This file is part of PolarSSL (http://www.polarssl.org)
|
2010-07-18 10:13:04 +00:00
|
|
|
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
2009-07-28 17:23:11 +00:00
|
|
|
* All rights reserved.
|
2009-01-04 16:27:10 +00:00
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* The SSL 3.0 specification was drafted by Netscape in 1996,
|
|
|
|
* and became an IETF standard in 1999.
|
|
|
|
*
|
|
|
|
* http://wp.netscape.com/eng/ssl3/
|
|
|
|
* http://www.ietf.org/rfc/rfc2246.txt
|
|
|
|
* http://www.ietf.org/rfc/rfc4346.txt
|
|
|
|
*/
|
|
|
|
|
2009-01-03 21:51:57 +00:00
|
|
|
#include "polarssl/config.h"
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2009-01-03 21:51:57 +00:00
|
|
|
#if defined(POLARSSL_SSL_TLS_C)
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-08-27 19:55:01 +00:00
|
|
|
#include "polarssl/debug.h"
|
|
|
|
#include "polarssl/ssl.h"
|
|
|
|
|
2013-07-03 11:37:05 +00:00
|
|
|
#if defined(POLARSSL_MEMORY_C)
|
|
|
|
#include "polarssl/memory.h"
|
|
|
|
#else
|
|
|
|
#define polarssl_malloc malloc
|
|
|
|
#define polarssl_free free
|
|
|
|
#endif
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2011-04-18 03:47:52 +00:00
|
|
|
#if defined _MSC_VER && !defined strcasecmp
|
|
|
|
#define strcasecmp _stricmp
|
|
|
|
#endif
|
|
|
|
|
2013-08-15 11:33:48 +00:00
|
|
|
#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
|
2013-07-18 10:32:27 +00:00
|
|
|
/*
|
|
|
|
* Convert max_fragment_length codes to length.
|
|
|
|
* RFC 6066 says:
|
|
|
|
* enum{
|
|
|
|
* 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
|
|
|
|
* } MaxFragmentLength;
|
|
|
|
* and we add 0 -> extension unused
|
|
|
|
*/
|
2013-07-18 12:07:09 +00:00
|
|
|
static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
|
2013-07-18 10:32:27 +00:00
|
|
|
{
|
|
|
|
SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
|
|
|
|
512, /* SSL_MAX_FRAG_LEN_512 */
|
|
|
|
1024, /* SSL_MAX_FRAG_LEN_1024 */
|
|
|
|
2048, /* SSL_MAX_FRAG_LEN_2048 */
|
|
|
|
4096, /* SSL_MAX_FRAG_LEN_4096 */
|
|
|
|
};
|
2013-08-15 11:33:48 +00:00
|
|
|
#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
|
2013-07-18 10:32:27 +00:00
|
|
|
|
2013-08-02 13:34:52 +00:00
|
|
|
static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ssl_session_free( dst );
|
|
|
|
memcpy( dst, src, sizeof( ssl_session ) );
|
|
|
|
|
|
|
|
#if defined(POLARSSL_X509_PARSE_C)
|
|
|
|
if( src->peer_cert != NULL )
|
|
|
|
{
|
|
|
|
if( ( dst->peer_cert = polarssl_malloc( sizeof(x509_cert) ) ) == NULL )
|
|
|
|
return( POLARSSL_ERR_SSL_MALLOC_FAILED );
|
|
|
|
|
|
|
|
memset( dst->peer_cert, 0, sizeof(x509_cert) );
|
|
|
|
|
|
|
|
if( ( ret = x509parse_crt( dst->peer_cert, src->peer_cert->raw.p,
|
|
|
|
src->peer_cert->raw.len ) != 0 ) )
|
|
|
|
{
|
|
|
|
polarssl_free( dst->peer_cert );
|
|
|
|
dst->peer_cert = NULL;
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* POLARSSL_X509_PARSE_C */
|
|
|
|
|
2013-08-14 11:48:06 +00:00
|
|
|
#if defined(POLARSSL_SSL_SESSION_TICKETS)
|
2013-08-02 13:34:52 +00:00
|
|
|
if( src->ticket != NULL )
|
|
|
|
{
|
|
|
|
if( ( dst->ticket = polarssl_malloc( src->ticket_len ) ) == NULL )
|
|
|
|
return( POLARSSL_ERR_SSL_MALLOC_FAILED );
|
|
|
|
|
|
|
|
memcpy( dst->ticket, src->ticket, src->ticket_len );
|
|
|
|
}
|
2013-08-14 11:48:06 +00:00
|
|
|
#endif /* POLARSSL_SSL_SESSION_TICKETS */
|
2013-08-02 13:34:52 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2012-05-08 09:17:57 +00:00
|
|
|
#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
|
|
|
|
int (*ssl_hw_record_init)(ssl_context *ssl,
|
|
|
|
const unsigned char *key_enc, const unsigned char *key_dec,
|
2012-12-19 13:42:06 +00:00
|
|
|
size_t keylen,
|
2012-05-08 09:17:57 +00:00
|
|
|
const unsigned char *iv_enc, const unsigned char *iv_dec,
|
2012-12-19 13:42:06 +00:00
|
|
|
size_t ivlen,
|
|
|
|
const unsigned char *mac_enc, const unsigned char *mac_dec,
|
|
|
|
size_t maclen) = NULL;
|
|
|
|
int (*ssl_hw_record_activate)(ssl_context *ssl, int direction) = NULL;
|
2012-05-08 09:17:57 +00:00
|
|
|
int (*ssl_hw_record_reset)(ssl_context *ssl) = NULL;
|
|
|
|
int (*ssl_hw_record_write)(ssl_context *ssl) = NULL;
|
|
|
|
int (*ssl_hw_record_read)(ssl_context *ssl) = NULL;
|
|
|
|
int (*ssl_hw_record_finish)(ssl_context *ssl) = NULL;
|
|
|
|
#endif
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* Key material generation
|
|
|
|
*/
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_SSL3)
|
2013-06-25 14:25:17 +00:00
|
|
|
static int ssl3_prf( const unsigned char *secret, size_t slen,
|
|
|
|
const char *label,
|
|
|
|
const unsigned char *random, size_t rlen,
|
2012-09-13 14:23:06 +00:00
|
|
|
unsigned char *dstbuf, size_t dlen )
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
md5_context md5;
|
|
|
|
sha1_context sha1;
|
|
|
|
unsigned char padding[16];
|
|
|
|
unsigned char sha1sum[20];
|
|
|
|
((void)label);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SSLv3:
|
|
|
|
* block =
|
|
|
|
* MD5( secret + SHA1( 'A' + secret + random ) ) +
|
|
|
|
* MD5( secret + SHA1( 'BB' + secret + random ) ) +
|
|
|
|
* MD5( secret + SHA1( 'CCC' + secret + random ) ) +
|
|
|
|
* ...
|
|
|
|
*/
|
|
|
|
for( i = 0; i < dlen / 16; i++ )
|
|
|
|
{
|
|
|
|
memset( padding, 'A' + i, 1 + i );
|
|
|
|
|
|
|
|
sha1_starts( &sha1 );
|
|
|
|
sha1_update( &sha1, padding, 1 + i );
|
|
|
|
sha1_update( &sha1, secret, slen );
|
|
|
|
sha1_update( &sha1, random, rlen );
|
|
|
|
sha1_finish( &sha1, sha1sum );
|
|
|
|
|
|
|
|
md5_starts( &md5 );
|
|
|
|
md5_update( &md5, secret, slen );
|
|
|
|
md5_update( &md5, sha1sum, 20 );
|
|
|
|
md5_finish( &md5, dstbuf + i * 16 );
|
|
|
|
}
|
|
|
|
|
|
|
|
memset( &md5, 0, sizeof( md5 ) );
|
|
|
|
memset( &sha1, 0, sizeof( sha1 ) );
|
|
|
|
|
|
|
|
memset( padding, 0, sizeof( padding ) );
|
|
|
|
memset( sha1sum, 0, sizeof( sha1sum ) );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif /* POLARSSL_SSL_PROTO_SSL3 */
|
2012-09-13 14:23:06 +00:00
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
|
2013-06-25 14:25:17 +00:00
|
|
|
static int tls1_prf( const unsigned char *secret, size_t slen,
|
|
|
|
const char *label,
|
|
|
|
const unsigned char *random, size_t rlen,
|
2011-04-24 08:57:21 +00:00
|
|
|
unsigned char *dstbuf, size_t dlen )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2011-04-24 08:57:21 +00:00
|
|
|
size_t nb, hs;
|
|
|
|
size_t i, j, k;
|
2013-06-25 14:25:17 +00:00
|
|
|
const unsigned char *S1, *S2;
|
2009-01-03 21:22:43 +00:00
|
|
|
unsigned char tmp[128];
|
|
|
|
unsigned char h_i[20];
|
|
|
|
|
|
|
|
if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
hs = ( slen + 1 ) / 2;
|
|
|
|
S1 = secret;
|
|
|
|
S2 = secret + slen - hs;
|
|
|
|
|
|
|
|
nb = strlen( label );
|
|
|
|
memcpy( tmp + 20, label, nb );
|
|
|
|
memcpy( tmp + 20 + nb, random, rlen );
|
|
|
|
nb += rlen;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First compute P_md5(secret,label+random)[0..dlen]
|
|
|
|
*/
|
|
|
|
md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
|
|
|
|
|
|
|
|
for( i = 0; i < dlen; i += 16 )
|
|
|
|
{
|
|
|
|
md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
|
|
|
|
md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
|
|
|
|
|
|
|
|
k = ( i + 16 > dlen ) ? dlen % 16 : 16;
|
|
|
|
|
|
|
|
for( j = 0; j < k; j++ )
|
|
|
|
dstbuf[i + j] = h_i[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XOR out with P_sha1(secret,label+random)[0..dlen]
|
|
|
|
*/
|
|
|
|
sha1_hmac( S2, hs, tmp + 20, nb, tmp );
|
|
|
|
|
|
|
|
for( i = 0; i < dlen; i += 20 )
|
|
|
|
{
|
|
|
|
sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
|
|
|
|
sha1_hmac( S2, hs, tmp, 20, tmp );
|
|
|
|
|
|
|
|
k = ( i + 20 > dlen ) ? dlen % 20 : 20;
|
|
|
|
|
|
|
|
for( j = 0; j < k; j++ )
|
|
|
|
dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
|
|
|
|
}
|
|
|
|
|
|
|
|
memset( tmp, 0, sizeof( tmp ) );
|
|
|
|
memset( h_i, 0, sizeof( h_i ) );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_TLS1_2)
|
|
|
|
#if defined(POLARSSL_SHA256_C)
|
2013-06-25 14:25:17 +00:00
|
|
|
static int tls_prf_sha256( const unsigned char *secret, size_t slen,
|
|
|
|
const char *label,
|
|
|
|
const unsigned char *random, size_t rlen,
|
2012-04-11 12:09:53 +00:00
|
|
|
unsigned char *dstbuf, size_t dlen )
|
|
|
|
{
|
|
|
|
size_t nb;
|
|
|
|
size_t i, j, k;
|
|
|
|
unsigned char tmp[128];
|
|
|
|
unsigned char h_i[32];
|
|
|
|
|
|
|
|
if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
|
|
|
|
return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
nb = strlen( label );
|
|
|
|
memcpy( tmp + 32, label, nb );
|
|
|
|
memcpy( tmp + 32 + nb, random, rlen );
|
|
|
|
nb += rlen;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute P_<hash>(secret, label + random)[0..dlen]
|
|
|
|
*/
|
2013-06-30 12:34:05 +00:00
|
|
|
sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
|
2012-04-11 12:09:53 +00:00
|
|
|
|
|
|
|
for( i = 0; i < dlen; i += 32 )
|
|
|
|
{
|
2013-06-30 12:34:05 +00:00
|
|
|
sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
|
|
|
|
sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
|
2012-04-11 12:09:53 +00:00
|
|
|
|
|
|
|
k = ( i + 32 > dlen ) ? dlen % 32 : 32;
|
|
|
|
|
|
|
|
for( j = 0; j < k; j++ )
|
|
|
|
dstbuf[i + j] = h_i[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
memset( tmp, 0, sizeof( tmp ) );
|
|
|
|
memset( h_i, 0, sizeof( h_i ) );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif /* POLARSSL_SHA256_C */
|
2012-04-11 12:09:53 +00:00
|
|
|
|
2013-06-30 12:34:05 +00:00
|
|
|
#if defined(POLARSSL_SHA512_C)
|
2013-06-25 14:25:17 +00:00
|
|
|
static int tls_prf_sha384( const unsigned char *secret, size_t slen,
|
|
|
|
const char *label,
|
|
|
|
const unsigned char *random, size_t rlen,
|
2012-04-18 14:23:57 +00:00
|
|
|
unsigned char *dstbuf, size_t dlen )
|
|
|
|
{
|
|
|
|
size_t nb;
|
|
|
|
size_t i, j, k;
|
|
|
|
unsigned char tmp[128];
|
|
|
|
unsigned char h_i[48];
|
|
|
|
|
|
|
|
if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
|
|
|
|
return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
nb = strlen( label );
|
|
|
|
memcpy( tmp + 48, label, nb );
|
|
|
|
memcpy( tmp + 48 + nb, random, rlen );
|
|
|
|
nb += rlen;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute P_<hash>(secret, label + random)[0..dlen]
|
|
|
|
*/
|
2013-06-30 12:34:05 +00:00
|
|
|
sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
|
2012-04-18 14:23:57 +00:00
|
|
|
|
|
|
|
for( i = 0; i < dlen; i += 48 )
|
|
|
|
{
|
2013-06-30 12:34:05 +00:00
|
|
|
sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
|
|
|
|
sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
|
2012-04-18 14:23:57 +00:00
|
|
|
|
|
|
|
k = ( i + 48 > dlen ) ? dlen % 48 : 48;
|
|
|
|
|
|
|
|
for( j = 0; j < k; j++ )
|
|
|
|
dstbuf[i + j] = h_i[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
memset( tmp, 0, sizeof( tmp ) );
|
|
|
|
memset( h_i, 0, sizeof( h_i ) );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif /* POLARSSL_SHA512_C */
|
|
|
|
#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
|
2012-04-18 14:23:57 +00:00
|
|
|
|
2013-06-25 14:25:17 +00:00
|
|
|
static void ssl_update_checksum_start(ssl_context *, const unsigned char *, size_t);
|
2013-08-27 19:19:20 +00:00
|
|
|
|
|
|
|
#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
|
|
|
|
defined(POLARSSL_SSL_PROTO_TLS1_1)
|
2013-06-25 14:25:17 +00:00
|
|
|
static void ssl_update_checksum_md5sha1(ssl_context *, const unsigned char *, size_t);
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif
|
2012-04-18 16:10:25 +00:00
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_SSL3)
|
2012-04-18 16:10:25 +00:00
|
|
|
static void ssl_calc_verify_ssl(ssl_context *,unsigned char *);
|
2012-04-18 14:23:57 +00:00
|
|
|
static void ssl_calc_finished_ssl(ssl_context *,unsigned char *,int);
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
|
|
|
|
static void ssl_calc_verify_tls(ssl_context *,unsigned char *);
|
2012-04-18 14:23:57 +00:00
|
|
|
static void ssl_calc_finished_tls(ssl_context *,unsigned char *,int);
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(POLARSSL_SSL_PROTO_TLS1_2)
|
|
|
|
#if defined(POLARSSL_SHA256_C)
|
|
|
|
static void ssl_update_checksum_sha256(ssl_context *, const unsigned char *, size_t);
|
|
|
|
static void ssl_calc_verify_tls_sha256(ssl_context *,unsigned char *);
|
2012-04-18 14:23:57 +00:00
|
|
|
static void ssl_calc_finished_tls_sha256(ssl_context *,unsigned char *,int);
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif
|
2012-11-24 10:26:46 +00:00
|
|
|
|
2013-06-30 12:34:05 +00:00
|
|
|
#if defined(POLARSSL_SHA512_C)
|
2013-06-25 14:25:17 +00:00
|
|
|
static void ssl_update_checksum_sha384(ssl_context *, const unsigned char *, size_t);
|
2012-11-24 10:26:46 +00:00
|
|
|
static void ssl_calc_verify_tls_sha384(ssl_context *,unsigned char *);
|
2012-04-18 14:23:57 +00:00
|
|
|
static void ssl_calc_finished_tls_sha384(ssl_context *,unsigned char *,int);
|
2012-11-24 10:26:46 +00:00
|
|
|
#endif
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif
|
2012-04-11 12:09:53 +00:00
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
int ssl_derive_keys( ssl_context *ssl )
|
|
|
|
{
|
|
|
|
unsigned char tmp[64];
|
|
|
|
unsigned char keyblk[256];
|
|
|
|
unsigned char *key1;
|
|
|
|
unsigned char *key2;
|
2013-01-07 17:20:04 +00:00
|
|
|
unsigned char *mac_enc;
|
|
|
|
unsigned char *mac_dec;
|
2012-04-18 14:23:57 +00:00
|
|
|
unsigned int iv_copy_len;
|
2013-01-07 17:20:04 +00:00
|
|
|
const cipher_info_t *cipher_info;
|
|
|
|
const md_info_t *md_info;
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl_session *session = ssl->session_negotiate;
|
|
|
|
ssl_transform *transform = ssl->transform_negotiate;
|
|
|
|
ssl_handshake_params *handshake = ssl->handshake;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
|
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
|
|
|
|
if( cipher_info == NULL )
|
|
|
|
{
|
2013-04-16 11:15:56 +00:00
|
|
|
SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
|
2013-01-07 17:20:04 +00:00
|
|
|
transform->ciphersuite_info->cipher ) );
|
|
|
|
return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
|
|
|
md_info = md_info_from_type( transform->ciphersuite_info->mac );
|
|
|
|
if( md_info == NULL )
|
|
|
|
{
|
2013-04-16 11:15:56 +00:00
|
|
|
SSL_DEBUG_MSG( 1, ( "md info for %d not found",
|
2013-01-07 17:20:04 +00:00
|
|
|
transform->ciphersuite_info->mac ) );
|
|
|
|
return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
2012-04-11 12:09:53 +00:00
|
|
|
/*
|
2012-04-18 14:23:57 +00:00
|
|
|
* Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
|
2012-04-11 12:09:53 +00:00
|
|
|
*/
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_SSL3)
|
2012-04-11 12:09:53 +00:00
|
|
|
if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
|
2012-04-18 14:23:57 +00:00
|
|
|
{
|
2012-09-16 19:57:18 +00:00
|
|
|
handshake->tls_prf = ssl3_prf;
|
|
|
|
handshake->calc_verify = ssl_calc_verify_ssl;
|
|
|
|
handshake->calc_finished = ssl_calc_finished_ssl;
|
2012-04-18 14:23:57 +00:00
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
|
|
|
|
if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
|
2012-04-18 14:23:57 +00:00
|
|
|
{
|
2012-09-16 19:57:18 +00:00
|
|
|
handshake->tls_prf = tls1_prf;
|
|
|
|
handshake->calc_verify = ssl_calc_verify_tls;
|
|
|
|
handshake->calc_finished = ssl_calc_finished_tls;
|
2012-04-18 14:23:57 +00:00
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_SSL_PROTO_TLS1_2)
|
2013-06-30 12:34:05 +00:00
|
|
|
#if defined(POLARSSL_SHA512_C)
|
2013-08-27 19:19:20 +00:00
|
|
|
if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
|
|
|
|
transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
|
2012-04-18 14:23:57 +00:00
|
|
|
{
|
2012-09-16 19:57:18 +00:00
|
|
|
handshake->tls_prf = tls_prf_sha384;
|
|
|
|
handshake->calc_verify = ssl_calc_verify_tls_sha384;
|
|
|
|
handshake->calc_finished = ssl_calc_finished_tls_sha384;
|
2012-04-18 14:23:57 +00:00
|
|
|
}
|
2012-04-11 12:09:53 +00:00
|
|
|
else
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_SHA256_C)
|
|
|
|
if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
|
2012-04-18 14:23:57 +00:00
|
|
|
{
|
2012-09-16 19:57:18 +00:00
|
|
|
handshake->tls_prf = tls_prf_sha256;
|
|
|
|
handshake->calc_verify = ssl_calc_verify_tls_sha256;
|
|
|
|
handshake->calc_finished = ssl_calc_finished_tls_sha256;
|
2012-04-18 14:23:57 +00:00
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#endif
|
2013-08-28 09:57:20 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "should never happen" ) );
|
2013-08-27 19:19:20 +00:00
|
|
|
return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
|
2013-08-28 09:57:20 +00:00
|
|
|
}
|
2012-04-11 12:09:53 +00:00
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* SSLv3:
|
|
|
|
* master =
|
|
|
|
* MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
|
|
|
|
* MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
|
|
|
|
* MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
|
2013-04-16 11:15:56 +00:00
|
|
|
*
|
2013-08-27 19:19:20 +00:00
|
|
|
* TLSv1+:
|
2009-01-03 21:22:43 +00:00
|
|
|
* master = PRF( premaster, "master secret", randbytes )[0..47]
|
|
|
|
*/
|
2012-09-25 21:55:46 +00:00
|
|
|
if( handshake->resume == 0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2012-09-16 19:57:18 +00:00
|
|
|
SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
|
|
|
|
handshake->pmslen );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
handshake->tls_prf( handshake->premaster, handshake->pmslen,
|
|
|
|
"master secret",
|
|
|
|
handshake->randbytes, 64, session->master, 48 );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
memset( handshake->premaster, 0, sizeof( handshake->premaster ) );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Swap the client and server random values.
|
|
|
|
*/
|
2012-09-16 19:57:18 +00:00
|
|
|
memcpy( tmp, handshake->randbytes, 64 );
|
|
|
|
memcpy( handshake->randbytes, tmp + 32, 32 );
|
|
|
|
memcpy( handshake->randbytes + 32, tmp, 32 );
|
2009-01-03 21:22:43 +00:00
|
|
|
memset( tmp, 0, sizeof( tmp ) );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SSLv3:
|
|
|
|
* key block =
|
|
|
|
* MD5( master + SHA1( 'A' + master + randbytes ) ) +
|
|
|
|
* MD5( master + SHA1( 'BB' + master + randbytes ) ) +
|
|
|
|
* MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
|
|
|
|
* MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* TLSv1:
|
|
|
|
* key block = PRF( master, "key expansion", randbytes )
|
|
|
|
*/
|
2012-09-16 19:57:18 +00:00
|
|
|
handshake->tls_prf( session->master, 48, "key expansion",
|
|
|
|
handshake->randbytes, 64, keyblk, 256 );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
|
|
|
|
ssl_get_ciphersuite_name( session->ciphersuite ) ) );
|
|
|
|
SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
|
|
|
|
SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
|
2009-01-03 21:22:43 +00:00
|
|
|
SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
memset( handshake->randbytes, 0, sizeof( handshake->randbytes ) );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine the appropriate key, IV and MAC length.
|
|
|
|
*/
|
2009-01-11 20:25:36 +00:00
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
if( cipher_info->mode == POLARSSL_MODE_GCM )
|
|
|
|
{
|
|
|
|
transform->keylen = cipher_info->key_length;
|
|
|
|
transform->keylen /= 8;
|
|
|
|
transform->minlen = 1;
|
|
|
|
transform->ivlen = 12;
|
|
|
|
transform->fixed_ivlen = 4;
|
|
|
|
transform->maclen = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( md_info->type != POLARSSL_MD_NONE )
|
|
|
|
{
|
2013-08-27 19:19:20 +00:00
|
|
|
int ret;
|
|
|
|
|
2013-07-04 09:51:43 +00:00
|
|
|
if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "md_init_ctx", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "md_init_ctx", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
2012-02-06 16:45:10 +00:00
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
transform->maclen = md_get_size( md_info );
|
2013-07-19 10:19:21 +00:00
|
|
|
|
2013-08-15 11:45:55 +00:00
|
|
|
#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
|
2013-07-19 10:19:21 +00:00
|
|
|
/*
|
|
|
|
* If HMAC is to be truncated, we shall keep the leftmost bytes,
|
|
|
|
* (rfc 6066 page 13 or rfc 2104 section 4),
|
|
|
|
* so we only need to adjust the length here.
|
|
|
|
*/
|
|
|
|
if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
|
|
|
|
transform->maclen = SSL_TRUNCATED_HMAC_LEN;
|
2013-08-15 11:45:55 +00:00
|
|
|
#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
|
2013-01-07 17:20:04 +00:00
|
|
|
}
|
2012-02-06 16:45:10 +00:00
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
transform->keylen = cipher_info->key_length;
|
|
|
|
transform->keylen /= 8;
|
|
|
|
transform->ivlen = cipher_info->iv_size;
|
2012-02-06 16:45:10 +00:00
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
transform->minlen = transform->keylen;
|
|
|
|
if( transform->minlen < transform->maclen )
|
|
|
|
{
|
|
|
|
if( cipher_info->mode == POLARSSL_MODE_STREAM )
|
|
|
|
transform->minlen = transform->maclen;
|
|
|
|
else
|
|
|
|
transform->minlen += transform->keylen;
|
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
|
2012-09-16 19:57:18 +00:00
|
|
|
transform->keylen, transform->minlen, transform->ivlen,
|
|
|
|
transform->maclen ) );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Finally setup the cipher contexts, IVs and MAC secrets.
|
|
|
|
*/
|
|
|
|
if( ssl->endpoint == SSL_IS_CLIENT )
|
|
|
|
{
|
2012-09-16 19:57:18 +00:00
|
|
|
key1 = keyblk + transform->maclen * 2;
|
|
|
|
key2 = keyblk + transform->maclen * 2 + transform->keylen;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
mac_enc = keyblk;
|
|
|
|
mac_dec = keyblk + transform->maclen;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2010-07-25 14:24:53 +00:00
|
|
|
/*
|
|
|
|
* This is not used in TLS v1.1.
|
|
|
|
*/
|
2012-09-16 19:57:18 +00:00
|
|
|
iv_copy_len = ( transform->fixed_ivlen ) ?
|
|
|
|
transform->fixed_ivlen : transform->ivlen;
|
|
|
|
memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
|
|
|
|
memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
|
2012-04-18 14:23:57 +00:00
|
|
|
iv_copy_len );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-09-16 19:57:18 +00:00
|
|
|
key1 = keyblk + transform->maclen * 2 + transform->keylen;
|
|
|
|
key2 = keyblk + transform->maclen * 2;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
mac_enc = keyblk + transform->maclen;
|
|
|
|
mac_dec = keyblk;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2010-07-25 14:24:53 +00:00
|
|
|
/*
|
|
|
|
* This is not used in TLS v1.1.
|
|
|
|
*/
|
2012-09-16 19:57:18 +00:00
|
|
|
iv_copy_len = ( transform->fixed_ivlen ) ?
|
|
|
|
transform->fixed_ivlen : transform->ivlen;
|
|
|
|
memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
|
|
|
|
memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
|
2012-04-18 14:23:57 +00:00
|
|
|
iv_copy_len );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_SSL3)
|
2013-01-07 17:20:04 +00:00
|
|
|
if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
|
|
|
|
{
|
|
|
|
memcpy( transform->mac_enc, mac_enc, transform->maclen );
|
|
|
|
memcpy( transform->mac_dec, mac_dec, transform->maclen );
|
|
|
|
}
|
|
|
|
else
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
|
|
|
|
defined(POLARSSL_SSL_PROTO_TLS1_2)
|
|
|
|
if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
|
2013-01-07 17:20:04 +00:00
|
|
|
{
|
|
|
|
md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
|
|
|
|
md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
|
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
else
|
|
|
|
#endif
|
2013-08-28 09:57:20 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "should never happen" ) );
|
2013-08-27 19:19:20 +00:00
|
|
|
return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
|
2013-08-28 09:57:20 +00:00
|
|
|
}
|
2013-01-07 17:20:04 +00:00
|
|
|
|
2012-05-08 09:17:57 +00:00
|
|
|
#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
|
|
|
|
if( ssl_hw_record_init != NULL)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
|
|
|
|
|
2012-12-19 13:42:06 +00:00
|
|
|
if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
|
|
|
|
transform->iv_enc, transform->iv_dec,
|
|
|
|
iv_copy_len,
|
2013-01-07 17:20:04 +00:00
|
|
|
mac_enc, mac_dec,
|
2012-12-19 13:42:06 +00:00
|
|
|
transform->maclen ) ) != 0 )
|
2012-05-08 09:17:57 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
|
|
|
|
return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
switch( cipher_info->type )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2009-01-03 21:51:57 +00:00
|
|
|
#if defined(POLARSSL_ARC4_C)
|
2013-01-07 17:20:04 +00:00
|
|
|
case POLARSSL_CIPHER_ARC4_128:
|
2012-09-16 19:57:18 +00:00
|
|
|
arc4_setup( (arc4_context *) transform->ctx_enc, key1,
|
|
|
|
transform->keylen );
|
|
|
|
arc4_setup( (arc4_context *) transform->ctx_dec, key2,
|
|
|
|
transform->keylen );
|
2009-01-03 21:22:43 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2009-01-03 21:51:57 +00:00
|
|
|
#if defined(POLARSSL_DES_C)
|
2013-01-07 17:20:04 +00:00
|
|
|
case POLARSSL_CIPHER_DES_EDE3_CBC:
|
|
|
|
des3_set3key_enc( (des3_context *) transform->ctx_enc, key1 );
|
|
|
|
des3_set3key_dec( (des3_context *) transform->ctx_dec, key2 );
|
|
|
|
break;
|
2009-01-03 21:22:43 +00:00
|
|
|
#endif
|
|
|
|
|
2009-01-03 21:51:57 +00:00
|
|
|
#if defined(POLARSSL_AES_C)
|
2013-01-07 17:20:04 +00:00
|
|
|
case POLARSSL_CIPHER_AES_128_CBC:
|
|
|
|
case POLARSSL_CIPHER_AES_256_CBC:
|
|
|
|
aes_setkey_enc( (aes_context*) transform->ctx_enc, key1,
|
|
|
|
cipher_info->key_length );
|
|
|
|
aes_setkey_dec( (aes_context*) transform->ctx_dec, key2,
|
|
|
|
cipher_info->key_length );
|
2009-01-03 21:22:43 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2009-01-11 20:25:36 +00:00
|
|
|
#if defined(POLARSSL_CAMELLIA_C)
|
2013-01-07 17:20:04 +00:00
|
|
|
case POLARSSL_CIPHER_CAMELLIA_128_CBC:
|
|
|
|
case POLARSSL_CIPHER_CAMELLIA_256_CBC:
|
|
|
|
camellia_setkey_enc( (camellia_context*) transform->ctx_enc, key1,
|
|
|
|
cipher_info->key_length );
|
|
|
|
camellia_setkey_dec( (camellia_context*) transform->ctx_dec, key2,
|
|
|
|
cipher_info->key_length );
|
2009-01-11 20:25:36 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2012-02-06 16:45:10 +00:00
|
|
|
#if defined(POLARSSL_DES_C)
|
2013-01-07 17:20:04 +00:00
|
|
|
case POLARSSL_CIPHER_DES_CBC:
|
2012-09-16 19:57:18 +00:00
|
|
|
des_setkey_enc( (des_context *) transform->ctx_enc, key1 );
|
|
|
|
des_setkey_dec( (des_context *) transform->ctx_dec, key2 );
|
2012-02-06 16:45:10 +00:00
|
|
|
break;
|
|
|
|
#endif
|
2013-01-07 17:20:04 +00:00
|
|
|
|
|
|
|
#if defined(POLARSSL_GCM_C)
|
|
|
|
case POLARSSL_CIPHER_AES_128_GCM:
|
|
|
|
case POLARSSL_CIPHER_AES_256_GCM:
|
|
|
|
gcm_init( (gcm_context *) transform->ctx_enc, key1,
|
|
|
|
cipher_info->key_length );
|
|
|
|
gcm_init( (gcm_context *) transform->ctx_dec, key2,
|
|
|
|
cipher_info->key_length );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case POLARSSL_CIPHER_NULL:
|
|
|
|
break;
|
2012-02-06 16:45:10 +00:00
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
default:
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memset( keyblk, 0, sizeof( keyblk ) );
|
|
|
|
|
2012-07-03 13:30:23 +00:00
|
|
|
#if defined(POLARSSL_ZLIB_SUPPORT)
|
|
|
|
// Initialize compression
|
|
|
|
//
|
2012-09-16 19:57:18 +00:00
|
|
|
if( session->compression == SSL_COMPRESS_DEFLATE )
|
2012-07-03 13:30:23 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
|
|
|
|
memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
|
2012-07-03 13:30:23 +00:00
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
if( deflateInit( &transform->ctx_deflate, Z_DEFAULT_COMPRESSION ) != Z_OK ||
|
|
|
|
inflateInit( &transform->ctx_inflate ) != Z_OK )
|
2012-07-03 13:30:23 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
|
|
|
|
return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* POLARSSL_ZLIB_SUPPORT */
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_SSL3)
|
2012-04-18 16:10:25 +00:00
|
|
|
void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
md5_context md5;
|
|
|
|
sha1_context sha1;
|
|
|
|
unsigned char pad_1[48];
|
|
|
|
unsigned char pad_2[48];
|
|
|
|
|
2012-04-18 16:10:25 +00:00
|
|
|
SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
|
|
|
|
memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-04-18 16:10:25 +00:00
|
|
|
memset( pad_1, 0x36, 48 );
|
|
|
|
memset( pad_2, 0x5C, 48 );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
md5_update( &md5, ssl->session_negotiate->master, 48 );
|
2012-04-18 16:10:25 +00:00
|
|
|
md5_update( &md5, pad_1, 48 );
|
|
|
|
md5_finish( &md5, hash );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-04-18 16:10:25 +00:00
|
|
|
md5_starts( &md5 );
|
2012-09-16 19:57:18 +00:00
|
|
|
md5_update( &md5, ssl->session_negotiate->master, 48 );
|
2012-04-18 16:10:25 +00:00
|
|
|
md5_update( &md5, pad_2, 48 );
|
|
|
|
md5_update( &md5, hash, 16 );
|
|
|
|
md5_finish( &md5, hash );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
sha1_update( &sha1, ssl->session_negotiate->master, 48 );
|
2012-04-18 16:10:25 +00:00
|
|
|
sha1_update( &sha1, pad_1, 40 );
|
|
|
|
sha1_finish( &sha1, hash + 16 );
|
|
|
|
|
|
|
|
sha1_starts( &sha1 );
|
2012-09-16 19:57:18 +00:00
|
|
|
sha1_update( &sha1, ssl->session_negotiate->master, 48 );
|
2012-04-18 16:10:25 +00:00
|
|
|
sha1_update( &sha1, pad_2, 40 );
|
|
|
|
sha1_update( &sha1, hash + 16, 20 );
|
|
|
|
sha1_finish( &sha1, hash + 16 );
|
|
|
|
|
|
|
|
SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif
|
2012-04-18 16:10:25 +00:00
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
|
2012-04-18 16:10:25 +00:00
|
|
|
void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
|
|
|
|
{
|
|
|
|
md5_context md5;
|
|
|
|
sha1_context sha1;
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
|
|
|
|
memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
|
2012-04-18 16:10:25 +00:00
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
md5_finish( &md5, hash );
|
2012-04-18 16:10:25 +00:00
|
|
|
sha1_finish( &sha1, hash + 16 );
|
|
|
|
|
|
|
|
SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
|
2012-04-18 16:10:25 +00:00
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_TLS1_2)
|
|
|
|
#if defined(POLARSSL_SHA256_C)
|
2012-04-18 16:10:25 +00:00
|
|
|
void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
|
|
|
|
{
|
2013-06-30 12:34:05 +00:00
|
|
|
sha256_context sha256;
|
2012-04-18 16:10:25 +00:00
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
|
|
|
|
|
2013-06-30 12:34:05 +00:00
|
|
|
memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
|
|
|
|
sha256_finish( &sha256, hash );
|
2012-04-18 16:10:25 +00:00
|
|
|
|
|
|
|
SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif /* POLARSSL_SHA256_C */
|
2012-04-18 16:10:25 +00:00
|
|
|
|
2013-06-30 12:34:05 +00:00
|
|
|
#if defined(POLARSSL_SHA512_C)
|
2012-04-18 16:10:25 +00:00
|
|
|
void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
|
|
|
|
{
|
2013-06-30 12:34:05 +00:00
|
|
|
sha512_context sha512;
|
2012-04-18 16:10:25 +00:00
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
|
|
|
|
|
2013-06-30 12:34:05 +00:00
|
|
|
memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
|
|
|
|
sha512_finish( &sha512, hash );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-04-18 14:23:57 +00:00
|
|
|
SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
|
2009-01-03 21:22:43 +00:00
|
|
|
SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif /* POLARSSL_SHA512_C */
|
|
|
|
#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_SSL3)
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* SSLv3.0 MAC functions
|
|
|
|
*/
|
2013-01-07 17:20:04 +00:00
|
|
|
static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
|
|
|
|
unsigned char *buf, size_t len,
|
|
|
|
unsigned char *ctr, int type )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
unsigned char header[11];
|
|
|
|
unsigned char padding[48];
|
2013-01-07 17:20:04 +00:00
|
|
|
int padlen = 0;
|
|
|
|
int md_size = md_get_size( md_ctx->md_info );
|
|
|
|
int md_type = md_get_type( md_ctx->md_info );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
if( md_type == POLARSSL_MD_MD5 )
|
|
|
|
padlen = 48;
|
|
|
|
else if( md_type == POLARSSL_MD_SHA1 )
|
|
|
|
padlen = 40;
|
|
|
|
else if( md_type == POLARSSL_MD_SHA256 )
|
|
|
|
padlen = 32;
|
2012-09-13 14:23:06 +00:00
|
|
|
|
|
|
|
memcpy( header, ctr, 8 );
|
|
|
|
header[ 8] = (unsigned char) type;
|
|
|
|
header[ 9] = (unsigned char)( len >> 8 );
|
|
|
|
header[10] = (unsigned char)( len );
|
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
memset( padding, 0x36, padlen );
|
|
|
|
md_starts( md_ctx );
|
|
|
|
md_update( md_ctx, secret, md_size );
|
|
|
|
md_update( md_ctx, padding, padlen );
|
|
|
|
md_update( md_ctx, header, 11 );
|
|
|
|
md_update( md_ctx, buf, len );
|
|
|
|
md_finish( md_ctx, buf + len );
|
2012-09-13 14:23:06 +00:00
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
memset( padding, 0x5C, padlen );
|
|
|
|
md_starts( md_ctx );
|
|
|
|
md_update( md_ctx, secret, md_size );
|
|
|
|
md_update( md_ctx, padding, padlen );
|
|
|
|
md_update( md_ctx, buf + len, md_size );
|
|
|
|
md_finish( md_ctx, buf + len );
|
2012-09-13 14:23:06 +00:00
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif /* POLARSSL_SSL_PROTO_SSL3 */
|
2012-09-13 14:23:06 +00:00
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* Encryption/decryption functions
|
2013-04-16 11:15:56 +00:00
|
|
|
*/
|
2009-01-03 21:22:43 +00:00
|
|
|
static int ssl_encrypt_buf( ssl_context *ssl )
|
|
|
|
{
|
2011-04-24 08:57:21 +00:00
|
|
|
size_t i, padlen;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add MAC then encrypt
|
|
|
|
*/
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_SSL3)
|
2009-01-03 21:22:43 +00:00
|
|
|
if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
|
|
|
|
{
|
2013-01-07 17:20:04 +00:00
|
|
|
ssl_mac( &ssl->transform_out->md_ctx_enc,
|
|
|
|
ssl->transform_out->mac_enc,
|
|
|
|
ssl->out_msg, ssl->out_msglen,
|
|
|
|
ssl->out_ctr, ssl->out_msgtype );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
else
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
|
|
|
|
defined(POLARSSL_SSL_PROTO_TLS1_2)
|
|
|
|
if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2013-01-07 17:20:04 +00:00
|
|
|
md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
|
|
|
|
md_hmac_update( &ssl->transform_out->md_ctx_enc,
|
|
|
|
ssl->out_msg, ssl->out_msglen );
|
|
|
|
md_hmac_finish( &ssl->transform_out->md_ctx_enc,
|
|
|
|
ssl->out_msg + ssl->out_msglen );
|
|
|
|
md_hmac_reset( &ssl->transform_out->md_ctx_enc );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
else
|
|
|
|
#endif
|
2013-08-28 09:57:20 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "should never happen" ) );
|
2013-08-27 19:19:20 +00:00
|
|
|
return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
|
2013-08-28 09:57:20 +00:00
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
SSL_DEBUG_BUF( 4, "computed mac",
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->out_msg + ssl->out_msglen, ssl->transform_out->maclen );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->out_msglen += ssl->transform_out->maclen;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
#if defined(POLARSSL_CIPHER_NULL_CIPHER)
|
|
|
|
if( ssl->transform_out->ciphersuite_info->cipher == POLARSSL_CIPHER_NULL )
|
|
|
|
{
|
|
|
|
padlen = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* POLARSSL_CIPHER_NULL_CIPHER */
|
|
|
|
#if defined(POLARSSL_ARC4_C)
|
|
|
|
if( ssl->transform_out->ciphersuite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
padlen = 0;
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
|
|
|
|
"including %d bytes of padding",
|
|
|
|
ssl->out_msglen, 0 ) );
|
|
|
|
|
|
|
|
SSL_DEBUG_BUF( 4, "before encrypt: output payload",
|
|
|
|
ssl->out_msg, ssl->out_msglen );
|
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
arc4_crypt( (arc4_context *) ssl->transform_out->ctx_enc,
|
|
|
|
ssl->out_msglen, ssl->out_msg,
|
|
|
|
ssl->out_msg );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
2013-01-07 17:20:04 +00:00
|
|
|
else
|
|
|
|
#endif /* POLARSSL_ARC4_C */
|
|
|
|
#if defined(POLARSSL_GCM_C)
|
|
|
|
if( ssl->transform_out->ciphersuite_info->cipher == POLARSSL_CIPHER_AES_128_GCM ||
|
|
|
|
ssl->transform_out->ciphersuite_info->cipher == POLARSSL_CIPHER_AES_256_GCM )
|
2012-04-18 14:23:57 +00:00
|
|
|
{
|
|
|
|
size_t enc_msglen;
|
|
|
|
unsigned char *enc_msg;
|
|
|
|
unsigned char add_data[13];
|
|
|
|
int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
|
|
|
|
|
|
|
|
padlen = 0;
|
|
|
|
enc_msglen = ssl->out_msglen;
|
|
|
|
|
|
|
|
memcpy( add_data, ssl->out_ctr, 8 );
|
|
|
|
add_data[8] = ssl->out_msgtype;
|
|
|
|
add_data[9] = ssl->major_ver;
|
|
|
|
add_data[10] = ssl->minor_ver;
|
|
|
|
add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
|
|
|
|
add_data[12] = ssl->out_msglen & 0xFF;
|
|
|
|
|
|
|
|
SSL_DEBUG_BUF( 4, "additional data used for AEAD",
|
|
|
|
add_data, 13 );
|
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
/*
|
|
|
|
* Generate IV
|
|
|
|
*/
|
|
|
|
ret = ssl->f_rng( ssl->p_rng,
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
|
2013-01-07 17:20:04 +00:00
|
|
|
ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
2012-04-18 14:23:57 +00:00
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
memcpy( ssl->out_iv,
|
|
|
|
ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
|
|
|
|
ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
|
2012-04-18 14:23:57 +00:00
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
/*
|
|
|
|
* Fix pointer positions and message length with added IV
|
|
|
|
*/
|
|
|
|
enc_msg = ssl->out_msg;
|
|
|
|
enc_msglen = ssl->out_msglen;
|
|
|
|
ssl->out_msglen += ssl->transform_out->ivlen -
|
|
|
|
ssl->transform_out->fixed_ivlen;
|
2012-04-18 14:23:57 +00:00
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
|
|
|
|
"including %d bytes of padding",
|
|
|
|
ssl->out_msglen, 0 ) );
|
2012-04-18 14:23:57 +00:00
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
SSL_DEBUG_BUF( 4, "before encrypt: output payload",
|
|
|
|
ssl->out_msg, ssl->out_msglen );
|
2012-04-18 14:23:57 +00:00
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
/*
|
|
|
|
* Adjust for tag
|
|
|
|
*/
|
|
|
|
ssl->out_msglen += 16;
|
2012-04-18 14:23:57 +00:00
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
gcm_crypt_and_tag( (gcm_context *) ssl->transform_out->ctx_enc,
|
|
|
|
GCM_ENCRYPT, enc_msglen,
|
|
|
|
ssl->transform_out->iv_enc, ssl->transform_out->ivlen,
|
|
|
|
add_data, 13,
|
|
|
|
enc_msg, enc_msg,
|
|
|
|
16, enc_msg + enc_msglen );
|
|
|
|
|
|
|
|
SSL_DEBUG_BUF( 4, "after encrypt: tag",
|
|
|
|
enc_msg + enc_msglen, 16 );
|
2012-04-18 14:23:57 +00:00
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
else
|
2013-01-07 17:20:04 +00:00
|
|
|
#endif /* POLARSSL_GCM_C */
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2010-07-25 14:24:53 +00:00
|
|
|
unsigned char *enc_msg;
|
2011-04-24 08:57:21 +00:00
|
|
|
size_t enc_msglen;
|
2010-07-25 14:24:53 +00:00
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
|
|
|
|
ssl->transform_out->ivlen;
|
|
|
|
if( padlen == ssl->transform_out->ivlen )
|
2009-01-03 21:22:43 +00:00
|
|
|
padlen = 0;
|
|
|
|
|
|
|
|
for( i = 0; i <= padlen; i++ )
|
|
|
|
ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
|
|
|
|
|
|
|
|
ssl->out_msglen += padlen + 1;
|
|
|
|
|
2010-07-25 14:24:53 +00:00
|
|
|
enc_msglen = ssl->out_msglen;
|
|
|
|
enc_msg = ssl->out_msg;
|
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
|
2010-07-25 14:24:53 +00:00
|
|
|
/*
|
2012-04-11 12:09:53 +00:00
|
|
|
* Prepend per-record IV for block cipher in TLS v1.1 and up as per
|
|
|
|
* Method 1 (6.2.3.2. in RFC4346 and RFC5246)
|
2010-07-25 14:24:53 +00:00
|
|
|
*/
|
2012-04-11 12:09:53 +00:00
|
|
|
if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
|
2010-07-25 14:24:53 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Generate IV
|
|
|
|
*/
|
2012-09-16 19:57:18 +00:00
|
|
|
int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
|
|
|
|
ssl->transform_out->ivlen );
|
2011-11-27 21:07:34 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
2010-07-25 14:24:53 +00:00
|
|
|
|
2013-01-02 16:30:03 +00:00
|
|
|
memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->transform_out->ivlen );
|
2010-07-25 14:24:53 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fix pointer positions and message length with added IV
|
|
|
|
*/
|
2013-01-02 16:30:03 +00:00
|
|
|
enc_msg = ssl->out_msg;
|
2010-07-25 14:24:53 +00:00
|
|
|
enc_msglen = ssl->out_msglen;
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->out_msglen += ssl->transform_out->ivlen;
|
2010-07-25 14:24:53 +00:00
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
|
2010-07-25 14:24:53 +00:00
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
|
2010-07-25 14:24:53 +00:00
|
|
|
"including %d bytes of IV and %d bytes of padding",
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->out_msglen, ssl->transform_out->ivlen, padlen + 1 ) );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
SSL_DEBUG_BUF( 4, "before encrypt: output payload",
|
2013-01-02 16:30:03 +00:00
|
|
|
ssl->out_iv, ssl->out_msglen );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
switch( ssl->transform_out->ciphersuite_info->cipher )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2013-08-19 11:29:26 +00:00
|
|
|
#if defined(POLARSSL_DES_C)
|
2013-01-07 17:20:04 +00:00
|
|
|
case POLARSSL_CIPHER_DES_CBC:
|
|
|
|
des_crypt_cbc( (des_context *) ssl->transform_out->ctx_enc,
|
|
|
|
DES_ENCRYPT, enc_msglen,
|
|
|
|
ssl->transform_out->iv_enc, enc_msg, enc_msg );
|
2009-01-03 21:22:43 +00:00
|
|
|
break;
|
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
case POLARSSL_CIPHER_DES_EDE3_CBC:
|
|
|
|
des3_crypt_cbc( (des3_context *) ssl->transform_out->ctx_enc,
|
|
|
|
DES_ENCRYPT, enc_msglen,
|
|
|
|
ssl->transform_out->iv_enc, enc_msg, enc_msg );
|
|
|
|
break;
|
2013-08-19 11:29:26 +00:00
|
|
|
#endif
|
2009-01-11 20:25:36 +00:00
|
|
|
|
2013-08-19 11:29:26 +00:00
|
|
|
#if defined(POLARSSL_AES_C)
|
2013-01-07 17:20:04 +00:00
|
|
|
case POLARSSL_CIPHER_AES_128_CBC:
|
|
|
|
case POLARSSL_CIPHER_AES_256_CBC:
|
|
|
|
aes_crypt_cbc( (aes_context *) ssl->transform_out->ctx_enc,
|
|
|
|
AES_ENCRYPT, enc_msglen,
|
|
|
|
ssl->transform_out->iv_enc, enc_msg, enc_msg );
|
|
|
|
break;
|
2013-08-19 11:29:26 +00:00
|
|
|
#endif
|
2013-01-07 17:20:04 +00:00
|
|
|
|
2013-08-19 11:29:26 +00:00
|
|
|
#if defined(POLARSSL_CAMELLIA_C)
|
2013-01-07 17:20:04 +00:00
|
|
|
case POLARSSL_CIPHER_CAMELLIA_128_CBC:
|
|
|
|
case POLARSSL_CIPHER_CAMELLIA_256_CBC:
|
|
|
|
camellia_crypt_cbc( (camellia_context *) ssl->transform_out->ctx_enc,
|
|
|
|
CAMELLIA_ENCRYPT, enc_msglen,
|
|
|
|
ssl->transform_out->iv_enc, enc_msg, enc_msg );
|
|
|
|
break;
|
2013-08-19 11:29:26 +00:00
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
default:
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-18 14:23:57 +00:00
|
|
|
for( i = 8; i > 0; i-- )
|
|
|
|
if( ++ssl->out_ctr[i - 1] != 0 )
|
|
|
|
break;
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2013-03-20 14:30:09 +00:00
|
|
|
#define POLARSSL_SSL_MAX_MAC_SIZE 48
|
2012-02-06 16:45:10 +00:00
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
static int ssl_decrypt_buf( ssl_context *ssl )
|
|
|
|
{
|
2013-01-03 13:52:21 +00:00
|
|
|
size_t i, padlen = 0, correct = 1;
|
2012-02-06 16:45:10 +00:00
|
|
|
unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
if( ssl->in_msglen < ssl->transform_in->minlen )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->in_msglen, ssl->transform_in->minlen ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_INVALID_MAC );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
#if defined(POLARSSL_CIPHER_NULL_CIPHER)
|
|
|
|
if( ssl->transform_in->ciphersuite_info->cipher == POLARSSL_CIPHER_NULL )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2013-01-07 17:20:04 +00:00
|
|
|
padlen = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* POLARSSL_CIPHER_NULL_CIPHER */
|
2009-01-03 21:51:57 +00:00
|
|
|
#if defined(POLARSSL_ARC4_C)
|
2013-01-07 17:20:04 +00:00
|
|
|
if( ssl->transform_in->ciphersuite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
|
|
|
|
{
|
|
|
|
padlen = 0;
|
|
|
|
|
|
|
|
arc4_crypt( (arc4_context *) ssl->transform_in->ctx_dec,
|
2010-03-21 15:42:15 +00:00
|
|
|
ssl->in_msglen, ssl->in_msg,
|
|
|
|
ssl->in_msg );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
2013-01-07 17:20:04 +00:00
|
|
|
else
|
|
|
|
#endif /* POLARSSL_ARC4_C */
|
|
|
|
#if defined(POLARSSL_GCM_C)
|
|
|
|
if( ssl->transform_in->ciphersuite_info->cipher == POLARSSL_CIPHER_AES_128_GCM ||
|
|
|
|
ssl->transform_in->ciphersuite_info->cipher == POLARSSL_CIPHER_AES_256_GCM )
|
2012-04-18 14:23:57 +00:00
|
|
|
{
|
|
|
|
unsigned char *dec_msg;
|
|
|
|
unsigned char *dec_msg_result;
|
|
|
|
size_t dec_msglen;
|
|
|
|
unsigned char add_data[13];
|
|
|
|
int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
|
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
padlen = 0;
|
|
|
|
|
|
|
|
dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
|
|
|
|
ssl->transform_in->fixed_ivlen );
|
|
|
|
dec_msglen -= 16;
|
|
|
|
dec_msg = ssl->in_msg;
|
|
|
|
dec_msg_result = ssl->in_msg;
|
|
|
|
ssl->in_msglen = dec_msglen;
|
|
|
|
|
|
|
|
memcpy( add_data, ssl->in_ctr, 8 );
|
|
|
|
add_data[8] = ssl->in_msgtype;
|
|
|
|
add_data[9] = ssl->major_ver;
|
|
|
|
add_data[10] = ssl->minor_ver;
|
|
|
|
add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
|
|
|
|
add_data[12] = ssl->in_msglen & 0xFF;
|
|
|
|
|
|
|
|
SSL_DEBUG_BUF( 4, "additional data used for AEAD",
|
|
|
|
add_data, 13 );
|
|
|
|
|
|
|
|
memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
|
|
|
|
ssl->in_iv,
|
|
|
|
ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
|
|
|
|
|
|
|
|
SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
|
|
|
|
ssl->transform_in->ivlen );
|
|
|
|
SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
|
|
|
|
|
|
|
|
ret = gcm_auth_decrypt( (gcm_context *) ssl->transform_in->ctx_dec,
|
|
|
|
dec_msglen,
|
|
|
|
ssl->transform_in->iv_dec,
|
|
|
|
ssl->transform_in->ivlen,
|
|
|
|
add_data, 13,
|
|
|
|
dec_msg + dec_msglen, 16,
|
|
|
|
dec_msg, dec_msg_result );
|
|
|
|
|
|
|
|
if( ret != 0 )
|
2012-04-18 14:23:57 +00:00
|
|
|
{
|
2013-01-07 17:20:04 +00:00
|
|
|
SSL_DEBUG_MSG( 1, ( "AEAD decrypt failed on validation (ret = -0x%02x)",
|
|
|
|
-ret ) );
|
2012-04-18 14:23:57 +00:00
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
return( POLARSSL_ERR_SSL_INVALID_MAC );
|
|
|
|
}
|
2012-04-18 14:23:57 +00:00
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
else
|
2013-01-07 17:20:04 +00:00
|
|
|
#endif /* POLARSSL_GCM_C */
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2013-01-03 13:52:21 +00:00
|
|
|
/*
|
|
|
|
* Decrypt and check the padding
|
|
|
|
*/
|
2010-07-25 14:24:53 +00:00
|
|
|
unsigned char *dec_msg;
|
|
|
|
unsigned char *dec_msg_result;
|
2011-04-24 08:57:21 +00:00
|
|
|
size_t dec_msglen;
|
2013-02-27 13:48:00 +00:00
|
|
|
size_t minlen = 0;
|
2010-07-25 14:24:53 +00:00
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
2013-01-03 13:52:21 +00:00
|
|
|
* Check immediate ciphertext sanity
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2012-09-16 19:57:18 +00:00
|
|
|
if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->in_msglen, ssl->transform_in->ivlen ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_INVALID_MAC );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
|
2013-01-03 13:52:21 +00:00
|
|
|
if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
|
|
|
|
minlen += ssl->transform_in->ivlen;
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif
|
2013-01-03 13:52:21 +00:00
|
|
|
|
|
|
|
if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
|
|
|
|
ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) + 1 ) ( + expl IV )",
|
|
|
|
ssl->in_msglen, ssl->transform_in->ivlen, ssl->transform_in->maclen ) );
|
|
|
|
return( POLARSSL_ERR_SSL_INVALID_MAC );
|
|
|
|
}
|
|
|
|
|
2010-07-25 14:24:53 +00:00
|
|
|
dec_msglen = ssl->in_msglen;
|
|
|
|
dec_msg = ssl->in_msg;
|
|
|
|
dec_msg_result = ssl->in_msg;
|
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
|
2010-07-25 14:24:53 +00:00
|
|
|
/*
|
2012-04-11 12:09:53 +00:00
|
|
|
* Initialize for prepended IV for block cipher in TLS v1.1 and up
|
2010-07-25 14:24:53 +00:00
|
|
|
*/
|
2012-04-11 12:09:53 +00:00
|
|
|
if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
|
2010-07-25 14:24:53 +00:00
|
|
|
{
|
2012-09-16 19:57:18 +00:00
|
|
|
dec_msglen -= ssl->transform_in->ivlen;
|
|
|
|
ssl->in_msglen -= ssl->transform_in->ivlen;
|
2010-07-25 14:24:53 +00:00
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
for( i = 0; i < ssl->transform_in->ivlen; i++ )
|
2013-01-02 16:30:03 +00:00
|
|
|
ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
|
2010-07-25 14:24:53 +00:00
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
|
2010-07-25 14:24:53 +00:00
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
switch( ssl->transform_in->ciphersuite_info->cipher )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2013-08-19 11:29:26 +00:00
|
|
|
#if defined(POLARSSL_DES_C)
|
2013-01-07 17:20:04 +00:00
|
|
|
case POLARSSL_CIPHER_DES_CBC:
|
|
|
|
des_crypt_cbc( (des_context *) ssl->transform_in->ctx_dec,
|
|
|
|
DES_DECRYPT, dec_msglen,
|
|
|
|
ssl->transform_in->iv_dec, dec_msg, dec_msg_result );
|
2009-01-03 21:22:43 +00:00
|
|
|
break;
|
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
case POLARSSL_CIPHER_DES_EDE3_CBC:
|
|
|
|
des3_crypt_cbc( (des3_context *) ssl->transform_in->ctx_dec,
|
|
|
|
DES_DECRYPT, dec_msglen,
|
|
|
|
ssl->transform_in->iv_dec, dec_msg, dec_msg_result );
|
|
|
|
break;
|
2013-08-19 11:29:26 +00:00
|
|
|
#endif
|
2009-01-11 20:25:36 +00:00
|
|
|
|
2013-08-19 11:29:26 +00:00
|
|
|
#if defined(POLARSSL_AES_C)
|
2013-01-07 17:20:04 +00:00
|
|
|
case POLARSSL_CIPHER_AES_128_CBC:
|
|
|
|
case POLARSSL_CIPHER_AES_256_CBC:
|
|
|
|
aes_crypt_cbc( (aes_context *) ssl->transform_in->ctx_dec,
|
|
|
|
AES_DECRYPT, dec_msglen,
|
|
|
|
ssl->transform_in->iv_dec, dec_msg, dec_msg_result );
|
|
|
|
break;
|
2013-08-19 11:29:26 +00:00
|
|
|
#endif
|
2013-01-07 17:20:04 +00:00
|
|
|
|
2013-08-19 11:29:26 +00:00
|
|
|
#if defined(POLARSSL_CAMELLIA_C)
|
2013-01-07 17:20:04 +00:00
|
|
|
case POLARSSL_CIPHER_CAMELLIA_128_CBC:
|
|
|
|
case POLARSSL_CIPHER_CAMELLIA_256_CBC:
|
|
|
|
camellia_crypt_cbc( (camellia_context *) ssl->transform_in->ctx_dec,
|
|
|
|
CAMELLIA_DECRYPT, dec_msglen,
|
|
|
|
ssl->transform_in->iv_dec, dec_msg, dec_msg_result );
|
|
|
|
break;
|
2013-08-19 11:29:26 +00:00
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
default:
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
|
2013-01-03 13:52:21 +00:00
|
|
|
|
|
|
|
if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
|
|
|
|
{
|
2013-01-31 15:57:45 +00:00
|
|
|
#if defined(POLARSSL_SSL_DEBUG_ALL)
|
2013-01-03 13:52:21 +00:00
|
|
|
SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
|
|
|
|
ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
|
2013-01-31 15:57:45 +00:00
|
|
|
#endif
|
2013-01-03 13:52:21 +00:00
|
|
|
padlen = 0;
|
|
|
|
correct = 0;
|
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_SSL3)
|
2009-01-03 21:22:43 +00:00
|
|
|
if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
|
|
|
|
{
|
2012-09-16 19:57:18 +00:00
|
|
|
if( padlen > ssl->transform_in->ivlen )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2013-01-31 15:57:45 +00:00
|
|
|
#if defined(POLARSSL_SSL_DEBUG_ALL)
|
2009-01-03 21:22:43 +00:00
|
|
|
SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
|
|
|
|
"should be no more than %d",
|
2012-09-16 19:57:18 +00:00
|
|
|
padlen, ssl->transform_in->ivlen ) );
|
2013-01-31 15:57:45 +00:00
|
|
|
#endif
|
2013-01-03 13:52:21 +00:00
|
|
|
correct = 0;
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
|
|
|
|
defined(POLARSSL_SSL_PROTO_TLS1_2)
|
|
|
|
if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
/*
|
2013-01-03 13:52:21 +00:00
|
|
|
* TLSv1+: always check the padding up to the first failure
|
|
|
|
* and fake check up to 256 bytes of padding
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2013-02-27 13:48:00 +00:00
|
|
|
size_t pad_count = 0, fake_pad_count = 0;
|
|
|
|
size_t padding_idx = ssl->in_msglen - padlen - 1;
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
for( i = 1; i <= padlen; i++ )
|
2013-02-27 13:48:00 +00:00
|
|
|
pad_count += ( ssl->in_msg[padding_idx + i] == padlen - 1 );
|
|
|
|
|
|
|
|
for( ; i <= 256; i++ )
|
|
|
|
fake_pad_count += ( ssl->in_msg[padding_idx + i] == padlen - 1 );
|
|
|
|
|
|
|
|
correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
|
|
|
|
correct &= ( pad_count + fake_pad_count < 512 ); /* Always 1 */
|
|
|
|
|
2013-01-31 15:57:45 +00:00
|
|
|
#if defined(POLARSSL_SSL_DEBUG_ALL)
|
2013-01-03 13:52:21 +00:00
|
|
|
if( padlen > 0 && correct == 0)
|
|
|
|
SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
|
2013-01-31 15:57:45 +00:00
|
|
|
#endif
|
2013-02-27 13:48:00 +00:00
|
|
|
padlen &= correct * 0x1FF;
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
else
|
|
|
|
#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
|
|
|
|
POLARSSL_SSL_PROTO_TLS1_2 */
|
2013-08-28 09:57:20 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "should never happen" ) );
|
2013-08-27 19:19:20 +00:00
|
|
|
return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
|
2013-08-28 09:57:20 +00:00
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SSL_DEBUG_BUF( 4, "raw buffer after decryption",
|
|
|
|
ssl->in_msg, ssl->in_msglen );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Always compute the MAC (RFC4346, CBCTIME).
|
|
|
|
*/
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
|
|
|
|
ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
|
|
|
|
|
2013-01-03 13:52:21 +00:00
|
|
|
memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_SSL3)
|
2009-01-03 21:22:43 +00:00
|
|
|
if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
|
|
|
|
{
|
2013-01-07 17:20:04 +00:00
|
|
|
ssl_mac( &ssl->transform_in->md_ctx_dec,
|
|
|
|
ssl->transform_in->mac_dec,
|
|
|
|
ssl->in_msg, ssl->in_msglen,
|
|
|
|
ssl->in_ctr, ssl->in_msgtype );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
else
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif /* POLARSSL_SSL_PROTO_SSL3 */
|
|
|
|
#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
|
|
|
|
defined(POLARSSL_SSL_PROTO_TLS1_2)
|
|
|
|
if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2013-01-03 13:52:21 +00:00
|
|
|
/*
|
|
|
|
* Process MAC and always update for padlen afterwards to make
|
|
|
|
* total time independent of padlen
|
2013-02-27 13:48:00 +00:00
|
|
|
*
|
|
|
|
* extra_run compensates MAC check for padlen
|
|
|
|
*
|
|
|
|
* Known timing attacks:
|
|
|
|
* - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
|
|
|
|
*
|
|
|
|
* We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
|
|
|
|
* correctly. (We round down instead of up, so -56 is the correct
|
|
|
|
* value for our calculations instead of -55)
|
2013-01-03 13:52:21 +00:00
|
|
|
*/
|
2013-02-27 13:48:00 +00:00
|
|
|
int j, extra_run = 0;
|
|
|
|
extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
|
|
|
|
( 13 + ssl->in_msglen + 8 ) / 64;
|
|
|
|
|
|
|
|
extra_run &= correct * 0xFF;
|
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
|
|
|
|
md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
|
|
|
|
ssl->in_msglen );
|
|
|
|
md_hmac_finish( &ssl->transform_in->md_ctx_dec,
|
|
|
|
ssl->in_msg + ssl->in_msglen );
|
|
|
|
for( j = 0; j < extra_run; j++ )
|
|
|
|
md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
|
|
|
|
|
|
|
|
md_hmac_reset( &ssl->transform_in->md_ctx_dec );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
else
|
|
|
|
#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
|
|
|
|
POLARSSL_SSL_PROTO_TLS1_2 */
|
2013-08-28 09:57:20 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "should never happen" ) );
|
2013-08-27 19:19:20 +00:00
|
|
|
return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
|
2013-08-28 09:57:20 +00:00
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
|
2009-01-03 21:22:43 +00:00
|
|
|
SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->transform_in->maclen );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
if( memcmp( tmp, ssl->in_msg + ssl->in_msglen,
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->transform_in->maclen ) != 0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2013-02-27 13:48:00 +00:00
|
|
|
#if defined(POLARSSL_SSL_DEBUG_ALL)
|
2009-01-03 21:22:43 +00:00
|
|
|
SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
|
2013-02-27 13:48:00 +00:00
|
|
|
#endif
|
2013-01-03 13:52:21 +00:00
|
|
|
correct = 0;
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-01-03 13:52:21 +00:00
|
|
|
* Finally check the correct flag
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2013-01-03 13:52:21 +00:00
|
|
|
if( correct == 0 )
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_INVALID_MAC );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
if( ssl->in_msglen == 0 )
|
|
|
|
{
|
|
|
|
ssl->nb_zero++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Three or more empty messages may be a DoS attack
|
|
|
|
* (excessive CPU consumption).
|
|
|
|
*/
|
|
|
|
if( ssl->nb_zero > 3 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
|
|
|
|
"messages, possible DoS attack" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_INVALID_MAC );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ssl->nb_zero = 0;
|
2013-04-16 11:15:56 +00:00
|
|
|
|
2011-04-24 08:57:21 +00:00
|
|
|
for( i = 8; i > 0; i-- )
|
|
|
|
if( ++ssl->in_ctr[i - 1] != 0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2012-07-03 13:30:23 +00:00
|
|
|
#if defined(POLARSSL_ZLIB_SUPPORT)
|
|
|
|
/*
|
|
|
|
* Compression/decompression functions
|
|
|
|
*/
|
|
|
|
static int ssl_compress_buf( ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned char *msg_post = ssl->out_msg;
|
|
|
|
size_t len_pre = ssl->out_msglen;
|
|
|
|
unsigned char *msg_pre;
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
|
|
|
|
|
2013-06-30 12:57:46 +00:00
|
|
|
if( len_pre == 0 )
|
|
|
|
return( 0 );
|
|
|
|
|
2013-07-03 11:37:05 +00:00
|
|
|
msg_pre = (unsigned char*) polarssl_malloc( len_pre );
|
2012-07-03 13:30:23 +00:00
|
|
|
if( msg_pre == NULL )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len_pre ) );
|
|
|
|
return( POLARSSL_ERR_SSL_MALLOC_FAILED );
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy( msg_pre, ssl->out_msg, len_pre );
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
|
|
|
|
ssl->out_msglen ) );
|
|
|
|
|
|
|
|
SSL_DEBUG_BUF( 4, "before compression: output payload",
|
|
|
|
ssl->out_msg, ssl->out_msglen );
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->transform_out->ctx_deflate.next_in = msg_pre;
|
|
|
|
ssl->transform_out->ctx_deflate.avail_in = len_pre;
|
|
|
|
ssl->transform_out->ctx_deflate.next_out = msg_post;
|
|
|
|
ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
|
2012-07-03 13:30:23 +00:00
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
|
2012-07-03 13:30:23 +00:00
|
|
|
if( ret != Z_OK )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
|
|
|
|
return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
|
|
|
|
}
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->out_msglen = SSL_BUFFER_LEN - ssl->transform_out->ctx_deflate.avail_out;
|
2012-07-03 13:30:23 +00:00
|
|
|
|
2013-07-03 11:37:05 +00:00
|
|
|
polarssl_free( msg_pre );
|
2012-07-03 13:30:23 +00:00
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
|
|
|
|
ssl->out_msglen ) );
|
|
|
|
|
|
|
|
SSL_DEBUG_BUF( 4, "after compression: output payload",
|
|
|
|
ssl->out_msg, ssl->out_msglen );
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ssl_decompress_buf( ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned char *msg_post = ssl->in_msg;
|
|
|
|
size_t len_pre = ssl->in_msglen;
|
|
|
|
unsigned char *msg_pre;
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
|
|
|
|
|
2013-06-30 12:57:46 +00:00
|
|
|
if( len_pre == 0 )
|
|
|
|
return( 0 );
|
|
|
|
|
2013-07-03 11:37:05 +00:00
|
|
|
msg_pre = (unsigned char*) polarssl_malloc( len_pre );
|
2012-07-03 13:30:23 +00:00
|
|
|
if( msg_pre == NULL )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len_pre ) );
|
|
|
|
return( POLARSSL_ERR_SSL_MALLOC_FAILED );
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy( msg_pre, ssl->in_msg, len_pre );
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
|
|
|
|
ssl->in_msglen ) );
|
|
|
|
|
|
|
|
SSL_DEBUG_BUF( 4, "before decompression: input payload",
|
|
|
|
ssl->in_msg, ssl->in_msglen );
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->transform_in->ctx_inflate.next_in = msg_pre;
|
|
|
|
ssl->transform_in->ctx_inflate.avail_in = len_pre;
|
|
|
|
ssl->transform_in->ctx_inflate.next_out = msg_post;
|
|
|
|
ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
|
2012-07-03 13:30:23 +00:00
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
|
2012-07-03 13:30:23 +00:00
|
|
|
if( ret != Z_OK )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
|
|
|
|
return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
|
|
|
|
}
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->in_msglen = SSL_MAX_CONTENT_LEN - ssl->transform_in->ctx_inflate.avail_out;
|
2012-07-03 13:30:23 +00:00
|
|
|
|
2013-07-03 11:37:05 +00:00
|
|
|
polarssl_free( msg_pre );
|
2012-07-03 13:30:23 +00:00
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
|
|
|
|
ssl->in_msglen ) );
|
|
|
|
|
|
|
|
SSL_DEBUG_BUF( 4, "after decompression: input payload",
|
|
|
|
ssl->in_msg, ssl->in_msglen );
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#endif /* POLARSSL_ZLIB_SUPPORT */
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* Fill the input message buffer
|
|
|
|
*/
|
2011-04-24 08:57:21 +00:00
|
|
|
int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2011-04-24 08:57:21 +00:00
|
|
|
int ret;
|
|
|
|
size_t len;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
|
|
|
|
|
|
|
|
while( ssl->in_left < nb_want )
|
|
|
|
{
|
|
|
|
len = nb_want - ssl->in_left;
|
|
|
|
ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
|
|
|
|
ssl->in_left, nb_want ) );
|
|
|
|
SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
|
|
|
|
|
2011-05-18 13:32:51 +00:00
|
|
|
if( ret == 0 )
|
|
|
|
return( POLARSSL_ERR_SSL_CONN_EOF );
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
if( ret < 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
ssl->in_left += ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Flush any data not yet written
|
|
|
|
*/
|
|
|
|
int ssl_flush_output( ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned char *buf;
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
|
|
|
|
|
|
|
|
while( ssl->out_left > 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
|
|
|
|
5 + ssl->out_msglen, ssl->out_left ) );
|
|
|
|
|
2012-12-19 13:40:42 +00:00
|
|
|
buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
|
2009-01-03 21:22:43 +00:00
|
|
|
ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
|
2012-05-08 13:16:14 +00:00
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
SSL_DEBUG_RET( 2, "ssl->f_send", ret );
|
|
|
|
|
|
|
|
if( ret <= 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
ssl->out_left -= ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Record layer functions
|
|
|
|
*/
|
|
|
|
int ssl_write_record( ssl_context *ssl )
|
|
|
|
{
|
2012-05-08 09:17:57 +00:00
|
|
|
int ret, done = 0;
|
2011-04-24 08:57:21 +00:00
|
|
|
size_t len = ssl->out_msglen;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> write record" ) );
|
|
|
|
|
|
|
|
if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
|
|
|
|
{
|
|
|
|
ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
|
|
|
|
ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
|
|
|
|
ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2012-07-03 13:30:23 +00:00
|
|
|
#if defined(POLARSSL_ZLIB_SUPPORT)
|
2012-09-16 19:57:18 +00:00
|
|
|
if( ssl->transform_out != NULL &&
|
|
|
|
ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
|
2012-07-03 13:30:23 +00:00
|
|
|
{
|
|
|
|
if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
len = ssl->out_msglen;
|
|
|
|
}
|
|
|
|
#endif /*POLARSSL_ZLIB_SUPPORT */
|
|
|
|
|
2012-05-08 09:17:57 +00:00
|
|
|
#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
|
|
|
|
if( ssl_hw_record_write != NULL)
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2012-05-08 09:17:57 +00:00
|
|
|
SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
|
|
|
|
|
|
|
|
ret = ssl_hw_record_write( ssl );
|
|
|
|
if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2012-05-08 09:17:57 +00:00
|
|
|
SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
|
|
|
|
return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
2012-12-19 13:41:14 +00:00
|
|
|
|
|
|
|
if( ret == 0 )
|
|
|
|
done = 1;
|
2012-05-08 09:17:57 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if( !done )
|
|
|
|
{
|
|
|
|
ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
|
|
|
|
ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
|
|
|
|
ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
|
2009-01-03 21:22:43 +00:00
|
|
|
ssl->out_hdr[3] = (unsigned char)( len >> 8 );
|
|
|
|
ssl->out_hdr[4] = (unsigned char)( len );
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
if( ssl->transform_out != NULL )
|
2012-05-08 09:17:57 +00:00
|
|
|
{
|
|
|
|
if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-05-08 09:17:57 +00:00
|
|
|
len = ssl->out_msglen;
|
|
|
|
ssl->out_hdr[3] = (unsigned char)( len >> 8 );
|
|
|
|
ssl->out_hdr[4] = (unsigned char)( len );
|
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-05-08 09:17:57 +00:00
|
|
|
ssl->out_left = 5 + ssl->out_msglen;
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
|
|
|
|
"version = [%d:%d], msglen = %d",
|
|
|
|
ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
|
|
|
|
( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
|
|
|
|
|
|
|
|
SSL_DEBUG_BUF( 4, "output record sent to network",
|
2012-12-19 13:40:42 +00:00
|
|
|
ssl->out_hdr, 5 + ssl->out_msglen );
|
2012-05-08 09:17:57 +00:00
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
if( ( ret = ssl_flush_output( ssl ) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= write record" ) );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int ssl_read_record( ssl_context *ssl )
|
|
|
|
{
|
2012-05-08 09:17:57 +00:00
|
|
|
int ret, done = 0;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> read record" ) );
|
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
SSL_DEBUG_BUF( 4, "input record from network",
|
|
|
|
ssl->in_hdr, 5 + ssl->in_msglen );
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
if( ssl->in_hslen != 0 &&
|
|
|
|
ssl->in_hslen < ssl->in_msglen )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Get next Handshake message in the current record
|
|
|
|
*/
|
|
|
|
ssl->in_msglen -= ssl->in_hslen;
|
|
|
|
|
2011-08-05 11:11:53 +00:00
|
|
|
memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->in_msglen );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
ssl->in_hslen = 4;
|
|
|
|
ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
|
|
|
|
" %d, type = %d, hslen = %d",
|
|
|
|
ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
|
|
|
|
|
|
|
|
if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_INVALID_RECORD );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ssl->in_msglen < ssl->in_hslen )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_INVALID_RECORD );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl->in_hslen = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the record header and validate it
|
|
|
|
*/
|
|
|
|
if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl->in_msgtype = ssl->in_hdr[0];
|
|
|
|
ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
|
|
|
|
"version = [%d:%d], msglen = %d",
|
|
|
|
ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
|
|
|
|
( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
|
|
|
|
|
|
|
|
if( ssl->in_hdr[1] != ssl->major_ver )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_INVALID_RECORD );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2010-07-25 14:24:53 +00:00
|
|
|
if( ssl->in_hdr[2] > ssl->max_minor_ver )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_INVALID_RECORD );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure the message length is acceptable
|
|
|
|
*/
|
2012-09-16 19:57:18 +00:00
|
|
|
if( ssl->transform_in == NULL )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
if( ssl->in_msglen < 1 ||
|
|
|
|
ssl->in_msglen > SSL_MAX_CONTENT_LEN )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "bad message length" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_INVALID_RECORD );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-09-16 19:57:18 +00:00
|
|
|
if( ssl->in_msglen < ssl->transform_in->minlen )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "bad message length" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_INVALID_RECORD );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_SSL3)
|
2009-01-03 21:22:43 +00:00
|
|
|
if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "bad message length" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_INVALID_RECORD );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
|
|
|
|
defined(POLARSSL_SSL_PROTO_TLS1_2)
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* TLS encrypted messages can have up to 256 bytes of padding
|
|
|
|
*/
|
2012-04-11 12:09:53 +00:00
|
|
|
if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN + 256 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "bad message length" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_INVALID_RECORD );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read and optionally decrypt the message contents
|
|
|
|
*/
|
|
|
|
if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_DEBUG_BUF( 4, "input record from network",
|
|
|
|
ssl->in_hdr, 5 + ssl->in_msglen );
|
|
|
|
|
2012-05-08 09:17:57 +00:00
|
|
|
#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
|
|
|
|
if( ssl_hw_record_read != NULL)
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
|
|
|
|
|
|
|
|
ret = ssl_hw_record_read( ssl );
|
|
|
|
if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
|
|
|
|
return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
|
|
|
|
}
|
2012-12-19 13:41:14 +00:00
|
|
|
|
|
|
|
if( ret == 0 )
|
|
|
|
done = 1;
|
2012-05-08 09:17:57 +00:00
|
|
|
}
|
|
|
|
#endif
|
2012-09-16 19:57:18 +00:00
|
|
|
if( !done && ssl->transform_in != NULL )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
|
|
|
|
{
|
2013-01-31 16:13:13 +00:00
|
|
|
#if defined(POLARSSL_SSL_ALERT_MESSAGES)
|
|
|
|
if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
|
|
|
|
{
|
|
|
|
ssl_send_alert_message( ssl,
|
|
|
|
SSL_ALERT_LEVEL_FATAL,
|
|
|
|
SSL_ALERT_MSG_BAD_RECORD_MAC );
|
|
|
|
}
|
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_DEBUG_BUF( 4, "input payload after decrypt",
|
|
|
|
ssl->in_msg, ssl->in_msglen );
|
|
|
|
|
|
|
|
if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "bad message length" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_INVALID_RECORD );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-03 13:30:23 +00:00
|
|
|
#if defined(POLARSSL_ZLIB_SUPPORT)
|
2012-09-16 19:57:18 +00:00
|
|
|
if( ssl->transform_in != NULL &&
|
|
|
|
ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
|
2012-07-03 13:30:23 +00:00
|
|
|
{
|
|
|
|
if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
|
|
|
|
ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
|
|
|
|
}
|
|
|
|
#endif /* POLARSSL_ZLIB_SUPPORT */
|
|
|
|
|
2012-04-16 06:46:41 +00:00
|
|
|
if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
|
|
|
|
ssl->in_msgtype != SSL_MSG_ALERT &&
|
|
|
|
ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
|
|
|
|
ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
if( ( ret = ssl_send_alert_message( ssl,
|
|
|
|
SSL_ALERT_LEVEL_FATAL,
|
|
|
|
SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
|
2012-04-16 06:46:41 +00:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( POLARSSL_ERR_SSL_INVALID_RECORD );
|
|
|
|
}
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
|
|
|
|
{
|
|
|
|
ssl->in_hslen = 4;
|
|
|
|
ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
|
|
|
|
" %d, type = %d, hslen = %d",
|
|
|
|
ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Additional checks to validate the handshake header
|
|
|
|
*/
|
|
|
|
if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_INVALID_RECORD );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ssl->in_msglen < ssl->in_hslen )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_INVALID_RECORD );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
if( ssl->state != SSL_HANDSHAKE_OVER )
|
|
|
|
ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ssl->in_msgtype == SSL_MSG_ALERT )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
|
|
|
|
ssl->in_msg[0], ssl->in_msg[1] ) );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ignore non-fatal alerts, except close_notify
|
|
|
|
*/
|
2010-07-25 14:24:53 +00:00
|
|
|
if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2012-07-03 13:30:23 +00:00
|
|
|
SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
|
|
|
|
ssl->in_msg[1] ) );
|
2011-05-09 16:17:09 +00:00
|
|
|
/**
|
|
|
|
* Subtract from error code as ssl->in_msg[1] is 7-bit positive
|
|
|
|
* error identifier.
|
|
|
|
*/
|
2012-07-03 13:30:23 +00:00
|
|
|
return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2010-07-25 14:24:53 +00:00
|
|
|
if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
|
|
|
|
ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl->in_left = 0;
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= read record" ) );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2012-09-17 09:18:12 +00:00
|
|
|
int ssl_send_fatal_handshake_failure( ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if( ( ret = ssl_send_alert_message( ssl,
|
|
|
|
SSL_ALERT_LEVEL_FATAL,
|
|
|
|
SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
|
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2012-04-16 06:46:41 +00:00
|
|
|
int ssl_send_alert_message( ssl_context *ssl,
|
|
|
|
unsigned char level,
|
|
|
|
unsigned char message )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
|
|
|
|
|
|
|
|
ssl->out_msgtype = SSL_MSG_ALERT;
|
|
|
|
ssl->out_msglen = 2;
|
|
|
|
ssl->out_msg[0] = level;
|
|
|
|
ssl->out_msg[1] = message;
|
|
|
|
|
|
|
|
if( ( ret = ssl_write_record( ssl ) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_write_record", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* Handshake functions
|
|
|
|
*/
|
2013-04-19 12:30:58 +00:00
|
|
|
#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
|
|
|
|
!defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
|
|
|
|
!defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
|
|
|
|
int ssl_write_certificate( ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
|
|
|
|
const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
|
|
|
|
|
|
|
|
if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
|
|
|
|
ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
|
|
|
|
ssl->state++;
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
int ssl_parse_certificate( ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
|
|
|
|
const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
|
|
|
|
|
|
|
|
if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
|
|
|
|
ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
|
|
|
|
ssl->state++;
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
#else
|
2009-01-03 21:22:43 +00:00
|
|
|
int ssl_write_certificate( ssl_context *ssl )
|
|
|
|
{
|
2013-04-18 20:46:23 +00:00
|
|
|
int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
|
2011-04-24 08:57:21 +00:00
|
|
|
size_t i, n;
|
2010-03-16 21:09:09 +00:00
|
|
|
const x509_cert *crt;
|
2013-04-16 16:05:29 +00:00
|
|
|
const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
|
|
|
|
|
2013-04-19 12:30:58 +00:00
|
|
|
if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
|
|
|
|
ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
|
2013-04-16 16:05:29 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
|
|
|
|
ssl->state++;
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
if( ssl->endpoint == SSL_IS_CLIENT )
|
|
|
|
{
|
|
|
|
if( ssl->client_auth == 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
|
|
|
|
ssl->state++;
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_SSL3)
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* If using SSLv3 and got no cert, send an Alert message
|
|
|
|
* (otherwise an empty Certificate message will be sent).
|
|
|
|
*/
|
|
|
|
if( ssl->own_cert == NULL &&
|
|
|
|
ssl->minor_ver == SSL_MINOR_VERSION_0 )
|
|
|
|
{
|
|
|
|
ssl->out_msglen = 2;
|
|
|
|
ssl->out_msgtype = SSL_MSG_ALERT;
|
2010-07-25 14:24:53 +00:00
|
|
|
ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
|
|
|
|
ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
|
|
|
|
goto write_msg;
|
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif /* POLARSSL_SSL_PROTO_SSL3 */
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
else /* SSL_IS_SERVER */
|
|
|
|
{
|
|
|
|
if( ssl->own_cert == NULL )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_DEBUG_CRT( 3, "own certificate", ssl->own_cert );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 0 . 0 handshake type
|
|
|
|
* 1 . 3 handshake length
|
|
|
|
* 4 . 6 length of all certs
|
|
|
|
* 7 . 9 length of cert. 1
|
|
|
|
* 10 . n-1 peer certificate
|
|
|
|
* n . n+2 length of cert. 2
|
|
|
|
* n+3 . ... upper level cert, etc.
|
|
|
|
*/
|
|
|
|
i = 7;
|
|
|
|
crt = ssl->own_cert;
|
|
|
|
|
2010-03-21 21:03:34 +00:00
|
|
|
while( crt != NULL )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
n = crt->raw.len;
|
|
|
|
if( i + 3 + n > SSL_MAX_CONTENT_LEN )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
|
|
|
|
i + 3 + n, SSL_MAX_CONTENT_LEN ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ssl->out_msg[i ] = (unsigned char)( n >> 16 );
|
|
|
|
ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
|
|
|
|
ssl->out_msg[i + 2] = (unsigned char)( n );
|
|
|
|
|
|
|
|
i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
|
|
|
|
i += n; crt = crt->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
|
|
|
|
ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
|
|
|
|
ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
|
|
|
|
|
|
|
|
ssl->out_msglen = i;
|
|
|
|
ssl->out_msgtype = SSL_MSG_HANDSHAKE;
|
|
|
|
ssl->out_msg[0] = SSL_HS_CERTIFICATE;
|
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_SSL3)
|
2009-01-03 21:22:43 +00:00
|
|
|
write_msg:
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
ssl->state++;
|
|
|
|
|
|
|
|
if( ( ret = ssl_write_record( ssl ) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_write_record", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
|
|
|
|
|
2013-04-18 20:46:23 +00:00
|
|
|
return( ret );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ssl_parse_certificate( ssl_context *ssl )
|
|
|
|
{
|
2013-04-18 20:46:23 +00:00
|
|
|
int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
|
2011-04-24 08:57:21 +00:00
|
|
|
size_t i, n;
|
2013-04-16 16:05:29 +00:00
|
|
|
const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
|
|
|
|
|
2013-04-19 12:30:58 +00:00
|
|
|
if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
|
|
|
|
ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
|
2013-04-16 16:05:29 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
|
|
|
|
ssl->state++;
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
if( ssl->endpoint == SSL_IS_SERVER &&
|
|
|
|
ssl->authmode == SSL_VERIFY_NONE )
|
|
|
|
{
|
2013-08-23 08:44:29 +00:00
|
|
|
ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
|
2009-01-03 21:22:43 +00:00
|
|
|
SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
|
|
|
|
ssl->state++;
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( ret = ssl_read_record( ssl ) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_read_record", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl->state++;
|
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_SSL3)
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* Check if the client sent an empty certificate
|
|
|
|
*/
|
|
|
|
if( ssl->endpoint == SSL_IS_SERVER &&
|
|
|
|
ssl->minor_ver == SSL_MINOR_VERSION_0 )
|
|
|
|
{
|
2010-07-25 14:24:53 +00:00
|
|
|
if( ssl->in_msglen == 2 &&
|
|
|
|
ssl->in_msgtype == SSL_MSG_ALERT &&
|
|
|
|
ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
|
|
|
|
ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
|
|
|
|
|
2013-08-23 08:44:29 +00:00
|
|
|
ssl->session_negotiate->verify_result = BADCERT_MISSING;
|
2009-01-03 21:22:43 +00:00
|
|
|
if( ssl->authmode == SSL_VERIFY_OPTIONAL )
|
|
|
|
return( 0 );
|
|
|
|
else
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif /* POLARSSL_SSL_PROTO_SSL3 */
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
|
|
|
|
defined(POLARSSL_SSL_PROTO_TLS1_2)
|
2009-01-03 21:22:43 +00:00
|
|
|
if( ssl->endpoint == SSL_IS_SERVER &&
|
|
|
|
ssl->minor_ver != SSL_MINOR_VERSION_0 )
|
|
|
|
{
|
|
|
|
if( ssl->in_hslen == 7 &&
|
|
|
|
ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
|
|
|
|
ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
|
|
|
|
memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
|
|
|
|
|
2013-08-23 08:44:29 +00:00
|
|
|
ssl->session_negotiate->verify_result = BADCERT_MISSING;
|
2009-01-03 21:22:43 +00:00
|
|
|
if( ssl->authmode == SSL_VERIFY_REQUIRED )
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
|
2009-01-03 21:22:43 +00:00
|
|
|
else
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
|
|
|
|
POLARSSL_SSL_PROTO_TLS1_2 */
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Same message structure as in ssl_write_certificate()
|
|
|
|
*/
|
|
|
|
n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
|
|
|
|
|
|
|
|
if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2013-07-03 11:37:05 +00:00
|
|
|
if( ( ssl->session_negotiate->peer_cert = (x509_cert *) polarssl_malloc(
|
2009-01-03 21:22:43 +00:00
|
|
|
sizeof( x509_cert ) ) ) == NULL )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
|
|
|
|
sizeof( x509_cert ) ) );
|
2011-12-10 21:55:01 +00:00
|
|
|
return( POLARSSL_ERR_SSL_MALLOC_FAILED );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
memset( ssl->session_negotiate->peer_cert, 0, sizeof( x509_cert ) );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
i = 7;
|
|
|
|
|
|
|
|
while( i < ssl->in_hslen )
|
|
|
|
{
|
|
|
|
if( ssl->in_msg[i] != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
|
|
|
|
| (unsigned int) ssl->in_msg[i + 2];
|
|
|
|
i += 3;
|
|
|
|
|
|
|
|
if( n < 128 || i + n > ssl->in_hslen )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2013-06-24 17:06:15 +00:00
|
|
|
ret = x509parse_crt_der( ssl->session_negotiate->peer_cert,
|
|
|
|
ssl->in_msg + i, n );
|
2009-01-03 21:22:43 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, " x509parse_crt", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
i += n;
|
|
|
|
}
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
if( ssl->authmode != SSL_VERIFY_NONE )
|
|
|
|
{
|
|
|
|
if( ssl->ca_chain == NULL )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
ret = x509parse_verify( ssl->session_negotiate->peer_cert,
|
2013-08-23 08:44:29 +00:00
|
|
|
ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
|
|
|
|
&ssl->session_negotiate->verify_result,
|
2011-01-13 17:54:59 +00:00
|
|
|
ssl->f_vrfy, ssl->p_vrfy );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
if( ret != 0 )
|
|
|
|
SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
|
|
|
|
|
|
|
|
if( ssl->authmode != SSL_VERIFY_REQUIRED )
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2013-04-19 12:30:58 +00:00
|
|
|
#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
|
|
|
|
!POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
|
|
|
|
!POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
int ssl_write_change_cipher_spec( ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
|
|
|
|
|
|
|
|
ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
|
|
|
|
ssl->out_msglen = 1;
|
|
|
|
ssl->out_msg[0] = 1;
|
|
|
|
|
|
|
|
ssl->state++;
|
|
|
|
|
|
|
|
if( ( ret = ssl_write_record( ssl ) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_write_record", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int ssl_parse_change_cipher_spec( ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
|
|
|
|
|
|
|
|
if( ( ret = ssl_read_record( ssl ) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_read_record", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ssl->state++;
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2013-03-20 13:39:14 +00:00
|
|
|
void ssl_optimize_checksum( ssl_context *ssl,
|
|
|
|
const ssl_ciphersuite_t *ciphersuite_info )
|
2012-04-18 16:10:25 +00:00
|
|
|
{
|
2013-08-27 13:06:26 +00:00
|
|
|
((void) ciphersuite_info);
|
2012-11-24 10:26:46 +00:00
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
|
|
|
|
defined(POLARSSL_SSL_PROTO_TLS1_1)
|
2012-04-18 16:10:25 +00:00
|
|
|
if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
|
2013-08-27 19:19:20 +00:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_SSL_PROTO_TLS1_2)
|
2013-06-30 12:34:05 +00:00
|
|
|
#if defined(POLARSSL_SHA512_C)
|
2013-08-27 19:19:20 +00:00
|
|
|
if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->handshake->update_checksum = ssl_update_checksum_sha384;
|
2012-04-18 16:10:25 +00:00
|
|
|
else
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_SHA256_C)
|
|
|
|
if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->handshake->update_checksum = ssl_update_checksum_sha256;
|
2013-08-27 19:19:20 +00:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
|
|
|
|
/* Should never happen */
|
|
|
|
return;
|
2012-04-18 16:10:25 +00:00
|
|
|
}
|
2013-04-16 11:15:56 +00:00
|
|
|
|
2013-06-25 14:25:17 +00:00
|
|
|
static void ssl_update_checksum_start( ssl_context *ssl,
|
|
|
|
const unsigned char *buf, size_t len )
|
2012-04-18 16:10:25 +00:00
|
|
|
{
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
|
|
|
|
defined(POLARSSL_SSL_PROTO_TLS1_1)
|
2012-09-16 19:57:18 +00:00
|
|
|
md5_update( &ssl->handshake->fin_md5 , buf, len );
|
|
|
|
sha1_update( &ssl->handshake->fin_sha1, buf, len );
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_SSL_PROTO_TLS1_2)
|
|
|
|
#if defined(POLARSSL_SHA256_C)
|
2013-06-30 12:34:05 +00:00
|
|
|
sha256_update( &ssl->handshake->fin_sha256, buf, len );
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif
|
2013-06-30 12:34:05 +00:00
|
|
|
#if defined(POLARSSL_SHA512_C)
|
|
|
|
sha512_update( &ssl->handshake->fin_sha512, buf, len );
|
2012-11-24 10:26:46 +00:00
|
|
|
#endif
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
|
2012-04-18 16:10:25 +00:00
|
|
|
}
|
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
|
|
|
|
defined(POLARSSL_SSL_PROTO_TLS1_1)
|
2013-06-25 14:25:17 +00:00
|
|
|
static void ssl_update_checksum_md5sha1( ssl_context *ssl,
|
|
|
|
const unsigned char *buf, size_t len )
|
2012-04-18 16:10:25 +00:00
|
|
|
{
|
2012-09-16 19:57:18 +00:00
|
|
|
md5_update( &ssl->handshake->fin_md5 , buf, len );
|
|
|
|
sha1_update( &ssl->handshake->fin_sha1, buf, len );
|
2012-04-18 16:10:25 +00:00
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif
|
2012-04-18 16:10:25 +00:00
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_TLS1_2)
|
|
|
|
#if defined(POLARSSL_SHA256_C)
|
2013-06-25 14:25:17 +00:00
|
|
|
static void ssl_update_checksum_sha256( ssl_context *ssl,
|
|
|
|
const unsigned char *buf, size_t len )
|
2012-04-18 16:10:25 +00:00
|
|
|
{
|
2013-06-30 12:34:05 +00:00
|
|
|
sha256_update( &ssl->handshake->fin_sha256, buf, len );
|
2012-04-18 16:10:25 +00:00
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif
|
2012-04-18 16:10:25 +00:00
|
|
|
|
2013-06-30 12:34:05 +00:00
|
|
|
#if defined(POLARSSL_SHA512_C)
|
2013-06-25 14:25:17 +00:00
|
|
|
static void ssl_update_checksum_sha384( ssl_context *ssl,
|
|
|
|
const unsigned char *buf, size_t len )
|
2012-04-18 16:10:25 +00:00
|
|
|
{
|
2013-06-30 12:34:05 +00:00
|
|
|
sha512_update( &ssl->handshake->fin_sha512, buf, len );
|
2012-04-18 16:10:25 +00:00
|
|
|
}
|
2012-11-24 10:26:46 +00:00
|
|
|
#endif
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
|
2012-04-18 16:10:25 +00:00
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_SSL3)
|
2012-04-11 12:09:53 +00:00
|
|
|
static void ssl_calc_finished_ssl(
|
|
|
|
ssl_context *ssl, unsigned char *buf, int from )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2013-06-24 17:03:14 +00:00
|
|
|
const char *sender;
|
2012-04-11 12:09:53 +00:00
|
|
|
md5_context md5;
|
|
|
|
sha1_context sha1;
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
unsigned char padbuf[48];
|
|
|
|
unsigned char md5sum[16];
|
|
|
|
unsigned char sha1sum[20];
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl_session *session = ssl->session_negotiate;
|
|
|
|
if( !session )
|
|
|
|
session = ssl->session;
|
|
|
|
|
2012-04-11 12:09:53 +00:00
|
|
|
SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
|
|
|
|
memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* SSLv3:
|
|
|
|
* hash =
|
|
|
|
* MD5( master + pad2 +
|
|
|
|
* MD5( handshake + sender + master + pad1 ) )
|
|
|
|
* + SHA1( master + pad2 +
|
|
|
|
* SHA1( handshake + sender + master + pad1 ) )
|
2012-04-11 12:09:53 +00:00
|
|
|
*/
|
|
|
|
|
2013-06-24 17:20:35 +00:00
|
|
|
#if !defined(POLARSSL_MD5_ALT)
|
2012-04-11 12:09:53 +00:00
|
|
|
SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
|
|
|
|
md5.state, sizeof( md5.state ) );
|
2013-06-24 17:20:35 +00:00
|
|
|
#endif
|
2012-04-11 12:09:53 +00:00
|
|
|
|
2013-06-24 17:20:35 +00:00
|
|
|
#if !defined(POLARSSL_SHA1_ALT)
|
2012-04-11 12:09:53 +00:00
|
|
|
SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
|
|
|
|
sha1.state, sizeof( sha1.state ) );
|
2013-06-24 17:20:35 +00:00
|
|
|
#endif
|
2012-04-11 12:09:53 +00:00
|
|
|
|
2013-06-24 17:03:14 +00:00
|
|
|
sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
|
|
|
|
: "SRVR";
|
2012-04-11 12:09:53 +00:00
|
|
|
|
|
|
|
memset( padbuf, 0x36, 48 );
|
|
|
|
|
2013-06-24 17:03:14 +00:00
|
|
|
md5_update( &md5, (const unsigned char *) sender, 4 );
|
2012-09-16 19:57:18 +00:00
|
|
|
md5_update( &md5, session->master, 48 );
|
2012-04-11 12:09:53 +00:00
|
|
|
md5_update( &md5, padbuf, 48 );
|
|
|
|
md5_finish( &md5, md5sum );
|
|
|
|
|
2013-06-24 17:03:14 +00:00
|
|
|
sha1_update( &sha1, (const unsigned char *) sender, 4 );
|
2012-09-16 19:57:18 +00:00
|
|
|
sha1_update( &sha1, session->master, 48 );
|
2012-04-11 12:09:53 +00:00
|
|
|
sha1_update( &sha1, padbuf, 40 );
|
|
|
|
sha1_finish( &sha1, sha1sum );
|
|
|
|
|
|
|
|
memset( padbuf, 0x5C, 48 );
|
|
|
|
|
|
|
|
md5_starts( &md5 );
|
2012-09-16 19:57:18 +00:00
|
|
|
md5_update( &md5, session->master, 48 );
|
2012-04-11 12:09:53 +00:00
|
|
|
md5_update( &md5, padbuf, 48 );
|
|
|
|
md5_update( &md5, md5sum, 16 );
|
|
|
|
md5_finish( &md5, buf );
|
|
|
|
|
|
|
|
sha1_starts( &sha1 );
|
2012-09-16 19:57:18 +00:00
|
|
|
sha1_update( &sha1, session->master, 48 );
|
2012-04-11 12:09:53 +00:00
|
|
|
sha1_update( &sha1, padbuf , 40 );
|
|
|
|
sha1_update( &sha1, sha1sum, 20 );
|
|
|
|
sha1_finish( &sha1, buf + 16 );
|
|
|
|
|
|
|
|
SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
|
|
|
|
|
|
|
|
memset( &md5, 0, sizeof( md5_context ) );
|
|
|
|
memset( &sha1, 0, sizeof( sha1_context ) );
|
|
|
|
|
|
|
|
memset( padbuf, 0, sizeof( padbuf ) );
|
|
|
|
memset( md5sum, 0, sizeof( md5sum ) );
|
|
|
|
memset( sha1sum, 0, sizeof( sha1sum ) );
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
|
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif /* POLARSSL_SSL_PROTO_SSL3 */
|
2012-04-11 12:09:53 +00:00
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
|
2012-04-11 12:09:53 +00:00
|
|
|
static void ssl_calc_finished_tls(
|
|
|
|
ssl_context *ssl, unsigned char *buf, int from )
|
|
|
|
{
|
|
|
|
int len = 12;
|
2013-06-24 17:03:14 +00:00
|
|
|
const char *sender;
|
2012-04-11 12:09:53 +00:00
|
|
|
md5_context md5;
|
|
|
|
sha1_context sha1;
|
|
|
|
unsigned char padbuf[36];
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl_session *session = ssl->session_negotiate;
|
|
|
|
if( !session )
|
|
|
|
session = ssl->session;
|
|
|
|
|
2012-04-11 12:09:53 +00:00
|
|
|
SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
|
|
|
|
memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
|
2012-04-11 12:09:53 +00:00
|
|
|
|
|
|
|
/*
|
2009-01-03 21:22:43 +00:00
|
|
|
* TLSv1:
|
|
|
|
* hash = PRF( master, finished_label,
|
|
|
|
* MD5( handshake ) + SHA1( handshake ) )[0..11]
|
|
|
|
*/
|
|
|
|
|
2013-06-24 17:20:35 +00:00
|
|
|
#if !defined(POLARSSL_MD5_ALT)
|
2009-01-03 21:22:43 +00:00
|
|
|
SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
|
2012-04-11 12:09:53 +00:00
|
|
|
md5.state, sizeof( md5.state ) );
|
2013-06-24 17:20:35 +00:00
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-06-24 17:20:35 +00:00
|
|
|
#if !defined(POLARSSL_SHA1_ALT)
|
2009-01-03 21:22:43 +00:00
|
|
|
SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
|
2012-04-11 12:09:53 +00:00
|
|
|
sha1.state, sizeof( sha1.state ) );
|
2013-06-24 17:20:35 +00:00
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-04-11 12:09:53 +00:00
|
|
|
sender = ( from == SSL_IS_CLIENT )
|
2013-06-24 17:03:14 +00:00
|
|
|
? "client finished"
|
|
|
|
: "server finished";
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-04-11 12:09:53 +00:00
|
|
|
md5_finish( &md5, padbuf );
|
|
|
|
sha1_finish( &sha1, padbuf + 16 );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-06-25 14:25:17 +00:00
|
|
|
ssl->handshake->tls_prf( session->master, 48, sender,
|
2012-09-16 19:57:18 +00:00
|
|
|
padbuf, 36, buf, len );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-04-11 12:09:53 +00:00
|
|
|
SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-04-11 12:09:53 +00:00
|
|
|
memset( &md5, 0, sizeof( md5_context ) );
|
|
|
|
memset( &sha1, 0, sizeof( sha1_context ) );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-04-11 12:09:53 +00:00
|
|
|
memset( padbuf, 0, sizeof( padbuf ) );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-04-11 12:09:53 +00:00
|
|
|
SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
|
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_TLS1_2)
|
|
|
|
#if defined(POLARSSL_SHA256_C)
|
2012-04-18 14:23:57 +00:00
|
|
|
static void ssl_calc_finished_tls_sha256(
|
2012-04-11 12:09:53 +00:00
|
|
|
ssl_context *ssl, unsigned char *buf, int from )
|
|
|
|
{
|
|
|
|
int len = 12;
|
2013-06-24 17:03:14 +00:00
|
|
|
const char *sender;
|
2013-06-30 12:34:05 +00:00
|
|
|
sha256_context sha256;
|
2012-04-11 12:09:53 +00:00
|
|
|
unsigned char padbuf[32];
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl_session *session = ssl->session_negotiate;
|
|
|
|
if( !session )
|
|
|
|
session = ssl->session;
|
|
|
|
|
2012-04-18 16:10:25 +00:00
|
|
|
SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-06-30 12:34:05 +00:00
|
|
|
memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
|
2012-04-11 12:09:53 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* TLSv1.2:
|
|
|
|
* hash = PRF( master, finished_label,
|
|
|
|
* Hash( handshake ) )[0.11]
|
|
|
|
*/
|
|
|
|
|
2013-06-30 12:34:05 +00:00
|
|
|
#if !defined(POLARSSL_SHA256_ALT)
|
2012-04-11 12:09:53 +00:00
|
|
|
SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
|
2013-06-30 12:34:05 +00:00
|
|
|
sha256.state, sizeof( sha256.state ) );
|
2013-06-24 17:20:35 +00:00
|
|
|
#endif
|
2012-04-11 12:09:53 +00:00
|
|
|
|
|
|
|
sender = ( from == SSL_IS_CLIENT )
|
2013-06-24 17:03:14 +00:00
|
|
|
? "client finished"
|
|
|
|
: "server finished";
|
2012-04-11 12:09:53 +00:00
|
|
|
|
2013-06-30 12:34:05 +00:00
|
|
|
sha256_finish( &sha256, padbuf );
|
2012-04-11 12:09:53 +00:00
|
|
|
|
2013-06-25 14:25:17 +00:00
|
|
|
ssl->handshake->tls_prf( session->master, 48, sender,
|
2012-09-16 19:57:18 +00:00
|
|
|
padbuf, 32, buf, len );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
|
|
|
|
|
2013-06-30 12:34:05 +00:00
|
|
|
memset( &sha256, 0, sizeof( sha256_context ) );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
memset( padbuf, 0, sizeof( padbuf ) );
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
|
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif /* POLARSSL_SHA256_C */
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-06-30 12:34:05 +00:00
|
|
|
#if defined(POLARSSL_SHA512_C)
|
2012-04-18 14:23:57 +00:00
|
|
|
static void ssl_calc_finished_tls_sha384(
|
|
|
|
ssl_context *ssl, unsigned char *buf, int from )
|
|
|
|
{
|
|
|
|
int len = 12;
|
2013-06-24 17:03:14 +00:00
|
|
|
const char *sender;
|
2013-06-30 12:34:05 +00:00
|
|
|
sha512_context sha512;
|
2012-04-18 14:23:57 +00:00
|
|
|
unsigned char padbuf[48];
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl_session *session = ssl->session_negotiate;
|
|
|
|
if( !session )
|
|
|
|
session = ssl->session;
|
|
|
|
|
2012-04-18 16:10:25 +00:00
|
|
|
SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
|
2012-04-18 14:23:57 +00:00
|
|
|
|
2013-06-30 12:34:05 +00:00
|
|
|
memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
|
2012-04-18 14:23:57 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* TLSv1.2:
|
|
|
|
* hash = PRF( master, finished_label,
|
|
|
|
* Hash( handshake ) )[0.11]
|
|
|
|
*/
|
|
|
|
|
2013-06-30 12:34:05 +00:00
|
|
|
#if !defined(POLARSSL_SHA512_ALT)
|
|
|
|
SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
|
|
|
|
sha512.state, sizeof( sha512.state ) );
|
2013-06-24 17:20:35 +00:00
|
|
|
#endif
|
2012-04-18 14:23:57 +00:00
|
|
|
|
|
|
|
sender = ( from == SSL_IS_CLIENT )
|
2013-06-24 17:03:14 +00:00
|
|
|
? "client finished"
|
|
|
|
: "server finished";
|
2012-04-18 14:23:57 +00:00
|
|
|
|
2013-06-30 12:34:05 +00:00
|
|
|
sha512_finish( &sha512, padbuf );
|
2012-04-18 14:23:57 +00:00
|
|
|
|
2013-06-25 14:25:17 +00:00
|
|
|
ssl->handshake->tls_prf( session->master, 48, sender,
|
2012-09-16 19:57:18 +00:00
|
|
|
padbuf, 48, buf, len );
|
2012-04-18 14:23:57 +00:00
|
|
|
|
|
|
|
SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
|
|
|
|
|
2013-06-30 12:34:05 +00:00
|
|
|
memset( &sha512, 0, sizeof( sha512_context ) );
|
2012-04-18 14:23:57 +00:00
|
|
|
|
|
|
|
memset( padbuf, 0, sizeof( padbuf ) );
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
|
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif /* POLARSSL_SHA512_C */
|
|
|
|
#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
|
2012-04-18 14:23:57 +00:00
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
void ssl_handshake_wrapup( ssl_context *ssl )
|
|
|
|
{
|
2013-08-02 12:13:02 +00:00
|
|
|
int resume = ssl->handshake->resume;
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free our handshake params
|
|
|
|
*/
|
|
|
|
ssl_handshake_free( ssl->handshake );
|
2013-07-03 11:37:05 +00:00
|
|
|
polarssl_free( ssl->handshake );
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->handshake = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Switch in our now active transform context
|
|
|
|
*/
|
|
|
|
if( ssl->transform )
|
|
|
|
{
|
|
|
|
ssl_transform_free( ssl->transform );
|
2013-07-03 11:37:05 +00:00
|
|
|
polarssl_free( ssl->transform );
|
2012-09-16 19:57:18 +00:00
|
|
|
}
|
|
|
|
ssl->transform = ssl->transform_negotiate;
|
|
|
|
ssl->transform_negotiate = NULL;
|
|
|
|
|
2012-09-25 21:55:46 +00:00
|
|
|
if( ssl->session )
|
|
|
|
{
|
|
|
|
ssl_session_free( ssl->session );
|
2013-07-03 11:37:05 +00:00
|
|
|
polarssl_free( ssl->session );
|
2012-09-25 21:55:46 +00:00
|
|
|
}
|
|
|
|
ssl->session = ssl->session_negotiate;
|
|
|
|
ssl->session_negotiate = NULL;
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
/*
|
2012-09-25 21:55:46 +00:00
|
|
|
* Add cache entry
|
2012-09-16 19:57:18 +00:00
|
|
|
*/
|
2013-08-02 12:13:02 +00:00
|
|
|
if( ssl->f_set_cache != NULL &&
|
|
|
|
ssl->session->length != 0 &&
|
|
|
|
resume == 0 )
|
|
|
|
{
|
2012-09-25 21:55:46 +00:00
|
|
|
if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
|
|
|
|
SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
|
2013-08-02 12:13:02 +00:00
|
|
|
}
|
2012-09-16 19:57:18 +00:00
|
|
|
|
|
|
|
ssl->state++;
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
|
|
|
|
}
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
int ssl_write_finished( ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int ret, hash_len;
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
|
|
|
|
|
2013-01-02 16:30:03 +00:00
|
|
|
/*
|
|
|
|
* Set the out_msg pointer to the correct location based on IV length
|
|
|
|
*/
|
|
|
|
if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
|
|
|
|
{
|
|
|
|
ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
|
|
|
|
ssl->transform_negotiate->fixed_ivlen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ssl->out_msg = ssl->out_iv;
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-04-11 12:09:53 +00:00
|
|
|
// TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
|
2009-01-03 21:22:43 +00:00
|
|
|
hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->verify_data_len = hash_len;
|
|
|
|
memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
ssl->out_msglen = 4 + hash_len;
|
|
|
|
ssl->out_msgtype = SSL_MSG_HANDSHAKE;
|
|
|
|
ssl->out_msg[0] = SSL_HS_FINISHED;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In case of session resuming, invert the client and server
|
|
|
|
* ChangeCipherSpec messages order.
|
|
|
|
*/
|
2012-09-25 21:55:46 +00:00
|
|
|
if( ssl->handshake->resume != 0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
if( ssl->endpoint == SSL_IS_CLIENT )
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->state = SSL_HANDSHAKE_WRAPUP;
|
2009-01-03 21:22:43 +00:00
|
|
|
else
|
|
|
|
ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ssl->state++;
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
/*
|
|
|
|
* Switch to our negotiated transform and session parameters for outbound data.
|
|
|
|
*/
|
|
|
|
SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
|
|
|
|
ssl->transform_out = ssl->transform_negotiate;
|
|
|
|
ssl->session_out = ssl->session_negotiate;
|
|
|
|
memset( ssl->out_ctr, 0, 8 );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-12-19 13:42:06 +00:00
|
|
|
#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
|
|
|
|
if( ssl_hw_record_activate != NULL)
|
|
|
|
{
|
|
|
|
if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
|
|
|
|
return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
if( ( ret = ssl_write_record( ssl ) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_write_record", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int ssl_parse_finished( ssl_context *ssl )
|
|
|
|
{
|
2011-04-24 08:57:21 +00:00
|
|
|
int ret;
|
|
|
|
unsigned int hash_len;
|
2009-01-03 21:22:43 +00:00
|
|
|
unsigned char buf[36];
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
/*
|
|
|
|
* Switch to our negotiated transform and session parameters for inbound data.
|
|
|
|
*/
|
|
|
|
SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
|
|
|
|
ssl->transform_in = ssl->transform_negotiate;
|
|
|
|
ssl->session_in = ssl->session_negotiate;
|
|
|
|
memset( ssl->in_ctr, 0, 8 );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-01-02 16:30:03 +00:00
|
|
|
/*
|
|
|
|
* Set the in_msg pointer to the correct location based on IV length
|
|
|
|
*/
|
|
|
|
if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
|
|
|
|
{
|
|
|
|
ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
|
|
|
|
ssl->transform_negotiate->fixed_ivlen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ssl->in_msg = ssl->in_iv;
|
|
|
|
|
2012-12-19 13:42:06 +00:00
|
|
|
#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
|
|
|
|
if( ssl_hw_record_activate != NULL)
|
|
|
|
{
|
|
|
|
if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
|
|
|
|
return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
if( ( ret = ssl_read_record( ssl ) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_read_record", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2012-04-11 12:09:53 +00:00
|
|
|
// TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
|
2009-01-03 21:22:43 +00:00
|
|
|
hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
|
|
|
|
|
|
|
|
if( ssl->in_msg[0] != SSL_HS_FINISHED ||
|
|
|
|
ssl->in_hslen != 4 + hash_len )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->verify_data_len = hash_len;
|
|
|
|
memcpy( ssl->peer_verify_data, buf, hash_len );
|
|
|
|
|
2012-09-25 21:55:46 +00:00
|
|
|
if( ssl->handshake->resume != 0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
if( ssl->endpoint == SSL_IS_CLIENT )
|
|
|
|
ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
|
|
|
|
|
|
|
|
if( ssl->endpoint == SSL_IS_SERVER )
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->state = SSL_HANDSHAKE_WRAPUP;
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
ssl->state++;
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2013-06-25 14:25:17 +00:00
|
|
|
static int ssl_handshake_init( ssl_context *ssl )
|
2012-09-16 19:57:18 +00:00
|
|
|
{
|
|
|
|
if( ssl->transform_negotiate )
|
|
|
|
ssl_transform_free( ssl->transform_negotiate );
|
|
|
|
else
|
2013-07-03 11:37:05 +00:00
|
|
|
ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) );
|
2012-09-16 19:57:18 +00:00
|
|
|
|
|
|
|
if( ssl->session_negotiate )
|
|
|
|
ssl_session_free( ssl->session_negotiate );
|
|
|
|
else
|
2013-07-03 11:37:05 +00:00
|
|
|
ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) );
|
2012-09-16 19:57:18 +00:00
|
|
|
|
|
|
|
if( ssl->handshake )
|
|
|
|
ssl_handshake_free( ssl->handshake );
|
|
|
|
else
|
2013-07-03 11:37:05 +00:00
|
|
|
ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) );
|
2012-09-16 19:57:18 +00:00
|
|
|
|
|
|
|
if( ssl->handshake == NULL ||
|
|
|
|
ssl->transform_negotiate == NULL ||
|
|
|
|
ssl->session_negotiate == NULL )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
|
|
|
|
return( POLARSSL_ERR_SSL_MALLOC_FAILED );
|
|
|
|
}
|
|
|
|
|
|
|
|
memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
|
|
|
|
memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
|
|
|
|
memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
|
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
|
|
|
|
defined(POLARSSL_SSL_PROTO_TLS1_1)
|
2012-09-16 19:57:18 +00:00
|
|
|
md5_starts( &ssl->handshake->fin_md5 );
|
|
|
|
sha1_starts( &ssl->handshake->fin_sha1 );
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_SSL_PROTO_TLS1_2)
|
|
|
|
#if defined(POLARSSL_SHA256_C)
|
2013-06-30 12:34:05 +00:00
|
|
|
sha256_starts( &ssl->handshake->fin_sha256, 0 );
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif
|
2013-06-30 12:34:05 +00:00
|
|
|
#if defined(POLARSSL_SHA512_C)
|
|
|
|
sha512_starts( &ssl->handshake->fin_sha512, 1 );
|
2012-11-24 10:26:46 +00:00
|
|
|
#endif
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
|
2012-09-16 19:57:18 +00:00
|
|
|
|
|
|
|
ssl->handshake->update_checksum = ssl_update_checksum_start;
|
2012-09-28 14:15:14 +00:00
|
|
|
ssl->handshake->sig_alg = SSL_HASH_SHA1;
|
2013-04-16 11:15:56 +00:00
|
|
|
|
2013-07-04 09:51:43 +00:00
|
|
|
#if defined(POLARSSL_ECDH_C)
|
|
|
|
ecdh_init( &ssl->handshake->ecdh_ctx );
|
|
|
|
#endif
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* Initialize an SSL context
|
|
|
|
*/
|
|
|
|
int ssl_init( ssl_context *ssl )
|
|
|
|
{
|
2012-09-16 19:57:18 +00:00
|
|
|
int ret;
|
2009-01-03 21:22:43 +00:00
|
|
|
int len = SSL_BUFFER_LEN;
|
|
|
|
|
|
|
|
memset( ssl, 0, sizeof( ssl_context ) );
|
|
|
|
|
2012-09-28 07:31:51 +00:00
|
|
|
/*
|
|
|
|
* Sane defaults
|
|
|
|
*/
|
2013-08-27 19:19:20 +00:00
|
|
|
ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
|
|
|
|
ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
|
|
|
|
ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
|
|
|
|
ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
|
2012-09-28 13:28:45 +00:00
|
|
|
|
2013-04-15 13:09:54 +00:00
|
|
|
ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
|
2012-10-31 12:32:41 +00:00
|
|
|
|
2012-09-28 07:31:51 +00:00
|
|
|
#if defined(POLARSSL_DHM_C)
|
|
|
|
if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
|
|
|
|
POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
|
|
|
|
( ret = mpi_read_string( &ssl->dhm_G, 16,
|
|
|
|
POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "mpi_read_string", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare base structures
|
|
|
|
*/
|
2013-07-03 11:37:05 +00:00
|
|
|
ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
|
2009-01-03 21:22:43 +00:00
|
|
|
ssl->in_hdr = ssl->in_ctr + 8;
|
2013-01-02 16:30:03 +00:00
|
|
|
ssl->in_iv = ssl->in_ctr + 13;
|
2009-01-03 21:22:43 +00:00
|
|
|
ssl->in_msg = ssl->in_ctr + 13;
|
|
|
|
|
|
|
|
if( ssl->in_ctr == NULL )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
|
2011-12-10 21:55:01 +00:00
|
|
|
return( POLARSSL_ERR_SSL_MALLOC_FAILED );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2013-07-03 11:37:05 +00:00
|
|
|
ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
|
2009-01-03 21:22:43 +00:00
|
|
|
ssl->out_hdr = ssl->out_ctr + 8;
|
2013-01-02 16:30:03 +00:00
|
|
|
ssl->out_iv = ssl->out_ctr + 13;
|
2012-12-19 13:40:42 +00:00
|
|
|
ssl->out_msg = ssl->out_ctr + 13;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
if( ssl->out_ctr == NULL )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
|
2013-07-03 11:37:05 +00:00
|
|
|
polarssl_free( ssl-> in_ctr );
|
2011-12-10 21:55:01 +00:00
|
|
|
return( POLARSSL_ERR_SSL_MALLOC_FAILED );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
|
|
|
|
memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
|
|
|
|
|
2013-08-27 19:55:01 +00:00
|
|
|
#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
|
2009-01-03 21:22:43 +00:00
|
|
|
ssl->hostname = NULL;
|
|
|
|
ssl->hostname_len = 0;
|
2013-08-27 19:55:01 +00:00
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-08-14 14:52:14 +00:00
|
|
|
#if defined(POLARSSL_SSL_SESSION_TICKETS)
|
|
|
|
ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
|
|
|
|
#endif
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
|
|
|
|
return( ret );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2011-10-06 12:37:39 +00:00
|
|
|
/*
|
|
|
|
* Reset an initialized and used SSL context for re-use while retaining
|
|
|
|
* all application-set variables, function pointers and data.
|
|
|
|
*/
|
2012-07-03 13:30:23 +00:00
|
|
|
int ssl_session_reset( ssl_context *ssl )
|
2011-10-06 12:37:39 +00:00
|
|
|
{
|
2012-09-16 19:57:18 +00:00
|
|
|
int ret;
|
|
|
|
|
2011-10-06 12:37:39 +00:00
|
|
|
ssl->state = SSL_HELLO_REQUEST;
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
|
|
|
|
ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
|
|
|
|
|
|
|
|
ssl->verify_data_len = 0;
|
|
|
|
memset( ssl->own_verify_data, 0, 36 );
|
|
|
|
memset( ssl->peer_verify_data, 0, 36 );
|
|
|
|
|
2011-10-06 12:37:39 +00:00
|
|
|
ssl->in_offt = NULL;
|
|
|
|
|
2013-01-02 16:30:03 +00:00
|
|
|
ssl->in_msg = ssl->in_ctr + 13;
|
2011-10-06 12:37:39 +00:00
|
|
|
ssl->in_msgtype = 0;
|
|
|
|
ssl->in_msglen = 0;
|
|
|
|
ssl->in_left = 0;
|
|
|
|
|
|
|
|
ssl->in_hslen = 0;
|
|
|
|
ssl->nb_zero = 0;
|
2013-04-16 16:05:29 +00:00
|
|
|
ssl->record_read = 0;
|
2011-10-06 12:37:39 +00:00
|
|
|
|
2013-01-02 16:30:03 +00:00
|
|
|
ssl->out_msg = ssl->out_ctr + 13;
|
2011-10-06 12:37:39 +00:00
|
|
|
ssl->out_msgtype = 0;
|
|
|
|
ssl->out_msglen = 0;
|
|
|
|
ssl->out_left = 0;
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->transform_in = NULL;
|
|
|
|
ssl->transform_out = NULL;
|
2011-10-06 12:37:39 +00:00
|
|
|
|
|
|
|
memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
|
|
|
|
memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
|
2012-05-08 09:17:57 +00:00
|
|
|
|
|
|
|
#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
|
|
|
|
if( ssl_hw_record_reset != NULL)
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
|
2012-12-19 13:42:06 +00:00
|
|
|
if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
|
2012-07-03 13:30:23 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
|
|
|
|
return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
|
|
|
|
}
|
2012-05-08 09:17:57 +00:00
|
|
|
}
|
|
|
|
#endif
|
2012-07-03 13:30:23 +00:00
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
if( ssl->transform )
|
2012-07-03 13:30:23 +00:00
|
|
|
{
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl_transform_free( ssl->transform );
|
2013-07-03 11:37:05 +00:00
|
|
|
polarssl_free( ssl->transform );
|
2012-09-16 19:57:18 +00:00
|
|
|
ssl->transform = NULL;
|
2012-07-03 13:30:23 +00:00
|
|
|
}
|
2012-09-16 19:57:18 +00:00
|
|
|
|
2013-02-14 10:19:38 +00:00
|
|
|
if( ssl->session )
|
|
|
|
{
|
|
|
|
ssl_session_free( ssl->session );
|
2013-07-03 11:37:05 +00:00
|
|
|
polarssl_free( ssl->session );
|
2013-02-14 10:19:38 +00:00
|
|
|
ssl->session = NULL;
|
|
|
|
}
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
|
|
|
|
return( ret );
|
2012-07-03 13:30:23 +00:00
|
|
|
|
|
|
|
return( 0 );
|
2011-10-06 12:37:39 +00:00
|
|
|
}
|
|
|
|
|
2013-08-14 11:48:06 +00:00
|
|
|
#if defined(POLARSSL_SSL_SESSION_TICKETS)
|
2013-08-03 11:50:48 +00:00
|
|
|
/*
|
|
|
|
* Allocate and initialize ticket keys
|
|
|
|
*/
|
|
|
|
static int ssl_ticket_keys_init( ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ssl_ticket_keys *tkeys;
|
2013-08-03 15:16:31 +00:00
|
|
|
unsigned char buf[16];
|
2013-08-03 11:50:48 +00:00
|
|
|
|
|
|
|
if( ssl->ticket_keys != NULL )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
if( ( tkeys = polarssl_malloc( sizeof( ssl_ticket_keys ) ) ) == NULL )
|
|
|
|
return( POLARSSL_ERR_SSL_MALLOC_FAILED );
|
|
|
|
|
|
|
|
if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
2013-08-03 13:37:58 +00:00
|
|
|
if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
|
|
|
|
( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
|
|
|
|
( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
|
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2013-08-03 15:16:31 +00:00
|
|
|
if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
2013-08-03 11:50:48 +00:00
|
|
|
ssl->ticket_keys = tkeys;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2013-08-14 11:48:06 +00:00
|
|
|
#endif /* POLARSSL_SSL_SESSION_TICKETS */
|
2013-08-03 11:50:48 +00:00
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* SSL set accessors
|
|
|
|
*/
|
|
|
|
void ssl_set_endpoint( ssl_context *ssl, int endpoint )
|
|
|
|
{
|
|
|
|
ssl->endpoint = endpoint;
|
2013-08-03 11:02:31 +00:00
|
|
|
|
2013-08-14 14:52:14 +00:00
|
|
|
#if defined(POLARSSL_SSL_SESSION_TICKETS)
|
2013-08-03 11:02:31 +00:00
|
|
|
if( endpoint == SSL_IS_CLIENT )
|
|
|
|
ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
|
2013-08-14 14:52:14 +00:00
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ssl_set_authmode( ssl_context *ssl, int authmode )
|
|
|
|
{
|
|
|
|
ssl->authmode = authmode;
|
|
|
|
}
|
|
|
|
|
2013-04-18 20:46:23 +00:00
|
|
|
#if defined(POLARSSL_X509_PARSE_C)
|
2011-01-13 17:54:59 +00:00
|
|
|
void ssl_set_verify( ssl_context *ssl,
|
2012-09-28 07:10:55 +00:00
|
|
|
int (*f_vrfy)(void *, x509_cert *, int, int *),
|
2011-01-13 17:54:59 +00:00
|
|
|
void *p_vrfy )
|
|
|
|
{
|
|
|
|
ssl->f_vrfy = f_vrfy;
|
|
|
|
ssl->p_vrfy = p_vrfy;
|
|
|
|
}
|
2013-04-18 20:46:23 +00:00
|
|
|
#endif /* POLARSSL_X509_PARSE_C */
|
2011-01-13 17:54:59 +00:00
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
void ssl_set_rng( ssl_context *ssl,
|
2011-11-27 21:07:34 +00:00
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
2009-01-03 21:22:43 +00:00
|
|
|
void *p_rng )
|
|
|
|
{
|
|
|
|
ssl->f_rng = f_rng;
|
|
|
|
ssl->p_rng = p_rng;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ssl_set_dbg( ssl_context *ssl,
|
2010-03-16 21:09:09 +00:00
|
|
|
void (*f_dbg)(void *, int, const char *),
|
2009-01-03 21:22:43 +00:00
|
|
|
void *p_dbg )
|
|
|
|
{
|
|
|
|
ssl->f_dbg = f_dbg;
|
|
|
|
ssl->p_dbg = p_dbg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ssl_set_bio( ssl_context *ssl,
|
2011-04-24 08:57:21 +00:00
|
|
|
int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
|
2011-06-21 07:36:43 +00:00
|
|
|
int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
ssl->f_recv = f_recv;
|
|
|
|
ssl->f_send = f_send;
|
|
|
|
ssl->p_recv = p_recv;
|
|
|
|
ssl->p_send = p_send;
|
|
|
|
}
|
|
|
|
|
2012-09-25 21:55:46 +00:00
|
|
|
void ssl_set_session_cache( ssl_context *ssl,
|
|
|
|
int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
|
|
|
|
int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2012-09-25 21:55:46 +00:00
|
|
|
ssl->f_get_cache = f_get_cache;
|
|
|
|
ssl->p_get_cache = p_get_cache;
|
|
|
|
ssl->f_set_cache = f_set_cache;
|
|
|
|
ssl->p_set_cache = p_set_cache;
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2013-08-02 13:34:52 +00:00
|
|
|
int ssl_set_session( ssl_context *ssl, const ssl_session *session )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2013-08-02 13:34:52 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if( ssl == NULL ||
|
|
|
|
session == NULL ||
|
|
|
|
ssl->session_negotiate == NULL ||
|
|
|
|
ssl->endpoint != SSL_IS_CLIENT )
|
|
|
|
{
|
|
|
|
return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
2012-09-25 21:55:46 +00:00
|
|
|
ssl->handshake->resume = 1;
|
2013-08-02 13:34:52 +00:00
|
|
|
|
|
|
|
return( 0 );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2012-08-23 08:34:18 +00:00
|
|
|
void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2013-04-15 13:09:54 +00:00
|
|
|
ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
|
|
|
|
ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
|
|
|
|
ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
|
|
|
|
ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ssl_set_ciphersuites_for_version( ssl_context *ssl, const int *ciphersuites,
|
|
|
|
int major, int minor )
|
|
|
|
{
|
|
|
|
if( major != SSL_MAJOR_VERSION_3 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
ssl->ciphersuite_list[minor] = ciphersuites;
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 20:46:23 +00:00
|
|
|
#if defined(POLARSSL_X509_PARSE_C)
|
2009-01-03 21:22:43 +00:00
|
|
|
void ssl_set_ca_chain( ssl_context *ssl, x509_cert *ca_chain,
|
2010-03-24 06:51:15 +00:00
|
|
|
x509_crl *ca_crl, const char *peer_cn )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
ssl->ca_chain = ca_chain;
|
2009-05-03 10:18:48 +00:00
|
|
|
ssl->ca_crl = ca_crl;
|
2009-01-03 21:22:43 +00:00
|
|
|
ssl->peer_cn = peer_cn;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ssl_set_own_cert( ssl_context *ssl, x509_cert *own_cert,
|
2013-08-19 12:10:16 +00:00
|
|
|
pk_context *pk_key )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
ssl->own_cert = own_cert;
|
2013-08-19 12:10:16 +00:00
|
|
|
ssl->pk_key = pk_key;
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2013-08-19 12:10:16 +00:00
|
|
|
#if defined(POLARSSL_RSA_C)
|
2013-08-21 14:14:26 +00:00
|
|
|
int ssl_set_own_cert_rsa( ssl_context *ssl, x509_cert *own_cert,
|
2013-08-19 12:10:16 +00:00
|
|
|
rsa_context *rsa_key )
|
2011-01-18 15:27:19 +00:00
|
|
|
{
|
2013-08-21 14:14:26 +00:00
|
|
|
int ret;
|
|
|
|
|
2011-01-18 15:27:19 +00:00
|
|
|
ssl->own_cert = own_cert;
|
2013-08-21 14:14:26 +00:00
|
|
|
|
|
|
|
if( ( ssl->pk_key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
|
|
|
|
return( POLARSSL_ERR_SSL_MALLOC_FAILED );
|
|
|
|
|
|
|
|
ssl->pk_key_own_alloc = 1;
|
|
|
|
|
|
|
|
pk_init( ssl->pk_key );
|
|
|
|
|
|
|
|
ret = pk_init_ctx( ssl->pk_key, pk_info_from_type( POLARSSL_PK_RSA ) );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
if( ( ret = rsa_copy( ssl->pk_key->pk_ctx, rsa_key ) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
return( 0 );
|
2013-08-19 12:10:16 +00:00
|
|
|
}
|
|
|
|
#endif /* POLARSSL_RSA_C */
|
|
|
|
|
2013-08-22 15:54:20 +00:00
|
|
|
int ssl_set_own_cert_alt( ssl_context *ssl, x509_cert *own_cert,
|
|
|
|
void *rsa_key,
|
|
|
|
rsa_decrypt_func rsa_decrypt,
|
|
|
|
rsa_sign_func rsa_sign,
|
|
|
|
rsa_key_len_func rsa_key_len )
|
2013-08-19 12:10:16 +00:00
|
|
|
{
|
|
|
|
ssl->own_cert = own_cert;
|
2013-08-21 13:09:31 +00:00
|
|
|
|
|
|
|
if( ( ssl->pk_key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
|
|
|
|
return( POLARSSL_ERR_SSL_MALLOC_FAILED );
|
|
|
|
|
|
|
|
ssl->pk_key_own_alloc = 1;
|
|
|
|
|
|
|
|
pk_init( ssl->pk_key );
|
|
|
|
|
|
|
|
return( pk_init_ctx_rsa_alt( ssl->pk_key, rsa_key,
|
|
|
|
rsa_decrypt, rsa_sign, rsa_key_len ) );
|
2011-01-18 15:27:19 +00:00
|
|
|
}
|
2013-04-18 20:46:23 +00:00
|
|
|
#endif /* POLARSSL_X509_PARSE_C */
|
2012-09-27 19:15:01 +00:00
|
|
|
|
2013-04-16 16:05:29 +00:00
|
|
|
#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
|
|
|
|
void ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
|
|
|
|
const unsigned char *psk_identity, size_t psk_identity_len )
|
|
|
|
{
|
|
|
|
ssl->psk = psk;
|
|
|
|
ssl->psk_len = psk_len;
|
|
|
|
ssl->psk_identity = psk_identity;
|
|
|
|
ssl->psk_identity_len = psk_identity_len;
|
|
|
|
}
|
|
|
|
#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
|
2011-01-18 15:27:19 +00:00
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
#if defined(POLARSSL_DHM_C)
|
2010-03-16 21:09:09 +00:00
|
|
|
int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "mpi_read_string", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "mpi_read_string", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2011-01-06 15:48:19 +00:00
|
|
|
int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
|
2011-01-06 15:48:19 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "mpi_copy", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
|
2011-01-06 15:48:19 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "mpi_copy", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2012-09-16 19:57:18 +00:00
|
|
|
#endif /* POLARSSL_DHM_C */
|
2011-01-06 15:48:19 +00:00
|
|
|
|
2013-08-27 19:55:01 +00:00
|
|
|
#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
|
2010-03-16 21:09:09 +00:00
|
|
|
int ssl_set_hostname( ssl_context *ssl, const char *hostname )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
if( hostname == NULL )
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
ssl->hostname_len = strlen( hostname );
|
2013-08-19 12:25:29 +00:00
|
|
|
|
|
|
|
if( ssl->hostname_len + 1 == 0 )
|
|
|
|
return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
2013-07-03 11:37:05 +00:00
|
|
|
ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-01-13 13:44:06 +00:00
|
|
|
if( ssl->hostname == NULL )
|
|
|
|
return( POLARSSL_ERR_SSL_MALLOC_FAILED );
|
|
|
|
|
2013-06-24 17:03:14 +00:00
|
|
|
memcpy( ssl->hostname, (const unsigned char *) hostname,
|
2009-01-03 21:22:43 +00:00
|
|
|
ssl->hostname_len );
|
2013-04-16 11:15:56 +00:00
|
|
|
|
2009-05-03 10:18:48 +00:00
|
|
|
ssl->hostname[ssl->hostname_len] = '\0';
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2012-09-27 21:49:42 +00:00
|
|
|
void ssl_set_sni( ssl_context *ssl,
|
|
|
|
int (*f_sni)(void *, ssl_context *,
|
|
|
|
const unsigned char *, size_t),
|
|
|
|
void *p_sni )
|
|
|
|
{
|
|
|
|
ssl->f_sni = f_sni;
|
|
|
|
ssl->p_sni = p_sni;
|
|
|
|
}
|
2013-08-27 19:55:01 +00:00
|
|
|
#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
|
2012-09-27 21:49:42 +00:00
|
|
|
|
2011-10-06 13:04:09 +00:00
|
|
|
void ssl_set_max_version( ssl_context *ssl, int major, int minor )
|
|
|
|
{
|
2013-08-27 19:19:20 +00:00
|
|
|
if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
|
|
|
|
minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
|
|
|
|
{
|
|
|
|
ssl->max_major_ver = major;
|
|
|
|
ssl->max_minor_ver = minor;
|
|
|
|
}
|
2011-10-06 13:04:09 +00:00
|
|
|
}
|
|
|
|
|
2012-09-28 13:28:45 +00:00
|
|
|
void ssl_set_min_version( ssl_context *ssl, int major, int minor )
|
|
|
|
{
|
2013-08-27 19:19:20 +00:00
|
|
|
if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
|
|
|
|
minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
|
|
|
|
{
|
|
|
|
ssl->min_major_ver = major;
|
|
|
|
ssl->min_minor_ver = minor;
|
|
|
|
}
|
2012-09-28 13:28:45 +00:00
|
|
|
}
|
|
|
|
|
2013-08-15 11:33:48 +00:00
|
|
|
#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
|
2013-07-16 10:45:26 +00:00
|
|
|
int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
|
|
|
|
{
|
2013-07-18 10:32:27 +00:00
|
|
|
if( mfl_code >= sizeof( mfl_code_to_length ) ||
|
|
|
|
mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
|
2013-07-16 10:45:26 +00:00
|
|
|
{
|
2013-07-17 12:33:38 +00:00
|
|
|
return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
|
2013-07-18 10:32:27 +00:00
|
|
|
}
|
2013-07-17 12:33:38 +00:00
|
|
|
|
2013-07-16 10:45:26 +00:00
|
|
|
ssl->mfl_code = mfl_code;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2013-08-15 11:33:48 +00:00
|
|
|
#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
|
2013-07-16 10:45:26 +00:00
|
|
|
|
2013-08-15 11:45:55 +00:00
|
|
|
#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
|
2013-07-19 12:14:37 +00:00
|
|
|
int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
|
2013-07-19 09:08:52 +00:00
|
|
|
{
|
|
|
|
if( ssl->endpoint != SSL_IS_CLIENT )
|
|
|
|
return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
2013-07-19 12:14:37 +00:00
|
|
|
ssl->trunc_hmac = truncate;
|
2013-07-19 09:08:52 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2013-08-15 11:45:55 +00:00
|
|
|
#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
|
2013-07-19 09:08:52 +00:00
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
|
|
|
|
{
|
|
|
|
ssl->disable_renegotiation = renegotiation;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
|
|
|
|
{
|
|
|
|
ssl->allow_legacy_renegotiation = allow_legacy;
|
|
|
|
}
|
|
|
|
|
2013-08-14 11:48:06 +00:00
|
|
|
#if defined(POLARSSL_SSL_SESSION_TICKETS)
|
2013-08-03 11:02:31 +00:00
|
|
|
int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
|
|
|
|
{
|
|
|
|
ssl->session_tickets = use_tickets;
|
|
|
|
|
2013-08-03 11:50:48 +00:00
|
|
|
if( ssl->endpoint == SSL_IS_CLIENT )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
if( ssl->f_rng == NULL )
|
|
|
|
return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
return( ssl_ticket_keys_init( ssl ) );
|
2013-08-03 11:02:31 +00:00
|
|
|
}
|
2013-08-14 14:52:14 +00:00
|
|
|
|
|
|
|
void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
|
|
|
|
{
|
|
|
|
ssl->ticket_lifetime = lifetime;
|
|
|
|
}
|
2013-08-14 11:48:06 +00:00
|
|
|
#endif /* POLARSSL_SSL_SESSION_TICKETS */
|
2013-08-03 11:02:31 +00:00
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* SSL get accessors
|
|
|
|
*/
|
2011-04-24 08:57:21 +00:00
|
|
|
size_t ssl_get_bytes_avail( const ssl_context *ssl )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
|
|
|
|
}
|
|
|
|
|
2010-03-16 21:09:09 +00:00
|
|
|
int ssl_get_verify_result( const ssl_context *ssl )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2013-08-23 08:44:29 +00:00
|
|
|
return( ssl->session->verify_result );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2011-01-27 17:40:50 +00:00
|
|
|
const char *ssl_get_ciphersuite( const ssl_context *ssl )
|
2011-01-16 21:27:44 +00:00
|
|
|
{
|
2013-03-06 09:23:34 +00:00
|
|
|
if( ssl == NULL || ssl->session == NULL )
|
|
|
|
return NULL;
|
|
|
|
|
2011-01-27 17:40:50 +00:00
|
|
|
return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
|
2011-01-16 21:27:44 +00:00
|
|
|
}
|
|
|
|
|
2011-01-15 17:35:19 +00:00
|
|
|
const char *ssl_get_version( const ssl_context *ssl )
|
|
|
|
{
|
|
|
|
switch( ssl->minor_ver )
|
|
|
|
{
|
|
|
|
case SSL_MINOR_VERSION_0:
|
|
|
|
return( "SSLv3.0" );
|
|
|
|
|
|
|
|
case SSL_MINOR_VERSION_1:
|
|
|
|
return( "TLSv1.0" );
|
|
|
|
|
|
|
|
case SSL_MINOR_VERSION_2:
|
|
|
|
return( "TLSv1.1" );
|
|
|
|
|
2012-04-11 12:09:53 +00:00
|
|
|
case SSL_MINOR_VERSION_3:
|
|
|
|
return( "TLSv1.2" );
|
|
|
|
|
2011-01-15 17:35:19 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return( "unknown" );
|
|
|
|
}
|
|
|
|
|
2013-04-18 20:46:23 +00:00
|
|
|
#if defined(POLARSSL_X509_PARSE_C)
|
2012-10-30 07:51:03 +00:00
|
|
|
const x509_cert *ssl_get_peer_cert( const ssl_context *ssl )
|
|
|
|
{
|
|
|
|
if( ssl == NULL || ssl->session == NULL )
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return ssl->session->peer_cert;
|
|
|
|
}
|
2013-04-18 20:46:23 +00:00
|
|
|
#endif /* POLARSSL_X509_PARSE_C */
|
2012-10-30 07:51:03 +00:00
|
|
|
|
2013-07-30 10:41:56 +00:00
|
|
|
int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
|
|
|
|
{
|
|
|
|
if( ssl == NULL ||
|
|
|
|
dst == NULL ||
|
|
|
|
ssl->session == NULL ||
|
|
|
|
ssl->endpoint != SSL_IS_CLIENT )
|
|
|
|
{
|
|
|
|
return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
2013-08-02 13:34:52 +00:00
|
|
|
return( ssl_session_copy( dst, ssl->session ) );
|
2013-07-30 10:41:56 +00:00
|
|
|
}
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
2013-01-25 13:49:24 +00:00
|
|
|
* Perform a single step of the SSL handshake
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2013-01-25 13:49:24 +00:00
|
|
|
int ssl_handshake_step( ssl_context *ssl )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2009-01-03 21:51:57 +00:00
|
|
|
int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2009-01-03 21:51:57 +00:00
|
|
|
#if defined(POLARSSL_SSL_CLI_C)
|
2009-01-03 21:22:43 +00:00
|
|
|
if( ssl->endpoint == SSL_IS_CLIENT )
|
2013-01-25 13:49:24 +00:00
|
|
|
ret = ssl_handshake_client_step( ssl );
|
2009-01-03 21:22:43 +00:00
|
|
|
#endif
|
|
|
|
|
2009-01-03 21:51:57 +00:00
|
|
|
#if defined(POLARSSL_SSL_SRV_C)
|
2009-01-03 21:22:43 +00:00
|
|
|
if( ssl->endpoint == SSL_IS_SERVER )
|
2013-01-25 13:49:24 +00:00
|
|
|
ret = ssl_handshake_server_step( ssl );
|
2009-01-03 21:22:43 +00:00
|
|
|
#endif
|
|
|
|
|
2013-01-25 13:49:24 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform the SSL handshake
|
|
|
|
*/
|
|
|
|
int ssl_handshake( ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
|
|
|
|
|
|
|
|
while( ssl->state != SSL_HANDSHAKE_OVER )
|
|
|
|
{
|
|
|
|
ret = ssl_handshake_step( ssl );
|
|
|
|
|
|
|
|
if( ret != 0 )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
/*
|
|
|
|
* Renegotiate current connection
|
|
|
|
*/
|
|
|
|
int ssl_renegotiate( ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
|
|
|
|
|
|
|
|
if( ssl->state != SSL_HANDSHAKE_OVER )
|
|
|
|
return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
ssl->state = SSL_HELLO_REQUEST;
|
|
|
|
ssl->renegotiation = SSL_RENEGOTIATION;
|
|
|
|
|
|
|
|
if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
if( ( ret = ssl_handshake( ssl ) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_handshake", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* Receive application data decrypted from the SSL layer
|
|
|
|
*/
|
2011-04-24 08:57:21 +00:00
|
|
|
int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2011-04-24 08:57:21 +00:00
|
|
|
int ret;
|
|
|
|
size_t n;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> read" ) );
|
|
|
|
|
|
|
|
if( ssl->state != SSL_HANDSHAKE_OVER )
|
|
|
|
{
|
|
|
|
if( ( ret = ssl_handshake( ssl ) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_handshake", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ssl->in_offt == NULL )
|
|
|
|
{
|
|
|
|
if( ( ret = ssl_read_record( ssl ) ) != 0 )
|
|
|
|
{
|
2011-05-18 13:32:51 +00:00
|
|
|
if( ret == POLARSSL_ERR_SSL_CONN_EOF )
|
|
|
|
return( 0 );
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
SSL_DEBUG_RET( 1, "ssl_read_record", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ssl->in_msglen == 0 &&
|
|
|
|
ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* OpenSSL sends empty messages to randomize the IV
|
|
|
|
*/
|
|
|
|
if( ( ret = ssl_read_record( ssl ) ) != 0 )
|
|
|
|
{
|
2011-05-18 13:32:51 +00:00
|
|
|
if( ret == POLARSSL_ERR_SSL_CONN_EOF )
|
|
|
|
return( 0 );
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
SSL_DEBUG_RET( 1, "ssl_read_record", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
|
|
|
|
|
|
|
|
if( ssl->endpoint == SSL_IS_CLIENT &&
|
|
|
|
( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
|
|
|
|
ssl->in_hslen != 4 ) )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
|
|
|
|
return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
|
|
|
|
}
|
|
|
|
|
2012-09-17 09:18:12 +00:00
|
|
|
if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
|
|
|
|
( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
|
|
|
|
ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) )
|
2012-09-16 19:57:18 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
|
|
|
|
|
2013-08-27 19:19:20 +00:00
|
|
|
#if defined(POLARSSL_SSL_PROTO_SSL3)
|
2012-09-17 09:18:12 +00:00
|
|
|
if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
|
2012-09-16 19:57:18 +00:00
|
|
|
{
|
2012-09-17 09:18:12 +00:00
|
|
|
/*
|
|
|
|
* SSLv3 does not have a "no_renegotiation" alert
|
|
|
|
*/
|
|
|
|
if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
else
|
2013-08-27 19:19:20 +00:00
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
|
|
|
|
defined(POLARSSL_SSL_PROTO_TLS1_2)
|
|
|
|
if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
|
2012-09-17 09:18:12 +00:00
|
|
|
{
|
|
|
|
if( ( ret = ssl_send_alert_message( ssl,
|
|
|
|
SSL_ALERT_LEVEL_WARNING,
|
|
|
|
SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
|
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
2012-09-16 19:57:18 +00:00
|
|
|
}
|
2013-08-27 19:19:20 +00:00
|
|
|
else
|
|
|
|
#endif
|
2013-08-28 09:57:20 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "should never happen" ) );
|
2013-08-27 19:19:20 +00:00
|
|
|
return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
|
2013-08-28 09:57:20 +00:00
|
|
|
}
|
2012-09-16 19:57:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( ( ret = ssl_renegotiate( ssl ) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_renegotiate", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( POLARSSL_ERR_NET_WANT_READ );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
|
2009-01-03 21:51:57 +00:00
|
|
|
return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ssl->in_offt = ssl->in_msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = ( len < ssl->in_msglen )
|
|
|
|
? len : ssl->in_msglen;
|
|
|
|
|
|
|
|
memcpy( buf, ssl->in_offt, n );
|
|
|
|
ssl->in_msglen -= n;
|
|
|
|
|
|
|
|
if( ssl->in_msglen == 0 )
|
|
|
|
/* all bytes consumed */
|
|
|
|
ssl->in_offt = NULL;
|
|
|
|
else
|
|
|
|
/* more data available */
|
|
|
|
ssl->in_offt += n;
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= read" ) );
|
|
|
|
|
2011-04-24 08:57:21 +00:00
|
|
|
return( (int) n );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send application data to be encrypted by the SSL layer
|
|
|
|
*/
|
2011-04-24 08:57:21 +00:00
|
|
|
int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2011-04-24 08:57:21 +00:00
|
|
|
int ret;
|
|
|
|
size_t n;
|
2013-08-15 11:33:48 +00:00
|
|
|
unsigned int max_len = SSL_MAX_CONTENT_LEN;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> write" ) );
|
|
|
|
|
|
|
|
if( ssl->state != SSL_HANDSHAKE_OVER )
|
|
|
|
{
|
|
|
|
if( ( ret = ssl_handshake( ssl ) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_handshake", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-15 11:33:48 +00:00
|
|
|
#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
|
2013-07-18 10:32:27 +00:00
|
|
|
/*
|
|
|
|
* Assume mfl_code is correct since it was checked when set
|
|
|
|
*/
|
|
|
|
max_len = mfl_code_to_length[ssl->mfl_code];
|
|
|
|
|
2013-07-18 12:07:09 +00:00
|
|
|
/*
|
2013-08-15 11:33:48 +00:00
|
|
|
* Check if a smaller max length was negotiated
|
2013-07-18 12:07:09 +00:00
|
|
|
*/
|
|
|
|
if( ssl->session_out != NULL &&
|
|
|
|
mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
|
|
|
|
{
|
|
|
|
max_len = mfl_code_to_length[ssl->session_out->mfl_code];
|
|
|
|
}
|
2013-08-15 11:33:48 +00:00
|
|
|
#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
|
2013-07-18 12:07:09 +00:00
|
|
|
|
2013-07-18 10:32:27 +00:00
|
|
|
n = ( len < max_len) ? len : max_len;
|
2011-06-08 13:10:54 +00:00
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
if( ssl->out_left != 0 )
|
|
|
|
{
|
|
|
|
if( ( ret = ssl_flush_output( ssl ) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
2011-06-08 13:10:54 +00:00
|
|
|
else
|
2011-03-14 20:50:15 +00:00
|
|
|
{
|
2011-06-08 13:10:54 +00:00
|
|
|
ssl->out_msglen = n;
|
|
|
|
ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
|
|
|
|
memcpy( ssl->out_msg, buf, n );
|
|
|
|
|
|
|
|
if( ( ret = ssl_write_record( ssl ) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_write_record", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= write" ) );
|
|
|
|
|
2011-04-24 08:57:21 +00:00
|
|
|
return( (int) n );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Notify the peer that the connection is being closed
|
|
|
|
*/
|
|
|
|
int ssl_close_notify( ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
|
|
|
|
|
|
|
|
if( ( ret = ssl_flush_output( ssl ) ) != 0 )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ssl->state == SSL_HANDSHAKE_OVER )
|
|
|
|
{
|
2012-09-16 19:57:18 +00:00
|
|
|
if( ( ret = ssl_send_alert_message( ssl,
|
|
|
|
SSL_ALERT_LEVEL_WARNING,
|
|
|
|
SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
void ssl_transform_free( ssl_transform *transform )
|
|
|
|
{
|
|
|
|
#if defined(POLARSSL_ZLIB_SUPPORT)
|
|
|
|
deflateEnd( &transform->ctx_deflate );
|
|
|
|
inflateEnd( &transform->ctx_inflate );
|
|
|
|
#endif
|
|
|
|
|
2013-07-04 09:51:43 +00:00
|
|
|
md_free_ctx( &transform->md_ctx_enc );
|
|
|
|
md_free_ctx( &transform->md_ctx_dec );
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
memset( transform, 0, sizeof( ssl_transform ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void ssl_handshake_free( ssl_handshake_params *handshake )
|
|
|
|
{
|
|
|
|
#if defined(POLARSSL_DHM_C)
|
|
|
|
dhm_free( &handshake->dhm_ctx );
|
|
|
|
#endif
|
2013-07-04 09:51:43 +00:00
|
|
|
#if defined(POLARSSL_ECDH_C)
|
|
|
|
ecdh_free( &handshake->ecdh_ctx );
|
|
|
|
#endif
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
memset( handshake, 0, sizeof( ssl_handshake_params ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void ssl_session_free( ssl_session *session )
|
|
|
|
{
|
2013-04-18 20:46:23 +00:00
|
|
|
#if defined(POLARSSL_X509_PARSE_C)
|
2012-09-25 21:55:46 +00:00
|
|
|
if( session->peer_cert != NULL )
|
2012-09-16 19:57:18 +00:00
|
|
|
{
|
2012-09-25 21:55:46 +00:00
|
|
|
x509_free( session->peer_cert );
|
2013-07-03 11:37:05 +00:00
|
|
|
polarssl_free( session->peer_cert );
|
2012-09-16 19:57:18 +00:00
|
|
|
}
|
2013-04-18 20:46:23 +00:00
|
|
|
#endif
|
2012-09-25 21:55:46 +00:00
|
|
|
|
2013-08-14 11:48:06 +00:00
|
|
|
#if defined(POLARSSL_SSL_SESSION_TICKETS)
|
2013-08-02 12:44:04 +00:00
|
|
|
polarssl_free( session->ticket );
|
2013-08-14 11:48:06 +00:00
|
|
|
#endif
|
2013-08-02 12:44:04 +00:00
|
|
|
|
2012-09-25 21:55:46 +00:00
|
|
|
memset( session, 0, sizeof( ssl_session ) );
|
2012-09-16 19:57:18 +00:00
|
|
|
}
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* Free an SSL context
|
|
|
|
*/
|
|
|
|
void ssl_free( ssl_context *ssl )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 2, ( "=> free" ) );
|
|
|
|
|
|
|
|
if( ssl->out_ctr != NULL )
|
|
|
|
{
|
|
|
|
memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
|
2013-07-03 11:37:05 +00:00
|
|
|
polarssl_free( ssl->out_ctr );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ssl->in_ctr != NULL )
|
|
|
|
{
|
|
|
|
memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
|
2013-07-03 11:37:05 +00:00
|
|
|
polarssl_free( ssl->in_ctr );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2009-01-03 21:51:57 +00:00
|
|
|
#if defined(POLARSSL_DHM_C)
|
2012-09-16 19:57:18 +00:00
|
|
|
mpi_free( &ssl->dhm_P );
|
|
|
|
mpi_free( &ssl->dhm_G );
|
2009-01-03 21:22:43 +00:00
|
|
|
#endif
|
|
|
|
|
2012-09-16 19:57:18 +00:00
|
|
|
if( ssl->transform )
|
|
|
|
{
|
|
|
|
ssl_transform_free( ssl->transform );
|
2013-07-03 11:37:05 +00:00
|
|
|
polarssl_free( ssl->transform );
|
2012-09-16 19:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ssl->handshake )
|
|
|
|
{
|
|
|
|
ssl_handshake_free( ssl->handshake );
|
|
|
|
ssl_transform_free( ssl->transform_negotiate );
|
|
|
|
ssl_session_free( ssl->session_negotiate );
|
|
|
|
|
2013-07-03 11:37:05 +00:00
|
|
|
polarssl_free( ssl->handshake );
|
|
|
|
polarssl_free( ssl->transform_negotiate );
|
|
|
|
polarssl_free( ssl->session_negotiate );
|
2012-09-16 19:57:18 +00:00
|
|
|
}
|
|
|
|
|
2013-02-14 10:19:38 +00:00
|
|
|
if( ssl->session )
|
|
|
|
{
|
|
|
|
ssl_session_free( ssl->session );
|
2013-07-03 11:37:05 +00:00
|
|
|
polarssl_free( ssl->session );
|
2013-02-14 10:19:38 +00:00
|
|
|
}
|
|
|
|
|
2013-08-14 11:48:06 +00:00
|
|
|
#if defined(POLARSSL_SSL_SESSION_TICKETS)
|
2013-08-03 11:50:48 +00:00
|
|
|
polarssl_free( ssl->ticket_keys );
|
2013-08-14 11:48:06 +00:00
|
|
|
#endif
|
2013-08-03 11:50:48 +00:00
|
|
|
|
2013-08-27 19:55:01 +00:00
|
|
|
#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
|
2009-01-03 21:22:43 +00:00
|
|
|
if ( ssl->hostname != NULL)
|
|
|
|
{
|
|
|
|
memset( ssl->hostname, 0, ssl->hostname_len );
|
2013-07-03 11:37:05 +00:00
|
|
|
polarssl_free( ssl->hostname );
|
2009-01-03 21:22:43 +00:00
|
|
|
ssl->hostname_len = 0;
|
|
|
|
}
|
2013-08-27 19:55:01 +00:00
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-08-21 13:09:31 +00:00
|
|
|
if( ssl->pk_key_own_alloc )
|
|
|
|
{
|
|
|
|
pk_free( ssl->pk_key );
|
|
|
|
polarssl_free( ssl->pk_key );
|
|
|
|
}
|
|
|
|
|
2012-05-08 09:17:57 +00:00
|
|
|
#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
|
|
|
|
if( ssl_hw_record_finish != NULL )
|
|
|
|
{
|
|
|
|
SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
|
|
|
|
ssl_hw_record_finish( ssl );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
SSL_DEBUG_MSG( 2, ( "<= free" ) );
|
2009-02-05 18:00:28 +00:00
|
|
|
|
2013-02-14 10:20:09 +00:00
|
|
|
/* Actually clear after last debug message */
|
2009-02-05 18:00:28 +00:00
|
|
|
memset( ssl, 0, sizeof( ssl_context ) );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2013-08-21 14:14:26 +00:00
|
|
|
/*
|
|
|
|
* Get the SSL_SIG_* constant corresponding to a public key
|
|
|
|
*/
|
|
|
|
unsigned char ssl_sig_from_pk( pk_context *pk )
|
|
|
|
{
|
|
|
|
#if defined(POLARSSL_RSA_C)
|
|
|
|
if( pk_can_do( pk, POLARSSL_PK_RSA ) )
|
|
|
|
return( SSL_SIG_RSA );
|
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_ECDSA_C)
|
|
|
|
if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
|
|
|
|
return( SSL_SIG_ECDSA );
|
|
|
|
#endif
|
|
|
|
return( SSL_SIG_ANON );
|
|
|
|
}
|
|
|
|
|
2013-08-22 11:52:48 +00:00
|
|
|
pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
|
|
|
|
{
|
|
|
|
switch( sig )
|
|
|
|
{
|
|
|
|
#if defined(POLARSSL_RSA_C)
|
|
|
|
case SSL_SIG_RSA:
|
|
|
|
return( POLARSSL_PK_RSA );
|
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_ECDSA_C)
|
|
|
|
case SSL_SIG_ECDSA:
|
|
|
|
return( POLARSSL_PK_ECDSA );
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return( POLARSSL_PK_NONE );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
md_type_t ssl_md_alg_from_hash( unsigned char hash )
|
|
|
|
{
|
|
|
|
switch( hash )
|
|
|
|
{
|
|
|
|
#if defined(POLARSSL_MD5_C)
|
|
|
|
case SSL_HASH_MD5:
|
|
|
|
return( POLARSSL_MD_MD5 );
|
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_SHA1_C)
|
|
|
|
case SSL_HASH_SHA1:
|
|
|
|
return( POLARSSL_MD_SHA1 );
|
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_SHA256_C)
|
|
|
|
case SSL_HASH_SHA224:
|
|
|
|
return( POLARSSL_MD_SHA224 );
|
|
|
|
case SSL_HASH_SHA256:
|
|
|
|
return( POLARSSL_MD_SHA256 );
|
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_SHA512_C)
|
|
|
|
case SSL_HASH_SHA384:
|
|
|
|
return( POLARSSL_MD_SHA384 );
|
|
|
|
case SSL_HASH_SHA512:
|
|
|
|
return( POLARSSL_MD_SHA512 );
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return( POLARSSL_MD_NONE );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
#endif
|