mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Committer:
ansond
Date:
Thu Jun 11 03:27:03 2015 +0000
Revision:
0:137634ff4186
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:137634ff4186 1 /*
ansond 0:137634ff4186 2 * SSLv3/TLSv1 shared functions
ansond 0:137634ff4186 3 *
ansond 0:137634ff4186 4 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
ansond 0:137634ff4186 5 *
ansond 0:137634ff4186 6 * This file is part of mbed TLS (https://tls.mbed.org)
ansond 0:137634ff4186 7 *
ansond 0:137634ff4186 8 * This program is free software; you can redistribute it and/or modify
ansond 0:137634ff4186 9 * it under the terms of the GNU General Public License as published by
ansond 0:137634ff4186 10 * the Free Software Foundation; either version 2 of the License, or
ansond 0:137634ff4186 11 * (at your option) any later version.
ansond 0:137634ff4186 12 *
ansond 0:137634ff4186 13 * This program is distributed in the hope that it will be useful,
ansond 0:137634ff4186 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ansond 0:137634ff4186 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ansond 0:137634ff4186 16 * GNU General Public License for more details.
ansond 0:137634ff4186 17 *
ansond 0:137634ff4186 18 * You should have received a copy of the GNU General Public License along
ansond 0:137634ff4186 19 * with this program; if not, write to the Free Software Foundation, Inc.,
ansond 0:137634ff4186 20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ansond 0:137634ff4186 21 */
ansond 0:137634ff4186 22 /*
ansond 0:137634ff4186 23 * The SSL 3.0 specification was drafted by Netscape in 1996,
ansond 0:137634ff4186 24 * and became an IETF standard in 1999.
ansond 0:137634ff4186 25 *
ansond 0:137634ff4186 26 * http://wp.netscape.com/eng/ssl3/
ansond 0:137634ff4186 27 * http://www.ietf.org/rfc/rfc2246.txt
ansond 0:137634ff4186 28 * http://www.ietf.org/rfc/rfc4346.txt
ansond 0:137634ff4186 29 */
ansond 0:137634ff4186 30
ansond 0:137634ff4186 31 #if !defined(POLARSSL_CONFIG_FILE)
ansond 0:137634ff4186 32 #include "polarssl/config.h"
ansond 0:137634ff4186 33 #else
ansond 0:137634ff4186 34 #include POLARSSL_CONFIG_FILE
ansond 0:137634ff4186 35 #endif
ansond 0:137634ff4186 36
ansond 0:137634ff4186 37 #if defined(POLARSSL_SSL_TLS_C)
ansond 0:137634ff4186 38
ansond 0:137634ff4186 39 #include "polarssl/debug.h"
ansond 0:137634ff4186 40 #include "polarssl/ssl.h"
ansond 0:137634ff4186 41
ansond 0:137634ff4186 42 #include <string.h>
ansond 0:137634ff4186 43
ansond 0:137634ff4186 44 #if defined(POLARSSL_X509_CRT_PARSE_C) && \
ansond 0:137634ff4186 45 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
ansond 0:137634ff4186 46 #include "polarssl/oid.h"
ansond 0:137634ff4186 47 #endif
ansond 0:137634ff4186 48
ansond 0:137634ff4186 49 #if defined(POLARSSL_PLATFORM_C)
ansond 0:137634ff4186 50 #include "polarssl/platform.h"
ansond 0:137634ff4186 51 #else
ansond 0:137634ff4186 52 #include <stdlib.h>
ansond 0:137634ff4186 53 #define polarssl_malloc malloc
ansond 0:137634ff4186 54 #define polarssl_free free
ansond 0:137634ff4186 55 #endif
ansond 0:137634ff4186 56
ansond 0:137634ff4186 57 #if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
ansond 0:137634ff4186 58 !defined(EFI32)
ansond 0:137634ff4186 59 #define strcasecmp _stricmp
ansond 0:137634ff4186 60 #endif
ansond 0:137634ff4186 61
ansond 0:137634ff4186 62 /* Implementation that should never be optimized out by the compiler */
ansond 0:137634ff4186 63 static void polarssl_zeroize( void *v, size_t n ) {
ansond 0:137634ff4186 64 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
ansond 0:137634ff4186 65 }
ansond 0:137634ff4186 66
ansond 0:137634ff4186 67 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
ansond 0:137634ff4186 68 /*
ansond 0:137634ff4186 69 * Convert max_fragment_length codes to length.
ansond 0:137634ff4186 70 * RFC 6066 says:
ansond 0:137634ff4186 71 * enum{
ansond 0:137634ff4186 72 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
ansond 0:137634ff4186 73 * } MaxFragmentLength;
ansond 0:137634ff4186 74 * and we add 0 -> extension unused
ansond 0:137634ff4186 75 */
ansond 0:137634ff4186 76 static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
ansond 0:137634ff4186 77 {
ansond 0:137634ff4186 78 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
ansond 0:137634ff4186 79 512, /* SSL_MAX_FRAG_LEN_512 */
ansond 0:137634ff4186 80 1024, /* SSL_MAX_FRAG_LEN_1024 */
ansond 0:137634ff4186 81 2048, /* SSL_MAX_FRAG_LEN_2048 */
ansond 0:137634ff4186 82 4096, /* SSL_MAX_FRAG_LEN_4096 */
ansond 0:137634ff4186 83 };
ansond 0:137634ff4186 84 #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
ansond 0:137634ff4186 85
ansond 0:137634ff4186 86 static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
ansond 0:137634ff4186 87 {
ansond 0:137634ff4186 88 ssl_session_free( dst );
ansond 0:137634ff4186 89 memcpy( dst, src, sizeof( ssl_session ) );
ansond 0:137634ff4186 90
ansond 0:137634ff4186 91 #if defined(POLARSSL_X509_CRT_PARSE_C)
ansond 0:137634ff4186 92 if( src->peer_cert != NULL )
ansond 0:137634ff4186 93 {
ansond 0:137634ff4186 94 int ret;
ansond 0:137634ff4186 95
ansond 0:137634ff4186 96 dst->peer_cert = polarssl_malloc( sizeof(x509_crt) );
ansond 0:137634ff4186 97 if( dst->peer_cert == NULL )
ansond 0:137634ff4186 98 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
ansond 0:137634ff4186 99
ansond 0:137634ff4186 100 x509_crt_init( dst->peer_cert );
ansond 0:137634ff4186 101
ansond 0:137634ff4186 102 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
ansond 0:137634ff4186 103 src->peer_cert->raw.len ) ) != 0 )
ansond 0:137634ff4186 104 {
ansond 0:137634ff4186 105 polarssl_free( dst->peer_cert );
ansond 0:137634ff4186 106 dst->peer_cert = NULL;
ansond 0:137634ff4186 107 return( ret );
ansond 0:137634ff4186 108 }
ansond 0:137634ff4186 109 }
ansond 0:137634ff4186 110 #endif /* POLARSSL_X509_CRT_PARSE_C */
ansond 0:137634ff4186 111
ansond 0:137634ff4186 112 #if defined(POLARSSL_SSL_SESSION_TICKETS)
ansond 0:137634ff4186 113 if( src->ticket != NULL )
ansond 0:137634ff4186 114 {
ansond 0:137634ff4186 115 dst->ticket = polarssl_malloc( src->ticket_len );
ansond 0:137634ff4186 116 if( dst->ticket == NULL )
ansond 0:137634ff4186 117 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
ansond 0:137634ff4186 118
ansond 0:137634ff4186 119 memcpy( dst->ticket, src->ticket, src->ticket_len );
ansond 0:137634ff4186 120 }
ansond 0:137634ff4186 121 #endif /* POLARSSL_SSL_SESSION_TICKETS */
ansond 0:137634ff4186 122
ansond 0:137634ff4186 123 return( 0 );
ansond 0:137634ff4186 124 }
ansond 0:137634ff4186 125
ansond 0:137634ff4186 126 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
ansond 0:137634ff4186 127 int (*ssl_hw_record_init)( ssl_context *ssl,
ansond 0:137634ff4186 128 const unsigned char *key_enc, const unsigned char *key_dec,
ansond 0:137634ff4186 129 size_t keylen,
ansond 0:137634ff4186 130 const unsigned char *iv_enc, const unsigned char *iv_dec,
ansond 0:137634ff4186 131 size_t ivlen,
ansond 0:137634ff4186 132 const unsigned char *mac_enc, const unsigned char *mac_dec,
ansond 0:137634ff4186 133 size_t maclen ) = NULL;
ansond 0:137634ff4186 134 int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
ansond 0:137634ff4186 135 int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
ansond 0:137634ff4186 136 int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
ansond 0:137634ff4186 137 int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
ansond 0:137634ff4186 138 int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
ansond 0:137634ff4186 139 #endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
ansond 0:137634ff4186 140
ansond 0:137634ff4186 141 /*
ansond 0:137634ff4186 142 * Key material generation
ansond 0:137634ff4186 143 */
ansond 0:137634ff4186 144 #if defined(POLARSSL_SSL_PROTO_SSL3)
ansond 0:137634ff4186 145 static int ssl3_prf( const unsigned char *secret, size_t slen,
ansond 0:137634ff4186 146 const char *label,
ansond 0:137634ff4186 147 const unsigned char *random, size_t rlen,
ansond 0:137634ff4186 148 unsigned char *dstbuf, size_t dlen )
ansond 0:137634ff4186 149 {
ansond 0:137634ff4186 150 size_t i;
ansond 0:137634ff4186 151 md5_context md5;
ansond 0:137634ff4186 152 sha1_context sha1;
ansond 0:137634ff4186 153 unsigned char padding[16];
ansond 0:137634ff4186 154 unsigned char sha1sum[20];
ansond 0:137634ff4186 155 ((void)label);
ansond 0:137634ff4186 156
ansond 0:137634ff4186 157 md5_init( &md5 );
ansond 0:137634ff4186 158 sha1_init( &sha1 );
ansond 0:137634ff4186 159
ansond 0:137634ff4186 160 /*
ansond 0:137634ff4186 161 * SSLv3:
ansond 0:137634ff4186 162 * block =
ansond 0:137634ff4186 163 * MD5( secret + SHA1( 'A' + secret + random ) ) +
ansond 0:137634ff4186 164 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
ansond 0:137634ff4186 165 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
ansond 0:137634ff4186 166 * ...
ansond 0:137634ff4186 167 */
ansond 0:137634ff4186 168 for( i = 0; i < dlen / 16; i++ )
ansond 0:137634ff4186 169 {
ansond 0:137634ff4186 170 memset( padding, (unsigned char) ('A' + i), 1 + i );
ansond 0:137634ff4186 171
ansond 0:137634ff4186 172 sha1_starts( &sha1 );
ansond 0:137634ff4186 173 sha1_update( &sha1, padding, 1 + i );
ansond 0:137634ff4186 174 sha1_update( &sha1, secret, slen );
ansond 0:137634ff4186 175 sha1_update( &sha1, random, rlen );
ansond 0:137634ff4186 176 sha1_finish( &sha1, sha1sum );
ansond 0:137634ff4186 177
ansond 0:137634ff4186 178 md5_starts( &md5 );
ansond 0:137634ff4186 179 md5_update( &md5, secret, slen );
ansond 0:137634ff4186 180 md5_update( &md5, sha1sum, 20 );
ansond 0:137634ff4186 181 md5_finish( &md5, dstbuf + i * 16 );
ansond 0:137634ff4186 182 }
ansond 0:137634ff4186 183
ansond 0:137634ff4186 184 md5_free( &md5 );
ansond 0:137634ff4186 185 sha1_free( &sha1 );
ansond 0:137634ff4186 186
ansond 0:137634ff4186 187 polarssl_zeroize( padding, sizeof( padding ) );
ansond 0:137634ff4186 188 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
ansond 0:137634ff4186 189
ansond 0:137634ff4186 190 return( 0 );
ansond 0:137634ff4186 191 }
ansond 0:137634ff4186 192 #endif /* POLARSSL_SSL_PROTO_SSL3 */
ansond 0:137634ff4186 193
ansond 0:137634ff4186 194 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
ansond 0:137634ff4186 195 static int tls1_prf( const unsigned char *secret, size_t slen,
ansond 0:137634ff4186 196 const char *label,
ansond 0:137634ff4186 197 const unsigned char *random, size_t rlen,
ansond 0:137634ff4186 198 unsigned char *dstbuf, size_t dlen )
ansond 0:137634ff4186 199 {
ansond 0:137634ff4186 200 size_t nb, hs;
ansond 0:137634ff4186 201 size_t i, j, k;
ansond 0:137634ff4186 202 const unsigned char *S1, *S2;
ansond 0:137634ff4186 203 unsigned char tmp[128];
ansond 0:137634ff4186 204 unsigned char h_i[20];
ansond 0:137634ff4186 205
ansond 0:137634ff4186 206 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
ansond 0:137634ff4186 207 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
ansond 0:137634ff4186 208
ansond 0:137634ff4186 209 hs = ( slen + 1 ) / 2;
ansond 0:137634ff4186 210 S1 = secret;
ansond 0:137634ff4186 211 S2 = secret + slen - hs;
ansond 0:137634ff4186 212
ansond 0:137634ff4186 213 nb = strlen( label );
ansond 0:137634ff4186 214 memcpy( tmp + 20, label, nb );
ansond 0:137634ff4186 215 memcpy( tmp + 20 + nb, random, rlen );
ansond 0:137634ff4186 216 nb += rlen;
ansond 0:137634ff4186 217
ansond 0:137634ff4186 218 /*
ansond 0:137634ff4186 219 * First compute P_md5(secret,label+random)[0..dlen]
ansond 0:137634ff4186 220 */
ansond 0:137634ff4186 221 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
ansond 0:137634ff4186 222
ansond 0:137634ff4186 223 for( i = 0; i < dlen; i += 16 )
ansond 0:137634ff4186 224 {
ansond 0:137634ff4186 225 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
ansond 0:137634ff4186 226 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
ansond 0:137634ff4186 227
ansond 0:137634ff4186 228 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
ansond 0:137634ff4186 229
ansond 0:137634ff4186 230 for( j = 0; j < k; j++ )
ansond 0:137634ff4186 231 dstbuf[i + j] = h_i[j];
ansond 0:137634ff4186 232 }
ansond 0:137634ff4186 233
ansond 0:137634ff4186 234 /*
ansond 0:137634ff4186 235 * XOR out with P_sha1(secret,label+random)[0..dlen]
ansond 0:137634ff4186 236 */
ansond 0:137634ff4186 237 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
ansond 0:137634ff4186 238
ansond 0:137634ff4186 239 for( i = 0; i < dlen; i += 20 )
ansond 0:137634ff4186 240 {
ansond 0:137634ff4186 241 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
ansond 0:137634ff4186 242 sha1_hmac( S2, hs, tmp, 20, tmp );
ansond 0:137634ff4186 243
ansond 0:137634ff4186 244 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
ansond 0:137634ff4186 245
ansond 0:137634ff4186 246 for( j = 0; j < k; j++ )
ansond 0:137634ff4186 247 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
ansond 0:137634ff4186 248 }
ansond 0:137634ff4186 249
ansond 0:137634ff4186 250 polarssl_zeroize( tmp, sizeof( tmp ) );
ansond 0:137634ff4186 251 polarssl_zeroize( h_i, sizeof( h_i ) );
ansond 0:137634ff4186 252
ansond 0:137634ff4186 253 return( 0 );
ansond 0:137634ff4186 254 }
ansond 0:137634ff4186 255 #endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
ansond 0:137634ff4186 256
ansond 0:137634ff4186 257 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
ansond 0:137634ff4186 258 #if defined(POLARSSL_SHA256_C)
ansond 0:137634ff4186 259 static int tls_prf_sha256( const unsigned char *secret, size_t slen,
ansond 0:137634ff4186 260 const char *label,
ansond 0:137634ff4186 261 const unsigned char *random, size_t rlen,
ansond 0:137634ff4186 262 unsigned char *dstbuf, size_t dlen )
ansond 0:137634ff4186 263 {
ansond 0:137634ff4186 264 size_t nb;
ansond 0:137634ff4186 265 size_t i, j, k;
ansond 0:137634ff4186 266 unsigned char tmp[128];
ansond 0:137634ff4186 267 unsigned char h_i[32];
ansond 0:137634ff4186 268
ansond 0:137634ff4186 269 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
ansond 0:137634ff4186 270 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
ansond 0:137634ff4186 271
ansond 0:137634ff4186 272 nb = strlen( label );
ansond 0:137634ff4186 273 memcpy( tmp + 32, label, nb );
ansond 0:137634ff4186 274 memcpy( tmp + 32 + nb, random, rlen );
ansond 0:137634ff4186 275 nb += rlen;
ansond 0:137634ff4186 276
ansond 0:137634ff4186 277 /*
ansond 0:137634ff4186 278 * Compute P_<hash>(secret, label + random)[0..dlen]
ansond 0:137634ff4186 279 */
ansond 0:137634ff4186 280 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
ansond 0:137634ff4186 281
ansond 0:137634ff4186 282 for( i = 0; i < dlen; i += 32 )
ansond 0:137634ff4186 283 {
ansond 0:137634ff4186 284 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
ansond 0:137634ff4186 285 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
ansond 0:137634ff4186 286
ansond 0:137634ff4186 287 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
ansond 0:137634ff4186 288
ansond 0:137634ff4186 289 for( j = 0; j < k; j++ )
ansond 0:137634ff4186 290 dstbuf[i + j] = h_i[j];
ansond 0:137634ff4186 291 }
ansond 0:137634ff4186 292
ansond 0:137634ff4186 293 polarssl_zeroize( tmp, sizeof( tmp ) );
ansond 0:137634ff4186 294 polarssl_zeroize( h_i, sizeof( h_i ) );
ansond 0:137634ff4186 295
ansond 0:137634ff4186 296 return( 0 );
ansond 0:137634ff4186 297 }
ansond 0:137634ff4186 298 #endif /* POLARSSL_SHA256_C */
ansond 0:137634ff4186 299
ansond 0:137634ff4186 300 #if defined(POLARSSL_SHA512_C)
ansond 0:137634ff4186 301 static int tls_prf_sha384( const unsigned char *secret, size_t slen,
ansond 0:137634ff4186 302 const char *label,
ansond 0:137634ff4186 303 const unsigned char *random, size_t rlen,
ansond 0:137634ff4186 304 unsigned char *dstbuf, size_t dlen )
ansond 0:137634ff4186 305 {
ansond 0:137634ff4186 306 size_t nb;
ansond 0:137634ff4186 307 size_t i, j, k;
ansond 0:137634ff4186 308 unsigned char tmp[128];
ansond 0:137634ff4186 309 unsigned char h_i[48];
ansond 0:137634ff4186 310
ansond 0:137634ff4186 311 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
ansond 0:137634ff4186 312 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
ansond 0:137634ff4186 313
ansond 0:137634ff4186 314 nb = strlen( label );
ansond 0:137634ff4186 315 memcpy( tmp + 48, label, nb );
ansond 0:137634ff4186 316 memcpy( tmp + 48 + nb, random, rlen );
ansond 0:137634ff4186 317 nb += rlen;
ansond 0:137634ff4186 318
ansond 0:137634ff4186 319 /*
ansond 0:137634ff4186 320 * Compute P_<hash>(secret, label + random)[0..dlen]
ansond 0:137634ff4186 321 */
ansond 0:137634ff4186 322 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
ansond 0:137634ff4186 323
ansond 0:137634ff4186 324 for( i = 0; i < dlen; i += 48 )
ansond 0:137634ff4186 325 {
ansond 0:137634ff4186 326 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
ansond 0:137634ff4186 327 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
ansond 0:137634ff4186 328
ansond 0:137634ff4186 329 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
ansond 0:137634ff4186 330
ansond 0:137634ff4186 331 for( j = 0; j < k; j++ )
ansond 0:137634ff4186 332 dstbuf[i + j] = h_i[j];
ansond 0:137634ff4186 333 }
ansond 0:137634ff4186 334
ansond 0:137634ff4186 335 polarssl_zeroize( tmp, sizeof( tmp ) );
ansond 0:137634ff4186 336 polarssl_zeroize( h_i, sizeof( h_i ) );
ansond 0:137634ff4186 337
ansond 0:137634ff4186 338 return( 0 );
ansond 0:137634ff4186 339 }
ansond 0:137634ff4186 340 #endif /* POLARSSL_SHA512_C */
ansond 0:137634ff4186 341 #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
ansond 0:137634ff4186 342
ansond 0:137634ff4186 343 static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
ansond 0:137634ff4186 344
ansond 0:137634ff4186 345 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
ansond 0:137634ff4186 346 defined(POLARSSL_SSL_PROTO_TLS1_1)
ansond 0:137634ff4186 347 static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
ansond 0:137634ff4186 348 #endif
ansond 0:137634ff4186 349
ansond 0:137634ff4186 350 #if defined(POLARSSL_SSL_PROTO_SSL3)
ansond 0:137634ff4186 351 static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
ansond 0:137634ff4186 352 static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
ansond 0:137634ff4186 353 #endif
ansond 0:137634ff4186 354
ansond 0:137634ff4186 355 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
ansond 0:137634ff4186 356 static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
ansond 0:137634ff4186 357 static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
ansond 0:137634ff4186 358 #endif
ansond 0:137634ff4186 359
ansond 0:137634ff4186 360 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
ansond 0:137634ff4186 361 #if defined(POLARSSL_SHA256_C)
ansond 0:137634ff4186 362 static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
ansond 0:137634ff4186 363 static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
ansond 0:137634ff4186 364 static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
ansond 0:137634ff4186 365 #endif
ansond 0:137634ff4186 366
ansond 0:137634ff4186 367 #if defined(POLARSSL_SHA512_C)
ansond 0:137634ff4186 368 static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
ansond 0:137634ff4186 369 static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
ansond 0:137634ff4186 370 static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
ansond 0:137634ff4186 371 #endif
ansond 0:137634ff4186 372 #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
ansond 0:137634ff4186 373
ansond 0:137634ff4186 374 int ssl_derive_keys( ssl_context *ssl )
ansond 0:137634ff4186 375 {
ansond 0:137634ff4186 376 int ret = 0;
ansond 0:137634ff4186 377 unsigned char tmp[64];
ansond 0:137634ff4186 378 unsigned char keyblk[256];
ansond 0:137634ff4186 379 unsigned char *key1;
ansond 0:137634ff4186 380 unsigned char *key2;
ansond 0:137634ff4186 381 unsigned char *mac_enc;
ansond 0:137634ff4186 382 unsigned char *mac_dec;
ansond 0:137634ff4186 383 size_t iv_copy_len;
ansond 0:137634ff4186 384 const cipher_info_t *cipher_info;
ansond 0:137634ff4186 385 const md_info_t *md_info;
ansond 0:137634ff4186 386
ansond 0:137634ff4186 387 ssl_session *session = ssl->session_negotiate;
ansond 0:137634ff4186 388 ssl_transform *transform = ssl->transform_negotiate;
ansond 0:137634ff4186 389 ssl_handshake_params *handshake = ssl->handshake;
ansond 0:137634ff4186 390
ansond 0:137634ff4186 391 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
ansond 0:137634ff4186 392
ansond 0:137634ff4186 393 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
ansond 0:137634ff4186 394 if( cipher_info == NULL )
ansond 0:137634ff4186 395 {
ansond 0:137634ff4186 396 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
ansond 0:137634ff4186 397 transform->ciphersuite_info->cipher ) );
ansond 0:137634ff4186 398 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
ansond 0:137634ff4186 399 }
ansond 0:137634ff4186 400
ansond 0:137634ff4186 401 md_info = md_info_from_type( transform->ciphersuite_info->mac );
ansond 0:137634ff4186 402 if( md_info == NULL )
ansond 0:137634ff4186 403 {
ansond 0:137634ff4186 404 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
ansond 0:137634ff4186 405 transform->ciphersuite_info->mac ) );
ansond 0:137634ff4186 406 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
ansond 0:137634ff4186 407 }
ansond 0:137634ff4186 408
ansond 0:137634ff4186 409 /*
ansond 0:137634ff4186 410 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
ansond 0:137634ff4186 411 */
ansond 0:137634ff4186 412 #if defined(POLARSSL_SSL_PROTO_SSL3)
ansond 0:137634ff4186 413 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
ansond 0:137634ff4186 414 {
ansond 0:137634ff4186 415 handshake->tls_prf = ssl3_prf;
ansond 0:137634ff4186 416 handshake->calc_verify = ssl_calc_verify_ssl;
ansond 0:137634ff4186 417 handshake->calc_finished = ssl_calc_finished_ssl;
ansond 0:137634ff4186 418 }
ansond 0:137634ff4186 419 else
ansond 0:137634ff4186 420 #endif
ansond 0:137634ff4186 421 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
ansond 0:137634ff4186 422 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
ansond 0:137634ff4186 423 {
ansond 0:137634ff4186 424 handshake->tls_prf = tls1_prf;
ansond 0:137634ff4186 425 handshake->calc_verify = ssl_calc_verify_tls;
ansond 0:137634ff4186 426 handshake->calc_finished = ssl_calc_finished_tls;
ansond 0:137634ff4186 427 }
ansond 0:137634ff4186 428 else
ansond 0:137634ff4186 429 #endif
ansond 0:137634ff4186 430 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
ansond 0:137634ff4186 431 #if defined(POLARSSL_SHA512_C)
ansond 0:137634ff4186 432 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
ansond 0:137634ff4186 433 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
ansond 0:137634ff4186 434 {
ansond 0:137634ff4186 435 handshake->tls_prf = tls_prf_sha384;
ansond 0:137634ff4186 436 handshake->calc_verify = ssl_calc_verify_tls_sha384;
ansond 0:137634ff4186 437 handshake->calc_finished = ssl_calc_finished_tls_sha384;
ansond 0:137634ff4186 438 }
ansond 0:137634ff4186 439 else
ansond 0:137634ff4186 440 #endif
ansond 0:137634ff4186 441 #if defined(POLARSSL_SHA256_C)
ansond 0:137634ff4186 442 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
ansond 0:137634ff4186 443 {
ansond 0:137634ff4186 444 handshake->tls_prf = tls_prf_sha256;
ansond 0:137634ff4186 445 handshake->calc_verify = ssl_calc_verify_tls_sha256;
ansond 0:137634ff4186 446 handshake->calc_finished = ssl_calc_finished_tls_sha256;
ansond 0:137634ff4186 447 }
ansond 0:137634ff4186 448 else
ansond 0:137634ff4186 449 #endif
ansond 0:137634ff4186 450 #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
ansond 0:137634ff4186 451 {
ansond 0:137634ff4186 452 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 453 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 454 }
ansond 0:137634ff4186 455
ansond 0:137634ff4186 456 /*
ansond 0:137634ff4186 457 * SSLv3:
ansond 0:137634ff4186 458 * master =
ansond 0:137634ff4186 459 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
ansond 0:137634ff4186 460 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
ansond 0:137634ff4186 461 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
ansond 0:137634ff4186 462 *
ansond 0:137634ff4186 463 * TLSv1+:
ansond 0:137634ff4186 464 * master = PRF( premaster, "master secret", randbytes )[0..47]
ansond 0:137634ff4186 465 */
ansond 0:137634ff4186 466 if( handshake->resume == 0 )
ansond 0:137634ff4186 467 {
ansond 0:137634ff4186 468 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
ansond 0:137634ff4186 469 handshake->pmslen );
ansond 0:137634ff4186 470
ansond 0:137634ff4186 471 #if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
ansond 0:137634ff4186 472 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
ansond 0:137634ff4186 473 {
ansond 0:137634ff4186 474 unsigned char session_hash[48];
ansond 0:137634ff4186 475 size_t hash_len;
ansond 0:137634ff4186 476
ansond 0:137634ff4186 477 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
ansond 0:137634ff4186 478
ansond 0:137634ff4186 479 ssl->handshake->calc_verify( ssl, session_hash );
ansond 0:137634ff4186 480
ansond 0:137634ff4186 481 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
ansond 0:137634ff4186 482 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
ansond 0:137634ff4186 483 {
ansond 0:137634ff4186 484 #if defined(POLARSSL_SHA512_C)
ansond 0:137634ff4186 485 if( ssl->transform_negotiate->ciphersuite_info->mac ==
ansond 0:137634ff4186 486 POLARSSL_MD_SHA384 )
ansond 0:137634ff4186 487 {
ansond 0:137634ff4186 488 hash_len = 48;
ansond 0:137634ff4186 489 }
ansond 0:137634ff4186 490 else
ansond 0:137634ff4186 491 #endif
ansond 0:137634ff4186 492 hash_len = 32;
ansond 0:137634ff4186 493 }
ansond 0:137634ff4186 494 else
ansond 0:137634ff4186 495 #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
ansond 0:137634ff4186 496 hash_len = 36;
ansond 0:137634ff4186 497
ansond 0:137634ff4186 498 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
ansond 0:137634ff4186 499
ansond 0:137634ff4186 500 handshake->tls_prf( handshake->premaster, handshake->pmslen,
ansond 0:137634ff4186 501 "extended master secret",
ansond 0:137634ff4186 502 session_hash, hash_len, session->master, 48 );
ansond 0:137634ff4186 503
ansond 0:137634ff4186 504 }
ansond 0:137634ff4186 505 else
ansond 0:137634ff4186 506 #endif
ansond 0:137634ff4186 507 handshake->tls_prf( handshake->premaster, handshake->pmslen,
ansond 0:137634ff4186 508 "master secret",
ansond 0:137634ff4186 509 handshake->randbytes, 64, session->master, 48 );
ansond 0:137634ff4186 510
ansond 0:137634ff4186 511
ansond 0:137634ff4186 512 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
ansond 0:137634ff4186 513 }
ansond 0:137634ff4186 514 else
ansond 0:137634ff4186 515 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
ansond 0:137634ff4186 516
ansond 0:137634ff4186 517 /*
ansond 0:137634ff4186 518 * Swap the client and server random values.
ansond 0:137634ff4186 519 */
ansond 0:137634ff4186 520 memcpy( tmp, handshake->randbytes, 64 );
ansond 0:137634ff4186 521 memcpy( handshake->randbytes, tmp + 32, 32 );
ansond 0:137634ff4186 522 memcpy( handshake->randbytes + 32, tmp, 32 );
ansond 0:137634ff4186 523 polarssl_zeroize( tmp, sizeof( tmp ) );
ansond 0:137634ff4186 524
ansond 0:137634ff4186 525 /*
ansond 0:137634ff4186 526 * SSLv3:
ansond 0:137634ff4186 527 * key block =
ansond 0:137634ff4186 528 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
ansond 0:137634ff4186 529 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
ansond 0:137634ff4186 530 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
ansond 0:137634ff4186 531 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
ansond 0:137634ff4186 532 * ...
ansond 0:137634ff4186 533 *
ansond 0:137634ff4186 534 * TLSv1:
ansond 0:137634ff4186 535 * key block = PRF( master, "key expansion", randbytes )
ansond 0:137634ff4186 536 */
ansond 0:137634ff4186 537 handshake->tls_prf( session->master, 48, "key expansion",
ansond 0:137634ff4186 538 handshake->randbytes, 64, keyblk, 256 );
ansond 0:137634ff4186 539
ansond 0:137634ff4186 540 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
ansond 0:137634ff4186 541 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
ansond 0:137634ff4186 542 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
ansond 0:137634ff4186 543 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
ansond 0:137634ff4186 544 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
ansond 0:137634ff4186 545
ansond 0:137634ff4186 546 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
ansond 0:137634ff4186 547
ansond 0:137634ff4186 548 /*
ansond 0:137634ff4186 549 * Determine the appropriate key, IV and MAC length.
ansond 0:137634ff4186 550 */
ansond 0:137634ff4186 551
ansond 0:137634ff4186 552 transform->keylen = cipher_info->key_length / 8;
ansond 0:137634ff4186 553
ansond 0:137634ff4186 554 if( cipher_info->mode == POLARSSL_MODE_GCM ||
ansond 0:137634ff4186 555 cipher_info->mode == POLARSSL_MODE_CCM )
ansond 0:137634ff4186 556 {
ansond 0:137634ff4186 557 transform->maclen = 0;
ansond 0:137634ff4186 558
ansond 0:137634ff4186 559 transform->ivlen = 12;
ansond 0:137634ff4186 560 transform->fixed_ivlen = 4;
ansond 0:137634ff4186 561
ansond 0:137634ff4186 562 /* Minimum length is expicit IV + tag */
ansond 0:137634ff4186 563 transform->minlen = transform->ivlen - transform->fixed_ivlen
ansond 0:137634ff4186 564 + ( transform->ciphersuite_info->flags &
ansond 0:137634ff4186 565 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
ansond 0:137634ff4186 566 }
ansond 0:137634ff4186 567 else
ansond 0:137634ff4186 568 {
ansond 0:137634ff4186 569 int ret;
ansond 0:137634ff4186 570
ansond 0:137634ff4186 571 /* Initialize HMAC contexts */
ansond 0:137634ff4186 572 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
ansond 0:137634ff4186 573 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
ansond 0:137634ff4186 574 {
ansond 0:137634ff4186 575 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
ansond 0:137634ff4186 576 return( ret );
ansond 0:137634ff4186 577 }
ansond 0:137634ff4186 578
ansond 0:137634ff4186 579 /* Get MAC length */
ansond 0:137634ff4186 580 transform->maclen = md_get_size( md_info );
ansond 0:137634ff4186 581
ansond 0:137634ff4186 582 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
ansond 0:137634ff4186 583 /*
ansond 0:137634ff4186 584 * If HMAC is to be truncated, we shall keep the leftmost bytes,
ansond 0:137634ff4186 585 * (rfc 6066 page 13 or rfc 2104 section 4),
ansond 0:137634ff4186 586 * so we only need to adjust the length here.
ansond 0:137634ff4186 587 */
ansond 0:137634ff4186 588 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
ansond 0:137634ff4186 589 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
ansond 0:137634ff4186 590 #endif /* POLARSSL_SSL_TRUNCATED_HMAC */
ansond 0:137634ff4186 591
ansond 0:137634ff4186 592 /* IV length */
ansond 0:137634ff4186 593 transform->ivlen = cipher_info->iv_size;
ansond 0:137634ff4186 594
ansond 0:137634ff4186 595 /* Minimum length */
ansond 0:137634ff4186 596 if( cipher_info->mode == POLARSSL_MODE_STREAM )
ansond 0:137634ff4186 597 transform->minlen = transform->maclen;
ansond 0:137634ff4186 598 else
ansond 0:137634ff4186 599 {
ansond 0:137634ff4186 600 /*
ansond 0:137634ff4186 601 * GenericBlockCipher:
ansond 0:137634ff4186 602 * 1. if EtM is in use: one block plus MAC
ansond 0:137634ff4186 603 * otherwise: * first multiple of blocklen greater than maclen
ansond 0:137634ff4186 604 * 2. IV except for SSL3 and TLS 1.0
ansond 0:137634ff4186 605 */
ansond 0:137634ff4186 606 #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
ansond 0:137634ff4186 607 if( session->encrypt_then_mac == SSL_ETM_ENABLED )
ansond 0:137634ff4186 608 {
ansond 0:137634ff4186 609 transform->minlen = transform->maclen
ansond 0:137634ff4186 610 + cipher_info->block_size;
ansond 0:137634ff4186 611 }
ansond 0:137634ff4186 612 else
ansond 0:137634ff4186 613 #endif
ansond 0:137634ff4186 614 {
ansond 0:137634ff4186 615 transform->minlen = transform->maclen
ansond 0:137634ff4186 616 + cipher_info->block_size
ansond 0:137634ff4186 617 - transform->maclen % cipher_info->block_size;
ansond 0:137634ff4186 618 }
ansond 0:137634ff4186 619
ansond 0:137634ff4186 620 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
ansond 0:137634ff4186 621 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
ansond 0:137634ff4186 622 ssl->minor_ver == SSL_MINOR_VERSION_1 )
ansond 0:137634ff4186 623 ; /* No need to adjust minlen */
ansond 0:137634ff4186 624 else
ansond 0:137634ff4186 625 #endif
ansond 0:137634ff4186 626 #if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
ansond 0:137634ff4186 627 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
ansond 0:137634ff4186 628 ssl->minor_ver == SSL_MINOR_VERSION_3 )
ansond 0:137634ff4186 629 {
ansond 0:137634ff4186 630 transform->minlen += transform->ivlen;
ansond 0:137634ff4186 631 }
ansond 0:137634ff4186 632 else
ansond 0:137634ff4186 633 #endif
ansond 0:137634ff4186 634 {
ansond 0:137634ff4186 635 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 636 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 637 }
ansond 0:137634ff4186 638 }
ansond 0:137634ff4186 639 }
ansond 0:137634ff4186 640
ansond 0:137634ff4186 641 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
ansond 0:137634ff4186 642 transform->keylen, transform->minlen, transform->ivlen,
ansond 0:137634ff4186 643 transform->maclen ) );
ansond 0:137634ff4186 644
ansond 0:137634ff4186 645 /*
ansond 0:137634ff4186 646 * Finally setup the cipher contexts, IVs and MAC secrets.
ansond 0:137634ff4186 647 */
ansond 0:137634ff4186 648 #if defined(POLARSSL_SSL_CLI_C)
ansond 0:137634ff4186 649 if( ssl->endpoint == SSL_IS_CLIENT )
ansond 0:137634ff4186 650 {
ansond 0:137634ff4186 651 key1 = keyblk + transform->maclen * 2;
ansond 0:137634ff4186 652 key2 = keyblk + transform->maclen * 2 + transform->keylen;
ansond 0:137634ff4186 653
ansond 0:137634ff4186 654 mac_enc = keyblk;
ansond 0:137634ff4186 655 mac_dec = keyblk + transform->maclen;
ansond 0:137634ff4186 656
ansond 0:137634ff4186 657 /*
ansond 0:137634ff4186 658 * This is not used in TLS v1.1.
ansond 0:137634ff4186 659 */
ansond 0:137634ff4186 660 iv_copy_len = ( transform->fixed_ivlen ) ?
ansond 0:137634ff4186 661 transform->fixed_ivlen : transform->ivlen;
ansond 0:137634ff4186 662 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
ansond 0:137634ff4186 663 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
ansond 0:137634ff4186 664 iv_copy_len );
ansond 0:137634ff4186 665 }
ansond 0:137634ff4186 666 else
ansond 0:137634ff4186 667 #endif /* POLARSSL_SSL_CLI_C */
ansond 0:137634ff4186 668 #if defined(POLARSSL_SSL_SRV_C)
ansond 0:137634ff4186 669 if( ssl->endpoint == SSL_IS_SERVER )
ansond 0:137634ff4186 670 {
ansond 0:137634ff4186 671 key1 = keyblk + transform->maclen * 2 + transform->keylen;
ansond 0:137634ff4186 672 key2 = keyblk + transform->maclen * 2;
ansond 0:137634ff4186 673
ansond 0:137634ff4186 674 mac_enc = keyblk + transform->maclen;
ansond 0:137634ff4186 675 mac_dec = keyblk;
ansond 0:137634ff4186 676
ansond 0:137634ff4186 677 /*
ansond 0:137634ff4186 678 * This is not used in TLS v1.1.
ansond 0:137634ff4186 679 */
ansond 0:137634ff4186 680 iv_copy_len = ( transform->fixed_ivlen ) ?
ansond 0:137634ff4186 681 transform->fixed_ivlen : transform->ivlen;
ansond 0:137634ff4186 682 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
ansond 0:137634ff4186 683 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
ansond 0:137634ff4186 684 iv_copy_len );
ansond 0:137634ff4186 685 }
ansond 0:137634ff4186 686 else
ansond 0:137634ff4186 687 #endif /* POLARSSL_SSL_SRV_C */
ansond 0:137634ff4186 688 {
ansond 0:137634ff4186 689 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 690 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 691 }
ansond 0:137634ff4186 692
ansond 0:137634ff4186 693 #if defined(POLARSSL_SSL_PROTO_SSL3)
ansond 0:137634ff4186 694 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
ansond 0:137634ff4186 695 {
ansond 0:137634ff4186 696 if( transform->maclen > sizeof transform->mac_enc )
ansond 0:137634ff4186 697 {
ansond 0:137634ff4186 698 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 699 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 700 }
ansond 0:137634ff4186 701
ansond 0:137634ff4186 702 memcpy( transform->mac_enc, mac_enc, transform->maclen );
ansond 0:137634ff4186 703 memcpy( transform->mac_dec, mac_dec, transform->maclen );
ansond 0:137634ff4186 704 }
ansond 0:137634ff4186 705 else
ansond 0:137634ff4186 706 #endif /* POLARSSL_SSL_PROTO_SSL3 */
ansond 0:137634ff4186 707 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
ansond 0:137634ff4186 708 defined(POLARSSL_SSL_PROTO_TLS1_2)
ansond 0:137634ff4186 709 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
ansond 0:137634ff4186 710 {
ansond 0:137634ff4186 711 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
ansond 0:137634ff4186 712 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
ansond 0:137634ff4186 713 }
ansond 0:137634ff4186 714 else
ansond 0:137634ff4186 715 #endif
ansond 0:137634ff4186 716 {
ansond 0:137634ff4186 717 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 718 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 719 }
ansond 0:137634ff4186 720
ansond 0:137634ff4186 721 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
ansond 0:137634ff4186 722 if( ssl_hw_record_init != NULL )
ansond 0:137634ff4186 723 {
ansond 0:137634ff4186 724 int ret = 0;
ansond 0:137634ff4186 725
ansond 0:137634ff4186 726 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
ansond 0:137634ff4186 727
ansond 0:137634ff4186 728 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
ansond 0:137634ff4186 729 transform->iv_enc, transform->iv_dec,
ansond 0:137634ff4186 730 iv_copy_len,
ansond 0:137634ff4186 731 mac_enc, mac_dec,
ansond 0:137634ff4186 732 transform->maclen ) ) != 0 )
ansond 0:137634ff4186 733 {
ansond 0:137634ff4186 734 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
ansond 0:137634ff4186 735 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
ansond 0:137634ff4186 736 }
ansond 0:137634ff4186 737 }
ansond 0:137634ff4186 738 #endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
ansond 0:137634ff4186 739
ansond 0:137634ff4186 740 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
ansond 0:137634ff4186 741 cipher_info ) ) != 0 )
ansond 0:137634ff4186 742 {
ansond 0:137634ff4186 743 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
ansond 0:137634ff4186 744 return( ret );
ansond 0:137634ff4186 745 }
ansond 0:137634ff4186 746
ansond 0:137634ff4186 747 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
ansond 0:137634ff4186 748 cipher_info ) ) != 0 )
ansond 0:137634ff4186 749 {
ansond 0:137634ff4186 750 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
ansond 0:137634ff4186 751 return( ret );
ansond 0:137634ff4186 752 }
ansond 0:137634ff4186 753
ansond 0:137634ff4186 754 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
ansond 0:137634ff4186 755 cipher_info->key_length,
ansond 0:137634ff4186 756 POLARSSL_ENCRYPT ) ) != 0 )
ansond 0:137634ff4186 757 {
ansond 0:137634ff4186 758 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
ansond 0:137634ff4186 759 return( ret );
ansond 0:137634ff4186 760 }
ansond 0:137634ff4186 761
ansond 0:137634ff4186 762 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
ansond 0:137634ff4186 763 cipher_info->key_length,
ansond 0:137634ff4186 764 POLARSSL_DECRYPT ) ) != 0 )
ansond 0:137634ff4186 765 {
ansond 0:137634ff4186 766 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
ansond 0:137634ff4186 767 return( ret );
ansond 0:137634ff4186 768 }
ansond 0:137634ff4186 769
ansond 0:137634ff4186 770 #if defined(POLARSSL_CIPHER_MODE_CBC)
ansond 0:137634ff4186 771 if( cipher_info->mode == POLARSSL_MODE_CBC )
ansond 0:137634ff4186 772 {
ansond 0:137634ff4186 773 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
ansond 0:137634ff4186 774 POLARSSL_PADDING_NONE ) ) != 0 )
ansond 0:137634ff4186 775 {
ansond 0:137634ff4186 776 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
ansond 0:137634ff4186 777 return( ret );
ansond 0:137634ff4186 778 }
ansond 0:137634ff4186 779
ansond 0:137634ff4186 780 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
ansond 0:137634ff4186 781 POLARSSL_PADDING_NONE ) ) != 0 )
ansond 0:137634ff4186 782 {
ansond 0:137634ff4186 783 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
ansond 0:137634ff4186 784 return( ret );
ansond 0:137634ff4186 785 }
ansond 0:137634ff4186 786 }
ansond 0:137634ff4186 787 #endif /* POLARSSL_CIPHER_MODE_CBC */
ansond 0:137634ff4186 788
ansond 0:137634ff4186 789 polarssl_zeroize( keyblk, sizeof( keyblk ) );
ansond 0:137634ff4186 790
ansond 0:137634ff4186 791 #if defined(POLARSSL_ZLIB_SUPPORT)
ansond 0:137634ff4186 792 // Initialize compression
ansond 0:137634ff4186 793 //
ansond 0:137634ff4186 794 if( session->compression == SSL_COMPRESS_DEFLATE )
ansond 0:137634ff4186 795 {
ansond 0:137634ff4186 796 if( ssl->compress_buf == NULL )
ansond 0:137634ff4186 797 {
ansond 0:137634ff4186 798 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
ansond 0:137634ff4186 799 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
ansond 0:137634ff4186 800 if( ssl->compress_buf == NULL )
ansond 0:137634ff4186 801 {
ansond 0:137634ff4186 802 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
ansond 0:137634ff4186 803 SSL_BUFFER_LEN ) );
ansond 0:137634ff4186 804 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
ansond 0:137634ff4186 805 }
ansond 0:137634ff4186 806 }
ansond 0:137634ff4186 807
ansond 0:137634ff4186 808 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
ansond 0:137634ff4186 809
ansond 0:137634ff4186 810 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
ansond 0:137634ff4186 811 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
ansond 0:137634ff4186 812
ansond 0:137634ff4186 813 if( deflateInit( &transform->ctx_deflate,
ansond 0:137634ff4186 814 Z_DEFAULT_COMPRESSION ) != Z_OK ||
ansond 0:137634ff4186 815 inflateInit( &transform->ctx_inflate ) != Z_OK )
ansond 0:137634ff4186 816 {
ansond 0:137634ff4186 817 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
ansond 0:137634ff4186 818 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
ansond 0:137634ff4186 819 }
ansond 0:137634ff4186 820 }
ansond 0:137634ff4186 821 #endif /* POLARSSL_ZLIB_SUPPORT */
ansond 0:137634ff4186 822
ansond 0:137634ff4186 823 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
ansond 0:137634ff4186 824
ansond 0:137634ff4186 825 return( 0 );
ansond 0:137634ff4186 826 }
ansond 0:137634ff4186 827
ansond 0:137634ff4186 828 #if defined(POLARSSL_SSL_PROTO_SSL3)
ansond 0:137634ff4186 829 void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
ansond 0:137634ff4186 830 {
ansond 0:137634ff4186 831 md5_context md5;
ansond 0:137634ff4186 832 sha1_context sha1;
ansond 0:137634ff4186 833 unsigned char pad_1[48];
ansond 0:137634ff4186 834 unsigned char pad_2[48];
ansond 0:137634ff4186 835
ansond 0:137634ff4186 836 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
ansond 0:137634ff4186 837
ansond 0:137634ff4186 838 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
ansond 0:137634ff4186 839 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
ansond 0:137634ff4186 840
ansond 0:137634ff4186 841 memset( pad_1, 0x36, 48 );
ansond 0:137634ff4186 842 memset( pad_2, 0x5C, 48 );
ansond 0:137634ff4186 843
ansond 0:137634ff4186 844 md5_update( &md5, ssl->session_negotiate->master, 48 );
ansond 0:137634ff4186 845 md5_update( &md5, pad_1, 48 );
ansond 0:137634ff4186 846 md5_finish( &md5, hash );
ansond 0:137634ff4186 847
ansond 0:137634ff4186 848 md5_starts( &md5 );
ansond 0:137634ff4186 849 md5_update( &md5, ssl->session_negotiate->master, 48 );
ansond 0:137634ff4186 850 md5_update( &md5, pad_2, 48 );
ansond 0:137634ff4186 851 md5_update( &md5, hash, 16 );
ansond 0:137634ff4186 852 md5_finish( &md5, hash );
ansond 0:137634ff4186 853
ansond 0:137634ff4186 854 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
ansond 0:137634ff4186 855 sha1_update( &sha1, pad_1, 40 );
ansond 0:137634ff4186 856 sha1_finish( &sha1, hash + 16 );
ansond 0:137634ff4186 857
ansond 0:137634ff4186 858 sha1_starts( &sha1 );
ansond 0:137634ff4186 859 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
ansond 0:137634ff4186 860 sha1_update( &sha1, pad_2, 40 );
ansond 0:137634ff4186 861 sha1_update( &sha1, hash + 16, 20 );
ansond 0:137634ff4186 862 sha1_finish( &sha1, hash + 16 );
ansond 0:137634ff4186 863
ansond 0:137634ff4186 864 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
ansond 0:137634ff4186 865 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
ansond 0:137634ff4186 866
ansond 0:137634ff4186 867 md5_free( &md5 );
ansond 0:137634ff4186 868 sha1_free( &sha1 );
ansond 0:137634ff4186 869
ansond 0:137634ff4186 870 return;
ansond 0:137634ff4186 871 }
ansond 0:137634ff4186 872 #endif /* POLARSSL_SSL_PROTO_SSL3 */
ansond 0:137634ff4186 873
ansond 0:137634ff4186 874 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
ansond 0:137634ff4186 875 void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
ansond 0:137634ff4186 876 {
ansond 0:137634ff4186 877 md5_context md5;
ansond 0:137634ff4186 878 sha1_context sha1;
ansond 0:137634ff4186 879
ansond 0:137634ff4186 880 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
ansond 0:137634ff4186 881
ansond 0:137634ff4186 882 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
ansond 0:137634ff4186 883 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
ansond 0:137634ff4186 884
ansond 0:137634ff4186 885 md5_finish( &md5, hash );
ansond 0:137634ff4186 886 sha1_finish( &sha1, hash + 16 );
ansond 0:137634ff4186 887
ansond 0:137634ff4186 888 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
ansond 0:137634ff4186 889 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
ansond 0:137634ff4186 890
ansond 0:137634ff4186 891 md5_free( &md5 );
ansond 0:137634ff4186 892 sha1_free( &sha1 );
ansond 0:137634ff4186 893
ansond 0:137634ff4186 894 return;
ansond 0:137634ff4186 895 }
ansond 0:137634ff4186 896 #endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
ansond 0:137634ff4186 897
ansond 0:137634ff4186 898 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
ansond 0:137634ff4186 899 #if defined(POLARSSL_SHA256_C)
ansond 0:137634ff4186 900 void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
ansond 0:137634ff4186 901 {
ansond 0:137634ff4186 902 sha256_context sha256;
ansond 0:137634ff4186 903
ansond 0:137634ff4186 904 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
ansond 0:137634ff4186 905
ansond 0:137634ff4186 906 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
ansond 0:137634ff4186 907 sha256_finish( &sha256, hash );
ansond 0:137634ff4186 908
ansond 0:137634ff4186 909 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
ansond 0:137634ff4186 910 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
ansond 0:137634ff4186 911
ansond 0:137634ff4186 912 sha256_free( &sha256 );
ansond 0:137634ff4186 913
ansond 0:137634ff4186 914 return;
ansond 0:137634ff4186 915 }
ansond 0:137634ff4186 916 #endif /* POLARSSL_SHA256_C */
ansond 0:137634ff4186 917
ansond 0:137634ff4186 918 #if defined(POLARSSL_SHA512_C)
ansond 0:137634ff4186 919 void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
ansond 0:137634ff4186 920 {
ansond 0:137634ff4186 921 sha512_context sha512;
ansond 0:137634ff4186 922
ansond 0:137634ff4186 923 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
ansond 0:137634ff4186 924
ansond 0:137634ff4186 925 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
ansond 0:137634ff4186 926 sha512_finish( &sha512, hash );
ansond 0:137634ff4186 927
ansond 0:137634ff4186 928 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
ansond 0:137634ff4186 929 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
ansond 0:137634ff4186 930
ansond 0:137634ff4186 931 sha512_free( &sha512 );
ansond 0:137634ff4186 932
ansond 0:137634ff4186 933 return;
ansond 0:137634ff4186 934 }
ansond 0:137634ff4186 935 #endif /* POLARSSL_SHA512_C */
ansond 0:137634ff4186 936 #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
ansond 0:137634ff4186 937
ansond 0:137634ff4186 938 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
ansond 0:137634ff4186 939 int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
ansond 0:137634ff4186 940 {
ansond 0:137634ff4186 941 unsigned char *p = ssl->handshake->premaster;
ansond 0:137634ff4186 942 unsigned char *end = p + sizeof( ssl->handshake->premaster );
ansond 0:137634ff4186 943
ansond 0:137634ff4186 944 /*
ansond 0:137634ff4186 945 * PMS = struct {
ansond 0:137634ff4186 946 * opaque other_secret<0..2^16-1>;
ansond 0:137634ff4186 947 * opaque psk<0..2^16-1>;
ansond 0:137634ff4186 948 * };
ansond 0:137634ff4186 949 * with "other_secret" depending on the particular key exchange
ansond 0:137634ff4186 950 */
ansond 0:137634ff4186 951 #if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
ansond 0:137634ff4186 952 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
ansond 0:137634ff4186 953 {
ansond 0:137634ff4186 954 if( end - p < 2 + (int) ssl->psk_len )
ansond 0:137634ff4186 955 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
ansond 0:137634ff4186 956
ansond 0:137634ff4186 957 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
ansond 0:137634ff4186 958 *(p++) = (unsigned char)( ssl->psk_len );
ansond 0:137634ff4186 959 p += ssl->psk_len;
ansond 0:137634ff4186 960 }
ansond 0:137634ff4186 961 else
ansond 0:137634ff4186 962 #endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
ansond 0:137634ff4186 963 #if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
ansond 0:137634ff4186 964 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
ansond 0:137634ff4186 965 {
ansond 0:137634ff4186 966 /*
ansond 0:137634ff4186 967 * other_secret already set by the ClientKeyExchange message,
ansond 0:137634ff4186 968 * and is 48 bytes long
ansond 0:137634ff4186 969 */
ansond 0:137634ff4186 970 *p++ = 0;
ansond 0:137634ff4186 971 *p++ = 48;
ansond 0:137634ff4186 972 p += 48;
ansond 0:137634ff4186 973 }
ansond 0:137634ff4186 974 else
ansond 0:137634ff4186 975 #endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
ansond 0:137634ff4186 976 #if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
ansond 0:137634ff4186 977 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
ansond 0:137634ff4186 978 {
ansond 0:137634ff4186 979 int ret;
ansond 0:137634ff4186 980 size_t len = end - ( p + 2 );
ansond 0:137634ff4186 981
ansond 0:137634ff4186 982 /* Write length only when we know the actual value */
ansond 0:137634ff4186 983 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
ansond 0:137634ff4186 984 p + 2, &len,
ansond 0:137634ff4186 985 ssl->f_rng, ssl->p_rng ) ) != 0 )
ansond 0:137634ff4186 986 {
ansond 0:137634ff4186 987 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
ansond 0:137634ff4186 988 return( ret );
ansond 0:137634ff4186 989 }
ansond 0:137634ff4186 990 *(p++) = (unsigned char)( len >> 8 );
ansond 0:137634ff4186 991 *(p++) = (unsigned char)( len );
ansond 0:137634ff4186 992 p += len;
ansond 0:137634ff4186 993
ansond 0:137634ff4186 994 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
ansond 0:137634ff4186 995 }
ansond 0:137634ff4186 996 else
ansond 0:137634ff4186 997 #endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
ansond 0:137634ff4186 998 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
ansond 0:137634ff4186 999 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
ansond 0:137634ff4186 1000 {
ansond 0:137634ff4186 1001 int ret;
ansond 0:137634ff4186 1002 size_t zlen;
ansond 0:137634ff4186 1003
ansond 0:137634ff4186 1004 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
ansond 0:137634ff4186 1005 p + 2, end - ( p + 2 ),
ansond 0:137634ff4186 1006 ssl->f_rng, ssl->p_rng ) ) != 0 )
ansond 0:137634ff4186 1007 {
ansond 0:137634ff4186 1008 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
ansond 0:137634ff4186 1009 return( ret );
ansond 0:137634ff4186 1010 }
ansond 0:137634ff4186 1011
ansond 0:137634ff4186 1012 *(p++) = (unsigned char)( zlen >> 8 );
ansond 0:137634ff4186 1013 *(p++) = (unsigned char)( zlen );
ansond 0:137634ff4186 1014 p += zlen;
ansond 0:137634ff4186 1015
ansond 0:137634ff4186 1016 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
ansond 0:137634ff4186 1017 }
ansond 0:137634ff4186 1018 else
ansond 0:137634ff4186 1019 #endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
ansond 0:137634ff4186 1020 {
ansond 0:137634ff4186 1021 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 1022 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 1023 }
ansond 0:137634ff4186 1024
ansond 0:137634ff4186 1025 /* opaque psk<0..2^16-1>; */
ansond 0:137634ff4186 1026 if( end - p < 2 + (int) ssl->psk_len )
ansond 0:137634ff4186 1027 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
ansond 0:137634ff4186 1028
ansond 0:137634ff4186 1029 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
ansond 0:137634ff4186 1030 *(p++) = (unsigned char)( ssl->psk_len );
ansond 0:137634ff4186 1031 memcpy( p, ssl->psk, ssl->psk_len );
ansond 0:137634ff4186 1032 p += ssl->psk_len;
ansond 0:137634ff4186 1033
ansond 0:137634ff4186 1034 ssl->handshake->pmslen = p - ssl->handshake->premaster;
ansond 0:137634ff4186 1035
ansond 0:137634ff4186 1036 return( 0 );
ansond 0:137634ff4186 1037 }
ansond 0:137634ff4186 1038 #endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
ansond 0:137634ff4186 1039
ansond 0:137634ff4186 1040 #if defined(POLARSSL_SSL_PROTO_SSL3)
ansond 0:137634ff4186 1041 /*
ansond 0:137634ff4186 1042 * SSLv3.0 MAC functions
ansond 0:137634ff4186 1043 */
ansond 0:137634ff4186 1044 static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
ansond 0:137634ff4186 1045 unsigned char *buf, size_t len,
ansond 0:137634ff4186 1046 unsigned char *ctr, int type )
ansond 0:137634ff4186 1047 {
ansond 0:137634ff4186 1048 unsigned char header[11];
ansond 0:137634ff4186 1049 unsigned char padding[48];
ansond 0:137634ff4186 1050 int padlen;
ansond 0:137634ff4186 1051 int md_size = md_get_size( md_ctx->md_info );
ansond 0:137634ff4186 1052 int md_type = md_get_type( md_ctx->md_info );
ansond 0:137634ff4186 1053
ansond 0:137634ff4186 1054 /* Only MD5 and SHA-1 supported */
ansond 0:137634ff4186 1055 if( md_type == POLARSSL_MD_MD5 )
ansond 0:137634ff4186 1056 padlen = 48;
ansond 0:137634ff4186 1057 else
ansond 0:137634ff4186 1058 padlen = 40;
ansond 0:137634ff4186 1059
ansond 0:137634ff4186 1060 memcpy( header, ctr, 8 );
ansond 0:137634ff4186 1061 header[ 8] = (unsigned char) type;
ansond 0:137634ff4186 1062 header[ 9] = (unsigned char)( len >> 8 );
ansond 0:137634ff4186 1063 header[10] = (unsigned char)( len );
ansond 0:137634ff4186 1064
ansond 0:137634ff4186 1065 memset( padding, 0x36, padlen );
ansond 0:137634ff4186 1066 md_starts( md_ctx );
ansond 0:137634ff4186 1067 md_update( md_ctx, secret, md_size );
ansond 0:137634ff4186 1068 md_update( md_ctx, padding, padlen );
ansond 0:137634ff4186 1069 md_update( md_ctx, header, 11 );
ansond 0:137634ff4186 1070 md_update( md_ctx, buf, len );
ansond 0:137634ff4186 1071 md_finish( md_ctx, buf + len );
ansond 0:137634ff4186 1072
ansond 0:137634ff4186 1073 memset( padding, 0x5C, padlen );
ansond 0:137634ff4186 1074 md_starts( md_ctx );
ansond 0:137634ff4186 1075 md_update( md_ctx, secret, md_size );
ansond 0:137634ff4186 1076 md_update( md_ctx, padding, padlen );
ansond 0:137634ff4186 1077 md_update( md_ctx, buf + len, md_size );
ansond 0:137634ff4186 1078 md_finish( md_ctx, buf + len );
ansond 0:137634ff4186 1079 }
ansond 0:137634ff4186 1080 #endif /* POLARSSL_SSL_PROTO_SSL3 */
ansond 0:137634ff4186 1081
ansond 0:137634ff4186 1082 #if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
ansond 0:137634ff4186 1083 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
ansond 0:137634ff4186 1084 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
ansond 0:137634ff4186 1085 #define POLARSSL_SOME_MODES_USE_MAC
ansond 0:137634ff4186 1086 #endif
ansond 0:137634ff4186 1087
ansond 0:137634ff4186 1088 /*
ansond 0:137634ff4186 1089 * Encryption/decryption functions
ansond 0:137634ff4186 1090 */
ansond 0:137634ff4186 1091 static int ssl_encrypt_buf( ssl_context *ssl )
ansond 0:137634ff4186 1092 {
ansond 0:137634ff4186 1093 size_t i;
ansond 0:137634ff4186 1094 cipher_mode_t mode;
ansond 0:137634ff4186 1095 int auth_done = 0;
ansond 0:137634ff4186 1096
ansond 0:137634ff4186 1097 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
ansond 0:137634ff4186 1098
ansond 0:137634ff4186 1099 if( ssl->session_out == NULL || ssl->transform_out == NULL )
ansond 0:137634ff4186 1100 {
ansond 0:137634ff4186 1101 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 1102 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 1103 }
ansond 0:137634ff4186 1104
ansond 0:137634ff4186 1105 mode = cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
ansond 0:137634ff4186 1106
ansond 0:137634ff4186 1107 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
ansond 0:137634ff4186 1108 ssl->out_msg, ssl->out_msglen );
ansond 0:137634ff4186 1109
ansond 0:137634ff4186 1110 /*
ansond 0:137634ff4186 1111 * Add MAC before if needed
ansond 0:137634ff4186 1112 */
ansond 0:137634ff4186 1113 #if defined(POLARSSL_SOME_MODES_USE_MAC)
ansond 0:137634ff4186 1114 if( mode == POLARSSL_MODE_STREAM ||
ansond 0:137634ff4186 1115 ( mode == POLARSSL_MODE_CBC
ansond 0:137634ff4186 1116 #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
ansond 0:137634ff4186 1117 && ssl->session_out->encrypt_then_mac == SSL_ETM_DISABLED
ansond 0:137634ff4186 1118 #endif
ansond 0:137634ff4186 1119 ) )
ansond 0:137634ff4186 1120 {
ansond 0:137634ff4186 1121 #if defined(POLARSSL_SSL_PROTO_SSL3)
ansond 0:137634ff4186 1122 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
ansond 0:137634ff4186 1123 {
ansond 0:137634ff4186 1124 ssl_mac( &ssl->transform_out->md_ctx_enc,
ansond 0:137634ff4186 1125 ssl->transform_out->mac_enc,
ansond 0:137634ff4186 1126 ssl->out_msg, ssl->out_msglen,
ansond 0:137634ff4186 1127 ssl->out_ctr, ssl->out_msgtype );
ansond 0:137634ff4186 1128 }
ansond 0:137634ff4186 1129 else
ansond 0:137634ff4186 1130 #endif
ansond 0:137634ff4186 1131 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
ansond 0:137634ff4186 1132 defined(POLARSSL_SSL_PROTO_TLS1_2)
ansond 0:137634ff4186 1133 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
ansond 0:137634ff4186 1134 {
ansond 0:137634ff4186 1135 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
ansond 0:137634ff4186 1136 md_hmac_update( &ssl->transform_out->md_ctx_enc,
ansond 0:137634ff4186 1137 ssl->out_msg, ssl->out_msglen );
ansond 0:137634ff4186 1138 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
ansond 0:137634ff4186 1139 ssl->out_msg + ssl->out_msglen );
ansond 0:137634ff4186 1140 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
ansond 0:137634ff4186 1141 }
ansond 0:137634ff4186 1142 else
ansond 0:137634ff4186 1143 #endif
ansond 0:137634ff4186 1144 {
ansond 0:137634ff4186 1145 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 1146 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 1147 }
ansond 0:137634ff4186 1148
ansond 0:137634ff4186 1149 SSL_DEBUG_BUF( 4, "computed mac",
ansond 0:137634ff4186 1150 ssl->out_msg + ssl->out_msglen,
ansond 0:137634ff4186 1151 ssl->transform_out->maclen );
ansond 0:137634ff4186 1152
ansond 0:137634ff4186 1153 ssl->out_msglen += ssl->transform_out->maclen;
ansond 0:137634ff4186 1154 auth_done++;
ansond 0:137634ff4186 1155 }
ansond 0:137634ff4186 1156 #endif /* AEAD not the only option */
ansond 0:137634ff4186 1157
ansond 0:137634ff4186 1158 /*
ansond 0:137634ff4186 1159 * Encrypt
ansond 0:137634ff4186 1160 */
ansond 0:137634ff4186 1161 #if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
ansond 0:137634ff4186 1162 if( mode == POLARSSL_MODE_STREAM )
ansond 0:137634ff4186 1163 {
ansond 0:137634ff4186 1164 int ret;
ansond 0:137634ff4186 1165 size_t olen = 0;
ansond 0:137634ff4186 1166
ansond 0:137634ff4186 1167 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
ansond 0:137634ff4186 1168 "including %d bytes of padding",
ansond 0:137634ff4186 1169 ssl->out_msglen, 0 ) );
ansond 0:137634ff4186 1170
ansond 0:137634ff4186 1171 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
ansond 0:137634ff4186 1172 ssl->transform_out->iv_enc,
ansond 0:137634ff4186 1173 ssl->transform_out->ivlen,
ansond 0:137634ff4186 1174 ssl->out_msg, ssl->out_msglen,
ansond 0:137634ff4186 1175 ssl->out_msg, &olen ) ) != 0 )
ansond 0:137634ff4186 1176 {
ansond 0:137634ff4186 1177 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
ansond 0:137634ff4186 1178 return( ret );
ansond 0:137634ff4186 1179 }
ansond 0:137634ff4186 1180
ansond 0:137634ff4186 1181 if( ssl->out_msglen != olen )
ansond 0:137634ff4186 1182 {
ansond 0:137634ff4186 1183 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 1184 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 1185 }
ansond 0:137634ff4186 1186 }
ansond 0:137634ff4186 1187 else
ansond 0:137634ff4186 1188 #endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
ansond 0:137634ff4186 1189 #if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
ansond 0:137634ff4186 1190 if( mode == POLARSSL_MODE_GCM ||
ansond 0:137634ff4186 1191 mode == POLARSSL_MODE_CCM )
ansond 0:137634ff4186 1192 {
ansond 0:137634ff4186 1193 int ret;
ansond 0:137634ff4186 1194 size_t enc_msglen, olen;
ansond 0:137634ff4186 1195 unsigned char *enc_msg;
ansond 0:137634ff4186 1196 unsigned char add_data[13];
ansond 0:137634ff4186 1197 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
ansond 0:137634ff4186 1198 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
ansond 0:137634ff4186 1199
ansond 0:137634ff4186 1200 memcpy( add_data, ssl->out_ctr, 8 );
ansond 0:137634ff4186 1201 add_data[8] = ssl->out_msgtype;
ansond 0:137634ff4186 1202 add_data[9] = ssl->major_ver;
ansond 0:137634ff4186 1203 add_data[10] = ssl->minor_ver;
ansond 0:137634ff4186 1204 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
ansond 0:137634ff4186 1205 add_data[12] = ssl->out_msglen & 0xFF;
ansond 0:137634ff4186 1206
ansond 0:137634ff4186 1207 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
ansond 0:137634ff4186 1208 add_data, 13 );
ansond 0:137634ff4186 1209
ansond 0:137634ff4186 1210 /*
ansond 0:137634ff4186 1211 * Generate IV
ansond 0:137634ff4186 1212 */
ansond 0:137634ff4186 1213 #if defined(POLARSSL_SSL_AEAD_RANDOM_IV)
ansond 0:137634ff4186 1214 ret = ssl->f_rng( ssl->p_rng,
ansond 0:137634ff4186 1215 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
ansond 0:137634ff4186 1216 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
ansond 0:137634ff4186 1217 if( ret != 0 )
ansond 0:137634ff4186 1218 return( ret );
ansond 0:137634ff4186 1219
ansond 0:137634ff4186 1220 memcpy( ssl->out_iv,
ansond 0:137634ff4186 1221 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
ansond 0:137634ff4186 1222 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
ansond 0:137634ff4186 1223 #else
ansond 0:137634ff4186 1224 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
ansond 0:137634ff4186 1225 {
ansond 0:137634ff4186 1226 /* Reminder if we ever add an AEAD mode with a different size */
ansond 0:137634ff4186 1227 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 1228 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 1229 }
ansond 0:137634ff4186 1230
ansond 0:137634ff4186 1231 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
ansond 0:137634ff4186 1232 ssl->out_ctr, 8 );
ansond 0:137634ff4186 1233 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
ansond 0:137634ff4186 1234 #endif
ansond 0:137634ff4186 1235
ansond 0:137634ff4186 1236 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
ansond 0:137634ff4186 1237 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
ansond 0:137634ff4186 1238
ansond 0:137634ff4186 1239 /*
ansond 0:137634ff4186 1240 * Fix pointer positions and message length with added IV
ansond 0:137634ff4186 1241 */
ansond 0:137634ff4186 1242 enc_msg = ssl->out_msg;
ansond 0:137634ff4186 1243 enc_msglen = ssl->out_msglen;
ansond 0:137634ff4186 1244 ssl->out_msglen += ssl->transform_out->ivlen -
ansond 0:137634ff4186 1245 ssl->transform_out->fixed_ivlen;
ansond 0:137634ff4186 1246
ansond 0:137634ff4186 1247 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
ansond 0:137634ff4186 1248 "including %d bytes of padding",
ansond 0:137634ff4186 1249 ssl->out_msglen, 0 ) );
ansond 0:137634ff4186 1250
ansond 0:137634ff4186 1251 /*
ansond 0:137634ff4186 1252 * Encrypt and authenticate
ansond 0:137634ff4186 1253 */
ansond 0:137634ff4186 1254 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
ansond 0:137634ff4186 1255 ssl->transform_out->iv_enc,
ansond 0:137634ff4186 1256 ssl->transform_out->ivlen,
ansond 0:137634ff4186 1257 add_data, 13,
ansond 0:137634ff4186 1258 enc_msg, enc_msglen,
ansond 0:137634ff4186 1259 enc_msg, &olen,
ansond 0:137634ff4186 1260 enc_msg + enc_msglen, taglen ) ) != 0 )
ansond 0:137634ff4186 1261 {
ansond 0:137634ff4186 1262 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
ansond 0:137634ff4186 1263 return( ret );
ansond 0:137634ff4186 1264 }
ansond 0:137634ff4186 1265
ansond 0:137634ff4186 1266 if( olen != enc_msglen )
ansond 0:137634ff4186 1267 {
ansond 0:137634ff4186 1268 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 1269 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 1270 }
ansond 0:137634ff4186 1271
ansond 0:137634ff4186 1272 ssl->out_msglen += taglen;
ansond 0:137634ff4186 1273 auth_done++;
ansond 0:137634ff4186 1274
ansond 0:137634ff4186 1275 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
ansond 0:137634ff4186 1276 }
ansond 0:137634ff4186 1277 else
ansond 0:137634ff4186 1278 #endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
ansond 0:137634ff4186 1279 #if defined(POLARSSL_CIPHER_MODE_CBC) && \
ansond 0:137634ff4186 1280 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
ansond 0:137634ff4186 1281 if( mode == POLARSSL_MODE_CBC )
ansond 0:137634ff4186 1282 {
ansond 0:137634ff4186 1283 int ret;
ansond 0:137634ff4186 1284 unsigned char *enc_msg;
ansond 0:137634ff4186 1285 size_t enc_msglen, padlen, olen = 0;
ansond 0:137634ff4186 1286
ansond 0:137634ff4186 1287 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
ansond 0:137634ff4186 1288 ssl->transform_out->ivlen;
ansond 0:137634ff4186 1289 if( padlen == ssl->transform_out->ivlen )
ansond 0:137634ff4186 1290 padlen = 0;
ansond 0:137634ff4186 1291
ansond 0:137634ff4186 1292 for( i = 0; i <= padlen; i++ )
ansond 0:137634ff4186 1293 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
ansond 0:137634ff4186 1294
ansond 0:137634ff4186 1295 ssl->out_msglen += padlen + 1;
ansond 0:137634ff4186 1296
ansond 0:137634ff4186 1297 enc_msglen = ssl->out_msglen;
ansond 0:137634ff4186 1298 enc_msg = ssl->out_msg;
ansond 0:137634ff4186 1299
ansond 0:137634ff4186 1300 #if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
ansond 0:137634ff4186 1301 /*
ansond 0:137634ff4186 1302 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
ansond 0:137634ff4186 1303 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
ansond 0:137634ff4186 1304 */
ansond 0:137634ff4186 1305 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
ansond 0:137634ff4186 1306 {
ansond 0:137634ff4186 1307 /*
ansond 0:137634ff4186 1308 * Generate IV
ansond 0:137634ff4186 1309 */
ansond 0:137634ff4186 1310 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
ansond 0:137634ff4186 1311 ssl->transform_out->ivlen );
ansond 0:137634ff4186 1312 if( ret != 0 )
ansond 0:137634ff4186 1313 return( ret );
ansond 0:137634ff4186 1314
ansond 0:137634ff4186 1315 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
ansond 0:137634ff4186 1316 ssl->transform_out->ivlen );
ansond 0:137634ff4186 1317
ansond 0:137634ff4186 1318 /*
ansond 0:137634ff4186 1319 * Fix pointer positions and message length with added IV
ansond 0:137634ff4186 1320 */
ansond 0:137634ff4186 1321 enc_msg = ssl->out_msg;
ansond 0:137634ff4186 1322 enc_msglen = ssl->out_msglen;
ansond 0:137634ff4186 1323 ssl->out_msglen += ssl->transform_out->ivlen;
ansond 0:137634ff4186 1324 }
ansond 0:137634ff4186 1325 #endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
ansond 0:137634ff4186 1326
ansond 0:137634ff4186 1327 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
ansond 0:137634ff4186 1328 "including %d bytes of IV and %d bytes of padding",
ansond 0:137634ff4186 1329 ssl->out_msglen, ssl->transform_out->ivlen,
ansond 0:137634ff4186 1330 padlen + 1 ) );
ansond 0:137634ff4186 1331
ansond 0:137634ff4186 1332 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
ansond 0:137634ff4186 1333 ssl->transform_out->iv_enc,
ansond 0:137634ff4186 1334 ssl->transform_out->ivlen,
ansond 0:137634ff4186 1335 enc_msg, enc_msglen,
ansond 0:137634ff4186 1336 enc_msg, &olen ) ) != 0 )
ansond 0:137634ff4186 1337 {
ansond 0:137634ff4186 1338 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
ansond 0:137634ff4186 1339 return( ret );
ansond 0:137634ff4186 1340 }
ansond 0:137634ff4186 1341
ansond 0:137634ff4186 1342 if( enc_msglen != olen )
ansond 0:137634ff4186 1343 {
ansond 0:137634ff4186 1344 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 1345 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 1346 }
ansond 0:137634ff4186 1347
ansond 0:137634ff4186 1348 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
ansond 0:137634ff4186 1349 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
ansond 0:137634ff4186 1350 {
ansond 0:137634ff4186 1351 /*
ansond 0:137634ff4186 1352 * Save IV in SSL3 and TLS1
ansond 0:137634ff4186 1353 */
ansond 0:137634ff4186 1354 memcpy( ssl->transform_out->iv_enc,
ansond 0:137634ff4186 1355 ssl->transform_out->cipher_ctx_enc.iv,
ansond 0:137634ff4186 1356 ssl->transform_out->ivlen );
ansond 0:137634ff4186 1357 }
ansond 0:137634ff4186 1358 #endif
ansond 0:137634ff4186 1359
ansond 0:137634ff4186 1360 #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
ansond 0:137634ff4186 1361 if( auth_done == 0 )
ansond 0:137634ff4186 1362 {
ansond 0:137634ff4186 1363 /*
ansond 0:137634ff4186 1364 * MAC(MAC_write_key, seq_num +
ansond 0:137634ff4186 1365 * TLSCipherText.type +
ansond 0:137634ff4186 1366 * TLSCipherText.version +
ansond 0:137634ff4186 1367 * length_of( (IV +) ENC(...) ) +
ansond 0:137634ff4186 1368 * IV + // except for TLS 1.0
ansond 0:137634ff4186 1369 * ENC(content + padding + padding_length));
ansond 0:137634ff4186 1370 */
ansond 0:137634ff4186 1371 unsigned char pseudo_hdr[13];
ansond 0:137634ff4186 1372
ansond 0:137634ff4186 1373 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
ansond 0:137634ff4186 1374
ansond 0:137634ff4186 1375 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
ansond 0:137634ff4186 1376 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
ansond 0:137634ff4186 1377 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
ansond 0:137634ff4186 1378 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
ansond 0:137634ff4186 1379
ansond 0:137634ff4186 1380 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
ansond 0:137634ff4186 1381
ansond 0:137634ff4186 1382 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
ansond 0:137634ff4186 1383 md_hmac_update( &ssl->transform_out->md_ctx_enc,
ansond 0:137634ff4186 1384 ssl->out_iv, ssl->out_msglen );
ansond 0:137634ff4186 1385 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
ansond 0:137634ff4186 1386 ssl->out_iv + ssl->out_msglen );
ansond 0:137634ff4186 1387 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
ansond 0:137634ff4186 1388
ansond 0:137634ff4186 1389 ssl->out_msglen += ssl->transform_out->maclen;
ansond 0:137634ff4186 1390 auth_done++;
ansond 0:137634ff4186 1391 }
ansond 0:137634ff4186 1392 #endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
ansond 0:137634ff4186 1393 }
ansond 0:137634ff4186 1394 else
ansond 0:137634ff4186 1395 #endif /* POLARSSL_CIPHER_MODE_CBC &&
ansond 0:137634ff4186 1396 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
ansond 0:137634ff4186 1397 {
ansond 0:137634ff4186 1398 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 1399 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 1400 }
ansond 0:137634ff4186 1401
ansond 0:137634ff4186 1402 /* Make extra sure authentication was performed, exactly once */
ansond 0:137634ff4186 1403 if( auth_done != 1 )
ansond 0:137634ff4186 1404 {
ansond 0:137634ff4186 1405 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 1406 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 1407 }
ansond 0:137634ff4186 1408
ansond 0:137634ff4186 1409 for( i = 8; i > 0; i-- )
ansond 0:137634ff4186 1410 if( ++ssl->out_ctr[i - 1] != 0 )
ansond 0:137634ff4186 1411 break;
ansond 0:137634ff4186 1412
ansond 0:137634ff4186 1413 /* The loops goes to its end iff the counter is wrapping */
ansond 0:137634ff4186 1414 if( i == 0 )
ansond 0:137634ff4186 1415 {
ansond 0:137634ff4186 1416 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
ansond 0:137634ff4186 1417 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
ansond 0:137634ff4186 1418 }
ansond 0:137634ff4186 1419
ansond 0:137634ff4186 1420 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
ansond 0:137634ff4186 1421
ansond 0:137634ff4186 1422 return( 0 );
ansond 0:137634ff4186 1423 }
ansond 0:137634ff4186 1424
ansond 0:137634ff4186 1425 #define POLARSSL_SSL_MAX_MAC_SIZE 48
ansond 0:137634ff4186 1426
ansond 0:137634ff4186 1427 static int ssl_decrypt_buf( ssl_context *ssl )
ansond 0:137634ff4186 1428 {
ansond 0:137634ff4186 1429 size_t i;
ansond 0:137634ff4186 1430 cipher_mode_t mode;
ansond 0:137634ff4186 1431 int auth_done = 0;
ansond 0:137634ff4186 1432 #if defined(POLARSSL_SOME_MODES_USE_MAC)
ansond 0:137634ff4186 1433 size_t padlen = 0, correct = 1;
ansond 0:137634ff4186 1434 #endif
ansond 0:137634ff4186 1435
ansond 0:137634ff4186 1436 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
ansond 0:137634ff4186 1437
ansond 0:137634ff4186 1438 if( ssl->session_in == NULL || ssl->transform_in == NULL )
ansond 0:137634ff4186 1439 {
ansond 0:137634ff4186 1440 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 1441 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 1442 }
ansond 0:137634ff4186 1443
ansond 0:137634ff4186 1444 mode = cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
ansond 0:137634ff4186 1445
ansond 0:137634ff4186 1446 if( ssl->in_msglen < ssl->transform_in->minlen )
ansond 0:137634ff4186 1447 {
ansond 0:137634ff4186 1448 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
ansond 0:137634ff4186 1449 ssl->in_msglen, ssl->transform_in->minlen ) );
ansond 0:137634ff4186 1450 return( POLARSSL_ERR_SSL_INVALID_MAC );
ansond 0:137634ff4186 1451 }
ansond 0:137634ff4186 1452
ansond 0:137634ff4186 1453 #if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
ansond 0:137634ff4186 1454 if( mode == POLARSSL_MODE_STREAM )
ansond 0:137634ff4186 1455 {
ansond 0:137634ff4186 1456 int ret;
ansond 0:137634ff4186 1457 size_t olen = 0;
ansond 0:137634ff4186 1458
ansond 0:137634ff4186 1459 padlen = 0;
ansond 0:137634ff4186 1460
ansond 0:137634ff4186 1461 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
ansond 0:137634ff4186 1462 ssl->transform_in->iv_dec,
ansond 0:137634ff4186 1463 ssl->transform_in->ivlen,
ansond 0:137634ff4186 1464 ssl->in_msg, ssl->in_msglen,
ansond 0:137634ff4186 1465 ssl->in_msg, &olen ) ) != 0 )
ansond 0:137634ff4186 1466 {
ansond 0:137634ff4186 1467 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
ansond 0:137634ff4186 1468 return( ret );
ansond 0:137634ff4186 1469 }
ansond 0:137634ff4186 1470
ansond 0:137634ff4186 1471 if( ssl->in_msglen != olen )
ansond 0:137634ff4186 1472 {
ansond 0:137634ff4186 1473 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 1474 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 1475 }
ansond 0:137634ff4186 1476 }
ansond 0:137634ff4186 1477 else
ansond 0:137634ff4186 1478 #endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
ansond 0:137634ff4186 1479 #if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
ansond 0:137634ff4186 1480 if( mode == POLARSSL_MODE_GCM ||
ansond 0:137634ff4186 1481 mode == POLARSSL_MODE_CCM )
ansond 0:137634ff4186 1482 {
ansond 0:137634ff4186 1483 int ret;
ansond 0:137634ff4186 1484 size_t dec_msglen, olen;
ansond 0:137634ff4186 1485 unsigned char *dec_msg;
ansond 0:137634ff4186 1486 unsigned char *dec_msg_result;
ansond 0:137634ff4186 1487 unsigned char add_data[13];
ansond 0:137634ff4186 1488 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
ansond 0:137634ff4186 1489 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
ansond 0:137634ff4186 1490 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
ansond 0:137634ff4186 1491 ssl->transform_in->fixed_ivlen;
ansond 0:137634ff4186 1492
ansond 0:137634ff4186 1493 if( ssl->in_msglen < (size_t) explicit_iv_len + taglen )
ansond 0:137634ff4186 1494 {
ansond 0:137634ff4186 1495 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
ansond 0:137634ff4186 1496 "+ taglen (%d)", ssl->in_msglen,
ansond 0:137634ff4186 1497 explicit_iv_len, taglen ) );
ansond 0:137634ff4186 1498 return( POLARSSL_ERR_SSL_INVALID_MAC );
ansond 0:137634ff4186 1499 }
ansond 0:137634ff4186 1500 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
ansond 0:137634ff4186 1501
ansond 0:137634ff4186 1502 dec_msg = ssl->in_msg;
ansond 0:137634ff4186 1503 dec_msg_result = ssl->in_msg;
ansond 0:137634ff4186 1504 ssl->in_msglen = dec_msglen;
ansond 0:137634ff4186 1505
ansond 0:137634ff4186 1506 memcpy( add_data, ssl->in_ctr, 8 );
ansond 0:137634ff4186 1507 add_data[8] = ssl->in_msgtype;
ansond 0:137634ff4186 1508 add_data[9] = ssl->major_ver;
ansond 0:137634ff4186 1509 add_data[10] = ssl->minor_ver;
ansond 0:137634ff4186 1510 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
ansond 0:137634ff4186 1511 add_data[12] = ssl->in_msglen & 0xFF;
ansond 0:137634ff4186 1512
ansond 0:137634ff4186 1513 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
ansond 0:137634ff4186 1514 add_data, 13 );
ansond 0:137634ff4186 1515
ansond 0:137634ff4186 1516 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
ansond 0:137634ff4186 1517 ssl->in_iv,
ansond 0:137634ff4186 1518 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
ansond 0:137634ff4186 1519
ansond 0:137634ff4186 1520 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
ansond 0:137634ff4186 1521 ssl->transform_in->ivlen );
ansond 0:137634ff4186 1522 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
ansond 0:137634ff4186 1523
ansond 0:137634ff4186 1524 /*
ansond 0:137634ff4186 1525 * Decrypt and authenticate
ansond 0:137634ff4186 1526 */
ansond 0:137634ff4186 1527 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
ansond 0:137634ff4186 1528 ssl->transform_in->iv_dec,
ansond 0:137634ff4186 1529 ssl->transform_in->ivlen,
ansond 0:137634ff4186 1530 add_data, 13,
ansond 0:137634ff4186 1531 dec_msg, dec_msglen,
ansond 0:137634ff4186 1532 dec_msg_result, &olen,
ansond 0:137634ff4186 1533 dec_msg + dec_msglen, taglen ) ) != 0 )
ansond 0:137634ff4186 1534 {
ansond 0:137634ff4186 1535 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
ansond 0:137634ff4186 1536
ansond 0:137634ff4186 1537 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
ansond 0:137634ff4186 1538 return( POLARSSL_ERR_SSL_INVALID_MAC );
ansond 0:137634ff4186 1539
ansond 0:137634ff4186 1540 return( ret );
ansond 0:137634ff4186 1541 }
ansond 0:137634ff4186 1542 auth_done++;
ansond 0:137634ff4186 1543
ansond 0:137634ff4186 1544 if( olen != dec_msglen )
ansond 0:137634ff4186 1545 {
ansond 0:137634ff4186 1546 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 1547 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 1548 }
ansond 0:137634ff4186 1549 }
ansond 0:137634ff4186 1550 else
ansond 0:137634ff4186 1551 #endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
ansond 0:137634ff4186 1552 #if defined(POLARSSL_CIPHER_MODE_CBC) && \
ansond 0:137634ff4186 1553 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
ansond 0:137634ff4186 1554 if( mode == POLARSSL_MODE_CBC )
ansond 0:137634ff4186 1555 {
ansond 0:137634ff4186 1556 /*
ansond 0:137634ff4186 1557 * Decrypt and check the padding
ansond 0:137634ff4186 1558 */
ansond 0:137634ff4186 1559 int ret;
ansond 0:137634ff4186 1560 unsigned char *dec_msg;
ansond 0:137634ff4186 1561 unsigned char *dec_msg_result;
ansond 0:137634ff4186 1562 size_t dec_msglen;
ansond 0:137634ff4186 1563 size_t minlen = 0;
ansond 0:137634ff4186 1564 size_t olen = 0;
ansond 0:137634ff4186 1565
ansond 0:137634ff4186 1566 /*
ansond 0:137634ff4186 1567 * Check immediate ciphertext sanity
ansond 0:137634ff4186 1568 */
ansond 0:137634ff4186 1569 #if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
ansond 0:137634ff4186 1570 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
ansond 0:137634ff4186 1571 minlen += ssl->transform_in->ivlen;
ansond 0:137634ff4186 1572 #endif
ansond 0:137634ff4186 1573
ansond 0:137634ff4186 1574 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
ansond 0:137634ff4186 1575 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
ansond 0:137634ff4186 1576 {
ansond 0:137634ff4186 1577 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
ansond 0:137634ff4186 1578 "+ 1 ) ( + expl IV )", ssl->in_msglen,
ansond 0:137634ff4186 1579 ssl->transform_in->ivlen,
ansond 0:137634ff4186 1580 ssl->transform_in->maclen ) );
ansond 0:137634ff4186 1581 return( POLARSSL_ERR_SSL_INVALID_MAC );
ansond 0:137634ff4186 1582 }
ansond 0:137634ff4186 1583
ansond 0:137634ff4186 1584 dec_msglen = ssl->in_msglen;
ansond 0:137634ff4186 1585 dec_msg = ssl->in_msg;
ansond 0:137634ff4186 1586 dec_msg_result = ssl->in_msg;
ansond 0:137634ff4186 1587
ansond 0:137634ff4186 1588 /*
ansond 0:137634ff4186 1589 * Authenticate before decrypt if enabled
ansond 0:137634ff4186 1590 */
ansond 0:137634ff4186 1591 #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
ansond 0:137634ff4186 1592 if( ssl->session_in->encrypt_then_mac == SSL_ETM_ENABLED )
ansond 0:137634ff4186 1593 {
ansond 0:137634ff4186 1594 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
ansond 0:137634ff4186 1595 unsigned char pseudo_hdr[13];
ansond 0:137634ff4186 1596
ansond 0:137634ff4186 1597 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
ansond 0:137634ff4186 1598
ansond 0:137634ff4186 1599 dec_msglen -= ssl->transform_in->maclen;
ansond 0:137634ff4186 1600 ssl->in_msglen -= ssl->transform_in->maclen;
ansond 0:137634ff4186 1601
ansond 0:137634ff4186 1602 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
ansond 0:137634ff4186 1603 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
ansond 0:137634ff4186 1604 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
ansond 0:137634ff4186 1605 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
ansond 0:137634ff4186 1606
ansond 0:137634ff4186 1607 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
ansond 0:137634ff4186 1608
ansond 0:137634ff4186 1609 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
ansond 0:137634ff4186 1610 md_hmac_update( &ssl->transform_in->md_ctx_dec,
ansond 0:137634ff4186 1611 ssl->in_iv, ssl->in_msglen );
ansond 0:137634ff4186 1612 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
ansond 0:137634ff4186 1613 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
ansond 0:137634ff4186 1614
ansond 0:137634ff4186 1615 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
ansond 0:137634ff4186 1616 ssl->transform_in->maclen );
ansond 0:137634ff4186 1617 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
ansond 0:137634ff4186 1618 ssl->transform_in->maclen );
ansond 0:137634ff4186 1619
ansond 0:137634ff4186 1620 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
ansond 0:137634ff4186 1621 ssl->transform_in->maclen ) != 0 )
ansond 0:137634ff4186 1622 {
ansond 0:137634ff4186 1623 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
ansond 0:137634ff4186 1624
ansond 0:137634ff4186 1625 return( POLARSSL_ERR_SSL_INVALID_MAC );
ansond 0:137634ff4186 1626 }
ansond 0:137634ff4186 1627 auth_done++;
ansond 0:137634ff4186 1628 }
ansond 0:137634ff4186 1629 #endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
ansond 0:137634ff4186 1630
ansond 0:137634ff4186 1631 /*
ansond 0:137634ff4186 1632 * Check length sanity
ansond 0:137634ff4186 1633 */
ansond 0:137634ff4186 1634 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
ansond 0:137634ff4186 1635 {
ansond 0:137634ff4186 1636 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
ansond 0:137634ff4186 1637 ssl->in_msglen, ssl->transform_in->ivlen ) );
ansond 0:137634ff4186 1638 return( POLARSSL_ERR_SSL_INVALID_MAC );
ansond 0:137634ff4186 1639 }
ansond 0:137634ff4186 1640
ansond 0:137634ff4186 1641 #if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
ansond 0:137634ff4186 1642 /*
ansond 0:137634ff4186 1643 * Initialize for prepended IV for block cipher in TLS v1.1 and up
ansond 0:137634ff4186 1644 */
ansond 0:137634ff4186 1645 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
ansond 0:137634ff4186 1646 {
ansond 0:137634ff4186 1647 dec_msglen -= ssl->transform_in->ivlen;
ansond 0:137634ff4186 1648 ssl->in_msglen -= ssl->transform_in->ivlen;
ansond 0:137634ff4186 1649
ansond 0:137634ff4186 1650 for( i = 0; i < ssl->transform_in->ivlen; i++ )
ansond 0:137634ff4186 1651 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
ansond 0:137634ff4186 1652 }
ansond 0:137634ff4186 1653 #endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
ansond 0:137634ff4186 1654
ansond 0:137634ff4186 1655 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
ansond 0:137634ff4186 1656 ssl->transform_in->iv_dec,
ansond 0:137634ff4186 1657 ssl->transform_in->ivlen,
ansond 0:137634ff4186 1658 dec_msg, dec_msglen,
ansond 0:137634ff4186 1659 dec_msg_result, &olen ) ) != 0 )
ansond 0:137634ff4186 1660 {
ansond 0:137634ff4186 1661 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
ansond 0:137634ff4186 1662 return( ret );
ansond 0:137634ff4186 1663 }
ansond 0:137634ff4186 1664
ansond 0:137634ff4186 1665 if( dec_msglen != olen )
ansond 0:137634ff4186 1666 {
ansond 0:137634ff4186 1667 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 1668 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 1669 }
ansond 0:137634ff4186 1670
ansond 0:137634ff4186 1671 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
ansond 0:137634ff4186 1672 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
ansond 0:137634ff4186 1673 {
ansond 0:137634ff4186 1674 /*
ansond 0:137634ff4186 1675 * Save IV in SSL3 and TLS1
ansond 0:137634ff4186 1676 */
ansond 0:137634ff4186 1677 memcpy( ssl->transform_in->iv_dec,
ansond 0:137634ff4186 1678 ssl->transform_in->cipher_ctx_dec.iv,
ansond 0:137634ff4186 1679 ssl->transform_in->ivlen );
ansond 0:137634ff4186 1680 }
ansond 0:137634ff4186 1681 #endif
ansond 0:137634ff4186 1682
ansond 0:137634ff4186 1683 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
ansond 0:137634ff4186 1684
ansond 0:137634ff4186 1685 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
ansond 0:137634ff4186 1686 auth_done == 0 )
ansond 0:137634ff4186 1687 {
ansond 0:137634ff4186 1688 #if defined(POLARSSL_SSL_DEBUG_ALL)
ansond 0:137634ff4186 1689 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
ansond 0:137634ff4186 1690 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
ansond 0:137634ff4186 1691 #endif
ansond 0:137634ff4186 1692 padlen = 0;
ansond 0:137634ff4186 1693 correct = 0;
ansond 0:137634ff4186 1694 }
ansond 0:137634ff4186 1695
ansond 0:137634ff4186 1696 #if defined(POLARSSL_SSL_PROTO_SSL3)
ansond 0:137634ff4186 1697 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
ansond 0:137634ff4186 1698 {
ansond 0:137634ff4186 1699 if( padlen > ssl->transform_in->ivlen )
ansond 0:137634ff4186 1700 {
ansond 0:137634ff4186 1701 #if defined(POLARSSL_SSL_DEBUG_ALL)
ansond 0:137634ff4186 1702 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
ansond 0:137634ff4186 1703 "should be no more than %d",
ansond 0:137634ff4186 1704 padlen, ssl->transform_in->ivlen ) );
ansond 0:137634ff4186 1705 #endif
ansond 0:137634ff4186 1706 correct = 0;
ansond 0:137634ff4186 1707 }
ansond 0:137634ff4186 1708 }
ansond 0:137634ff4186 1709 else
ansond 0:137634ff4186 1710 #endif /* POLARSSL_SSL_PROTO_SSL3 */
ansond 0:137634ff4186 1711 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
ansond 0:137634ff4186 1712 defined(POLARSSL_SSL_PROTO_TLS1_2)
ansond 0:137634ff4186 1713 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
ansond 0:137634ff4186 1714 {
ansond 0:137634ff4186 1715 /*
ansond 0:137634ff4186 1716 * TLSv1+: always check the padding up to the first failure
ansond 0:137634ff4186 1717 * and fake check up to 256 bytes of padding
ansond 0:137634ff4186 1718 */
ansond 0:137634ff4186 1719 size_t pad_count = 0, real_count = 1;
ansond 0:137634ff4186 1720 size_t padding_idx = ssl->in_msglen - padlen - 1;
ansond 0:137634ff4186 1721
ansond 0:137634ff4186 1722 /*
ansond 0:137634ff4186 1723 * Padding is guaranteed to be incorrect if:
ansond 0:137634ff4186 1724 * 1. padlen >= ssl->in_msglen
ansond 0:137634ff4186 1725 *
ansond 0:137634ff4186 1726 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
ansond 0:137634ff4186 1727 * ssl->transform_in->maclen
ansond 0:137634ff4186 1728 *
ansond 0:137634ff4186 1729 * In both cases we reset padding_idx to a safe value (0) to
ansond 0:137634ff4186 1730 * prevent out-of-buffer reads.
ansond 0:137634ff4186 1731 */
ansond 0:137634ff4186 1732 correct &= ( ssl->in_msglen >= padlen + 1 );
ansond 0:137634ff4186 1733 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
ansond 0:137634ff4186 1734 ssl->transform_in->maclen );
ansond 0:137634ff4186 1735
ansond 0:137634ff4186 1736 padding_idx *= correct;
ansond 0:137634ff4186 1737
ansond 0:137634ff4186 1738 for( i = 1; i <= 256; i++ )
ansond 0:137634ff4186 1739 {
ansond 0:137634ff4186 1740 real_count &= ( i <= padlen );
ansond 0:137634ff4186 1741 pad_count += real_count *
ansond 0:137634ff4186 1742 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
ansond 0:137634ff4186 1743 }
ansond 0:137634ff4186 1744
ansond 0:137634ff4186 1745 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
ansond 0:137634ff4186 1746
ansond 0:137634ff4186 1747 #if defined(POLARSSL_SSL_DEBUG_ALL)
ansond 0:137634ff4186 1748 if( padlen > 0 && correct == 0 )
ansond 0:137634ff4186 1749 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
ansond 0:137634ff4186 1750 #endif
ansond 0:137634ff4186 1751 padlen &= correct * 0x1FF;
ansond 0:137634ff4186 1752 }
ansond 0:137634ff4186 1753 else
ansond 0:137634ff4186 1754 #endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
ansond 0:137634ff4186 1755 POLARSSL_SSL_PROTO_TLS1_2 */
ansond 0:137634ff4186 1756 {
ansond 0:137634ff4186 1757 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 1758 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 1759 }
ansond 0:137634ff4186 1760
ansond 0:137634ff4186 1761 ssl->in_msglen -= padlen;
ansond 0:137634ff4186 1762 }
ansond 0:137634ff4186 1763 else
ansond 0:137634ff4186 1764 #endif /* POLARSSL_CIPHER_MODE_CBC &&
ansond 0:137634ff4186 1765 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
ansond 0:137634ff4186 1766 {
ansond 0:137634ff4186 1767 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 1768 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 1769 }
ansond 0:137634ff4186 1770
ansond 0:137634ff4186 1771 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
ansond 0:137634ff4186 1772 ssl->in_msg, ssl->in_msglen );
ansond 0:137634ff4186 1773
ansond 0:137634ff4186 1774 /*
ansond 0:137634ff4186 1775 * Authenticate if not done yet.
ansond 0:137634ff4186 1776 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
ansond 0:137634ff4186 1777 */
ansond 0:137634ff4186 1778 #if defined(POLARSSL_SOME_MODES_USE_MAC)
ansond 0:137634ff4186 1779 if( auth_done == 0 )
ansond 0:137634ff4186 1780 {
ansond 0:137634ff4186 1781 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
ansond 0:137634ff4186 1782
ansond 0:137634ff4186 1783 ssl->in_msglen -= ssl->transform_in->maclen;
ansond 0:137634ff4186 1784
ansond 0:137634ff4186 1785 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
ansond 0:137634ff4186 1786 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
ansond 0:137634ff4186 1787
ansond 0:137634ff4186 1788 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
ansond 0:137634ff4186 1789
ansond 0:137634ff4186 1790 #if defined(POLARSSL_SSL_PROTO_SSL3)
ansond 0:137634ff4186 1791 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
ansond 0:137634ff4186 1792 {
ansond 0:137634ff4186 1793 ssl_mac( &ssl->transform_in->md_ctx_dec,
ansond 0:137634ff4186 1794 ssl->transform_in->mac_dec,
ansond 0:137634ff4186 1795 ssl->in_msg, ssl->in_msglen,
ansond 0:137634ff4186 1796 ssl->in_ctr, ssl->in_msgtype );
ansond 0:137634ff4186 1797 }
ansond 0:137634ff4186 1798 else
ansond 0:137634ff4186 1799 #endif /* POLARSSL_SSL_PROTO_SSL3 */
ansond 0:137634ff4186 1800 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
ansond 0:137634ff4186 1801 defined(POLARSSL_SSL_PROTO_TLS1_2)
ansond 0:137634ff4186 1802 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
ansond 0:137634ff4186 1803 {
ansond 0:137634ff4186 1804 /*
ansond 0:137634ff4186 1805 * Process MAC and always update for padlen afterwards to make
ansond 0:137634ff4186 1806 * total time independent of padlen
ansond 0:137634ff4186 1807 *
ansond 0:137634ff4186 1808 * extra_run compensates MAC check for padlen
ansond 0:137634ff4186 1809 *
ansond 0:137634ff4186 1810 * Known timing attacks:
ansond 0:137634ff4186 1811 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
ansond 0:137634ff4186 1812 *
ansond 0:137634ff4186 1813 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
ansond 0:137634ff4186 1814 * correctly. (We round down instead of up, so -56 is the correct
ansond 0:137634ff4186 1815 * value for our calculations instead of -55)
ansond 0:137634ff4186 1816 */
ansond 0:137634ff4186 1817 size_t j, extra_run = 0;
ansond 0:137634ff4186 1818 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
ansond 0:137634ff4186 1819 ( 13 + ssl->in_msglen + 8 ) / 64;
ansond 0:137634ff4186 1820
ansond 0:137634ff4186 1821 extra_run &= correct * 0xFF;
ansond 0:137634ff4186 1822
ansond 0:137634ff4186 1823 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
ansond 0:137634ff4186 1824 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
ansond 0:137634ff4186 1825 ssl->in_msglen );
ansond 0:137634ff4186 1826 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
ansond 0:137634ff4186 1827 ssl->in_msg + ssl->in_msglen );
ansond 0:137634ff4186 1828 /* Call md_process at least once due to cache attacks */
ansond 0:137634ff4186 1829 for( j = 0; j < extra_run + 1; j++ )
ansond 0:137634ff4186 1830 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
ansond 0:137634ff4186 1831
ansond 0:137634ff4186 1832 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
ansond 0:137634ff4186 1833 }
ansond 0:137634ff4186 1834 else
ansond 0:137634ff4186 1835 #endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
ansond 0:137634ff4186 1836 POLARSSL_SSL_PROTO_TLS1_2 */
ansond 0:137634ff4186 1837 {
ansond 0:137634ff4186 1838 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 1839 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 1840 }
ansond 0:137634ff4186 1841
ansond 0:137634ff4186 1842 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
ansond 0:137634ff4186 1843 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
ansond 0:137634ff4186 1844 ssl->transform_in->maclen );
ansond 0:137634ff4186 1845
ansond 0:137634ff4186 1846 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
ansond 0:137634ff4186 1847 ssl->transform_in->maclen ) != 0 )
ansond 0:137634ff4186 1848 {
ansond 0:137634ff4186 1849 #if defined(POLARSSL_SSL_DEBUG_ALL)
ansond 0:137634ff4186 1850 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
ansond 0:137634ff4186 1851 #endif
ansond 0:137634ff4186 1852 correct = 0;
ansond 0:137634ff4186 1853 }
ansond 0:137634ff4186 1854 auth_done++;
ansond 0:137634ff4186 1855
ansond 0:137634ff4186 1856 /*
ansond 0:137634ff4186 1857 * Finally check the correct flag
ansond 0:137634ff4186 1858 */
ansond 0:137634ff4186 1859 if( correct == 0 )
ansond 0:137634ff4186 1860 return( POLARSSL_ERR_SSL_INVALID_MAC );
ansond 0:137634ff4186 1861 }
ansond 0:137634ff4186 1862 #endif /* POLARSSL_SOME_MODES_USE_MAC */
ansond 0:137634ff4186 1863
ansond 0:137634ff4186 1864 /* Make extra sure authentication was performed, exactly once */
ansond 0:137634ff4186 1865 if( auth_done != 1 )
ansond 0:137634ff4186 1866 {
ansond 0:137634ff4186 1867 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 1868 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 1869 }
ansond 0:137634ff4186 1870
ansond 0:137634ff4186 1871 if( ssl->in_msglen == 0 )
ansond 0:137634ff4186 1872 {
ansond 0:137634ff4186 1873 ssl->nb_zero++;
ansond 0:137634ff4186 1874
ansond 0:137634ff4186 1875 /*
ansond 0:137634ff4186 1876 * Three or more empty messages may be a DoS attack
ansond 0:137634ff4186 1877 * (excessive CPU consumption).
ansond 0:137634ff4186 1878 */
ansond 0:137634ff4186 1879 if( ssl->nb_zero > 3 )
ansond 0:137634ff4186 1880 {
ansond 0:137634ff4186 1881 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
ansond 0:137634ff4186 1882 "messages, possible DoS attack" ) );
ansond 0:137634ff4186 1883 return( POLARSSL_ERR_SSL_INVALID_MAC );
ansond 0:137634ff4186 1884 }
ansond 0:137634ff4186 1885 }
ansond 0:137634ff4186 1886 else
ansond 0:137634ff4186 1887 ssl->nb_zero = 0;
ansond 0:137634ff4186 1888
ansond 0:137634ff4186 1889 for( i = 8; i > 0; i-- )
ansond 0:137634ff4186 1890 if( ++ssl->in_ctr[i - 1] != 0 )
ansond 0:137634ff4186 1891 break;
ansond 0:137634ff4186 1892
ansond 0:137634ff4186 1893 /* The loops goes to its end iff the counter is wrapping */
ansond 0:137634ff4186 1894 if( i == 0 )
ansond 0:137634ff4186 1895 {
ansond 0:137634ff4186 1896 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
ansond 0:137634ff4186 1897 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
ansond 0:137634ff4186 1898 }
ansond 0:137634ff4186 1899
ansond 0:137634ff4186 1900 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
ansond 0:137634ff4186 1901
ansond 0:137634ff4186 1902 return( 0 );
ansond 0:137634ff4186 1903 }
ansond 0:137634ff4186 1904
ansond 0:137634ff4186 1905 #undef MAC_NONE
ansond 0:137634ff4186 1906 #undef MAC_PLAINTEXT
ansond 0:137634ff4186 1907 #undef MAC_CIPHERTEXT
ansond 0:137634ff4186 1908
ansond 0:137634ff4186 1909 #if defined(POLARSSL_ZLIB_SUPPORT)
ansond 0:137634ff4186 1910 /*
ansond 0:137634ff4186 1911 * Compression/decompression functions
ansond 0:137634ff4186 1912 */
ansond 0:137634ff4186 1913 static int ssl_compress_buf( ssl_context *ssl )
ansond 0:137634ff4186 1914 {
ansond 0:137634ff4186 1915 int ret;
ansond 0:137634ff4186 1916 unsigned char *msg_post = ssl->out_msg;
ansond 0:137634ff4186 1917 size_t len_pre = ssl->out_msglen;
ansond 0:137634ff4186 1918 unsigned char *msg_pre = ssl->compress_buf;
ansond 0:137634ff4186 1919
ansond 0:137634ff4186 1920 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
ansond 0:137634ff4186 1921
ansond 0:137634ff4186 1922 if( len_pre == 0 )
ansond 0:137634ff4186 1923 return( 0 );
ansond 0:137634ff4186 1924
ansond 0:137634ff4186 1925 memcpy( msg_pre, ssl->out_msg, len_pre );
ansond 0:137634ff4186 1926
ansond 0:137634ff4186 1927 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
ansond 0:137634ff4186 1928 ssl->out_msglen ) );
ansond 0:137634ff4186 1929
ansond 0:137634ff4186 1930 SSL_DEBUG_BUF( 4, "before compression: output payload",
ansond 0:137634ff4186 1931 ssl->out_msg, ssl->out_msglen );
ansond 0:137634ff4186 1932
ansond 0:137634ff4186 1933 ssl->transform_out->ctx_deflate.next_in = msg_pre;
ansond 0:137634ff4186 1934 ssl->transform_out->ctx_deflate.avail_in = len_pre;
ansond 0:137634ff4186 1935 ssl->transform_out->ctx_deflate.next_out = msg_post;
ansond 0:137634ff4186 1936 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
ansond 0:137634ff4186 1937
ansond 0:137634ff4186 1938 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
ansond 0:137634ff4186 1939 if( ret != Z_OK )
ansond 0:137634ff4186 1940 {
ansond 0:137634ff4186 1941 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
ansond 0:137634ff4186 1942 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
ansond 0:137634ff4186 1943 }
ansond 0:137634ff4186 1944
ansond 0:137634ff4186 1945 ssl->out_msglen = SSL_BUFFER_LEN -
ansond 0:137634ff4186 1946 ssl->transform_out->ctx_deflate.avail_out;
ansond 0:137634ff4186 1947
ansond 0:137634ff4186 1948 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
ansond 0:137634ff4186 1949 ssl->out_msglen ) );
ansond 0:137634ff4186 1950
ansond 0:137634ff4186 1951 SSL_DEBUG_BUF( 4, "after compression: output payload",
ansond 0:137634ff4186 1952 ssl->out_msg, ssl->out_msglen );
ansond 0:137634ff4186 1953
ansond 0:137634ff4186 1954 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
ansond 0:137634ff4186 1955
ansond 0:137634ff4186 1956 return( 0 );
ansond 0:137634ff4186 1957 }
ansond 0:137634ff4186 1958
ansond 0:137634ff4186 1959 static int ssl_decompress_buf( ssl_context *ssl )
ansond 0:137634ff4186 1960 {
ansond 0:137634ff4186 1961 int ret;
ansond 0:137634ff4186 1962 unsigned char *msg_post = ssl->in_msg;
ansond 0:137634ff4186 1963 size_t len_pre = ssl->in_msglen;
ansond 0:137634ff4186 1964 unsigned char *msg_pre = ssl->compress_buf;
ansond 0:137634ff4186 1965
ansond 0:137634ff4186 1966 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
ansond 0:137634ff4186 1967
ansond 0:137634ff4186 1968 if( len_pre == 0 )
ansond 0:137634ff4186 1969 return( 0 );
ansond 0:137634ff4186 1970
ansond 0:137634ff4186 1971 memcpy( msg_pre, ssl->in_msg, len_pre );
ansond 0:137634ff4186 1972
ansond 0:137634ff4186 1973 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
ansond 0:137634ff4186 1974 ssl->in_msglen ) );
ansond 0:137634ff4186 1975
ansond 0:137634ff4186 1976 SSL_DEBUG_BUF( 4, "before decompression: input payload",
ansond 0:137634ff4186 1977 ssl->in_msg, ssl->in_msglen );
ansond 0:137634ff4186 1978
ansond 0:137634ff4186 1979 ssl->transform_in->ctx_inflate.next_in = msg_pre;
ansond 0:137634ff4186 1980 ssl->transform_in->ctx_inflate.avail_in = len_pre;
ansond 0:137634ff4186 1981 ssl->transform_in->ctx_inflate.next_out = msg_post;
ansond 0:137634ff4186 1982 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
ansond 0:137634ff4186 1983
ansond 0:137634ff4186 1984 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
ansond 0:137634ff4186 1985 if( ret != Z_OK )
ansond 0:137634ff4186 1986 {
ansond 0:137634ff4186 1987 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
ansond 0:137634ff4186 1988 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
ansond 0:137634ff4186 1989 }
ansond 0:137634ff4186 1990
ansond 0:137634ff4186 1991 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
ansond 0:137634ff4186 1992 ssl->transform_in->ctx_inflate.avail_out;
ansond 0:137634ff4186 1993
ansond 0:137634ff4186 1994 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
ansond 0:137634ff4186 1995 ssl->in_msglen ) );
ansond 0:137634ff4186 1996
ansond 0:137634ff4186 1997 SSL_DEBUG_BUF( 4, "after decompression: input payload",
ansond 0:137634ff4186 1998 ssl->in_msg, ssl->in_msglen );
ansond 0:137634ff4186 1999
ansond 0:137634ff4186 2000 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
ansond 0:137634ff4186 2001
ansond 0:137634ff4186 2002 return( 0 );
ansond 0:137634ff4186 2003 }
ansond 0:137634ff4186 2004 #endif /* POLARSSL_ZLIB_SUPPORT */
ansond 0:137634ff4186 2005
ansond 0:137634ff4186 2006 /*
ansond 0:137634ff4186 2007 * Fill the input message buffer
ansond 0:137634ff4186 2008 */
ansond 0:137634ff4186 2009 int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
ansond 0:137634ff4186 2010 {
ansond 0:137634ff4186 2011 int ret;
ansond 0:137634ff4186 2012 size_t len;
ansond 0:137634ff4186 2013
ansond 0:137634ff4186 2014 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
ansond 0:137634ff4186 2015
ansond 0:137634ff4186 2016 if( nb_want > SSL_BUFFER_LEN - 8 )
ansond 0:137634ff4186 2017 {
ansond 0:137634ff4186 2018 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
ansond 0:137634ff4186 2019 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
ansond 0:137634ff4186 2020 }
ansond 0:137634ff4186 2021
ansond 0:137634ff4186 2022 while( ssl->in_left < nb_want )
ansond 0:137634ff4186 2023 {
ansond 0:137634ff4186 2024 len = nb_want - ssl->in_left;
ansond 0:137634ff4186 2025 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
ansond 0:137634ff4186 2026
ansond 0:137634ff4186 2027 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
ansond 0:137634ff4186 2028 ssl->in_left, nb_want ) );
ansond 0:137634ff4186 2029 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
ansond 0:137634ff4186 2030
ansond 0:137634ff4186 2031 if( ret == 0 )
ansond 0:137634ff4186 2032 return( POLARSSL_ERR_SSL_CONN_EOF );
ansond 0:137634ff4186 2033
ansond 0:137634ff4186 2034 if( ret < 0 )
ansond 0:137634ff4186 2035 return( ret );
ansond 0:137634ff4186 2036
ansond 0:137634ff4186 2037 ssl->in_left += ret;
ansond 0:137634ff4186 2038 }
ansond 0:137634ff4186 2039
ansond 0:137634ff4186 2040 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
ansond 0:137634ff4186 2041
ansond 0:137634ff4186 2042 return( 0 );
ansond 0:137634ff4186 2043 }
ansond 0:137634ff4186 2044
ansond 0:137634ff4186 2045 /*
ansond 0:137634ff4186 2046 * Flush any data not yet written
ansond 0:137634ff4186 2047 */
ansond 0:137634ff4186 2048 int ssl_flush_output( ssl_context *ssl )
ansond 0:137634ff4186 2049 {
ansond 0:137634ff4186 2050 int ret;
ansond 0:137634ff4186 2051 unsigned char *buf;
ansond 0:137634ff4186 2052
ansond 0:137634ff4186 2053 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
ansond 0:137634ff4186 2054
ansond 0:137634ff4186 2055 while( ssl->out_left > 0 )
ansond 0:137634ff4186 2056 {
ansond 0:137634ff4186 2057 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
ansond 0:137634ff4186 2058 5 + ssl->out_msglen, ssl->out_left ) );
ansond 0:137634ff4186 2059
ansond 0:137634ff4186 2060 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
ansond 0:137634ff4186 2061 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
ansond 0:137634ff4186 2062
ansond 0:137634ff4186 2063 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
ansond 0:137634ff4186 2064
ansond 0:137634ff4186 2065 if( ret <= 0 )
ansond 0:137634ff4186 2066 return( ret );
ansond 0:137634ff4186 2067
ansond 0:137634ff4186 2068 ssl->out_left -= ret;
ansond 0:137634ff4186 2069 }
ansond 0:137634ff4186 2070
ansond 0:137634ff4186 2071 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
ansond 0:137634ff4186 2072
ansond 0:137634ff4186 2073 return( 0 );
ansond 0:137634ff4186 2074 }
ansond 0:137634ff4186 2075
ansond 0:137634ff4186 2076 /*
ansond 0:137634ff4186 2077 * Record layer functions
ansond 0:137634ff4186 2078 */
ansond 0:137634ff4186 2079 int ssl_write_record( ssl_context *ssl )
ansond 0:137634ff4186 2080 {
ansond 0:137634ff4186 2081 int ret, done = 0;
ansond 0:137634ff4186 2082 size_t len = ssl->out_msglen;
ansond 0:137634ff4186 2083
ansond 0:137634ff4186 2084 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
ansond 0:137634ff4186 2085
ansond 0:137634ff4186 2086 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
ansond 0:137634ff4186 2087 {
ansond 0:137634ff4186 2088 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
ansond 0:137634ff4186 2089 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
ansond 0:137634ff4186 2090 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
ansond 0:137634ff4186 2091
ansond 0:137634ff4186 2092 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
ansond 0:137634ff4186 2093 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
ansond 0:137634ff4186 2094 }
ansond 0:137634ff4186 2095
ansond 0:137634ff4186 2096 #if defined(POLARSSL_ZLIB_SUPPORT)
ansond 0:137634ff4186 2097 if( ssl->transform_out != NULL &&
ansond 0:137634ff4186 2098 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
ansond 0:137634ff4186 2099 {
ansond 0:137634ff4186 2100 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
ansond 0:137634ff4186 2101 {
ansond 0:137634ff4186 2102 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
ansond 0:137634ff4186 2103 return( ret );
ansond 0:137634ff4186 2104 }
ansond 0:137634ff4186 2105
ansond 0:137634ff4186 2106 len = ssl->out_msglen;
ansond 0:137634ff4186 2107 }
ansond 0:137634ff4186 2108 #endif /*POLARSSL_ZLIB_SUPPORT */
ansond 0:137634ff4186 2109
ansond 0:137634ff4186 2110 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
ansond 0:137634ff4186 2111 if( ssl_hw_record_write != NULL )
ansond 0:137634ff4186 2112 {
ansond 0:137634ff4186 2113 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
ansond 0:137634ff4186 2114
ansond 0:137634ff4186 2115 ret = ssl_hw_record_write( ssl );
ansond 0:137634ff4186 2116 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
ansond 0:137634ff4186 2117 {
ansond 0:137634ff4186 2118 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
ansond 0:137634ff4186 2119 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
ansond 0:137634ff4186 2120 }
ansond 0:137634ff4186 2121
ansond 0:137634ff4186 2122 if( ret == 0 )
ansond 0:137634ff4186 2123 done = 1;
ansond 0:137634ff4186 2124 }
ansond 0:137634ff4186 2125 #endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
ansond 0:137634ff4186 2126 if( !done )
ansond 0:137634ff4186 2127 {
ansond 0:137634ff4186 2128 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
ansond 0:137634ff4186 2129 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
ansond 0:137634ff4186 2130 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
ansond 0:137634ff4186 2131 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
ansond 0:137634ff4186 2132 ssl->out_hdr[4] = (unsigned char)( len );
ansond 0:137634ff4186 2133
ansond 0:137634ff4186 2134 if( ssl->transform_out != NULL )
ansond 0:137634ff4186 2135 {
ansond 0:137634ff4186 2136 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
ansond 0:137634ff4186 2137 {
ansond 0:137634ff4186 2138 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
ansond 0:137634ff4186 2139 return( ret );
ansond 0:137634ff4186 2140 }
ansond 0:137634ff4186 2141
ansond 0:137634ff4186 2142 len = ssl->out_msglen;
ansond 0:137634ff4186 2143 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
ansond 0:137634ff4186 2144 ssl->out_hdr[4] = (unsigned char)( len );
ansond 0:137634ff4186 2145 }
ansond 0:137634ff4186 2146
ansond 0:137634ff4186 2147 ssl->out_left = 5 + ssl->out_msglen;
ansond 0:137634ff4186 2148
ansond 0:137634ff4186 2149 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
ansond 0:137634ff4186 2150 "version = [%d:%d], msglen = %d",
ansond 0:137634ff4186 2151 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
ansond 0:137634ff4186 2152 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
ansond 0:137634ff4186 2153
ansond 0:137634ff4186 2154 SSL_DEBUG_BUF( 4, "output record sent to network",
ansond 0:137634ff4186 2155 ssl->out_hdr, 5 + ssl->out_msglen );
ansond 0:137634ff4186 2156 }
ansond 0:137634ff4186 2157
ansond 0:137634ff4186 2158 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
ansond 0:137634ff4186 2159 {
ansond 0:137634ff4186 2160 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
ansond 0:137634ff4186 2161 return( ret );
ansond 0:137634ff4186 2162 }
ansond 0:137634ff4186 2163
ansond 0:137634ff4186 2164 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
ansond 0:137634ff4186 2165
ansond 0:137634ff4186 2166 return( 0 );
ansond 0:137634ff4186 2167 }
ansond 0:137634ff4186 2168
ansond 0:137634ff4186 2169 int ssl_read_record( ssl_context *ssl )
ansond 0:137634ff4186 2170 {
ansond 0:137634ff4186 2171 int ret, done = 0;
ansond 0:137634ff4186 2172
ansond 0:137634ff4186 2173 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
ansond 0:137634ff4186 2174
ansond 0:137634ff4186 2175 if( ssl->in_hslen != 0 &&
ansond 0:137634ff4186 2176 ssl->in_hslen < ssl->in_msglen )
ansond 0:137634ff4186 2177 {
ansond 0:137634ff4186 2178 /*
ansond 0:137634ff4186 2179 * Get next Handshake message in the current record
ansond 0:137634ff4186 2180 */
ansond 0:137634ff4186 2181 ssl->in_msglen -= ssl->in_hslen;
ansond 0:137634ff4186 2182
ansond 0:137634ff4186 2183 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
ansond 0:137634ff4186 2184 ssl->in_msglen );
ansond 0:137634ff4186 2185
ansond 0:137634ff4186 2186 ssl->in_hslen = 4;
ansond 0:137634ff4186 2187 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
ansond 0:137634ff4186 2188
ansond 0:137634ff4186 2189 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
ansond 0:137634ff4186 2190 " %d, type = %d, hslen = %d",
ansond 0:137634ff4186 2191 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
ansond 0:137634ff4186 2192
ansond 0:137634ff4186 2193 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
ansond 0:137634ff4186 2194 {
ansond 0:137634ff4186 2195 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
ansond 0:137634ff4186 2196 return( POLARSSL_ERR_SSL_INVALID_RECORD );
ansond 0:137634ff4186 2197 }
ansond 0:137634ff4186 2198
ansond 0:137634ff4186 2199 if( ssl->in_msglen < ssl->in_hslen )
ansond 0:137634ff4186 2200 {
ansond 0:137634ff4186 2201 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
ansond 0:137634ff4186 2202 return( POLARSSL_ERR_SSL_INVALID_RECORD );
ansond 0:137634ff4186 2203 }
ansond 0:137634ff4186 2204
ansond 0:137634ff4186 2205 if( ssl->state != SSL_HANDSHAKE_OVER )
ansond 0:137634ff4186 2206 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
ansond 0:137634ff4186 2207
ansond 0:137634ff4186 2208 return( 0 );
ansond 0:137634ff4186 2209 }
ansond 0:137634ff4186 2210
ansond 0:137634ff4186 2211 ssl->in_hslen = 0;
ansond 0:137634ff4186 2212
ansond 0:137634ff4186 2213 /*
ansond 0:137634ff4186 2214 * Read the record header and validate it
ansond 0:137634ff4186 2215 */
ansond 0:137634ff4186 2216 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
ansond 0:137634ff4186 2217 {
ansond 0:137634ff4186 2218 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
ansond 0:137634ff4186 2219 return( ret );
ansond 0:137634ff4186 2220 }
ansond 0:137634ff4186 2221
ansond 0:137634ff4186 2222 ssl->in_msgtype = ssl->in_hdr[0];
ansond 0:137634ff4186 2223 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
ansond 0:137634ff4186 2224
ansond 0:137634ff4186 2225 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
ansond 0:137634ff4186 2226 "version = [%d:%d], msglen = %d",
ansond 0:137634ff4186 2227 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
ansond 0:137634ff4186 2228 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
ansond 0:137634ff4186 2229
ansond 0:137634ff4186 2230 if( ssl->in_hdr[1] != ssl->major_ver )
ansond 0:137634ff4186 2231 {
ansond 0:137634ff4186 2232 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
ansond 0:137634ff4186 2233 return( POLARSSL_ERR_SSL_INVALID_RECORD );
ansond 0:137634ff4186 2234 }
ansond 0:137634ff4186 2235
ansond 0:137634ff4186 2236 if( ssl->in_hdr[2] > ssl->max_minor_ver )
ansond 0:137634ff4186 2237 {
ansond 0:137634ff4186 2238 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
ansond 0:137634ff4186 2239 return( POLARSSL_ERR_SSL_INVALID_RECORD );
ansond 0:137634ff4186 2240 }
ansond 0:137634ff4186 2241
ansond 0:137634ff4186 2242 /* Sanity check (outer boundaries) */
ansond 0:137634ff4186 2243 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
ansond 0:137634ff4186 2244 {
ansond 0:137634ff4186 2245 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
ansond 0:137634ff4186 2246 return( POLARSSL_ERR_SSL_INVALID_RECORD );
ansond 0:137634ff4186 2247 }
ansond 0:137634ff4186 2248
ansond 0:137634ff4186 2249 /*
ansond 0:137634ff4186 2250 * Make sure the message length is acceptable for the current transform
ansond 0:137634ff4186 2251 * and protocol version.
ansond 0:137634ff4186 2252 */
ansond 0:137634ff4186 2253 if( ssl->transform_in == NULL )
ansond 0:137634ff4186 2254 {
ansond 0:137634ff4186 2255 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
ansond 0:137634ff4186 2256 {
ansond 0:137634ff4186 2257 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
ansond 0:137634ff4186 2258 return( POLARSSL_ERR_SSL_INVALID_RECORD );
ansond 0:137634ff4186 2259 }
ansond 0:137634ff4186 2260 }
ansond 0:137634ff4186 2261 else
ansond 0:137634ff4186 2262 {
ansond 0:137634ff4186 2263 if( ssl->in_msglen < ssl->transform_in->minlen )
ansond 0:137634ff4186 2264 {
ansond 0:137634ff4186 2265 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
ansond 0:137634ff4186 2266 return( POLARSSL_ERR_SSL_INVALID_RECORD );
ansond 0:137634ff4186 2267 }
ansond 0:137634ff4186 2268
ansond 0:137634ff4186 2269 #if defined(POLARSSL_SSL_PROTO_SSL3)
ansond 0:137634ff4186 2270 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
ansond 0:137634ff4186 2271 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
ansond 0:137634ff4186 2272 {
ansond 0:137634ff4186 2273 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
ansond 0:137634ff4186 2274 return( POLARSSL_ERR_SSL_INVALID_RECORD );
ansond 0:137634ff4186 2275 }
ansond 0:137634ff4186 2276 #endif
ansond 0:137634ff4186 2277
ansond 0:137634ff4186 2278 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
ansond 0:137634ff4186 2279 defined(POLARSSL_SSL_PROTO_TLS1_2)
ansond 0:137634ff4186 2280 /*
ansond 0:137634ff4186 2281 * TLS encrypted messages can have up to 256 bytes of padding
ansond 0:137634ff4186 2282 */
ansond 0:137634ff4186 2283 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
ansond 0:137634ff4186 2284 ssl->in_msglen > ssl->transform_in->minlen +
ansond 0:137634ff4186 2285 SSL_MAX_CONTENT_LEN + 256 )
ansond 0:137634ff4186 2286 {
ansond 0:137634ff4186 2287 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
ansond 0:137634ff4186 2288 return( POLARSSL_ERR_SSL_INVALID_RECORD );
ansond 0:137634ff4186 2289 }
ansond 0:137634ff4186 2290 #endif
ansond 0:137634ff4186 2291 }
ansond 0:137634ff4186 2292
ansond 0:137634ff4186 2293 /*
ansond 0:137634ff4186 2294 * Read and optionally decrypt the message contents
ansond 0:137634ff4186 2295 */
ansond 0:137634ff4186 2296 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
ansond 0:137634ff4186 2297 {
ansond 0:137634ff4186 2298 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
ansond 0:137634ff4186 2299 return( ret );
ansond 0:137634ff4186 2300 }
ansond 0:137634ff4186 2301
ansond 0:137634ff4186 2302 SSL_DEBUG_BUF( 4, "input record from network",
ansond 0:137634ff4186 2303 ssl->in_hdr, 5 + ssl->in_msglen );
ansond 0:137634ff4186 2304
ansond 0:137634ff4186 2305 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
ansond 0:137634ff4186 2306 if( ssl_hw_record_read != NULL )
ansond 0:137634ff4186 2307 {
ansond 0:137634ff4186 2308 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
ansond 0:137634ff4186 2309
ansond 0:137634ff4186 2310 ret = ssl_hw_record_read( ssl );
ansond 0:137634ff4186 2311 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
ansond 0:137634ff4186 2312 {
ansond 0:137634ff4186 2313 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
ansond 0:137634ff4186 2314 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
ansond 0:137634ff4186 2315 }
ansond 0:137634ff4186 2316
ansond 0:137634ff4186 2317 if( ret == 0 )
ansond 0:137634ff4186 2318 done = 1;
ansond 0:137634ff4186 2319 }
ansond 0:137634ff4186 2320 #endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
ansond 0:137634ff4186 2321 if( !done && ssl->transform_in != NULL )
ansond 0:137634ff4186 2322 {
ansond 0:137634ff4186 2323 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
ansond 0:137634ff4186 2324 {
ansond 0:137634ff4186 2325 #if defined(POLARSSL_SSL_ALERT_MESSAGES)
ansond 0:137634ff4186 2326 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
ansond 0:137634ff4186 2327 {
ansond 0:137634ff4186 2328 ssl_send_alert_message( ssl,
ansond 0:137634ff4186 2329 SSL_ALERT_LEVEL_FATAL,
ansond 0:137634ff4186 2330 SSL_ALERT_MSG_BAD_RECORD_MAC );
ansond 0:137634ff4186 2331 }
ansond 0:137634ff4186 2332 #endif
ansond 0:137634ff4186 2333 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
ansond 0:137634ff4186 2334 return( ret );
ansond 0:137634ff4186 2335 }
ansond 0:137634ff4186 2336
ansond 0:137634ff4186 2337 SSL_DEBUG_BUF( 4, "input payload after decrypt",
ansond 0:137634ff4186 2338 ssl->in_msg, ssl->in_msglen );
ansond 0:137634ff4186 2339
ansond 0:137634ff4186 2340 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
ansond 0:137634ff4186 2341 {
ansond 0:137634ff4186 2342 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
ansond 0:137634ff4186 2343 return( POLARSSL_ERR_SSL_INVALID_RECORD );
ansond 0:137634ff4186 2344 }
ansond 0:137634ff4186 2345 }
ansond 0:137634ff4186 2346
ansond 0:137634ff4186 2347 #if defined(POLARSSL_ZLIB_SUPPORT)
ansond 0:137634ff4186 2348 if( ssl->transform_in != NULL &&
ansond 0:137634ff4186 2349 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
ansond 0:137634ff4186 2350 {
ansond 0:137634ff4186 2351 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
ansond 0:137634ff4186 2352 {
ansond 0:137634ff4186 2353 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
ansond 0:137634ff4186 2354 return( ret );
ansond 0:137634ff4186 2355 }
ansond 0:137634ff4186 2356
ansond 0:137634ff4186 2357 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
ansond 0:137634ff4186 2358 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
ansond 0:137634ff4186 2359 }
ansond 0:137634ff4186 2360 #endif /* POLARSSL_ZLIB_SUPPORT */
ansond 0:137634ff4186 2361
ansond 0:137634ff4186 2362 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
ansond 0:137634ff4186 2363 ssl->in_msgtype != SSL_MSG_ALERT &&
ansond 0:137634ff4186 2364 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
ansond 0:137634ff4186 2365 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
ansond 0:137634ff4186 2366 {
ansond 0:137634ff4186 2367 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
ansond 0:137634ff4186 2368
ansond 0:137634ff4186 2369 if( ( ret = ssl_send_alert_message( ssl,
ansond 0:137634ff4186 2370 SSL_ALERT_LEVEL_FATAL,
ansond 0:137634ff4186 2371 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
ansond 0:137634ff4186 2372 {
ansond 0:137634ff4186 2373 return( ret );
ansond 0:137634ff4186 2374 }
ansond 0:137634ff4186 2375
ansond 0:137634ff4186 2376 return( POLARSSL_ERR_SSL_INVALID_RECORD );
ansond 0:137634ff4186 2377 }
ansond 0:137634ff4186 2378
ansond 0:137634ff4186 2379 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
ansond 0:137634ff4186 2380 {
ansond 0:137634ff4186 2381 ssl->in_hslen = 4;
ansond 0:137634ff4186 2382 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
ansond 0:137634ff4186 2383
ansond 0:137634ff4186 2384 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
ansond 0:137634ff4186 2385 " %d, type = %d, hslen = %d",
ansond 0:137634ff4186 2386 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
ansond 0:137634ff4186 2387
ansond 0:137634ff4186 2388 /*
ansond 0:137634ff4186 2389 * Additional checks to validate the handshake header
ansond 0:137634ff4186 2390 */
ansond 0:137634ff4186 2391 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
ansond 0:137634ff4186 2392 {
ansond 0:137634ff4186 2393 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
ansond 0:137634ff4186 2394 return( POLARSSL_ERR_SSL_INVALID_RECORD );
ansond 0:137634ff4186 2395 }
ansond 0:137634ff4186 2396
ansond 0:137634ff4186 2397 if( ssl->in_msglen < ssl->in_hslen )
ansond 0:137634ff4186 2398 {
ansond 0:137634ff4186 2399 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
ansond 0:137634ff4186 2400 return( POLARSSL_ERR_SSL_INVALID_RECORD );
ansond 0:137634ff4186 2401 }
ansond 0:137634ff4186 2402
ansond 0:137634ff4186 2403 if( ssl->state != SSL_HANDSHAKE_OVER )
ansond 0:137634ff4186 2404 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
ansond 0:137634ff4186 2405 }
ansond 0:137634ff4186 2406
ansond 0:137634ff4186 2407 if( ssl->in_msgtype == SSL_MSG_ALERT )
ansond 0:137634ff4186 2408 {
ansond 0:137634ff4186 2409 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
ansond 0:137634ff4186 2410 ssl->in_msg[0], ssl->in_msg[1] ) );
ansond 0:137634ff4186 2411
ansond 0:137634ff4186 2412 /*
ansond 0:137634ff4186 2413 * Ignore non-fatal alerts, except close_notify
ansond 0:137634ff4186 2414 */
ansond 0:137634ff4186 2415 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
ansond 0:137634ff4186 2416 {
ansond 0:137634ff4186 2417 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
ansond 0:137634ff4186 2418 ssl->in_msg[1] ) );
ansond 0:137634ff4186 2419 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
ansond 0:137634ff4186 2420 }
ansond 0:137634ff4186 2421
ansond 0:137634ff4186 2422 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
ansond 0:137634ff4186 2423 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
ansond 0:137634ff4186 2424 {
ansond 0:137634ff4186 2425 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
ansond 0:137634ff4186 2426 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
ansond 0:137634ff4186 2427 }
ansond 0:137634ff4186 2428 }
ansond 0:137634ff4186 2429
ansond 0:137634ff4186 2430 ssl->in_left = 0;
ansond 0:137634ff4186 2431
ansond 0:137634ff4186 2432 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
ansond 0:137634ff4186 2433
ansond 0:137634ff4186 2434 return( 0 );
ansond 0:137634ff4186 2435 }
ansond 0:137634ff4186 2436
ansond 0:137634ff4186 2437 int ssl_send_fatal_handshake_failure( ssl_context *ssl )
ansond 0:137634ff4186 2438 {
ansond 0:137634ff4186 2439 int ret;
ansond 0:137634ff4186 2440
ansond 0:137634ff4186 2441 if( ( ret = ssl_send_alert_message( ssl,
ansond 0:137634ff4186 2442 SSL_ALERT_LEVEL_FATAL,
ansond 0:137634ff4186 2443 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
ansond 0:137634ff4186 2444 {
ansond 0:137634ff4186 2445 return( ret );
ansond 0:137634ff4186 2446 }
ansond 0:137634ff4186 2447
ansond 0:137634ff4186 2448 return( 0 );
ansond 0:137634ff4186 2449 }
ansond 0:137634ff4186 2450
ansond 0:137634ff4186 2451 int ssl_send_alert_message( ssl_context *ssl,
ansond 0:137634ff4186 2452 unsigned char level,
ansond 0:137634ff4186 2453 unsigned char message )
ansond 0:137634ff4186 2454 {
ansond 0:137634ff4186 2455 int ret;
ansond 0:137634ff4186 2456
ansond 0:137634ff4186 2457 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
ansond 0:137634ff4186 2458
ansond 0:137634ff4186 2459 ssl->out_msgtype = SSL_MSG_ALERT;
ansond 0:137634ff4186 2460 ssl->out_msglen = 2;
ansond 0:137634ff4186 2461 ssl->out_msg[0] = level;
ansond 0:137634ff4186 2462 ssl->out_msg[1] = message;
ansond 0:137634ff4186 2463
ansond 0:137634ff4186 2464 if( ( ret = ssl_write_record( ssl ) ) != 0 )
ansond 0:137634ff4186 2465 {
ansond 0:137634ff4186 2466 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
ansond 0:137634ff4186 2467 return( ret );
ansond 0:137634ff4186 2468 }
ansond 0:137634ff4186 2469
ansond 0:137634ff4186 2470 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
ansond 0:137634ff4186 2471
ansond 0:137634ff4186 2472 return( 0 );
ansond 0:137634ff4186 2473 }
ansond 0:137634ff4186 2474
ansond 0:137634ff4186 2475 /*
ansond 0:137634ff4186 2476 * Handshake functions
ansond 0:137634ff4186 2477 */
ansond 0:137634ff4186 2478 #if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
ansond 0:137634ff4186 2479 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
ansond 0:137634ff4186 2480 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
ansond 0:137634ff4186 2481 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
ansond 0:137634ff4186 2482 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
ansond 0:137634ff4186 2483 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
ansond 0:137634ff4186 2484 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
ansond 0:137634ff4186 2485 int ssl_write_certificate( ssl_context *ssl )
ansond 0:137634ff4186 2486 {
ansond 0:137634ff4186 2487 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
ansond 0:137634ff4186 2488
ansond 0:137634ff4186 2489 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
ansond 0:137634ff4186 2490
ansond 0:137634ff4186 2491 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
ansond 0:137634ff4186 2492 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
ansond 0:137634ff4186 2493 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
ansond 0:137634ff4186 2494 {
ansond 0:137634ff4186 2495 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
ansond 0:137634ff4186 2496 ssl->state++;
ansond 0:137634ff4186 2497 return( 0 );
ansond 0:137634ff4186 2498 }
ansond 0:137634ff4186 2499
ansond 0:137634ff4186 2500 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 2501 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 2502 }
ansond 0:137634ff4186 2503
ansond 0:137634ff4186 2504 int ssl_parse_certificate( ssl_context *ssl )
ansond 0:137634ff4186 2505 {
ansond 0:137634ff4186 2506 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
ansond 0:137634ff4186 2507
ansond 0:137634ff4186 2508 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
ansond 0:137634ff4186 2509
ansond 0:137634ff4186 2510 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
ansond 0:137634ff4186 2511 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
ansond 0:137634ff4186 2512 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
ansond 0:137634ff4186 2513 {
ansond 0:137634ff4186 2514 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
ansond 0:137634ff4186 2515 ssl->state++;
ansond 0:137634ff4186 2516 return( 0 );
ansond 0:137634ff4186 2517 }
ansond 0:137634ff4186 2518
ansond 0:137634ff4186 2519 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 2520 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 2521 }
ansond 0:137634ff4186 2522 #else
ansond 0:137634ff4186 2523 int ssl_write_certificate( ssl_context *ssl )
ansond 0:137634ff4186 2524 {
ansond 0:137634ff4186 2525 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
ansond 0:137634ff4186 2526 size_t i, n;
ansond 0:137634ff4186 2527 const x509_crt *crt;
ansond 0:137634ff4186 2528 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
ansond 0:137634ff4186 2529
ansond 0:137634ff4186 2530 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
ansond 0:137634ff4186 2531
ansond 0:137634ff4186 2532 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
ansond 0:137634ff4186 2533 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
ansond 0:137634ff4186 2534 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
ansond 0:137634ff4186 2535 {
ansond 0:137634ff4186 2536 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
ansond 0:137634ff4186 2537 ssl->state++;
ansond 0:137634ff4186 2538 return( 0 );
ansond 0:137634ff4186 2539 }
ansond 0:137634ff4186 2540
ansond 0:137634ff4186 2541 #if defined(POLARSSL_SSL_CLI_C)
ansond 0:137634ff4186 2542 if( ssl->endpoint == SSL_IS_CLIENT )
ansond 0:137634ff4186 2543 {
ansond 0:137634ff4186 2544 if( ssl->client_auth == 0 )
ansond 0:137634ff4186 2545 {
ansond 0:137634ff4186 2546 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
ansond 0:137634ff4186 2547 ssl->state++;
ansond 0:137634ff4186 2548 return( 0 );
ansond 0:137634ff4186 2549 }
ansond 0:137634ff4186 2550
ansond 0:137634ff4186 2551 #if defined(POLARSSL_SSL_PROTO_SSL3)
ansond 0:137634ff4186 2552 /*
ansond 0:137634ff4186 2553 * If using SSLv3 and got no cert, send an Alert message
ansond 0:137634ff4186 2554 * (otherwise an empty Certificate message will be sent).
ansond 0:137634ff4186 2555 */
ansond 0:137634ff4186 2556 if( ssl_own_cert( ssl ) == NULL &&
ansond 0:137634ff4186 2557 ssl->minor_ver == SSL_MINOR_VERSION_0 )
ansond 0:137634ff4186 2558 {
ansond 0:137634ff4186 2559 ssl->out_msglen = 2;
ansond 0:137634ff4186 2560 ssl->out_msgtype = SSL_MSG_ALERT;
ansond 0:137634ff4186 2561 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
ansond 0:137634ff4186 2562 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
ansond 0:137634ff4186 2563
ansond 0:137634ff4186 2564 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
ansond 0:137634ff4186 2565 goto write_msg;
ansond 0:137634ff4186 2566 }
ansond 0:137634ff4186 2567 #endif /* POLARSSL_SSL_PROTO_SSL3 */
ansond 0:137634ff4186 2568 }
ansond 0:137634ff4186 2569 #endif /* POLARSSL_SSL_CLI_C */
ansond 0:137634ff4186 2570 #if defined(POLARSSL_SSL_SRV_C)
ansond 0:137634ff4186 2571 if( ssl->endpoint == SSL_IS_SERVER )
ansond 0:137634ff4186 2572 {
ansond 0:137634ff4186 2573 if( ssl_own_cert( ssl ) == NULL )
ansond 0:137634ff4186 2574 {
ansond 0:137634ff4186 2575 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
ansond 0:137634ff4186 2576 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
ansond 0:137634ff4186 2577 }
ansond 0:137634ff4186 2578 }
ansond 0:137634ff4186 2579 #endif
ansond 0:137634ff4186 2580
ansond 0:137634ff4186 2581 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
ansond 0:137634ff4186 2582
ansond 0:137634ff4186 2583 /*
ansond 0:137634ff4186 2584 * 0 . 0 handshake type
ansond 0:137634ff4186 2585 * 1 . 3 handshake length
ansond 0:137634ff4186 2586 * 4 . 6 length of all certs
ansond 0:137634ff4186 2587 * 7 . 9 length of cert. 1
ansond 0:137634ff4186 2588 * 10 . n-1 peer certificate
ansond 0:137634ff4186 2589 * n . n+2 length of cert. 2
ansond 0:137634ff4186 2590 * n+3 . ... upper level cert, etc.
ansond 0:137634ff4186 2591 */
ansond 0:137634ff4186 2592 i = 7;
ansond 0:137634ff4186 2593 crt = ssl_own_cert( ssl );
ansond 0:137634ff4186 2594
ansond 0:137634ff4186 2595 while( crt != NULL )
ansond 0:137634ff4186 2596 {
ansond 0:137634ff4186 2597 n = crt->raw.len;
ansond 0:137634ff4186 2598 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
ansond 0:137634ff4186 2599 {
ansond 0:137634ff4186 2600 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
ansond 0:137634ff4186 2601 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
ansond 0:137634ff4186 2602 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
ansond 0:137634ff4186 2603 }
ansond 0:137634ff4186 2604
ansond 0:137634ff4186 2605 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
ansond 0:137634ff4186 2606 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
ansond 0:137634ff4186 2607 ssl->out_msg[i + 2] = (unsigned char)( n );
ansond 0:137634ff4186 2608
ansond 0:137634ff4186 2609 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
ansond 0:137634ff4186 2610 i += n; crt = crt->next;
ansond 0:137634ff4186 2611 }
ansond 0:137634ff4186 2612
ansond 0:137634ff4186 2613 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
ansond 0:137634ff4186 2614 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
ansond 0:137634ff4186 2615 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
ansond 0:137634ff4186 2616
ansond 0:137634ff4186 2617 ssl->out_msglen = i;
ansond 0:137634ff4186 2618 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
ansond 0:137634ff4186 2619 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
ansond 0:137634ff4186 2620
ansond 0:137634ff4186 2621 #if defined(POLARSSL_SSL_PROTO_SSL3)
ansond 0:137634ff4186 2622 write_msg:
ansond 0:137634ff4186 2623 #endif
ansond 0:137634ff4186 2624
ansond 0:137634ff4186 2625 ssl->state++;
ansond 0:137634ff4186 2626
ansond 0:137634ff4186 2627 if( ( ret = ssl_write_record( ssl ) ) != 0 )
ansond 0:137634ff4186 2628 {
ansond 0:137634ff4186 2629 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
ansond 0:137634ff4186 2630 return( ret );
ansond 0:137634ff4186 2631 }
ansond 0:137634ff4186 2632
ansond 0:137634ff4186 2633 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
ansond 0:137634ff4186 2634
ansond 0:137634ff4186 2635 return( ret );
ansond 0:137634ff4186 2636 }
ansond 0:137634ff4186 2637
ansond 0:137634ff4186 2638 int ssl_parse_certificate( ssl_context *ssl )
ansond 0:137634ff4186 2639 {
ansond 0:137634ff4186 2640 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
ansond 0:137634ff4186 2641 size_t i, n;
ansond 0:137634ff4186 2642 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
ansond 0:137634ff4186 2643
ansond 0:137634ff4186 2644 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
ansond 0:137634ff4186 2645
ansond 0:137634ff4186 2646 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
ansond 0:137634ff4186 2647 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
ansond 0:137634ff4186 2648 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
ansond 0:137634ff4186 2649 {
ansond 0:137634ff4186 2650 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
ansond 0:137634ff4186 2651 ssl->state++;
ansond 0:137634ff4186 2652 return( 0 );
ansond 0:137634ff4186 2653 }
ansond 0:137634ff4186 2654
ansond 0:137634ff4186 2655 #if defined(POLARSSL_SSL_SRV_C)
ansond 0:137634ff4186 2656 if( ssl->endpoint == SSL_IS_SERVER &&
ansond 0:137634ff4186 2657 ( ssl->authmode == SSL_VERIFY_NONE ||
ansond 0:137634ff4186 2658 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
ansond 0:137634ff4186 2659 {
ansond 0:137634ff4186 2660 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
ansond 0:137634ff4186 2661 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
ansond 0:137634ff4186 2662 ssl->state++;
ansond 0:137634ff4186 2663 return( 0 );
ansond 0:137634ff4186 2664 }
ansond 0:137634ff4186 2665 #endif
ansond 0:137634ff4186 2666
ansond 0:137634ff4186 2667 if( ( ret = ssl_read_record( ssl ) ) != 0 )
ansond 0:137634ff4186 2668 {
ansond 0:137634ff4186 2669 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
ansond 0:137634ff4186 2670 return( ret );
ansond 0:137634ff4186 2671 }
ansond 0:137634ff4186 2672
ansond 0:137634ff4186 2673 ssl->state++;
ansond 0:137634ff4186 2674
ansond 0:137634ff4186 2675 #if defined(POLARSSL_SSL_SRV_C)
ansond 0:137634ff4186 2676 #if defined(POLARSSL_SSL_PROTO_SSL3)
ansond 0:137634ff4186 2677 /*
ansond 0:137634ff4186 2678 * Check if the client sent an empty certificate
ansond 0:137634ff4186 2679 */
ansond 0:137634ff4186 2680 if( ssl->endpoint == SSL_IS_SERVER &&
ansond 0:137634ff4186 2681 ssl->minor_ver == SSL_MINOR_VERSION_0 )
ansond 0:137634ff4186 2682 {
ansond 0:137634ff4186 2683 if( ssl->in_msglen == 2 &&
ansond 0:137634ff4186 2684 ssl->in_msgtype == SSL_MSG_ALERT &&
ansond 0:137634ff4186 2685 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
ansond 0:137634ff4186 2686 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
ansond 0:137634ff4186 2687 {
ansond 0:137634ff4186 2688 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
ansond 0:137634ff4186 2689
ansond 0:137634ff4186 2690 ssl->session_negotiate->verify_result = BADCERT_MISSING;
ansond 0:137634ff4186 2691 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
ansond 0:137634ff4186 2692 return( 0 );
ansond 0:137634ff4186 2693 else
ansond 0:137634ff4186 2694 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
ansond 0:137634ff4186 2695 }
ansond 0:137634ff4186 2696 }
ansond 0:137634ff4186 2697 #endif /* POLARSSL_SSL_PROTO_SSL3 */
ansond 0:137634ff4186 2698
ansond 0:137634ff4186 2699 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
ansond 0:137634ff4186 2700 defined(POLARSSL_SSL_PROTO_TLS1_2)
ansond 0:137634ff4186 2701 if( ssl->endpoint == SSL_IS_SERVER &&
ansond 0:137634ff4186 2702 ssl->minor_ver != SSL_MINOR_VERSION_0 )
ansond 0:137634ff4186 2703 {
ansond 0:137634ff4186 2704 if( ssl->in_hslen == 7 &&
ansond 0:137634ff4186 2705 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
ansond 0:137634ff4186 2706 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
ansond 0:137634ff4186 2707 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
ansond 0:137634ff4186 2708 {
ansond 0:137634ff4186 2709 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
ansond 0:137634ff4186 2710
ansond 0:137634ff4186 2711 ssl->session_negotiate->verify_result = BADCERT_MISSING;
ansond 0:137634ff4186 2712 if( ssl->authmode == SSL_VERIFY_REQUIRED )
ansond 0:137634ff4186 2713 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
ansond 0:137634ff4186 2714 else
ansond 0:137634ff4186 2715 return( 0 );
ansond 0:137634ff4186 2716 }
ansond 0:137634ff4186 2717 }
ansond 0:137634ff4186 2718 #endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
ansond 0:137634ff4186 2719 POLARSSL_SSL_PROTO_TLS1_2 */
ansond 0:137634ff4186 2720 #endif /* POLARSSL_SSL_SRV_C */
ansond 0:137634ff4186 2721
ansond 0:137634ff4186 2722 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
ansond 0:137634ff4186 2723 {
ansond 0:137634ff4186 2724 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
ansond 0:137634ff4186 2725 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
ansond 0:137634ff4186 2726 }
ansond 0:137634ff4186 2727
ansond 0:137634ff4186 2728 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
ansond 0:137634ff4186 2729 {
ansond 0:137634ff4186 2730 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
ansond 0:137634ff4186 2731 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
ansond 0:137634ff4186 2732 }
ansond 0:137634ff4186 2733
ansond 0:137634ff4186 2734 /*
ansond 0:137634ff4186 2735 * Same message structure as in ssl_write_certificate()
ansond 0:137634ff4186 2736 */
ansond 0:137634ff4186 2737 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
ansond 0:137634ff4186 2738
ansond 0:137634ff4186 2739 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
ansond 0:137634ff4186 2740 {
ansond 0:137634ff4186 2741 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
ansond 0:137634ff4186 2742 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
ansond 0:137634ff4186 2743 }
ansond 0:137634ff4186 2744
ansond 0:137634ff4186 2745 /* In case we tried to reuse a session but it failed */
ansond 0:137634ff4186 2746 if( ssl->session_negotiate->peer_cert != NULL )
ansond 0:137634ff4186 2747 {
ansond 0:137634ff4186 2748 x509_crt_free( ssl->session_negotiate->peer_cert );
ansond 0:137634ff4186 2749 polarssl_free( ssl->session_negotiate->peer_cert );
ansond 0:137634ff4186 2750 }
ansond 0:137634ff4186 2751
ansond 0:137634ff4186 2752 if( ( ssl->session_negotiate->peer_cert = polarssl_malloc(
ansond 0:137634ff4186 2753 sizeof( x509_crt ) ) ) == NULL )
ansond 0:137634ff4186 2754 {
ansond 0:137634ff4186 2755 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
ansond 0:137634ff4186 2756 sizeof( x509_crt ) ) );
ansond 0:137634ff4186 2757 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
ansond 0:137634ff4186 2758 }
ansond 0:137634ff4186 2759
ansond 0:137634ff4186 2760 x509_crt_init( ssl->session_negotiate->peer_cert );
ansond 0:137634ff4186 2761
ansond 0:137634ff4186 2762 i = 7;
ansond 0:137634ff4186 2763
ansond 0:137634ff4186 2764 while( i < ssl->in_hslen )
ansond 0:137634ff4186 2765 {
ansond 0:137634ff4186 2766 if( ssl->in_msg[i] != 0 )
ansond 0:137634ff4186 2767 {
ansond 0:137634ff4186 2768 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
ansond 0:137634ff4186 2769 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
ansond 0:137634ff4186 2770 }
ansond 0:137634ff4186 2771
ansond 0:137634ff4186 2772 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
ansond 0:137634ff4186 2773 | (unsigned int) ssl->in_msg[i + 2];
ansond 0:137634ff4186 2774 i += 3;
ansond 0:137634ff4186 2775
ansond 0:137634ff4186 2776 if( n < 128 || i + n > ssl->in_hslen )
ansond 0:137634ff4186 2777 {
ansond 0:137634ff4186 2778 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
ansond 0:137634ff4186 2779 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
ansond 0:137634ff4186 2780 }
ansond 0:137634ff4186 2781
ansond 0:137634ff4186 2782 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
ansond 0:137634ff4186 2783 ssl->in_msg + i, n );
ansond 0:137634ff4186 2784 if( ret != 0 )
ansond 0:137634ff4186 2785 {
ansond 0:137634ff4186 2786 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
ansond 0:137634ff4186 2787 return( ret );
ansond 0:137634ff4186 2788 }
ansond 0:137634ff4186 2789
ansond 0:137634ff4186 2790 i += n;
ansond 0:137634ff4186 2791 }
ansond 0:137634ff4186 2792
ansond 0:137634ff4186 2793 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
ansond 0:137634ff4186 2794
ansond 0:137634ff4186 2795 /*
ansond 0:137634ff4186 2796 * On client, make sure the server cert doesn't change during renego to
ansond 0:137634ff4186 2797 * avoid "triple handshake" attack: https://secure-resumption.com/
ansond 0:137634ff4186 2798 */
ansond 0:137634ff4186 2799 #if defined(POLARSSL_SSL_RENEGOTIATION) && defined(POLARSSL_SSL_CLI_C)
ansond 0:137634ff4186 2800 if( ssl->endpoint == SSL_IS_CLIENT &&
ansond 0:137634ff4186 2801 ssl->renegotiation == SSL_RENEGOTIATION )
ansond 0:137634ff4186 2802 {
ansond 0:137634ff4186 2803 if( ssl->session->peer_cert == NULL )
ansond 0:137634ff4186 2804 {
ansond 0:137634ff4186 2805 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
ansond 0:137634ff4186 2806 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
ansond 0:137634ff4186 2807 }
ansond 0:137634ff4186 2808
ansond 0:137634ff4186 2809 if( ssl->session->peer_cert->raw.len !=
ansond 0:137634ff4186 2810 ssl->session_negotiate->peer_cert->raw.len ||
ansond 0:137634ff4186 2811 memcmp( ssl->session->peer_cert->raw.p,
ansond 0:137634ff4186 2812 ssl->session_negotiate->peer_cert->raw.p,
ansond 0:137634ff4186 2813 ssl->session->peer_cert->raw.len ) != 0 )
ansond 0:137634ff4186 2814 {
ansond 0:137634ff4186 2815 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
ansond 0:137634ff4186 2816 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
ansond 0:137634ff4186 2817 }
ansond 0:137634ff4186 2818 }
ansond 0:137634ff4186 2819 #endif /* POLARSSL_SSL_RENEGOTIATION && POLARSSL_SSL_CLI_C */
ansond 0:137634ff4186 2820
ansond 0:137634ff4186 2821 if( ssl->authmode != SSL_VERIFY_NONE )
ansond 0:137634ff4186 2822 {
ansond 0:137634ff4186 2823 if( ssl->ca_chain == NULL )
ansond 0:137634ff4186 2824 {
ansond 0:137634ff4186 2825 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
ansond 0:137634ff4186 2826 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
ansond 0:137634ff4186 2827 }
ansond 0:137634ff4186 2828
ansond 0:137634ff4186 2829 /*
ansond 0:137634ff4186 2830 * Main check: verify certificate
ansond 0:137634ff4186 2831 */
ansond 0:137634ff4186 2832 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
ansond 0:137634ff4186 2833 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
ansond 0:137634ff4186 2834 &ssl->session_negotiate->verify_result,
ansond 0:137634ff4186 2835 ssl->f_vrfy, ssl->p_vrfy );
ansond 0:137634ff4186 2836
ansond 0:137634ff4186 2837 if( ret != 0 )
ansond 0:137634ff4186 2838 {
ansond 0:137634ff4186 2839 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
ansond 0:137634ff4186 2840 }
ansond 0:137634ff4186 2841
ansond 0:137634ff4186 2842 /*
ansond 0:137634ff4186 2843 * Secondary checks: always done, but change 'ret' only if it was 0
ansond 0:137634ff4186 2844 */
ansond 0:137634ff4186 2845
ansond 0:137634ff4186 2846 #if defined(POLARSSL_SSL_SET_CURVES)
ansond 0:137634ff4186 2847 {
ansond 0:137634ff4186 2848 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
ansond 0:137634ff4186 2849
ansond 0:137634ff4186 2850 /* If certificate uses an EC key, make sure the curve is OK */
ansond 0:137634ff4186 2851 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
ansond 0:137634ff4186 2852 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
ansond 0:137634ff4186 2853 {
ansond 0:137634ff4186 2854 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
ansond 0:137634ff4186 2855 if( ret == 0 )
ansond 0:137634ff4186 2856 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
ansond 0:137634ff4186 2857 }
ansond 0:137634ff4186 2858 }
ansond 0:137634ff4186 2859 #endif /* POLARSSL_SSL_SET_CURVES */
ansond 0:137634ff4186 2860
ansond 0:137634ff4186 2861 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
ansond 0:137634ff4186 2862 ciphersuite_info,
ansond 0:137634ff4186 2863 ! ssl->endpoint,
ansond 0:137634ff4186 2864 &ssl->session_negotiate->verify_result ) != 0 )
ansond 0:137634ff4186 2865 {
ansond 0:137634ff4186 2866 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
ansond 0:137634ff4186 2867 if( ret == 0 )
ansond 0:137634ff4186 2868 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
ansond 0:137634ff4186 2869 }
ansond 0:137634ff4186 2870
ansond 0:137634ff4186 2871 if( ssl->authmode != SSL_VERIFY_REQUIRED )
ansond 0:137634ff4186 2872 ret = 0;
ansond 0:137634ff4186 2873 }
ansond 0:137634ff4186 2874
ansond 0:137634ff4186 2875 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
ansond 0:137634ff4186 2876
ansond 0:137634ff4186 2877 return( ret );
ansond 0:137634ff4186 2878 }
ansond 0:137634ff4186 2879 #endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
ansond 0:137634ff4186 2880 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
ansond 0:137634ff4186 2881 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
ansond 0:137634ff4186 2882 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
ansond 0:137634ff4186 2883 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
ansond 0:137634ff4186 2884 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
ansond 0:137634ff4186 2885 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
ansond 0:137634ff4186 2886
ansond 0:137634ff4186 2887 int ssl_write_change_cipher_spec( ssl_context *ssl )
ansond 0:137634ff4186 2888 {
ansond 0:137634ff4186 2889 int ret;
ansond 0:137634ff4186 2890
ansond 0:137634ff4186 2891 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
ansond 0:137634ff4186 2892
ansond 0:137634ff4186 2893 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
ansond 0:137634ff4186 2894 ssl->out_msglen = 1;
ansond 0:137634ff4186 2895 ssl->out_msg[0] = 1;
ansond 0:137634ff4186 2896
ansond 0:137634ff4186 2897 ssl->state++;
ansond 0:137634ff4186 2898
ansond 0:137634ff4186 2899 if( ( ret = ssl_write_record( ssl ) ) != 0 )
ansond 0:137634ff4186 2900 {
ansond 0:137634ff4186 2901 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
ansond 0:137634ff4186 2902 return( ret );
ansond 0:137634ff4186 2903 }
ansond 0:137634ff4186 2904
ansond 0:137634ff4186 2905 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
ansond 0:137634ff4186 2906
ansond 0:137634ff4186 2907 return( 0 );
ansond 0:137634ff4186 2908 }
ansond 0:137634ff4186 2909
ansond 0:137634ff4186 2910 int ssl_parse_change_cipher_spec( ssl_context *ssl )
ansond 0:137634ff4186 2911 {
ansond 0:137634ff4186 2912 int ret;
ansond 0:137634ff4186 2913
ansond 0:137634ff4186 2914 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
ansond 0:137634ff4186 2915
ansond 0:137634ff4186 2916 if( ( ret = ssl_read_record( ssl ) ) != 0 )
ansond 0:137634ff4186 2917 {
ansond 0:137634ff4186 2918 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
ansond 0:137634ff4186 2919 return( ret );
ansond 0:137634ff4186 2920 }
ansond 0:137634ff4186 2921
ansond 0:137634ff4186 2922 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
ansond 0:137634ff4186 2923 {
ansond 0:137634ff4186 2924 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
ansond 0:137634ff4186 2925 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
ansond 0:137634ff4186 2926 }
ansond 0:137634ff4186 2927
ansond 0:137634ff4186 2928 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
ansond 0:137634ff4186 2929 {
ansond 0:137634ff4186 2930 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
ansond 0:137634ff4186 2931 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
ansond 0:137634ff4186 2932 }
ansond 0:137634ff4186 2933
ansond 0:137634ff4186 2934 ssl->state++;
ansond 0:137634ff4186 2935
ansond 0:137634ff4186 2936 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
ansond 0:137634ff4186 2937
ansond 0:137634ff4186 2938 return( 0 );
ansond 0:137634ff4186 2939 }
ansond 0:137634ff4186 2940
ansond 0:137634ff4186 2941 void ssl_optimize_checksum( ssl_context *ssl,
ansond 0:137634ff4186 2942 const ssl_ciphersuite_t *ciphersuite_info )
ansond 0:137634ff4186 2943 {
ansond 0:137634ff4186 2944 ((void) ciphersuite_info);
ansond 0:137634ff4186 2945
ansond 0:137634ff4186 2946 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
ansond 0:137634ff4186 2947 defined(POLARSSL_SSL_PROTO_TLS1_1)
ansond 0:137634ff4186 2948 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
ansond 0:137634ff4186 2949 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
ansond 0:137634ff4186 2950 else
ansond 0:137634ff4186 2951 #endif
ansond 0:137634ff4186 2952 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
ansond 0:137634ff4186 2953 #if defined(POLARSSL_SHA512_C)
ansond 0:137634ff4186 2954 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
ansond 0:137634ff4186 2955 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
ansond 0:137634ff4186 2956 else
ansond 0:137634ff4186 2957 #endif
ansond 0:137634ff4186 2958 #if defined(POLARSSL_SHA256_C)
ansond 0:137634ff4186 2959 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
ansond 0:137634ff4186 2960 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
ansond 0:137634ff4186 2961 else
ansond 0:137634ff4186 2962 #endif
ansond 0:137634ff4186 2963 #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
ansond 0:137634ff4186 2964 {
ansond 0:137634ff4186 2965 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 2966 return;
ansond 0:137634ff4186 2967 }
ansond 0:137634ff4186 2968 }
ansond 0:137634ff4186 2969
ansond 0:137634ff4186 2970 static void ssl_update_checksum_start( ssl_context *ssl,
ansond 0:137634ff4186 2971 const unsigned char *buf, size_t len )
ansond 0:137634ff4186 2972 {
ansond 0:137634ff4186 2973 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
ansond 0:137634ff4186 2974 defined(POLARSSL_SSL_PROTO_TLS1_1)
ansond 0:137634ff4186 2975 md5_update( &ssl->handshake->fin_md5 , buf, len );
ansond 0:137634ff4186 2976 sha1_update( &ssl->handshake->fin_sha1, buf, len );
ansond 0:137634ff4186 2977 #endif
ansond 0:137634ff4186 2978 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
ansond 0:137634ff4186 2979 #if defined(POLARSSL_SHA256_C)
ansond 0:137634ff4186 2980 sha256_update( &ssl->handshake->fin_sha256, buf, len );
ansond 0:137634ff4186 2981 #endif
ansond 0:137634ff4186 2982 #if defined(POLARSSL_SHA512_C)
ansond 0:137634ff4186 2983 sha512_update( &ssl->handshake->fin_sha512, buf, len );
ansond 0:137634ff4186 2984 #endif
ansond 0:137634ff4186 2985 #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
ansond 0:137634ff4186 2986 }
ansond 0:137634ff4186 2987
ansond 0:137634ff4186 2988 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
ansond 0:137634ff4186 2989 defined(POLARSSL_SSL_PROTO_TLS1_1)
ansond 0:137634ff4186 2990 static void ssl_update_checksum_md5sha1( ssl_context *ssl,
ansond 0:137634ff4186 2991 const unsigned char *buf, size_t len )
ansond 0:137634ff4186 2992 {
ansond 0:137634ff4186 2993 md5_update( &ssl->handshake->fin_md5 , buf, len );
ansond 0:137634ff4186 2994 sha1_update( &ssl->handshake->fin_sha1, buf, len );
ansond 0:137634ff4186 2995 }
ansond 0:137634ff4186 2996 #endif
ansond 0:137634ff4186 2997
ansond 0:137634ff4186 2998 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
ansond 0:137634ff4186 2999 #if defined(POLARSSL_SHA256_C)
ansond 0:137634ff4186 3000 static void ssl_update_checksum_sha256( ssl_context *ssl,
ansond 0:137634ff4186 3001 const unsigned char *buf, size_t len )
ansond 0:137634ff4186 3002 {
ansond 0:137634ff4186 3003 sha256_update( &ssl->handshake->fin_sha256, buf, len );
ansond 0:137634ff4186 3004 }
ansond 0:137634ff4186 3005 #endif
ansond 0:137634ff4186 3006
ansond 0:137634ff4186 3007 #if defined(POLARSSL_SHA512_C)
ansond 0:137634ff4186 3008 static void ssl_update_checksum_sha384( ssl_context *ssl,
ansond 0:137634ff4186 3009 const unsigned char *buf, size_t len )
ansond 0:137634ff4186 3010 {
ansond 0:137634ff4186 3011 sha512_update( &ssl->handshake->fin_sha512, buf, len );
ansond 0:137634ff4186 3012 }
ansond 0:137634ff4186 3013 #endif
ansond 0:137634ff4186 3014 #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
ansond 0:137634ff4186 3015
ansond 0:137634ff4186 3016 #if defined(POLARSSL_SSL_PROTO_SSL3)
ansond 0:137634ff4186 3017 static void ssl_calc_finished_ssl(
ansond 0:137634ff4186 3018 ssl_context *ssl, unsigned char *buf, int from )
ansond 0:137634ff4186 3019 {
ansond 0:137634ff4186 3020 const char *sender;
ansond 0:137634ff4186 3021 md5_context md5;
ansond 0:137634ff4186 3022 sha1_context sha1;
ansond 0:137634ff4186 3023
ansond 0:137634ff4186 3024 unsigned char padbuf[48];
ansond 0:137634ff4186 3025 unsigned char md5sum[16];
ansond 0:137634ff4186 3026 unsigned char sha1sum[20];
ansond 0:137634ff4186 3027
ansond 0:137634ff4186 3028 ssl_session *session = ssl->session_negotiate;
ansond 0:137634ff4186 3029 if( !session )
ansond 0:137634ff4186 3030 session = ssl->session;
ansond 0:137634ff4186 3031
ansond 0:137634ff4186 3032 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
ansond 0:137634ff4186 3033
ansond 0:137634ff4186 3034 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
ansond 0:137634ff4186 3035 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
ansond 0:137634ff4186 3036
ansond 0:137634ff4186 3037 /*
ansond 0:137634ff4186 3038 * SSLv3:
ansond 0:137634ff4186 3039 * hash =
ansond 0:137634ff4186 3040 * MD5( master + pad2 +
ansond 0:137634ff4186 3041 * MD5( handshake + sender + master + pad1 ) )
ansond 0:137634ff4186 3042 * + SHA1( master + pad2 +
ansond 0:137634ff4186 3043 * SHA1( handshake + sender + master + pad1 ) )
ansond 0:137634ff4186 3044 */
ansond 0:137634ff4186 3045
ansond 0:137634ff4186 3046 #if !defined(POLARSSL_MD5_ALT)
ansond 0:137634ff4186 3047 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
ansond 0:137634ff4186 3048 md5.state, sizeof( md5.state ) );
ansond 0:137634ff4186 3049 #endif
ansond 0:137634ff4186 3050
ansond 0:137634ff4186 3051 #if !defined(POLARSSL_SHA1_ALT)
ansond 0:137634ff4186 3052 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
ansond 0:137634ff4186 3053 sha1.state, sizeof( sha1.state ) );
ansond 0:137634ff4186 3054 #endif
ansond 0:137634ff4186 3055
ansond 0:137634ff4186 3056 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
ansond 0:137634ff4186 3057 : "SRVR";
ansond 0:137634ff4186 3058
ansond 0:137634ff4186 3059 memset( padbuf, 0x36, 48 );
ansond 0:137634ff4186 3060
ansond 0:137634ff4186 3061 md5_update( &md5, (const unsigned char *) sender, 4 );
ansond 0:137634ff4186 3062 md5_update( &md5, session->master, 48 );
ansond 0:137634ff4186 3063 md5_update( &md5, padbuf, 48 );
ansond 0:137634ff4186 3064 md5_finish( &md5, md5sum );
ansond 0:137634ff4186 3065
ansond 0:137634ff4186 3066 sha1_update( &sha1, (const unsigned char *) sender, 4 );
ansond 0:137634ff4186 3067 sha1_update( &sha1, session->master, 48 );
ansond 0:137634ff4186 3068 sha1_update( &sha1, padbuf, 40 );
ansond 0:137634ff4186 3069 sha1_finish( &sha1, sha1sum );
ansond 0:137634ff4186 3070
ansond 0:137634ff4186 3071 memset( padbuf, 0x5C, 48 );
ansond 0:137634ff4186 3072
ansond 0:137634ff4186 3073 md5_starts( &md5 );
ansond 0:137634ff4186 3074 md5_update( &md5, session->master, 48 );
ansond 0:137634ff4186 3075 md5_update( &md5, padbuf, 48 );
ansond 0:137634ff4186 3076 md5_update( &md5, md5sum, 16 );
ansond 0:137634ff4186 3077 md5_finish( &md5, buf );
ansond 0:137634ff4186 3078
ansond 0:137634ff4186 3079 sha1_starts( &sha1 );
ansond 0:137634ff4186 3080 sha1_update( &sha1, session->master, 48 );
ansond 0:137634ff4186 3081 sha1_update( &sha1, padbuf , 40 );
ansond 0:137634ff4186 3082 sha1_update( &sha1, sha1sum, 20 );
ansond 0:137634ff4186 3083 sha1_finish( &sha1, buf + 16 );
ansond 0:137634ff4186 3084
ansond 0:137634ff4186 3085 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
ansond 0:137634ff4186 3086
ansond 0:137634ff4186 3087 md5_free( &md5 );
ansond 0:137634ff4186 3088 sha1_free( &sha1 );
ansond 0:137634ff4186 3089
ansond 0:137634ff4186 3090 polarssl_zeroize( padbuf, sizeof( padbuf ) );
ansond 0:137634ff4186 3091 polarssl_zeroize( md5sum, sizeof( md5sum ) );
ansond 0:137634ff4186 3092 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
ansond 0:137634ff4186 3093
ansond 0:137634ff4186 3094 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
ansond 0:137634ff4186 3095 }
ansond 0:137634ff4186 3096 #endif /* POLARSSL_SSL_PROTO_SSL3 */
ansond 0:137634ff4186 3097
ansond 0:137634ff4186 3098 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
ansond 0:137634ff4186 3099 static void ssl_calc_finished_tls(
ansond 0:137634ff4186 3100 ssl_context *ssl, unsigned char *buf, int from )
ansond 0:137634ff4186 3101 {
ansond 0:137634ff4186 3102 int len = 12;
ansond 0:137634ff4186 3103 const char *sender;
ansond 0:137634ff4186 3104 md5_context md5;
ansond 0:137634ff4186 3105 sha1_context sha1;
ansond 0:137634ff4186 3106 unsigned char padbuf[36];
ansond 0:137634ff4186 3107
ansond 0:137634ff4186 3108 ssl_session *session = ssl->session_negotiate;
ansond 0:137634ff4186 3109 if( !session )
ansond 0:137634ff4186 3110 session = ssl->session;
ansond 0:137634ff4186 3111
ansond 0:137634ff4186 3112 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
ansond 0:137634ff4186 3113
ansond 0:137634ff4186 3114 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
ansond 0:137634ff4186 3115 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
ansond 0:137634ff4186 3116
ansond 0:137634ff4186 3117 /*
ansond 0:137634ff4186 3118 * TLSv1:
ansond 0:137634ff4186 3119 * hash = PRF( master, finished_label,
ansond 0:137634ff4186 3120 * MD5( handshake ) + SHA1( handshake ) )[0..11]
ansond 0:137634ff4186 3121 */
ansond 0:137634ff4186 3122
ansond 0:137634ff4186 3123 #if !defined(POLARSSL_MD5_ALT)
ansond 0:137634ff4186 3124 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
ansond 0:137634ff4186 3125 md5.state, sizeof( md5.state ) );
ansond 0:137634ff4186 3126 #endif
ansond 0:137634ff4186 3127
ansond 0:137634ff4186 3128 #if !defined(POLARSSL_SHA1_ALT)
ansond 0:137634ff4186 3129 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
ansond 0:137634ff4186 3130 sha1.state, sizeof( sha1.state ) );
ansond 0:137634ff4186 3131 #endif
ansond 0:137634ff4186 3132
ansond 0:137634ff4186 3133 sender = ( from == SSL_IS_CLIENT )
ansond 0:137634ff4186 3134 ? "client finished"
ansond 0:137634ff4186 3135 : "server finished";
ansond 0:137634ff4186 3136
ansond 0:137634ff4186 3137 md5_finish( &md5, padbuf );
ansond 0:137634ff4186 3138 sha1_finish( &sha1, padbuf + 16 );
ansond 0:137634ff4186 3139
ansond 0:137634ff4186 3140 ssl->handshake->tls_prf( session->master, 48, sender,
ansond 0:137634ff4186 3141 padbuf, 36, buf, len );
ansond 0:137634ff4186 3142
ansond 0:137634ff4186 3143 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
ansond 0:137634ff4186 3144
ansond 0:137634ff4186 3145 md5_free( &md5 );
ansond 0:137634ff4186 3146 sha1_free( &sha1 );
ansond 0:137634ff4186 3147
ansond 0:137634ff4186 3148 polarssl_zeroize( padbuf, sizeof( padbuf ) );
ansond 0:137634ff4186 3149
ansond 0:137634ff4186 3150 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
ansond 0:137634ff4186 3151 }
ansond 0:137634ff4186 3152 #endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
ansond 0:137634ff4186 3153
ansond 0:137634ff4186 3154 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
ansond 0:137634ff4186 3155 #if defined(POLARSSL_SHA256_C)
ansond 0:137634ff4186 3156 static void ssl_calc_finished_tls_sha256(
ansond 0:137634ff4186 3157 ssl_context *ssl, unsigned char *buf, int from )
ansond 0:137634ff4186 3158 {
ansond 0:137634ff4186 3159 int len = 12;
ansond 0:137634ff4186 3160 const char *sender;
ansond 0:137634ff4186 3161 sha256_context sha256;
ansond 0:137634ff4186 3162 unsigned char padbuf[32];
ansond 0:137634ff4186 3163
ansond 0:137634ff4186 3164 ssl_session *session = ssl->session_negotiate;
ansond 0:137634ff4186 3165 if( !session )
ansond 0:137634ff4186 3166 session = ssl->session;
ansond 0:137634ff4186 3167
ansond 0:137634ff4186 3168 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
ansond 0:137634ff4186 3169
ansond 0:137634ff4186 3170 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
ansond 0:137634ff4186 3171
ansond 0:137634ff4186 3172 /*
ansond 0:137634ff4186 3173 * TLSv1.2:
ansond 0:137634ff4186 3174 * hash = PRF( master, finished_label,
ansond 0:137634ff4186 3175 * Hash( handshake ) )[0.11]
ansond 0:137634ff4186 3176 */
ansond 0:137634ff4186 3177
ansond 0:137634ff4186 3178 #if !defined(POLARSSL_SHA256_ALT)
ansond 0:137634ff4186 3179 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
ansond 0:137634ff4186 3180 sha256.state, sizeof( sha256.state ) );
ansond 0:137634ff4186 3181 #endif
ansond 0:137634ff4186 3182
ansond 0:137634ff4186 3183 sender = ( from == SSL_IS_CLIENT )
ansond 0:137634ff4186 3184 ? "client finished"
ansond 0:137634ff4186 3185 : "server finished";
ansond 0:137634ff4186 3186
ansond 0:137634ff4186 3187 sha256_finish( &sha256, padbuf );
ansond 0:137634ff4186 3188
ansond 0:137634ff4186 3189 ssl->handshake->tls_prf( session->master, 48, sender,
ansond 0:137634ff4186 3190 padbuf, 32, buf, len );
ansond 0:137634ff4186 3191
ansond 0:137634ff4186 3192 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
ansond 0:137634ff4186 3193
ansond 0:137634ff4186 3194 sha256_free( &sha256 );
ansond 0:137634ff4186 3195
ansond 0:137634ff4186 3196 polarssl_zeroize( padbuf, sizeof( padbuf ) );
ansond 0:137634ff4186 3197
ansond 0:137634ff4186 3198 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
ansond 0:137634ff4186 3199 }
ansond 0:137634ff4186 3200 #endif /* POLARSSL_SHA256_C */
ansond 0:137634ff4186 3201
ansond 0:137634ff4186 3202 #if defined(POLARSSL_SHA512_C)
ansond 0:137634ff4186 3203 static void ssl_calc_finished_tls_sha384(
ansond 0:137634ff4186 3204 ssl_context *ssl, unsigned char *buf, int from )
ansond 0:137634ff4186 3205 {
ansond 0:137634ff4186 3206 int len = 12;
ansond 0:137634ff4186 3207 const char *sender;
ansond 0:137634ff4186 3208 sha512_context sha512;
ansond 0:137634ff4186 3209 unsigned char padbuf[48];
ansond 0:137634ff4186 3210
ansond 0:137634ff4186 3211 ssl_session *session = ssl->session_negotiate;
ansond 0:137634ff4186 3212 if( !session )
ansond 0:137634ff4186 3213 session = ssl->session;
ansond 0:137634ff4186 3214
ansond 0:137634ff4186 3215 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
ansond 0:137634ff4186 3216
ansond 0:137634ff4186 3217 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
ansond 0:137634ff4186 3218
ansond 0:137634ff4186 3219 /*
ansond 0:137634ff4186 3220 * TLSv1.2:
ansond 0:137634ff4186 3221 * hash = PRF( master, finished_label,
ansond 0:137634ff4186 3222 * Hash( handshake ) )[0.11]
ansond 0:137634ff4186 3223 */
ansond 0:137634ff4186 3224
ansond 0:137634ff4186 3225 #if !defined(POLARSSL_SHA512_ALT)
ansond 0:137634ff4186 3226 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
ansond 0:137634ff4186 3227 sha512.state, sizeof( sha512.state ) );
ansond 0:137634ff4186 3228 #endif
ansond 0:137634ff4186 3229
ansond 0:137634ff4186 3230 sender = ( from == SSL_IS_CLIENT )
ansond 0:137634ff4186 3231 ? "client finished"
ansond 0:137634ff4186 3232 : "server finished";
ansond 0:137634ff4186 3233
ansond 0:137634ff4186 3234 sha512_finish( &sha512, padbuf );
ansond 0:137634ff4186 3235
ansond 0:137634ff4186 3236 ssl->handshake->tls_prf( session->master, 48, sender,
ansond 0:137634ff4186 3237 padbuf, 48, buf, len );
ansond 0:137634ff4186 3238
ansond 0:137634ff4186 3239 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
ansond 0:137634ff4186 3240
ansond 0:137634ff4186 3241 sha512_free( &sha512 );
ansond 0:137634ff4186 3242
ansond 0:137634ff4186 3243 polarssl_zeroize( padbuf, sizeof( padbuf ) );
ansond 0:137634ff4186 3244
ansond 0:137634ff4186 3245 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
ansond 0:137634ff4186 3246 }
ansond 0:137634ff4186 3247 #endif /* POLARSSL_SHA512_C */
ansond 0:137634ff4186 3248 #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
ansond 0:137634ff4186 3249
ansond 0:137634ff4186 3250 void ssl_handshake_wrapup( ssl_context *ssl )
ansond 0:137634ff4186 3251 {
ansond 0:137634ff4186 3252 int resume = ssl->handshake->resume;
ansond 0:137634ff4186 3253
ansond 0:137634ff4186 3254 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
ansond 0:137634ff4186 3255
ansond 0:137634ff4186 3256 /*
ansond 0:137634ff4186 3257 * Free our handshake params
ansond 0:137634ff4186 3258 */
ansond 0:137634ff4186 3259 ssl_handshake_free( ssl->handshake );
ansond 0:137634ff4186 3260 polarssl_free( ssl->handshake );
ansond 0:137634ff4186 3261 ssl->handshake = NULL;
ansond 0:137634ff4186 3262
ansond 0:137634ff4186 3263 #if defined(POLARSSL_SSL_RENEGOTIATION)
ansond 0:137634ff4186 3264 if( ssl->renegotiation == SSL_RENEGOTIATION )
ansond 0:137634ff4186 3265 {
ansond 0:137634ff4186 3266 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
ansond 0:137634ff4186 3267 ssl->renego_records_seen = 0;
ansond 0:137634ff4186 3268 }
ansond 0:137634ff4186 3269 #endif
ansond 0:137634ff4186 3270
ansond 0:137634ff4186 3271 /*
ansond 0:137634ff4186 3272 * Switch in our now active transform context
ansond 0:137634ff4186 3273 */
ansond 0:137634ff4186 3274 if( ssl->transform )
ansond 0:137634ff4186 3275 {
ansond 0:137634ff4186 3276 ssl_transform_free( ssl->transform );
ansond 0:137634ff4186 3277 polarssl_free( ssl->transform );
ansond 0:137634ff4186 3278 }
ansond 0:137634ff4186 3279 ssl->transform = ssl->transform_negotiate;
ansond 0:137634ff4186 3280 ssl->transform_negotiate = NULL;
ansond 0:137634ff4186 3281
ansond 0:137634ff4186 3282 if( ssl->session )
ansond 0:137634ff4186 3283 {
ansond 0:137634ff4186 3284 #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
ansond 0:137634ff4186 3285 /* RFC 7366 3.1: keep the EtM state */
ansond 0:137634ff4186 3286 ssl->session_negotiate->encrypt_then_mac =
ansond 0:137634ff4186 3287 ssl->session->encrypt_then_mac;
ansond 0:137634ff4186 3288 #endif
ansond 0:137634ff4186 3289
ansond 0:137634ff4186 3290 ssl_session_free( ssl->session );
ansond 0:137634ff4186 3291 polarssl_free( ssl->session );
ansond 0:137634ff4186 3292 }
ansond 0:137634ff4186 3293 ssl->session = ssl->session_negotiate;
ansond 0:137634ff4186 3294 ssl->session_negotiate = NULL;
ansond 0:137634ff4186 3295
ansond 0:137634ff4186 3296 /*
ansond 0:137634ff4186 3297 * Add cache entry
ansond 0:137634ff4186 3298 */
ansond 0:137634ff4186 3299 if( ssl->f_set_cache != NULL &&
ansond 0:137634ff4186 3300 ssl->session->length != 0 &&
ansond 0:137634ff4186 3301 resume == 0 )
ansond 0:137634ff4186 3302 {
ansond 0:137634ff4186 3303 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
ansond 0:137634ff4186 3304 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
ansond 0:137634ff4186 3305 }
ansond 0:137634ff4186 3306
ansond 0:137634ff4186 3307 ssl->state++;
ansond 0:137634ff4186 3308
ansond 0:137634ff4186 3309 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
ansond 0:137634ff4186 3310 }
ansond 0:137634ff4186 3311
ansond 0:137634ff4186 3312 int ssl_write_finished( ssl_context *ssl )
ansond 0:137634ff4186 3313 {
ansond 0:137634ff4186 3314 int ret, hash_len;
ansond 0:137634ff4186 3315
ansond 0:137634ff4186 3316 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
ansond 0:137634ff4186 3317
ansond 0:137634ff4186 3318 /*
ansond 0:137634ff4186 3319 * Set the out_msg pointer to the correct location based on IV length
ansond 0:137634ff4186 3320 */
ansond 0:137634ff4186 3321 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
ansond 0:137634ff4186 3322 {
ansond 0:137634ff4186 3323 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
ansond 0:137634ff4186 3324 ssl->transform_negotiate->fixed_ivlen;
ansond 0:137634ff4186 3325 }
ansond 0:137634ff4186 3326 else
ansond 0:137634ff4186 3327 ssl->out_msg = ssl->out_iv;
ansond 0:137634ff4186 3328
ansond 0:137634ff4186 3329 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
ansond 0:137634ff4186 3330
ansond 0:137634ff4186 3331 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
ansond 0:137634ff4186 3332 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
ansond 0:137634ff4186 3333
ansond 0:137634ff4186 3334 #if defined(POLARSSL_SSL_RENEGOTIATION)
ansond 0:137634ff4186 3335 ssl->verify_data_len = hash_len;
ansond 0:137634ff4186 3336 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
ansond 0:137634ff4186 3337 #endif
ansond 0:137634ff4186 3338
ansond 0:137634ff4186 3339 ssl->out_msglen = 4 + hash_len;
ansond 0:137634ff4186 3340 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
ansond 0:137634ff4186 3341 ssl->out_msg[0] = SSL_HS_FINISHED;
ansond 0:137634ff4186 3342
ansond 0:137634ff4186 3343 /*
ansond 0:137634ff4186 3344 * In case of session resuming, invert the client and server
ansond 0:137634ff4186 3345 * ChangeCipherSpec messages order.
ansond 0:137634ff4186 3346 */
ansond 0:137634ff4186 3347 if( ssl->handshake->resume != 0 )
ansond 0:137634ff4186 3348 {
ansond 0:137634ff4186 3349 #if defined(POLARSSL_SSL_CLI_C)
ansond 0:137634ff4186 3350 if( ssl->endpoint == SSL_IS_CLIENT )
ansond 0:137634ff4186 3351 ssl->state = SSL_HANDSHAKE_WRAPUP;
ansond 0:137634ff4186 3352 #endif
ansond 0:137634ff4186 3353 #if defined(POLARSSL_SSL_SRV_C)
ansond 0:137634ff4186 3354 if( ssl->endpoint == SSL_IS_SERVER )
ansond 0:137634ff4186 3355 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
ansond 0:137634ff4186 3356 #endif
ansond 0:137634ff4186 3357 }
ansond 0:137634ff4186 3358 else
ansond 0:137634ff4186 3359 ssl->state++;
ansond 0:137634ff4186 3360
ansond 0:137634ff4186 3361 /*
ansond 0:137634ff4186 3362 * Switch to our negotiated transform and session parameters for outbound
ansond 0:137634ff4186 3363 * data.
ansond 0:137634ff4186 3364 */
ansond 0:137634ff4186 3365 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
ansond 0:137634ff4186 3366 ssl->transform_out = ssl->transform_negotiate;
ansond 0:137634ff4186 3367 ssl->session_out = ssl->session_negotiate;
ansond 0:137634ff4186 3368 memset( ssl->out_ctr, 0, 8 );
ansond 0:137634ff4186 3369
ansond 0:137634ff4186 3370 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
ansond 0:137634ff4186 3371 if( ssl_hw_record_activate != NULL )
ansond 0:137634ff4186 3372 {
ansond 0:137634ff4186 3373 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
ansond 0:137634ff4186 3374 {
ansond 0:137634ff4186 3375 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
ansond 0:137634ff4186 3376 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
ansond 0:137634ff4186 3377 }
ansond 0:137634ff4186 3378 }
ansond 0:137634ff4186 3379 #endif
ansond 0:137634ff4186 3380
ansond 0:137634ff4186 3381 if( ( ret = ssl_write_record( ssl ) ) != 0 )
ansond 0:137634ff4186 3382 {
ansond 0:137634ff4186 3383 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
ansond 0:137634ff4186 3384 return( ret );
ansond 0:137634ff4186 3385 }
ansond 0:137634ff4186 3386
ansond 0:137634ff4186 3387 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
ansond 0:137634ff4186 3388
ansond 0:137634ff4186 3389 return( 0 );
ansond 0:137634ff4186 3390 }
ansond 0:137634ff4186 3391
ansond 0:137634ff4186 3392 int ssl_parse_finished( ssl_context *ssl )
ansond 0:137634ff4186 3393 {
ansond 0:137634ff4186 3394 int ret;
ansond 0:137634ff4186 3395 unsigned int hash_len;
ansond 0:137634ff4186 3396 unsigned char buf[36];
ansond 0:137634ff4186 3397
ansond 0:137634ff4186 3398 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
ansond 0:137634ff4186 3399
ansond 0:137634ff4186 3400 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
ansond 0:137634ff4186 3401
ansond 0:137634ff4186 3402 /*
ansond 0:137634ff4186 3403 * Switch to our negotiated transform and session parameters for inbound
ansond 0:137634ff4186 3404 * data.
ansond 0:137634ff4186 3405 */
ansond 0:137634ff4186 3406 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
ansond 0:137634ff4186 3407 ssl->transform_in = ssl->transform_negotiate;
ansond 0:137634ff4186 3408 ssl->session_in = ssl->session_negotiate;
ansond 0:137634ff4186 3409 memset( ssl->in_ctr, 0, 8 );
ansond 0:137634ff4186 3410
ansond 0:137634ff4186 3411 /*
ansond 0:137634ff4186 3412 * Set the in_msg pointer to the correct location based on IV length
ansond 0:137634ff4186 3413 */
ansond 0:137634ff4186 3414 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
ansond 0:137634ff4186 3415 {
ansond 0:137634ff4186 3416 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
ansond 0:137634ff4186 3417 ssl->transform_negotiate->fixed_ivlen;
ansond 0:137634ff4186 3418 }
ansond 0:137634ff4186 3419 else
ansond 0:137634ff4186 3420 ssl->in_msg = ssl->in_iv;
ansond 0:137634ff4186 3421
ansond 0:137634ff4186 3422 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
ansond 0:137634ff4186 3423 if( ssl_hw_record_activate != NULL )
ansond 0:137634ff4186 3424 {
ansond 0:137634ff4186 3425 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
ansond 0:137634ff4186 3426 {
ansond 0:137634ff4186 3427 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
ansond 0:137634ff4186 3428 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
ansond 0:137634ff4186 3429 }
ansond 0:137634ff4186 3430 }
ansond 0:137634ff4186 3431 #endif
ansond 0:137634ff4186 3432
ansond 0:137634ff4186 3433 if( ( ret = ssl_read_record( ssl ) ) != 0 )
ansond 0:137634ff4186 3434 {
ansond 0:137634ff4186 3435 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
ansond 0:137634ff4186 3436 return( ret );
ansond 0:137634ff4186 3437 }
ansond 0:137634ff4186 3438
ansond 0:137634ff4186 3439 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
ansond 0:137634ff4186 3440 {
ansond 0:137634ff4186 3441 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
ansond 0:137634ff4186 3442 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
ansond 0:137634ff4186 3443 }
ansond 0:137634ff4186 3444
ansond 0:137634ff4186 3445 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
ansond 0:137634ff4186 3446 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
ansond 0:137634ff4186 3447
ansond 0:137634ff4186 3448 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
ansond 0:137634ff4186 3449 ssl->in_hslen != 4 + hash_len )
ansond 0:137634ff4186 3450 {
ansond 0:137634ff4186 3451 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
ansond 0:137634ff4186 3452 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
ansond 0:137634ff4186 3453 }
ansond 0:137634ff4186 3454
ansond 0:137634ff4186 3455 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
ansond 0:137634ff4186 3456 {
ansond 0:137634ff4186 3457 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
ansond 0:137634ff4186 3458 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
ansond 0:137634ff4186 3459 }
ansond 0:137634ff4186 3460
ansond 0:137634ff4186 3461 #if defined(POLARSSL_SSL_RENEGOTIATION)
ansond 0:137634ff4186 3462 ssl->verify_data_len = hash_len;
ansond 0:137634ff4186 3463 memcpy( ssl->peer_verify_data, buf, hash_len );
ansond 0:137634ff4186 3464 #endif
ansond 0:137634ff4186 3465
ansond 0:137634ff4186 3466 if( ssl->handshake->resume != 0 )
ansond 0:137634ff4186 3467 {
ansond 0:137634ff4186 3468 #if defined(POLARSSL_SSL_CLI_C)
ansond 0:137634ff4186 3469 if( ssl->endpoint == SSL_IS_CLIENT )
ansond 0:137634ff4186 3470 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
ansond 0:137634ff4186 3471 #endif
ansond 0:137634ff4186 3472 #if defined(POLARSSL_SSL_SRV_C)
ansond 0:137634ff4186 3473 if( ssl->endpoint == SSL_IS_SERVER )
ansond 0:137634ff4186 3474 ssl->state = SSL_HANDSHAKE_WRAPUP;
ansond 0:137634ff4186 3475 #endif
ansond 0:137634ff4186 3476 }
ansond 0:137634ff4186 3477 else
ansond 0:137634ff4186 3478 ssl->state++;
ansond 0:137634ff4186 3479
ansond 0:137634ff4186 3480 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
ansond 0:137634ff4186 3481
ansond 0:137634ff4186 3482 return( 0 );
ansond 0:137634ff4186 3483 }
ansond 0:137634ff4186 3484
ansond 0:137634ff4186 3485 static void ssl_handshake_params_init( ssl_handshake_params *handshake )
ansond 0:137634ff4186 3486 {
ansond 0:137634ff4186 3487 memset( handshake, 0, sizeof( ssl_handshake_params ) );
ansond 0:137634ff4186 3488
ansond 0:137634ff4186 3489 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
ansond 0:137634ff4186 3490 defined(POLARSSL_SSL_PROTO_TLS1_1)
ansond 0:137634ff4186 3491 md5_init( &handshake->fin_md5 );
ansond 0:137634ff4186 3492 sha1_init( &handshake->fin_sha1 );
ansond 0:137634ff4186 3493 md5_starts( &handshake->fin_md5 );
ansond 0:137634ff4186 3494 sha1_starts( &handshake->fin_sha1 );
ansond 0:137634ff4186 3495 #endif
ansond 0:137634ff4186 3496 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
ansond 0:137634ff4186 3497 #if defined(POLARSSL_SHA256_C)
ansond 0:137634ff4186 3498 sha256_init( &handshake->fin_sha256 );
ansond 0:137634ff4186 3499 sha256_starts( &handshake->fin_sha256, 0 );
ansond 0:137634ff4186 3500 #endif
ansond 0:137634ff4186 3501 #if defined(POLARSSL_SHA512_C)
ansond 0:137634ff4186 3502 sha512_init( &handshake->fin_sha512 );
ansond 0:137634ff4186 3503 sha512_starts( &handshake->fin_sha512, 1 );
ansond 0:137634ff4186 3504 #endif
ansond 0:137634ff4186 3505 #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
ansond 0:137634ff4186 3506
ansond 0:137634ff4186 3507 handshake->update_checksum = ssl_update_checksum_start;
ansond 0:137634ff4186 3508 handshake->sig_alg = SSL_HASH_SHA1;
ansond 0:137634ff4186 3509
ansond 0:137634ff4186 3510 #if defined(POLARSSL_DHM_C)
ansond 0:137634ff4186 3511 dhm_init( &handshake->dhm_ctx );
ansond 0:137634ff4186 3512 #endif
ansond 0:137634ff4186 3513 #if defined(POLARSSL_ECDH_C)
ansond 0:137634ff4186 3514 ecdh_init( &handshake->ecdh_ctx );
ansond 0:137634ff4186 3515 #endif
ansond 0:137634ff4186 3516 }
ansond 0:137634ff4186 3517
ansond 0:137634ff4186 3518 static void ssl_transform_init( ssl_transform *transform )
ansond 0:137634ff4186 3519 {
ansond 0:137634ff4186 3520 memset( transform, 0, sizeof(ssl_transform) );
ansond 0:137634ff4186 3521
ansond 0:137634ff4186 3522 cipher_init( &transform->cipher_ctx_enc );
ansond 0:137634ff4186 3523 cipher_init( &transform->cipher_ctx_dec );
ansond 0:137634ff4186 3524
ansond 0:137634ff4186 3525 md_init( &transform->md_ctx_enc );
ansond 0:137634ff4186 3526 md_init( &transform->md_ctx_dec );
ansond 0:137634ff4186 3527 }
ansond 0:137634ff4186 3528
ansond 0:137634ff4186 3529 void ssl_session_init( ssl_session *session )
ansond 0:137634ff4186 3530 {
ansond 0:137634ff4186 3531 memset( session, 0, sizeof(ssl_session) );
ansond 0:137634ff4186 3532 }
ansond 0:137634ff4186 3533
ansond 0:137634ff4186 3534 static int ssl_handshake_init( ssl_context *ssl )
ansond 0:137634ff4186 3535 {
ansond 0:137634ff4186 3536 /* Clear old handshake information if present */
ansond 0:137634ff4186 3537 if( ssl->transform_negotiate )
ansond 0:137634ff4186 3538 ssl_transform_free( ssl->transform_negotiate );
ansond 0:137634ff4186 3539 if( ssl->session_negotiate )
ansond 0:137634ff4186 3540 ssl_session_free( ssl->session_negotiate );
ansond 0:137634ff4186 3541 if( ssl->handshake )
ansond 0:137634ff4186 3542 ssl_handshake_free( ssl->handshake );
ansond 0:137634ff4186 3543
ansond 0:137634ff4186 3544 /*
ansond 0:137634ff4186 3545 * Either the pointers are now NULL or cleared properly and can be freed.
ansond 0:137634ff4186 3546 * Now allocate missing structures.
ansond 0:137634ff4186 3547 */
ansond 0:137634ff4186 3548 if( ssl->transform_negotiate == NULL )
ansond 0:137634ff4186 3549 {
ansond 0:137634ff4186 3550 ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) );
ansond 0:137634ff4186 3551 }
ansond 0:137634ff4186 3552
ansond 0:137634ff4186 3553 if( ssl->session_negotiate == NULL )
ansond 0:137634ff4186 3554 {
ansond 0:137634ff4186 3555 ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) );
ansond 0:137634ff4186 3556 }
ansond 0:137634ff4186 3557
ansond 0:137634ff4186 3558 if( ssl->handshake == NULL )
ansond 0:137634ff4186 3559 {
ansond 0:137634ff4186 3560 ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) );
ansond 0:137634ff4186 3561 }
ansond 0:137634ff4186 3562
ansond 0:137634ff4186 3563 /* All pointers should exist and can be directly freed without issue */
ansond 0:137634ff4186 3564 if( ssl->handshake == NULL ||
ansond 0:137634ff4186 3565 ssl->transform_negotiate == NULL ||
ansond 0:137634ff4186 3566 ssl->session_negotiate == NULL )
ansond 0:137634ff4186 3567 {
ansond 0:137634ff4186 3568 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
ansond 0:137634ff4186 3569
ansond 0:137634ff4186 3570 polarssl_free( ssl->handshake );
ansond 0:137634ff4186 3571 polarssl_free( ssl->transform_negotiate );
ansond 0:137634ff4186 3572 polarssl_free( ssl->session_negotiate );
ansond 0:137634ff4186 3573
ansond 0:137634ff4186 3574 ssl->handshake = NULL;
ansond 0:137634ff4186 3575 ssl->transform_negotiate = NULL;
ansond 0:137634ff4186 3576 ssl->session_negotiate = NULL;
ansond 0:137634ff4186 3577
ansond 0:137634ff4186 3578 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
ansond 0:137634ff4186 3579 }
ansond 0:137634ff4186 3580
ansond 0:137634ff4186 3581 /* Initialize structures */
ansond 0:137634ff4186 3582 ssl_session_init( ssl->session_negotiate );
ansond 0:137634ff4186 3583 ssl_transform_init( ssl->transform_negotiate );
ansond 0:137634ff4186 3584 ssl_handshake_params_init( ssl->handshake );
ansond 0:137634ff4186 3585
ansond 0:137634ff4186 3586 #if defined(POLARSSL_X509_CRT_PARSE_C)
ansond 0:137634ff4186 3587 ssl->handshake->key_cert = ssl->key_cert;
ansond 0:137634ff4186 3588 #endif
ansond 0:137634ff4186 3589
ansond 0:137634ff4186 3590 return( 0 );
ansond 0:137634ff4186 3591 }
ansond 0:137634ff4186 3592
ansond 0:137634ff4186 3593 /*
ansond 0:137634ff4186 3594 * Initialize an SSL context
ansond 0:137634ff4186 3595 */
ansond 0:137634ff4186 3596 int ssl_init( ssl_context *ssl )
ansond 0:137634ff4186 3597 {
ansond 0:137634ff4186 3598 int ret;
ansond 0:137634ff4186 3599 int len = SSL_BUFFER_LEN;
ansond 0:137634ff4186 3600
ansond 0:137634ff4186 3601 memset( ssl, 0, sizeof( ssl_context ) );
ansond 0:137634ff4186 3602
ansond 0:137634ff4186 3603 /*
ansond 0:137634ff4186 3604 * Sane defaults
ansond 0:137634ff4186 3605 */
ansond 0:137634ff4186 3606 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
ansond 0:137634ff4186 3607 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
ansond 0:137634ff4186 3608 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
ansond 0:137634ff4186 3609 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
ansond 0:137634ff4186 3610
ansond 0:137634ff4186 3611 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
ansond 0:137634ff4186 3612
ansond 0:137634ff4186 3613 #if defined(POLARSSL_SSL_RENEGOTIATION)
ansond 0:137634ff4186 3614 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
ansond 0:137634ff4186 3615 memset( ssl->renego_period, 0xFF, 7 );
ansond 0:137634ff4186 3616 ssl->renego_period[7] = 0x00;
ansond 0:137634ff4186 3617 #endif
ansond 0:137634ff4186 3618
ansond 0:137634ff4186 3619 #if defined(POLARSSL_DHM_C)
ansond 0:137634ff4186 3620 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
ansond 0:137634ff4186 3621 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
ansond 0:137634ff4186 3622 ( ret = mpi_read_string( &ssl->dhm_G, 16,
ansond 0:137634ff4186 3623 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
ansond 0:137634ff4186 3624 {
ansond 0:137634ff4186 3625 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
ansond 0:137634ff4186 3626 return( ret );
ansond 0:137634ff4186 3627 }
ansond 0:137634ff4186 3628 #endif
ansond 0:137634ff4186 3629
ansond 0:137634ff4186 3630 /*
ansond 0:137634ff4186 3631 * Prepare base structures
ansond 0:137634ff4186 3632 */
ansond 0:137634ff4186 3633 if( ( ssl->in_ctr = polarssl_malloc( len ) ) == NULL ||
ansond 0:137634ff4186 3634 ( ssl->out_ctr = polarssl_malloc( len ) ) == NULL )
ansond 0:137634ff4186 3635 {
ansond 0:137634ff4186 3636 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
ansond 0:137634ff4186 3637 polarssl_free( ssl->in_ctr );
ansond 0:137634ff4186 3638 ssl->in_ctr = NULL;
ansond 0:137634ff4186 3639 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
ansond 0:137634ff4186 3640 }
ansond 0:137634ff4186 3641
ansond 0:137634ff4186 3642 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
ansond 0:137634ff4186 3643 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
ansond 0:137634ff4186 3644
ansond 0:137634ff4186 3645 ssl->in_hdr = ssl->in_ctr + 8;
ansond 0:137634ff4186 3646 ssl->in_iv = ssl->in_ctr + 13;
ansond 0:137634ff4186 3647 ssl->in_msg = ssl->in_ctr + 13;
ansond 0:137634ff4186 3648
ansond 0:137634ff4186 3649 ssl->out_hdr = ssl->out_ctr + 8;
ansond 0:137634ff4186 3650 ssl->out_iv = ssl->out_ctr + 13;
ansond 0:137634ff4186 3651 ssl->out_msg = ssl->out_ctr + 13;
ansond 0:137634ff4186 3652
ansond 0:137634ff4186 3653 #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
ansond 0:137634ff4186 3654 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
ansond 0:137634ff4186 3655 #endif
ansond 0:137634ff4186 3656
ansond 0:137634ff4186 3657 #if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
ansond 0:137634ff4186 3658 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
ansond 0:137634ff4186 3659 #endif
ansond 0:137634ff4186 3660
ansond 0:137634ff4186 3661 #if defined(POLARSSL_SSL_SESSION_TICKETS)
ansond 0:137634ff4186 3662 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
ansond 0:137634ff4186 3663 #endif
ansond 0:137634ff4186 3664
ansond 0:137634ff4186 3665 #if defined(POLARSSL_SSL_SET_CURVES)
ansond 0:137634ff4186 3666 ssl->curve_list = ecp_grp_id_list( );
ansond 0:137634ff4186 3667 #endif
ansond 0:137634ff4186 3668
ansond 0:137634ff4186 3669 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
ansond 0:137634ff4186 3670 return( ret );
ansond 0:137634ff4186 3671
ansond 0:137634ff4186 3672 return( 0 );
ansond 0:137634ff4186 3673 }
ansond 0:137634ff4186 3674
ansond 0:137634ff4186 3675 /*
ansond 0:137634ff4186 3676 * Reset an initialized and used SSL context for re-use while retaining
ansond 0:137634ff4186 3677 * all application-set variables, function pointers and data.
ansond 0:137634ff4186 3678 */
ansond 0:137634ff4186 3679 int ssl_session_reset( ssl_context *ssl )
ansond 0:137634ff4186 3680 {
ansond 0:137634ff4186 3681 int ret;
ansond 0:137634ff4186 3682
ansond 0:137634ff4186 3683 ssl->state = SSL_HELLO_REQUEST;
ansond 0:137634ff4186 3684
ansond 0:137634ff4186 3685 #if defined(POLARSSL_SSL_RENEGOTIATION)
ansond 0:137634ff4186 3686 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
ansond 0:137634ff4186 3687 ssl->renego_records_seen = 0;
ansond 0:137634ff4186 3688
ansond 0:137634ff4186 3689 ssl->verify_data_len = 0;
ansond 0:137634ff4186 3690 memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
ansond 0:137634ff4186 3691 memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
ansond 0:137634ff4186 3692 #endif
ansond 0:137634ff4186 3693 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
ansond 0:137634ff4186 3694
ansond 0:137634ff4186 3695 ssl->in_offt = NULL;
ansond 0:137634ff4186 3696
ansond 0:137634ff4186 3697 ssl->in_msg = ssl->in_ctr + 13;
ansond 0:137634ff4186 3698 ssl->in_msgtype = 0;
ansond 0:137634ff4186 3699 ssl->in_msglen = 0;
ansond 0:137634ff4186 3700 ssl->in_left = 0;
ansond 0:137634ff4186 3701
ansond 0:137634ff4186 3702 ssl->in_hslen = 0;
ansond 0:137634ff4186 3703 ssl->nb_zero = 0;
ansond 0:137634ff4186 3704 ssl->record_read = 0;
ansond 0:137634ff4186 3705
ansond 0:137634ff4186 3706 ssl->out_msg = ssl->out_ctr + 13;
ansond 0:137634ff4186 3707 ssl->out_msgtype = 0;
ansond 0:137634ff4186 3708 ssl->out_msglen = 0;
ansond 0:137634ff4186 3709 ssl->out_left = 0;
ansond 0:137634ff4186 3710 #if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
ansond 0:137634ff4186 3711 if( ssl->split_done != SSL_CBC_RECORD_SPLITTING_DISABLED )
ansond 0:137634ff4186 3712 ssl->split_done = 0;
ansond 0:137634ff4186 3713 #endif
ansond 0:137634ff4186 3714
ansond 0:137634ff4186 3715 ssl->transform_in = NULL;
ansond 0:137634ff4186 3716 ssl->transform_out = NULL;
ansond 0:137634ff4186 3717
ansond 0:137634ff4186 3718 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
ansond 0:137634ff4186 3719 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
ansond 0:137634ff4186 3720
ansond 0:137634ff4186 3721 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
ansond 0:137634ff4186 3722 if( ssl_hw_record_reset != NULL )
ansond 0:137634ff4186 3723 {
ansond 0:137634ff4186 3724 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
ansond 0:137634ff4186 3725 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
ansond 0:137634ff4186 3726 {
ansond 0:137634ff4186 3727 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
ansond 0:137634ff4186 3728 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
ansond 0:137634ff4186 3729 }
ansond 0:137634ff4186 3730 }
ansond 0:137634ff4186 3731 #endif
ansond 0:137634ff4186 3732
ansond 0:137634ff4186 3733 if( ssl->transform )
ansond 0:137634ff4186 3734 {
ansond 0:137634ff4186 3735 ssl_transform_free( ssl->transform );
ansond 0:137634ff4186 3736 polarssl_free( ssl->transform );
ansond 0:137634ff4186 3737 ssl->transform = NULL;
ansond 0:137634ff4186 3738 }
ansond 0:137634ff4186 3739
ansond 0:137634ff4186 3740 if( ssl->session )
ansond 0:137634ff4186 3741 {
ansond 0:137634ff4186 3742 ssl_session_free( ssl->session );
ansond 0:137634ff4186 3743 polarssl_free( ssl->session );
ansond 0:137634ff4186 3744 ssl->session = NULL;
ansond 0:137634ff4186 3745 }
ansond 0:137634ff4186 3746
ansond 0:137634ff4186 3747 #if defined(POLARSSL_SSL_ALPN)
ansond 0:137634ff4186 3748 ssl->alpn_chosen = NULL;
ansond 0:137634ff4186 3749 #endif
ansond 0:137634ff4186 3750
ansond 0:137634ff4186 3751 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
ansond 0:137634ff4186 3752 return( ret );
ansond 0:137634ff4186 3753
ansond 0:137634ff4186 3754 return( 0 );
ansond 0:137634ff4186 3755 }
ansond 0:137634ff4186 3756
ansond 0:137634ff4186 3757 #if defined(POLARSSL_SSL_SESSION_TICKETS)
ansond 0:137634ff4186 3758 static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
ansond 0:137634ff4186 3759 {
ansond 0:137634ff4186 3760 aes_free( &tkeys->enc );
ansond 0:137634ff4186 3761 aes_free( &tkeys->dec );
ansond 0:137634ff4186 3762
ansond 0:137634ff4186 3763 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
ansond 0:137634ff4186 3764 }
ansond 0:137634ff4186 3765
ansond 0:137634ff4186 3766 /*
ansond 0:137634ff4186 3767 * Allocate and initialize ticket keys
ansond 0:137634ff4186 3768 */
ansond 0:137634ff4186 3769 static int ssl_ticket_keys_init( ssl_context *ssl )
ansond 0:137634ff4186 3770 {
ansond 0:137634ff4186 3771 int ret;
ansond 0:137634ff4186 3772 ssl_ticket_keys *tkeys;
ansond 0:137634ff4186 3773 unsigned char buf[16];
ansond 0:137634ff4186 3774
ansond 0:137634ff4186 3775 if( ssl->ticket_keys != NULL )
ansond 0:137634ff4186 3776 return( 0 );
ansond 0:137634ff4186 3777
ansond 0:137634ff4186 3778 tkeys = polarssl_malloc( sizeof(ssl_ticket_keys) );
ansond 0:137634ff4186 3779 if( tkeys == NULL )
ansond 0:137634ff4186 3780 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
ansond 0:137634ff4186 3781
ansond 0:137634ff4186 3782 aes_init( &tkeys->enc );
ansond 0:137634ff4186 3783 aes_init( &tkeys->dec );
ansond 0:137634ff4186 3784
ansond 0:137634ff4186 3785 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
ansond 0:137634ff4186 3786 {
ansond 0:137634ff4186 3787 ssl_ticket_keys_free( tkeys );
ansond 0:137634ff4186 3788 polarssl_free( tkeys );
ansond 0:137634ff4186 3789 return( ret );
ansond 0:137634ff4186 3790 }
ansond 0:137634ff4186 3791
ansond 0:137634ff4186 3792 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
ansond 0:137634ff4186 3793 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
ansond 0:137634ff4186 3794 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
ansond 0:137634ff4186 3795 {
ansond 0:137634ff4186 3796 ssl_ticket_keys_free( tkeys );
ansond 0:137634ff4186 3797 polarssl_free( tkeys );
ansond 0:137634ff4186 3798 return( ret );
ansond 0:137634ff4186 3799 }
ansond 0:137634ff4186 3800
ansond 0:137634ff4186 3801 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
ansond 0:137634ff4186 3802 {
ansond 0:137634ff4186 3803 ssl_ticket_keys_free( tkeys );
ansond 0:137634ff4186 3804 polarssl_free( tkeys );
ansond 0:137634ff4186 3805 return( ret );
ansond 0:137634ff4186 3806 }
ansond 0:137634ff4186 3807
ansond 0:137634ff4186 3808 ssl->ticket_keys = tkeys;
ansond 0:137634ff4186 3809
ansond 0:137634ff4186 3810 return( 0 );
ansond 0:137634ff4186 3811 }
ansond 0:137634ff4186 3812 #endif /* POLARSSL_SSL_SESSION_TICKETS */
ansond 0:137634ff4186 3813
ansond 0:137634ff4186 3814 /*
ansond 0:137634ff4186 3815 * SSL set accessors
ansond 0:137634ff4186 3816 */
ansond 0:137634ff4186 3817 void ssl_set_endpoint( ssl_context *ssl, int endpoint )
ansond 0:137634ff4186 3818 {
ansond 0:137634ff4186 3819 ssl->endpoint = endpoint;
ansond 0:137634ff4186 3820
ansond 0:137634ff4186 3821 #if defined(POLARSSL_SSL_SESSION_TICKETS) && \
ansond 0:137634ff4186 3822 defined(POLARSSL_SSL_CLI_C)
ansond 0:137634ff4186 3823 if( endpoint == SSL_IS_CLIENT )
ansond 0:137634ff4186 3824 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
ansond 0:137634ff4186 3825 #endif
ansond 0:137634ff4186 3826
ansond 0:137634ff4186 3827 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
ansond 0:137634ff4186 3828 if( endpoint == SSL_IS_SERVER )
ansond 0:137634ff4186 3829 ssl->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
ansond 0:137634ff4186 3830 #endif
ansond 0:137634ff4186 3831 }
ansond 0:137634ff4186 3832
ansond 0:137634ff4186 3833 void ssl_set_authmode( ssl_context *ssl, int authmode )
ansond 0:137634ff4186 3834 {
ansond 0:137634ff4186 3835 ssl->authmode = authmode;
ansond 0:137634ff4186 3836 }
ansond 0:137634ff4186 3837
ansond 0:137634ff4186 3838 #if defined(POLARSSL_X509_CRT_PARSE_C)
ansond 0:137634ff4186 3839 void ssl_set_verify( ssl_context *ssl,
ansond 0:137634ff4186 3840 int (*f_vrfy)(void *, x509_crt *, int, int *),
ansond 0:137634ff4186 3841 void *p_vrfy )
ansond 0:137634ff4186 3842 {
ansond 0:137634ff4186 3843 ssl->f_vrfy = f_vrfy;
ansond 0:137634ff4186 3844 ssl->p_vrfy = p_vrfy;
ansond 0:137634ff4186 3845 }
ansond 0:137634ff4186 3846 #endif /* POLARSSL_X509_CRT_PARSE_C */
ansond 0:137634ff4186 3847
ansond 0:137634ff4186 3848 void ssl_set_rng( ssl_context *ssl,
ansond 0:137634ff4186 3849 int (*f_rng)(void *, unsigned char *, size_t),
ansond 0:137634ff4186 3850 void *p_rng )
ansond 0:137634ff4186 3851 {
ansond 0:137634ff4186 3852 ssl->f_rng = f_rng;
ansond 0:137634ff4186 3853 ssl->p_rng = p_rng;
ansond 0:137634ff4186 3854 }
ansond 0:137634ff4186 3855
ansond 0:137634ff4186 3856 void ssl_set_dbg( ssl_context *ssl,
ansond 0:137634ff4186 3857 void (*f_dbg)(void *, int, const char *),
ansond 0:137634ff4186 3858 void *p_dbg )
ansond 0:137634ff4186 3859 {
ansond 0:137634ff4186 3860 ssl->f_dbg = f_dbg;
ansond 0:137634ff4186 3861 ssl->p_dbg = p_dbg;
ansond 0:137634ff4186 3862 }
ansond 0:137634ff4186 3863
ansond 0:137634ff4186 3864 void ssl_set_bio( ssl_context *ssl,
ansond 0:137634ff4186 3865 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
ansond 0:137634ff4186 3866 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
ansond 0:137634ff4186 3867 {
ansond 0:137634ff4186 3868 ssl->f_recv = f_recv;
ansond 0:137634ff4186 3869 ssl->f_send = f_send;
ansond 0:137634ff4186 3870 ssl->p_recv = p_recv;
ansond 0:137634ff4186 3871 ssl->p_send = p_send;
ansond 0:137634ff4186 3872 }
ansond 0:137634ff4186 3873
ansond 0:137634ff4186 3874 #if defined(POLARSSL_SSL_SRV_C)
ansond 0:137634ff4186 3875 void ssl_set_session_cache( ssl_context *ssl,
ansond 0:137634ff4186 3876 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
ansond 0:137634ff4186 3877 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
ansond 0:137634ff4186 3878 {
ansond 0:137634ff4186 3879 ssl->f_get_cache = f_get_cache;
ansond 0:137634ff4186 3880 ssl->p_get_cache = p_get_cache;
ansond 0:137634ff4186 3881 ssl->f_set_cache = f_set_cache;
ansond 0:137634ff4186 3882 ssl->p_set_cache = p_set_cache;
ansond 0:137634ff4186 3883 }
ansond 0:137634ff4186 3884 #endif /* POLARSSL_SSL_SRV_C */
ansond 0:137634ff4186 3885
ansond 0:137634ff4186 3886 #if defined(POLARSSL_SSL_CLI_C)
ansond 0:137634ff4186 3887 int ssl_set_session( ssl_context *ssl, const ssl_session *session )
ansond 0:137634ff4186 3888 {
ansond 0:137634ff4186 3889 int ret;
ansond 0:137634ff4186 3890
ansond 0:137634ff4186 3891 if( ssl == NULL ||
ansond 0:137634ff4186 3892 session == NULL ||
ansond 0:137634ff4186 3893 ssl->session_negotiate == NULL ||
ansond 0:137634ff4186 3894 ssl->endpoint != SSL_IS_CLIENT )
ansond 0:137634ff4186 3895 {
ansond 0:137634ff4186 3896 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
ansond 0:137634ff4186 3897 }
ansond 0:137634ff4186 3898
ansond 0:137634ff4186 3899 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
ansond 0:137634ff4186 3900 return( ret );
ansond 0:137634ff4186 3901
ansond 0:137634ff4186 3902 ssl->handshake->resume = 1;
ansond 0:137634ff4186 3903
ansond 0:137634ff4186 3904 return( 0 );
ansond 0:137634ff4186 3905 }
ansond 0:137634ff4186 3906 #endif /* POLARSSL_SSL_CLI_C */
ansond 0:137634ff4186 3907
ansond 0:137634ff4186 3908 void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
ansond 0:137634ff4186 3909 {
ansond 0:137634ff4186 3910 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
ansond 0:137634ff4186 3911 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
ansond 0:137634ff4186 3912 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
ansond 0:137634ff4186 3913 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
ansond 0:137634ff4186 3914 }
ansond 0:137634ff4186 3915
ansond 0:137634ff4186 3916 void ssl_set_ciphersuites_for_version( ssl_context *ssl,
ansond 0:137634ff4186 3917 const int *ciphersuites,
ansond 0:137634ff4186 3918 int major, int minor )
ansond 0:137634ff4186 3919 {
ansond 0:137634ff4186 3920 if( major != SSL_MAJOR_VERSION_3 )
ansond 0:137634ff4186 3921 return;
ansond 0:137634ff4186 3922
ansond 0:137634ff4186 3923 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
ansond 0:137634ff4186 3924 return;
ansond 0:137634ff4186 3925
ansond 0:137634ff4186 3926 ssl->ciphersuite_list[minor] = ciphersuites;
ansond 0:137634ff4186 3927 }
ansond 0:137634ff4186 3928
ansond 0:137634ff4186 3929 #if defined(POLARSSL_X509_CRT_PARSE_C)
ansond 0:137634ff4186 3930 /* Add a new (empty) key_cert entry an return a pointer to it */
ansond 0:137634ff4186 3931 static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
ansond 0:137634ff4186 3932 {
ansond 0:137634ff4186 3933 ssl_key_cert *key_cert, *last;
ansond 0:137634ff4186 3934
ansond 0:137634ff4186 3935 key_cert = polarssl_malloc( sizeof(ssl_key_cert) );
ansond 0:137634ff4186 3936 if( key_cert == NULL )
ansond 0:137634ff4186 3937 return( NULL );
ansond 0:137634ff4186 3938
ansond 0:137634ff4186 3939 memset( key_cert, 0, sizeof( ssl_key_cert ) );
ansond 0:137634ff4186 3940
ansond 0:137634ff4186 3941 /* Append the new key_cert to the (possibly empty) current list */
ansond 0:137634ff4186 3942 if( ssl->key_cert == NULL )
ansond 0:137634ff4186 3943 {
ansond 0:137634ff4186 3944 ssl->key_cert = key_cert;
ansond 0:137634ff4186 3945 if( ssl->handshake != NULL )
ansond 0:137634ff4186 3946 ssl->handshake->key_cert = key_cert;
ansond 0:137634ff4186 3947 }
ansond 0:137634ff4186 3948 else
ansond 0:137634ff4186 3949 {
ansond 0:137634ff4186 3950 last = ssl->key_cert;
ansond 0:137634ff4186 3951 while( last->next != NULL )
ansond 0:137634ff4186 3952 last = last->next;
ansond 0:137634ff4186 3953 last->next = key_cert;
ansond 0:137634ff4186 3954 }
ansond 0:137634ff4186 3955
ansond 0:137634ff4186 3956 return( key_cert );
ansond 0:137634ff4186 3957 }
ansond 0:137634ff4186 3958
ansond 0:137634ff4186 3959 void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
ansond 0:137634ff4186 3960 x509_crl *ca_crl, const char *peer_cn )
ansond 0:137634ff4186 3961 {
ansond 0:137634ff4186 3962 ssl->ca_chain = ca_chain;
ansond 0:137634ff4186 3963 ssl->ca_crl = ca_crl;
ansond 0:137634ff4186 3964 ssl->peer_cn = peer_cn;
ansond 0:137634ff4186 3965 }
ansond 0:137634ff4186 3966
ansond 0:137634ff4186 3967 int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
ansond 0:137634ff4186 3968 pk_context *pk_key )
ansond 0:137634ff4186 3969 {
ansond 0:137634ff4186 3970 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
ansond 0:137634ff4186 3971
ansond 0:137634ff4186 3972 if( key_cert == NULL )
ansond 0:137634ff4186 3973 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
ansond 0:137634ff4186 3974
ansond 0:137634ff4186 3975 key_cert->cert = own_cert;
ansond 0:137634ff4186 3976 key_cert->key = pk_key;
ansond 0:137634ff4186 3977
ansond 0:137634ff4186 3978 return( 0 );
ansond 0:137634ff4186 3979 }
ansond 0:137634ff4186 3980
ansond 0:137634ff4186 3981 #if ! defined(POLARSSL_DEPRECATED_REMOVED)
ansond 0:137634ff4186 3982 #if defined(POLARSSL_RSA_C)
ansond 0:137634ff4186 3983 int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
ansond 0:137634ff4186 3984 rsa_context *rsa_key )
ansond 0:137634ff4186 3985 {
ansond 0:137634ff4186 3986 int ret;
ansond 0:137634ff4186 3987 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
ansond 0:137634ff4186 3988
ansond 0:137634ff4186 3989 if( key_cert == NULL )
ansond 0:137634ff4186 3990 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
ansond 0:137634ff4186 3991
ansond 0:137634ff4186 3992 key_cert->key = polarssl_malloc( sizeof(pk_context) );
ansond 0:137634ff4186 3993 if( key_cert->key == NULL )
ansond 0:137634ff4186 3994 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
ansond 0:137634ff4186 3995
ansond 0:137634ff4186 3996 pk_init( key_cert->key );
ansond 0:137634ff4186 3997
ansond 0:137634ff4186 3998 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
ansond 0:137634ff4186 3999 if( ret != 0 )
ansond 0:137634ff4186 4000 return( ret );
ansond 0:137634ff4186 4001
ansond 0:137634ff4186 4002 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
ansond 0:137634ff4186 4003 return( ret );
ansond 0:137634ff4186 4004
ansond 0:137634ff4186 4005 key_cert->cert = own_cert;
ansond 0:137634ff4186 4006 key_cert->key_own_alloc = 1;
ansond 0:137634ff4186 4007
ansond 0:137634ff4186 4008 return( 0 );
ansond 0:137634ff4186 4009 }
ansond 0:137634ff4186 4010 #endif /* POLARSSL_RSA_C */
ansond 0:137634ff4186 4011
ansond 0:137634ff4186 4012 int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
ansond 0:137634ff4186 4013 void *rsa_key,
ansond 0:137634ff4186 4014 rsa_decrypt_func rsa_decrypt,
ansond 0:137634ff4186 4015 rsa_sign_func rsa_sign,
ansond 0:137634ff4186 4016 rsa_key_len_func rsa_key_len )
ansond 0:137634ff4186 4017 {
ansond 0:137634ff4186 4018 int ret;
ansond 0:137634ff4186 4019 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
ansond 0:137634ff4186 4020
ansond 0:137634ff4186 4021 if( key_cert == NULL )
ansond 0:137634ff4186 4022 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
ansond 0:137634ff4186 4023
ansond 0:137634ff4186 4024 key_cert->key = polarssl_malloc( sizeof(pk_context) );
ansond 0:137634ff4186 4025 if( key_cert->key == NULL )
ansond 0:137634ff4186 4026 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
ansond 0:137634ff4186 4027
ansond 0:137634ff4186 4028 pk_init( key_cert->key );
ansond 0:137634ff4186 4029
ansond 0:137634ff4186 4030 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
ansond 0:137634ff4186 4031 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
ansond 0:137634ff4186 4032 return( ret );
ansond 0:137634ff4186 4033
ansond 0:137634ff4186 4034 key_cert->cert = own_cert;
ansond 0:137634ff4186 4035 key_cert->key_own_alloc = 1;
ansond 0:137634ff4186 4036
ansond 0:137634ff4186 4037 return( 0 );
ansond 0:137634ff4186 4038 }
ansond 0:137634ff4186 4039 #endif /* POLARSSL_DEPRECATED_REMOVED */
ansond 0:137634ff4186 4040 #endif /* POLARSSL_X509_CRT_PARSE_C */
ansond 0:137634ff4186 4041
ansond 0:137634ff4186 4042 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
ansond 0:137634ff4186 4043 int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
ansond 0:137634ff4186 4044 const unsigned char *psk_identity, size_t psk_identity_len )
ansond 0:137634ff4186 4045 {
ansond 0:137634ff4186 4046 if( psk == NULL || psk_identity == NULL )
ansond 0:137634ff4186 4047 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
ansond 0:137634ff4186 4048
ansond 0:137634ff4186 4049 if( psk_len > POLARSSL_PSK_MAX_LEN )
ansond 0:137634ff4186 4050 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
ansond 0:137634ff4186 4051
ansond 0:137634ff4186 4052 if( ssl->psk != NULL || ssl->psk_identity != NULL )
ansond 0:137634ff4186 4053 {
ansond 0:137634ff4186 4054 polarssl_free( ssl->psk );
ansond 0:137634ff4186 4055 polarssl_free( ssl->psk_identity );
ansond 0:137634ff4186 4056 }
ansond 0:137634ff4186 4057
ansond 0:137634ff4186 4058 if( ( ssl->psk = polarssl_malloc( psk_len ) ) == NULL ||
ansond 0:137634ff4186 4059 ( ssl->psk_identity = polarssl_malloc( psk_identity_len ) ) == NULL )
ansond 0:137634ff4186 4060 {
ansond 0:137634ff4186 4061 polarssl_free( ssl->psk );
ansond 0:137634ff4186 4062 ssl->psk = NULL;
ansond 0:137634ff4186 4063 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
ansond 0:137634ff4186 4064 }
ansond 0:137634ff4186 4065
ansond 0:137634ff4186 4066 ssl->psk_len = psk_len;
ansond 0:137634ff4186 4067 ssl->psk_identity_len = psk_identity_len;
ansond 0:137634ff4186 4068
ansond 0:137634ff4186 4069 memcpy( ssl->psk, psk, ssl->psk_len );
ansond 0:137634ff4186 4070 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
ansond 0:137634ff4186 4071
ansond 0:137634ff4186 4072 return( 0 );
ansond 0:137634ff4186 4073 }
ansond 0:137634ff4186 4074
ansond 0:137634ff4186 4075 void ssl_set_psk_cb( ssl_context *ssl,
ansond 0:137634ff4186 4076 int (*f_psk)(void *, ssl_context *, const unsigned char *,
ansond 0:137634ff4186 4077 size_t),
ansond 0:137634ff4186 4078 void *p_psk )
ansond 0:137634ff4186 4079 {
ansond 0:137634ff4186 4080 ssl->f_psk = f_psk;
ansond 0:137634ff4186 4081 ssl->p_psk = p_psk;
ansond 0:137634ff4186 4082 }
ansond 0:137634ff4186 4083 #endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
ansond 0:137634ff4186 4084
ansond 0:137634ff4186 4085 #if defined(POLARSSL_DHM_C)
ansond 0:137634ff4186 4086 int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
ansond 0:137634ff4186 4087 {
ansond 0:137634ff4186 4088 int ret;
ansond 0:137634ff4186 4089
ansond 0:137634ff4186 4090 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
ansond 0:137634ff4186 4091 {
ansond 0:137634ff4186 4092 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
ansond 0:137634ff4186 4093 return( ret );
ansond 0:137634ff4186 4094 }
ansond 0:137634ff4186 4095
ansond 0:137634ff4186 4096 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
ansond 0:137634ff4186 4097 {
ansond 0:137634ff4186 4098 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
ansond 0:137634ff4186 4099 return( ret );
ansond 0:137634ff4186 4100 }
ansond 0:137634ff4186 4101
ansond 0:137634ff4186 4102 return( 0 );
ansond 0:137634ff4186 4103 }
ansond 0:137634ff4186 4104
ansond 0:137634ff4186 4105 int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
ansond 0:137634ff4186 4106 {
ansond 0:137634ff4186 4107 int ret;
ansond 0:137634ff4186 4108
ansond 0:137634ff4186 4109 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
ansond 0:137634ff4186 4110 {
ansond 0:137634ff4186 4111 SSL_DEBUG_RET( 1, "mpi_copy", ret );
ansond 0:137634ff4186 4112 return( ret );
ansond 0:137634ff4186 4113 }
ansond 0:137634ff4186 4114
ansond 0:137634ff4186 4115 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
ansond 0:137634ff4186 4116 {
ansond 0:137634ff4186 4117 SSL_DEBUG_RET( 1, "mpi_copy", ret );
ansond 0:137634ff4186 4118 return( ret );
ansond 0:137634ff4186 4119 }
ansond 0:137634ff4186 4120
ansond 0:137634ff4186 4121 return( 0 );
ansond 0:137634ff4186 4122 }
ansond 0:137634ff4186 4123 #endif /* POLARSSL_DHM_C */
ansond 0:137634ff4186 4124
ansond 0:137634ff4186 4125 #if defined(POLARSSL_SSL_SET_CURVES)
ansond 0:137634ff4186 4126 /*
ansond 0:137634ff4186 4127 * Set the allowed elliptic curves
ansond 0:137634ff4186 4128 */
ansond 0:137634ff4186 4129 void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
ansond 0:137634ff4186 4130 {
ansond 0:137634ff4186 4131 ssl->curve_list = curve_list;
ansond 0:137634ff4186 4132 }
ansond 0:137634ff4186 4133 #endif
ansond 0:137634ff4186 4134
ansond 0:137634ff4186 4135 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
ansond 0:137634ff4186 4136 int ssl_set_hostname( ssl_context *ssl, const char *hostname )
ansond 0:137634ff4186 4137 {
ansond 0:137634ff4186 4138 if( hostname == NULL )
ansond 0:137634ff4186 4139 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
ansond 0:137634ff4186 4140
ansond 0:137634ff4186 4141 ssl->hostname_len = strlen( hostname );
ansond 0:137634ff4186 4142
ansond 0:137634ff4186 4143 if( ssl->hostname_len + 1 == 0 )
ansond 0:137634ff4186 4144 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
ansond 0:137634ff4186 4145
ansond 0:137634ff4186 4146 ssl->hostname = polarssl_malloc( ssl->hostname_len + 1 );
ansond 0:137634ff4186 4147
ansond 0:137634ff4186 4148 if( ssl->hostname == NULL )
ansond 0:137634ff4186 4149 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
ansond 0:137634ff4186 4150
ansond 0:137634ff4186 4151 memcpy( ssl->hostname, (const unsigned char *) hostname,
ansond 0:137634ff4186 4152 ssl->hostname_len );
ansond 0:137634ff4186 4153
ansond 0:137634ff4186 4154 ssl->hostname[ssl->hostname_len] = '\0';
ansond 0:137634ff4186 4155
ansond 0:137634ff4186 4156 return( 0 );
ansond 0:137634ff4186 4157 }
ansond 0:137634ff4186 4158
ansond 0:137634ff4186 4159 void ssl_set_sni( ssl_context *ssl,
ansond 0:137634ff4186 4160 int (*f_sni)(void *, ssl_context *,
ansond 0:137634ff4186 4161 const unsigned char *, size_t),
ansond 0:137634ff4186 4162 void *p_sni )
ansond 0:137634ff4186 4163 {
ansond 0:137634ff4186 4164 ssl->f_sni = f_sni;
ansond 0:137634ff4186 4165 ssl->p_sni = p_sni;
ansond 0:137634ff4186 4166 }
ansond 0:137634ff4186 4167 #endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
ansond 0:137634ff4186 4168
ansond 0:137634ff4186 4169 #if defined(POLARSSL_SSL_ALPN)
ansond 0:137634ff4186 4170 int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
ansond 0:137634ff4186 4171 {
ansond 0:137634ff4186 4172 size_t cur_len, tot_len;
ansond 0:137634ff4186 4173 const char **p;
ansond 0:137634ff4186 4174
ansond 0:137634ff4186 4175 /*
ansond 0:137634ff4186 4176 * "Empty strings MUST NOT be included and byte strings MUST NOT be
ansond 0:137634ff4186 4177 * truncated". Check lengths now rather than later.
ansond 0:137634ff4186 4178 */
ansond 0:137634ff4186 4179 tot_len = 0;
ansond 0:137634ff4186 4180 for( p = protos; *p != NULL; p++ )
ansond 0:137634ff4186 4181 {
ansond 0:137634ff4186 4182 cur_len = strlen( *p );
ansond 0:137634ff4186 4183 tot_len += cur_len;
ansond 0:137634ff4186 4184
ansond 0:137634ff4186 4185 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
ansond 0:137634ff4186 4186 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
ansond 0:137634ff4186 4187 }
ansond 0:137634ff4186 4188
ansond 0:137634ff4186 4189 ssl->alpn_list = protos;
ansond 0:137634ff4186 4190
ansond 0:137634ff4186 4191 return( 0 );
ansond 0:137634ff4186 4192 }
ansond 0:137634ff4186 4193
ansond 0:137634ff4186 4194 const char *ssl_get_alpn_protocol( const ssl_context *ssl )
ansond 0:137634ff4186 4195 {
ansond 0:137634ff4186 4196 return( ssl->alpn_chosen );
ansond 0:137634ff4186 4197 }
ansond 0:137634ff4186 4198 #endif /* POLARSSL_SSL_ALPN */
ansond 0:137634ff4186 4199
ansond 0:137634ff4186 4200 void ssl_set_max_version( ssl_context *ssl, int major, int minor )
ansond 0:137634ff4186 4201 {
ansond 0:137634ff4186 4202 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
ansond 0:137634ff4186 4203 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
ansond 0:137634ff4186 4204 {
ansond 0:137634ff4186 4205 ssl->max_major_ver = major;
ansond 0:137634ff4186 4206 ssl->max_minor_ver = minor;
ansond 0:137634ff4186 4207 }
ansond 0:137634ff4186 4208 }
ansond 0:137634ff4186 4209
ansond 0:137634ff4186 4210 void ssl_set_min_version( ssl_context *ssl, int major, int minor )
ansond 0:137634ff4186 4211 {
ansond 0:137634ff4186 4212 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
ansond 0:137634ff4186 4213 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
ansond 0:137634ff4186 4214 {
ansond 0:137634ff4186 4215 ssl->min_major_ver = major;
ansond 0:137634ff4186 4216 ssl->min_minor_ver = minor;
ansond 0:137634ff4186 4217 }
ansond 0:137634ff4186 4218 }
ansond 0:137634ff4186 4219
ansond 0:137634ff4186 4220 #if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
ansond 0:137634ff4186 4221 void ssl_set_fallback( ssl_context *ssl, char fallback )
ansond 0:137634ff4186 4222 {
ansond 0:137634ff4186 4223 ssl->fallback = fallback;
ansond 0:137634ff4186 4224 }
ansond 0:137634ff4186 4225 #endif
ansond 0:137634ff4186 4226
ansond 0:137634ff4186 4227 #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
ansond 0:137634ff4186 4228 void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
ansond 0:137634ff4186 4229 {
ansond 0:137634ff4186 4230 ssl->encrypt_then_mac = etm;
ansond 0:137634ff4186 4231 }
ansond 0:137634ff4186 4232 #endif
ansond 0:137634ff4186 4233
ansond 0:137634ff4186 4234 #if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
ansond 0:137634ff4186 4235 void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
ansond 0:137634ff4186 4236 {
ansond 0:137634ff4186 4237 ssl->extended_ms = ems;
ansond 0:137634ff4186 4238 }
ansond 0:137634ff4186 4239 #endif
ansond 0:137634ff4186 4240
ansond 0:137634ff4186 4241 void ssl_set_arc4_support( ssl_context *ssl, char arc4 )
ansond 0:137634ff4186 4242 {
ansond 0:137634ff4186 4243 ssl->arc4_disabled = arc4;
ansond 0:137634ff4186 4244 }
ansond 0:137634ff4186 4245
ansond 0:137634ff4186 4246 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
ansond 0:137634ff4186 4247 int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
ansond 0:137634ff4186 4248 {
ansond 0:137634ff4186 4249 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
ansond 0:137634ff4186 4250 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
ansond 0:137634ff4186 4251 {
ansond 0:137634ff4186 4252 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
ansond 0:137634ff4186 4253 }
ansond 0:137634ff4186 4254
ansond 0:137634ff4186 4255 ssl->mfl_code = mfl_code;
ansond 0:137634ff4186 4256
ansond 0:137634ff4186 4257 return( 0 );
ansond 0:137634ff4186 4258 }
ansond 0:137634ff4186 4259 #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
ansond 0:137634ff4186 4260
ansond 0:137634ff4186 4261 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
ansond 0:137634ff4186 4262 int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
ansond 0:137634ff4186 4263 {
ansond 0:137634ff4186 4264 ssl->trunc_hmac = truncate;
ansond 0:137634ff4186 4265
ansond 0:137634ff4186 4266 return( 0 );
ansond 0:137634ff4186 4267 }
ansond 0:137634ff4186 4268 #endif /* POLARSSL_SSL_TRUNCATED_HMAC */
ansond 0:137634ff4186 4269
ansond 0:137634ff4186 4270 #if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
ansond 0:137634ff4186 4271 void ssl_set_cbc_record_splitting( ssl_context *ssl, char split )
ansond 0:137634ff4186 4272 {
ansond 0:137634ff4186 4273 ssl->split_done = split;
ansond 0:137634ff4186 4274 }
ansond 0:137634ff4186 4275 #endif
ansond 0:137634ff4186 4276
ansond 0:137634ff4186 4277 void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
ansond 0:137634ff4186 4278 {
ansond 0:137634ff4186 4279 ssl->allow_legacy_renegotiation = allow_legacy;
ansond 0:137634ff4186 4280 }
ansond 0:137634ff4186 4281
ansond 0:137634ff4186 4282 #if defined(POLARSSL_SSL_RENEGOTIATION)
ansond 0:137634ff4186 4283 void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
ansond 0:137634ff4186 4284 {
ansond 0:137634ff4186 4285 ssl->disable_renegotiation = renegotiation;
ansond 0:137634ff4186 4286 }
ansond 0:137634ff4186 4287
ansond 0:137634ff4186 4288 void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
ansond 0:137634ff4186 4289 {
ansond 0:137634ff4186 4290 ssl->renego_max_records = max_records;
ansond 0:137634ff4186 4291 }
ansond 0:137634ff4186 4292
ansond 0:137634ff4186 4293 void ssl_set_renegotiation_period( ssl_context *ssl,
ansond 0:137634ff4186 4294 const unsigned char period[8] )
ansond 0:137634ff4186 4295 {
ansond 0:137634ff4186 4296 memcpy( ssl->renego_period, period, 8 );
ansond 0:137634ff4186 4297 }
ansond 0:137634ff4186 4298 #endif /* POLARSSL_SSL_RENEGOTIATION */
ansond 0:137634ff4186 4299
ansond 0:137634ff4186 4300 #if defined(POLARSSL_SSL_SESSION_TICKETS)
ansond 0:137634ff4186 4301 int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
ansond 0:137634ff4186 4302 {
ansond 0:137634ff4186 4303 ssl->session_tickets = use_tickets;
ansond 0:137634ff4186 4304
ansond 0:137634ff4186 4305 #if defined(POLARSSL_SSL_CLI_C)
ansond 0:137634ff4186 4306 if( ssl->endpoint == SSL_IS_CLIENT )
ansond 0:137634ff4186 4307 return( 0 );
ansond 0:137634ff4186 4308 #endif
ansond 0:137634ff4186 4309
ansond 0:137634ff4186 4310 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
ansond 0:137634ff4186 4311 return( 0 );
ansond 0:137634ff4186 4312
ansond 0:137634ff4186 4313 if( ssl->f_rng == NULL )
ansond 0:137634ff4186 4314 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
ansond 0:137634ff4186 4315
ansond 0:137634ff4186 4316 return( ssl_ticket_keys_init( ssl ) );
ansond 0:137634ff4186 4317 }
ansond 0:137634ff4186 4318
ansond 0:137634ff4186 4319 void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
ansond 0:137634ff4186 4320 {
ansond 0:137634ff4186 4321 ssl->ticket_lifetime = lifetime;
ansond 0:137634ff4186 4322 }
ansond 0:137634ff4186 4323 #endif /* POLARSSL_SSL_SESSION_TICKETS */
ansond 0:137634ff4186 4324
ansond 0:137634ff4186 4325 /*
ansond 0:137634ff4186 4326 * SSL get accessors
ansond 0:137634ff4186 4327 */
ansond 0:137634ff4186 4328 size_t ssl_get_bytes_avail( const ssl_context *ssl )
ansond 0:137634ff4186 4329 {
ansond 0:137634ff4186 4330 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
ansond 0:137634ff4186 4331 }
ansond 0:137634ff4186 4332
ansond 0:137634ff4186 4333 int ssl_get_verify_result( const ssl_context *ssl )
ansond 0:137634ff4186 4334 {
ansond 0:137634ff4186 4335 if( ssl->session != NULL )
ansond 0:137634ff4186 4336 return( ssl->session->verify_result );
ansond 0:137634ff4186 4337
ansond 0:137634ff4186 4338 if( ssl->session_negotiate != NULL )
ansond 0:137634ff4186 4339 return( ssl->session_negotiate->verify_result );
ansond 0:137634ff4186 4340
ansond 0:137634ff4186 4341 return( -1 );
ansond 0:137634ff4186 4342 }
ansond 0:137634ff4186 4343
ansond 0:137634ff4186 4344 const char *ssl_get_ciphersuite( const ssl_context *ssl )
ansond 0:137634ff4186 4345 {
ansond 0:137634ff4186 4346 if( ssl == NULL || ssl->session == NULL )
ansond 0:137634ff4186 4347 return( NULL );
ansond 0:137634ff4186 4348
ansond 0:137634ff4186 4349 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
ansond 0:137634ff4186 4350 }
ansond 0:137634ff4186 4351
ansond 0:137634ff4186 4352 const char *ssl_get_version( const ssl_context *ssl )
ansond 0:137634ff4186 4353 {
ansond 0:137634ff4186 4354 switch( ssl->minor_ver )
ansond 0:137634ff4186 4355 {
ansond 0:137634ff4186 4356 case SSL_MINOR_VERSION_0:
ansond 0:137634ff4186 4357 return( "SSLv3.0" );
ansond 0:137634ff4186 4358
ansond 0:137634ff4186 4359 case SSL_MINOR_VERSION_1:
ansond 0:137634ff4186 4360 return( "TLSv1.0" );
ansond 0:137634ff4186 4361
ansond 0:137634ff4186 4362 case SSL_MINOR_VERSION_2:
ansond 0:137634ff4186 4363 return( "TLSv1.1" );
ansond 0:137634ff4186 4364
ansond 0:137634ff4186 4365 case SSL_MINOR_VERSION_3:
ansond 0:137634ff4186 4366 return( "TLSv1.2" );
ansond 0:137634ff4186 4367
ansond 0:137634ff4186 4368 default:
ansond 0:137634ff4186 4369 break;
ansond 0:137634ff4186 4370 }
ansond 0:137634ff4186 4371 return( "unknown" );
ansond 0:137634ff4186 4372 }
ansond 0:137634ff4186 4373
ansond 0:137634ff4186 4374 #if defined(POLARSSL_X509_CRT_PARSE_C)
ansond 0:137634ff4186 4375 const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
ansond 0:137634ff4186 4376 {
ansond 0:137634ff4186 4377 if( ssl == NULL || ssl->session == NULL )
ansond 0:137634ff4186 4378 return( NULL );
ansond 0:137634ff4186 4379
ansond 0:137634ff4186 4380 return( ssl->session->peer_cert );
ansond 0:137634ff4186 4381 }
ansond 0:137634ff4186 4382 #endif /* POLARSSL_X509_CRT_PARSE_C */
ansond 0:137634ff4186 4383
ansond 0:137634ff4186 4384 #if defined(POLARSSL_SSL_CLI_C)
ansond 0:137634ff4186 4385 int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
ansond 0:137634ff4186 4386 {
ansond 0:137634ff4186 4387 if( ssl == NULL ||
ansond 0:137634ff4186 4388 dst == NULL ||
ansond 0:137634ff4186 4389 ssl->session == NULL ||
ansond 0:137634ff4186 4390 ssl->endpoint != SSL_IS_CLIENT )
ansond 0:137634ff4186 4391 {
ansond 0:137634ff4186 4392 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
ansond 0:137634ff4186 4393 }
ansond 0:137634ff4186 4394
ansond 0:137634ff4186 4395 return( ssl_session_copy( dst, ssl->session ) );
ansond 0:137634ff4186 4396 }
ansond 0:137634ff4186 4397 #endif /* POLARSSL_SSL_CLI_C */
ansond 0:137634ff4186 4398
ansond 0:137634ff4186 4399 /*
ansond 0:137634ff4186 4400 * Perform a single step of the SSL handshake
ansond 0:137634ff4186 4401 */
ansond 0:137634ff4186 4402 int ssl_handshake_step( ssl_context *ssl )
ansond 0:137634ff4186 4403 {
ansond 0:137634ff4186 4404 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
ansond 0:137634ff4186 4405
ansond 0:137634ff4186 4406 #if defined(POLARSSL_SSL_CLI_C)
ansond 0:137634ff4186 4407 if( ssl->endpoint == SSL_IS_CLIENT )
ansond 0:137634ff4186 4408 ret = ssl_handshake_client_step( ssl );
ansond 0:137634ff4186 4409 #endif
ansond 0:137634ff4186 4410 #if defined(POLARSSL_SSL_SRV_C)
ansond 0:137634ff4186 4411 if( ssl->endpoint == SSL_IS_SERVER )
ansond 0:137634ff4186 4412 ret = ssl_handshake_server_step( ssl );
ansond 0:137634ff4186 4413 #endif
ansond 0:137634ff4186 4414
ansond 0:137634ff4186 4415 return( ret );
ansond 0:137634ff4186 4416 }
ansond 0:137634ff4186 4417
ansond 0:137634ff4186 4418 /*
ansond 0:137634ff4186 4419 * Perform the SSL handshake
ansond 0:137634ff4186 4420 */
ansond 0:137634ff4186 4421 int ssl_handshake( ssl_context *ssl )
ansond 0:137634ff4186 4422 {
ansond 0:137634ff4186 4423 int ret = 0;
ansond 0:137634ff4186 4424
ansond 0:137634ff4186 4425 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
ansond 0:137634ff4186 4426
ansond 0:137634ff4186 4427 while( ssl->state != SSL_HANDSHAKE_OVER )
ansond 0:137634ff4186 4428 {
ansond 0:137634ff4186 4429 ret = ssl_handshake_step( ssl );
ansond 0:137634ff4186 4430
ansond 0:137634ff4186 4431 if( ret != 0 )
ansond 0:137634ff4186 4432 break;
ansond 0:137634ff4186 4433 }
ansond 0:137634ff4186 4434
ansond 0:137634ff4186 4435 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
ansond 0:137634ff4186 4436
ansond 0:137634ff4186 4437 return( ret );
ansond 0:137634ff4186 4438 }
ansond 0:137634ff4186 4439
ansond 0:137634ff4186 4440 #if defined(POLARSSL_SSL_RENEGOTIATION)
ansond 0:137634ff4186 4441 #if defined(POLARSSL_SSL_SRV_C)
ansond 0:137634ff4186 4442 /*
ansond 0:137634ff4186 4443 * Write HelloRequest to request renegotiation on server
ansond 0:137634ff4186 4444 */
ansond 0:137634ff4186 4445 static int ssl_write_hello_request( ssl_context *ssl )
ansond 0:137634ff4186 4446 {
ansond 0:137634ff4186 4447 int ret;
ansond 0:137634ff4186 4448
ansond 0:137634ff4186 4449 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
ansond 0:137634ff4186 4450
ansond 0:137634ff4186 4451 ssl->out_msglen = 4;
ansond 0:137634ff4186 4452 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
ansond 0:137634ff4186 4453 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
ansond 0:137634ff4186 4454
ansond 0:137634ff4186 4455 if( ( ret = ssl_write_record( ssl ) ) != 0 )
ansond 0:137634ff4186 4456 {
ansond 0:137634ff4186 4457 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
ansond 0:137634ff4186 4458 return( ret );
ansond 0:137634ff4186 4459 }
ansond 0:137634ff4186 4460
ansond 0:137634ff4186 4461 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
ansond 0:137634ff4186 4462
ansond 0:137634ff4186 4463 return( 0 );
ansond 0:137634ff4186 4464 }
ansond 0:137634ff4186 4465 #endif /* POLARSSL_SSL_SRV_C */
ansond 0:137634ff4186 4466
ansond 0:137634ff4186 4467 /*
ansond 0:137634ff4186 4468 * Actually renegotiate current connection, triggered by either:
ansond 0:137634ff4186 4469 * - any side: calling ssl_renegotiate(),
ansond 0:137634ff4186 4470 * - client: receiving a HelloRequest during ssl_read(),
ansond 0:137634ff4186 4471 * - server: receiving any handshake message on server during ssl_read() after
ansond 0:137634ff4186 4472 * the initial handshake is completed.
ansond 0:137634ff4186 4473 * If the handshake doesn't complete due to waiting for I/O, it will continue
ansond 0:137634ff4186 4474 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
ansond 0:137634ff4186 4475 */
ansond 0:137634ff4186 4476 static int ssl_start_renegotiation( ssl_context *ssl )
ansond 0:137634ff4186 4477 {
ansond 0:137634ff4186 4478 int ret;
ansond 0:137634ff4186 4479
ansond 0:137634ff4186 4480 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
ansond 0:137634ff4186 4481
ansond 0:137634ff4186 4482 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
ansond 0:137634ff4186 4483 return( ret );
ansond 0:137634ff4186 4484
ansond 0:137634ff4186 4485 ssl->state = SSL_HELLO_REQUEST;
ansond 0:137634ff4186 4486 ssl->renegotiation = SSL_RENEGOTIATION;
ansond 0:137634ff4186 4487
ansond 0:137634ff4186 4488 if( ( ret = ssl_handshake( ssl ) ) != 0 )
ansond 0:137634ff4186 4489 {
ansond 0:137634ff4186 4490 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
ansond 0:137634ff4186 4491 return( ret );
ansond 0:137634ff4186 4492 }
ansond 0:137634ff4186 4493
ansond 0:137634ff4186 4494 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
ansond 0:137634ff4186 4495
ansond 0:137634ff4186 4496 return( 0 );
ansond 0:137634ff4186 4497 }
ansond 0:137634ff4186 4498
ansond 0:137634ff4186 4499 /*
ansond 0:137634ff4186 4500 * Renegotiate current connection on client,
ansond 0:137634ff4186 4501 * or request renegotiation on server
ansond 0:137634ff4186 4502 */
ansond 0:137634ff4186 4503 int ssl_renegotiate( ssl_context *ssl )
ansond 0:137634ff4186 4504 {
ansond 0:137634ff4186 4505 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
ansond 0:137634ff4186 4506
ansond 0:137634ff4186 4507 #if defined(POLARSSL_SSL_SRV_C)
ansond 0:137634ff4186 4508 /* On server, just send the request */
ansond 0:137634ff4186 4509 if( ssl->endpoint == SSL_IS_SERVER )
ansond 0:137634ff4186 4510 {
ansond 0:137634ff4186 4511 if( ssl->state != SSL_HANDSHAKE_OVER )
ansond 0:137634ff4186 4512 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
ansond 0:137634ff4186 4513
ansond 0:137634ff4186 4514 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
ansond 0:137634ff4186 4515
ansond 0:137634ff4186 4516 /* Did we already try/start sending HelloRequest? */
ansond 0:137634ff4186 4517 if( ssl->out_left != 0 )
ansond 0:137634ff4186 4518 return( ssl_flush_output( ssl ) );
ansond 0:137634ff4186 4519
ansond 0:137634ff4186 4520 return( ssl_write_hello_request( ssl ) );
ansond 0:137634ff4186 4521 }
ansond 0:137634ff4186 4522 #endif /* POLARSSL_SSL_SRV_C */
ansond 0:137634ff4186 4523
ansond 0:137634ff4186 4524 #if defined(POLARSSL_SSL_CLI_C)
ansond 0:137634ff4186 4525 /*
ansond 0:137634ff4186 4526 * On client, either start the renegotiation process or,
ansond 0:137634ff4186 4527 * if already in progress, continue the handshake
ansond 0:137634ff4186 4528 */
ansond 0:137634ff4186 4529 if( ssl->renegotiation != SSL_RENEGOTIATION )
ansond 0:137634ff4186 4530 {
ansond 0:137634ff4186 4531 if( ssl->state != SSL_HANDSHAKE_OVER )
ansond 0:137634ff4186 4532 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
ansond 0:137634ff4186 4533
ansond 0:137634ff4186 4534 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
ansond 0:137634ff4186 4535 {
ansond 0:137634ff4186 4536 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
ansond 0:137634ff4186 4537 return( ret );
ansond 0:137634ff4186 4538 }
ansond 0:137634ff4186 4539 }
ansond 0:137634ff4186 4540 else
ansond 0:137634ff4186 4541 {
ansond 0:137634ff4186 4542 if( ( ret = ssl_handshake( ssl ) ) != 0 )
ansond 0:137634ff4186 4543 {
ansond 0:137634ff4186 4544 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
ansond 0:137634ff4186 4545 return( ret );
ansond 0:137634ff4186 4546 }
ansond 0:137634ff4186 4547 }
ansond 0:137634ff4186 4548 #endif /* POLARSSL_SSL_CLI_C */
ansond 0:137634ff4186 4549
ansond 0:137634ff4186 4550 return( ret );
ansond 0:137634ff4186 4551 }
ansond 0:137634ff4186 4552
ansond 0:137634ff4186 4553 /*
ansond 0:137634ff4186 4554 * Check record counters and renegotiate if they're above the limit.
ansond 0:137634ff4186 4555 */
ansond 0:137634ff4186 4556 static int ssl_check_ctr_renegotiate( ssl_context *ssl )
ansond 0:137634ff4186 4557 {
ansond 0:137634ff4186 4558 if( ssl->state != SSL_HANDSHAKE_OVER ||
ansond 0:137634ff4186 4559 ssl->renegotiation == SSL_RENEGOTIATION_PENDING ||
ansond 0:137634ff4186 4560 ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED )
ansond 0:137634ff4186 4561 {
ansond 0:137634ff4186 4562 return( 0 );
ansond 0:137634ff4186 4563 }
ansond 0:137634ff4186 4564
ansond 0:137634ff4186 4565 // TODO: adapt for DTLS
ansond 0:137634ff4186 4566 if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 &&
ansond 0:137634ff4186 4567 memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 )
ansond 0:137634ff4186 4568 {
ansond 0:137634ff4186 4569 return( 0 );
ansond 0:137634ff4186 4570 }
ansond 0:137634ff4186 4571
ansond 0:137634ff4186 4572 SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) );
ansond 0:137634ff4186 4573 return( ssl_renegotiate( ssl ) );
ansond 0:137634ff4186 4574 }
ansond 0:137634ff4186 4575 #endif /* POLARSSL_SSL_RENEGOTIATION */
ansond 0:137634ff4186 4576
ansond 0:137634ff4186 4577 /*
ansond 0:137634ff4186 4578 * Receive application data decrypted from the SSL layer
ansond 0:137634ff4186 4579 */
ansond 0:137634ff4186 4580 int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
ansond 0:137634ff4186 4581 {
ansond 0:137634ff4186 4582 int ret, record_read = 0;
ansond 0:137634ff4186 4583 size_t n;
ansond 0:137634ff4186 4584
ansond 0:137634ff4186 4585 SSL_DEBUG_MSG( 2, ( "=> read" ) );
ansond 0:137634ff4186 4586
ansond 0:137634ff4186 4587 #if defined(POLARSSL_SSL_RENEGOTIATION)
ansond 0:137634ff4186 4588 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
ansond 0:137634ff4186 4589 {
ansond 0:137634ff4186 4590 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
ansond 0:137634ff4186 4591 return( ret );
ansond 0:137634ff4186 4592 }
ansond 0:137634ff4186 4593 #endif
ansond 0:137634ff4186 4594
ansond 0:137634ff4186 4595 if( ssl->state != SSL_HANDSHAKE_OVER )
ansond 0:137634ff4186 4596 {
ansond 0:137634ff4186 4597 ret = ssl_handshake( ssl );
ansond 0:137634ff4186 4598 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
ansond 0:137634ff4186 4599 {
ansond 0:137634ff4186 4600 record_read = 1;
ansond 0:137634ff4186 4601 }
ansond 0:137634ff4186 4602 else if( ret != 0 )
ansond 0:137634ff4186 4603 {
ansond 0:137634ff4186 4604 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
ansond 0:137634ff4186 4605 return( ret );
ansond 0:137634ff4186 4606 }
ansond 0:137634ff4186 4607 }
ansond 0:137634ff4186 4608
ansond 0:137634ff4186 4609 if( ssl->in_offt == NULL )
ansond 0:137634ff4186 4610 {
ansond 0:137634ff4186 4611 if( ! record_read )
ansond 0:137634ff4186 4612 {
ansond 0:137634ff4186 4613 if( ( ret = ssl_read_record( ssl ) ) != 0 )
ansond 0:137634ff4186 4614 {
ansond 0:137634ff4186 4615 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
ansond 0:137634ff4186 4616 return( 0 );
ansond 0:137634ff4186 4617
ansond 0:137634ff4186 4618 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
ansond 0:137634ff4186 4619 return( ret );
ansond 0:137634ff4186 4620 }
ansond 0:137634ff4186 4621 }
ansond 0:137634ff4186 4622
ansond 0:137634ff4186 4623 if( ssl->in_msglen == 0 &&
ansond 0:137634ff4186 4624 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
ansond 0:137634ff4186 4625 {
ansond 0:137634ff4186 4626 /*
ansond 0:137634ff4186 4627 * OpenSSL sends empty messages to randomize the IV
ansond 0:137634ff4186 4628 */
ansond 0:137634ff4186 4629 if( ( ret = ssl_read_record( ssl ) ) != 0 )
ansond 0:137634ff4186 4630 {
ansond 0:137634ff4186 4631 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
ansond 0:137634ff4186 4632 return( 0 );
ansond 0:137634ff4186 4633
ansond 0:137634ff4186 4634 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
ansond 0:137634ff4186 4635 return( ret );
ansond 0:137634ff4186 4636 }
ansond 0:137634ff4186 4637 }
ansond 0:137634ff4186 4638
ansond 0:137634ff4186 4639 #if defined(POLARSSL_SSL_RENEGOTIATION)
ansond 0:137634ff4186 4640 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
ansond 0:137634ff4186 4641 {
ansond 0:137634ff4186 4642 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
ansond 0:137634ff4186 4643
ansond 0:137634ff4186 4644 #if defined(POLARSSL_SSL_CLI_C)
ansond 0:137634ff4186 4645 if( ssl->endpoint == SSL_IS_CLIENT &&
ansond 0:137634ff4186 4646 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
ansond 0:137634ff4186 4647 ssl->in_hslen != 4 ) )
ansond 0:137634ff4186 4648 {
ansond 0:137634ff4186 4649 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
ansond 0:137634ff4186 4650 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
ansond 0:137634ff4186 4651 }
ansond 0:137634ff4186 4652 #endif
ansond 0:137634ff4186 4653
ansond 0:137634ff4186 4654 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
ansond 0:137634ff4186 4655 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
ansond 0:137634ff4186 4656 ssl->allow_legacy_renegotiation ==
ansond 0:137634ff4186 4657 SSL_LEGACY_NO_RENEGOTIATION ) )
ansond 0:137634ff4186 4658 {
ansond 0:137634ff4186 4659 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
ansond 0:137634ff4186 4660
ansond 0:137634ff4186 4661 #if defined(POLARSSL_SSL_PROTO_SSL3)
ansond 0:137634ff4186 4662 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
ansond 0:137634ff4186 4663 {
ansond 0:137634ff4186 4664 /*
ansond 0:137634ff4186 4665 * SSLv3 does not have a "no_renegotiation" alert
ansond 0:137634ff4186 4666 */
ansond 0:137634ff4186 4667 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
ansond 0:137634ff4186 4668 return( ret );
ansond 0:137634ff4186 4669 }
ansond 0:137634ff4186 4670 else
ansond 0:137634ff4186 4671 #endif /* POLARSSL_SSL_PROTO_SSL3 */
ansond 0:137634ff4186 4672 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
ansond 0:137634ff4186 4673 defined(POLARSSL_SSL_PROTO_TLS1_2)
ansond 0:137634ff4186 4674 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
ansond 0:137634ff4186 4675 {
ansond 0:137634ff4186 4676 if( ( ret = ssl_send_alert_message( ssl,
ansond 0:137634ff4186 4677 SSL_ALERT_LEVEL_WARNING,
ansond 0:137634ff4186 4678 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
ansond 0:137634ff4186 4679 {
ansond 0:137634ff4186 4680 return( ret );
ansond 0:137634ff4186 4681 }
ansond 0:137634ff4186 4682 }
ansond 0:137634ff4186 4683 else
ansond 0:137634ff4186 4684 #endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
ansond 0:137634ff4186 4685 POLARSSL_SSL_PROTO_TLS1_2 */
ansond 0:137634ff4186 4686 {
ansond 0:137634ff4186 4687 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
ansond 0:137634ff4186 4688 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
ansond 0:137634ff4186 4689 }
ansond 0:137634ff4186 4690 }
ansond 0:137634ff4186 4691 else
ansond 0:137634ff4186 4692 {
ansond 0:137634ff4186 4693 ret = ssl_start_renegotiation( ssl );
ansond 0:137634ff4186 4694 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
ansond 0:137634ff4186 4695 {
ansond 0:137634ff4186 4696 record_read = 1;
ansond 0:137634ff4186 4697 }
ansond 0:137634ff4186 4698 else if( ret != 0 )
ansond 0:137634ff4186 4699 {
ansond 0:137634ff4186 4700 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
ansond 0:137634ff4186 4701 return( ret );
ansond 0:137634ff4186 4702 }
ansond 0:137634ff4186 4703 }
ansond 0:137634ff4186 4704
ansond 0:137634ff4186 4705 /* If a non-handshake record was read during renego, fallthrough,
ansond 0:137634ff4186 4706 * else tell the user they should call ssl_read() again */
ansond 0:137634ff4186 4707 if( ! record_read )
ansond 0:137634ff4186 4708 return( POLARSSL_ERR_NET_WANT_READ );
ansond 0:137634ff4186 4709 }
ansond 0:137634ff4186 4710 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
ansond 0:137634ff4186 4711 {
ansond 0:137634ff4186 4712 ssl->renego_records_seen++;
ansond 0:137634ff4186 4713
ansond 0:137634ff4186 4714 if( ssl->renego_max_records >= 0 &&
ansond 0:137634ff4186 4715 ssl->renego_records_seen > ssl->renego_max_records )
ansond 0:137634ff4186 4716 {
ansond 0:137634ff4186 4717 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
ansond 0:137634ff4186 4718 "but not honored by client" ) );
ansond 0:137634ff4186 4719 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
ansond 0:137634ff4186 4720 }
ansond 0:137634ff4186 4721 }
ansond 0:137634ff4186 4722 #endif /* POLARSSL_SSL_RENEGOTIATION */
ansond 0:137634ff4186 4723
ansond 0:137634ff4186 4724 /* Fatal and closure alerts handled by ssl_read_record() */
ansond 0:137634ff4186 4725 if( ssl->in_msgtype == SSL_MSG_ALERT )
ansond 0:137634ff4186 4726 {
ansond 0:137634ff4186 4727 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
ansond 0:137634ff4186 4728 return( POLARSSL_ERR_NET_WANT_READ );
ansond 0:137634ff4186 4729 }
ansond 0:137634ff4186 4730
ansond 0:137634ff4186 4731 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
ansond 0:137634ff4186 4732 {
ansond 0:137634ff4186 4733 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
ansond 0:137634ff4186 4734 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
ansond 0:137634ff4186 4735 }
ansond 0:137634ff4186 4736
ansond 0:137634ff4186 4737 ssl->in_offt = ssl->in_msg;
ansond 0:137634ff4186 4738 }
ansond 0:137634ff4186 4739
ansond 0:137634ff4186 4740 n = ( len < ssl->in_msglen )
ansond 0:137634ff4186 4741 ? len : ssl->in_msglen;
ansond 0:137634ff4186 4742
ansond 0:137634ff4186 4743 memcpy( buf, ssl->in_offt, n );
ansond 0:137634ff4186 4744 ssl->in_msglen -= n;
ansond 0:137634ff4186 4745
ansond 0:137634ff4186 4746 if( ssl->in_msglen == 0 )
ansond 0:137634ff4186 4747 /* all bytes consumed */
ansond 0:137634ff4186 4748 ssl->in_offt = NULL;
ansond 0:137634ff4186 4749 else
ansond 0:137634ff4186 4750 /* more data available */
ansond 0:137634ff4186 4751 ssl->in_offt += n;
ansond 0:137634ff4186 4752
ansond 0:137634ff4186 4753 SSL_DEBUG_MSG( 2, ( "<= read" ) );
ansond 0:137634ff4186 4754
ansond 0:137634ff4186 4755 return( (int) n );
ansond 0:137634ff4186 4756 }
ansond 0:137634ff4186 4757
ansond 0:137634ff4186 4758 /*
ansond 0:137634ff4186 4759 * Send application data to be encrypted by the SSL layer,
ansond 0:137634ff4186 4760 * taking care of max fragment length and buffer size
ansond 0:137634ff4186 4761 */
ansond 0:137634ff4186 4762 static int ssl_write_real( ssl_context *ssl,
ansond 0:137634ff4186 4763 const unsigned char *buf, size_t len )
ansond 0:137634ff4186 4764 {
ansond 0:137634ff4186 4765 int ret;
ansond 0:137634ff4186 4766 size_t n;
ansond 0:137634ff4186 4767 unsigned int max_len = SSL_MAX_CONTENT_LEN;
ansond 0:137634ff4186 4768
ansond 0:137634ff4186 4769 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
ansond 0:137634ff4186 4770 /*
ansond 0:137634ff4186 4771 * Assume mfl_code is correct since it was checked when set
ansond 0:137634ff4186 4772 */
ansond 0:137634ff4186 4773 max_len = mfl_code_to_length[ssl->mfl_code];
ansond 0:137634ff4186 4774
ansond 0:137634ff4186 4775 /*
ansond 0:137634ff4186 4776 * Check if a smaller max length was negotiated
ansond 0:137634ff4186 4777 */
ansond 0:137634ff4186 4778 if( ssl->session_out != NULL &&
ansond 0:137634ff4186 4779 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
ansond 0:137634ff4186 4780 {
ansond 0:137634ff4186 4781 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
ansond 0:137634ff4186 4782 }
ansond 0:137634ff4186 4783 #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
ansond 0:137634ff4186 4784
ansond 0:137634ff4186 4785 n = ( len < max_len) ? len : max_len;
ansond 0:137634ff4186 4786
ansond 0:137634ff4186 4787 if( ssl->out_left != 0 )
ansond 0:137634ff4186 4788 {
ansond 0:137634ff4186 4789 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
ansond 0:137634ff4186 4790 {
ansond 0:137634ff4186 4791 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
ansond 0:137634ff4186 4792 return( ret );
ansond 0:137634ff4186 4793 }
ansond 0:137634ff4186 4794 }
ansond 0:137634ff4186 4795 else
ansond 0:137634ff4186 4796 {
ansond 0:137634ff4186 4797 ssl->out_msglen = n;
ansond 0:137634ff4186 4798 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
ansond 0:137634ff4186 4799 memcpy( ssl->out_msg, buf, n );
ansond 0:137634ff4186 4800
ansond 0:137634ff4186 4801 if( ( ret = ssl_write_record( ssl ) ) != 0 )
ansond 0:137634ff4186 4802 {
ansond 0:137634ff4186 4803 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
ansond 0:137634ff4186 4804 return( ret );
ansond 0:137634ff4186 4805 }
ansond 0:137634ff4186 4806 }
ansond 0:137634ff4186 4807
ansond 0:137634ff4186 4808 return( (int) n );
ansond 0:137634ff4186 4809 }
ansond 0:137634ff4186 4810
ansond 0:137634ff4186 4811 /*
ansond 0:137634ff4186 4812 * Write application data, doing 1/n-1 splitting if necessary.
ansond 0:137634ff4186 4813 *
ansond 0:137634ff4186 4814 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
ansond 0:137634ff4186 4815 * then the caller will call us again with the same arguments, so
ansond 0:137634ff4186 4816 * remember wether we already did the split or not.
ansond 0:137634ff4186 4817 */
ansond 0:137634ff4186 4818 #if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
ansond 0:137634ff4186 4819 static int ssl_write_split( ssl_context *ssl,
ansond 0:137634ff4186 4820 const unsigned char *buf, size_t len )
ansond 0:137634ff4186 4821 {
ansond 0:137634ff4186 4822 int ret;
ansond 0:137634ff4186 4823
ansond 0:137634ff4186 4824 if( ssl->split_done == SSL_CBC_RECORD_SPLITTING_DISABLED ||
ansond 0:137634ff4186 4825 len <= 1 ||
ansond 0:137634ff4186 4826 ssl->minor_ver > SSL_MINOR_VERSION_1 ||
ansond 0:137634ff4186 4827 cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
ansond 0:137634ff4186 4828 != POLARSSL_MODE_CBC )
ansond 0:137634ff4186 4829 {
ansond 0:137634ff4186 4830 return( ssl_write_real( ssl, buf, len ) );
ansond 0:137634ff4186 4831 }
ansond 0:137634ff4186 4832
ansond 0:137634ff4186 4833 if( ssl->split_done == 0 )
ansond 0:137634ff4186 4834 {
ansond 0:137634ff4186 4835 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
ansond 0:137634ff4186 4836 return( ret );
ansond 0:137634ff4186 4837 ssl->split_done = 1;
ansond 0:137634ff4186 4838 }
ansond 0:137634ff4186 4839
ansond 0:137634ff4186 4840 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
ansond 0:137634ff4186 4841 return( ret );
ansond 0:137634ff4186 4842 ssl->split_done = 0;
ansond 0:137634ff4186 4843
ansond 0:137634ff4186 4844 return( ret + 1 );
ansond 0:137634ff4186 4845 }
ansond 0:137634ff4186 4846 #endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */
ansond 0:137634ff4186 4847
ansond 0:137634ff4186 4848 /*
ansond 0:137634ff4186 4849 * Write application data (public-facing wrapper)
ansond 0:137634ff4186 4850 */
ansond 0:137634ff4186 4851 int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
ansond 0:137634ff4186 4852 {
ansond 0:137634ff4186 4853 int ret;
ansond 0:137634ff4186 4854
ansond 0:137634ff4186 4855 SSL_DEBUG_MSG( 2, ( "=> write" ) );
ansond 0:137634ff4186 4856
ansond 0:137634ff4186 4857 #if defined(POLARSSL_SSL_RENEGOTIATION)
ansond 0:137634ff4186 4858 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
ansond 0:137634ff4186 4859 {
ansond 0:137634ff4186 4860 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
ansond 0:137634ff4186 4861 return( ret );
ansond 0:137634ff4186 4862 }
ansond 0:137634ff4186 4863 #endif
ansond 0:137634ff4186 4864
ansond 0:137634ff4186 4865 if( ssl->state != SSL_HANDSHAKE_OVER )
ansond 0:137634ff4186 4866 {
ansond 0:137634ff4186 4867 if( ( ret = ssl_handshake( ssl ) ) != 0 )
ansond 0:137634ff4186 4868 {
ansond 0:137634ff4186 4869 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
ansond 0:137634ff4186 4870 return( ret );
ansond 0:137634ff4186 4871 }
ansond 0:137634ff4186 4872 }
ansond 0:137634ff4186 4873
ansond 0:137634ff4186 4874 #if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
ansond 0:137634ff4186 4875 ret = ssl_write_split( ssl, buf, len );
ansond 0:137634ff4186 4876 #else
ansond 0:137634ff4186 4877 ret = ssl_write_real( ssl, buf, len );
ansond 0:137634ff4186 4878 #endif
ansond 0:137634ff4186 4879
ansond 0:137634ff4186 4880 SSL_DEBUG_MSG( 2, ( "<= write" ) );
ansond 0:137634ff4186 4881
ansond 0:137634ff4186 4882 return( ret );
ansond 0:137634ff4186 4883 }
ansond 0:137634ff4186 4884
ansond 0:137634ff4186 4885 /*
ansond 0:137634ff4186 4886 * Notify the peer that the connection is being closed
ansond 0:137634ff4186 4887 */
ansond 0:137634ff4186 4888 int ssl_close_notify( ssl_context *ssl )
ansond 0:137634ff4186 4889 {
ansond 0:137634ff4186 4890 int ret;
ansond 0:137634ff4186 4891
ansond 0:137634ff4186 4892 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
ansond 0:137634ff4186 4893
ansond 0:137634ff4186 4894 if( ssl->out_left != 0 )
ansond 0:137634ff4186 4895 return( ssl_flush_output( ssl ) );
ansond 0:137634ff4186 4896
ansond 0:137634ff4186 4897 if( ssl->state == SSL_HANDSHAKE_OVER )
ansond 0:137634ff4186 4898 {
ansond 0:137634ff4186 4899 if( ( ret = ssl_send_alert_message( ssl,
ansond 0:137634ff4186 4900 SSL_ALERT_LEVEL_WARNING,
ansond 0:137634ff4186 4901 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
ansond 0:137634ff4186 4902 {
ansond 0:137634ff4186 4903 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
ansond 0:137634ff4186 4904 return( ret );
ansond 0:137634ff4186 4905 }
ansond 0:137634ff4186 4906 }
ansond 0:137634ff4186 4907
ansond 0:137634ff4186 4908 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
ansond 0:137634ff4186 4909
ansond 0:137634ff4186 4910 return( 0 );
ansond 0:137634ff4186 4911 }
ansond 0:137634ff4186 4912
ansond 0:137634ff4186 4913 void ssl_transform_free( ssl_transform *transform )
ansond 0:137634ff4186 4914 {
ansond 0:137634ff4186 4915 if( transform == NULL )
ansond 0:137634ff4186 4916 return;
ansond 0:137634ff4186 4917
ansond 0:137634ff4186 4918 #if defined(POLARSSL_ZLIB_SUPPORT)
ansond 0:137634ff4186 4919 deflateEnd( &transform->ctx_deflate );
ansond 0:137634ff4186 4920 inflateEnd( &transform->ctx_inflate );
ansond 0:137634ff4186 4921 #endif
ansond 0:137634ff4186 4922
ansond 0:137634ff4186 4923 cipher_free( &transform->cipher_ctx_enc );
ansond 0:137634ff4186 4924 cipher_free( &transform->cipher_ctx_dec );
ansond 0:137634ff4186 4925
ansond 0:137634ff4186 4926 md_free( &transform->md_ctx_enc );
ansond 0:137634ff4186 4927 md_free( &transform->md_ctx_dec );
ansond 0:137634ff4186 4928
ansond 0:137634ff4186 4929 polarssl_zeroize( transform, sizeof( ssl_transform ) );
ansond 0:137634ff4186 4930 }
ansond 0:137634ff4186 4931
ansond 0:137634ff4186 4932 #if defined(POLARSSL_X509_CRT_PARSE_C)
ansond 0:137634ff4186 4933 static void ssl_key_cert_free( ssl_key_cert *key_cert )
ansond 0:137634ff4186 4934 {
ansond 0:137634ff4186 4935 ssl_key_cert *cur = key_cert, *next;
ansond 0:137634ff4186 4936
ansond 0:137634ff4186 4937 while( cur != NULL )
ansond 0:137634ff4186 4938 {
ansond 0:137634ff4186 4939 next = cur->next;
ansond 0:137634ff4186 4940
ansond 0:137634ff4186 4941 if( cur->key_own_alloc )
ansond 0:137634ff4186 4942 {
ansond 0:137634ff4186 4943 pk_free( cur->key );
ansond 0:137634ff4186 4944 polarssl_free( cur->key );
ansond 0:137634ff4186 4945 }
ansond 0:137634ff4186 4946 polarssl_free( cur );
ansond 0:137634ff4186 4947
ansond 0:137634ff4186 4948 cur = next;
ansond 0:137634ff4186 4949 }
ansond 0:137634ff4186 4950 }
ansond 0:137634ff4186 4951 #endif /* POLARSSL_X509_CRT_PARSE_C */
ansond 0:137634ff4186 4952
ansond 0:137634ff4186 4953 void ssl_handshake_free( ssl_handshake_params *handshake )
ansond 0:137634ff4186 4954 {
ansond 0:137634ff4186 4955 if( handshake == NULL )
ansond 0:137634ff4186 4956 return;
ansond 0:137634ff4186 4957
ansond 0:137634ff4186 4958 #if defined(POLARSSL_DHM_C)
ansond 0:137634ff4186 4959 dhm_free( &handshake->dhm_ctx );
ansond 0:137634ff4186 4960 #endif
ansond 0:137634ff4186 4961 #if defined(POLARSSL_ECDH_C)
ansond 0:137634ff4186 4962 ecdh_free( &handshake->ecdh_ctx );
ansond 0:137634ff4186 4963 #endif
ansond 0:137634ff4186 4964
ansond 0:137634ff4186 4965 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
ansond 0:137634ff4186 4966 /* explicit void pointer cast for buggy MS compiler */
ansond 0:137634ff4186 4967 polarssl_free( (void *) handshake->curves );
ansond 0:137634ff4186 4968 #endif
ansond 0:137634ff4186 4969
ansond 0:137634ff4186 4970 #if defined(POLARSSL_X509_CRT_PARSE_C) && \
ansond 0:137634ff4186 4971 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
ansond 0:137634ff4186 4972 /*
ansond 0:137634ff4186 4973 * Free only the linked list wrapper, not the keys themselves
ansond 0:137634ff4186 4974 * since the belong to the SNI callback
ansond 0:137634ff4186 4975 */
ansond 0:137634ff4186 4976 if( handshake->sni_key_cert != NULL )
ansond 0:137634ff4186 4977 {
ansond 0:137634ff4186 4978 ssl_key_cert *cur = handshake->sni_key_cert, *next;
ansond 0:137634ff4186 4979
ansond 0:137634ff4186 4980 while( cur != NULL )
ansond 0:137634ff4186 4981 {
ansond 0:137634ff4186 4982 next = cur->next;
ansond 0:137634ff4186 4983 polarssl_free( cur );
ansond 0:137634ff4186 4984 cur = next;
ansond 0:137634ff4186 4985 }
ansond 0:137634ff4186 4986 }
ansond 0:137634ff4186 4987 #endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
ansond 0:137634ff4186 4988
ansond 0:137634ff4186 4989 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
ansond 0:137634ff4186 4990 }
ansond 0:137634ff4186 4991
ansond 0:137634ff4186 4992 void ssl_session_free( ssl_session *session )
ansond 0:137634ff4186 4993 {
ansond 0:137634ff4186 4994 if( session == NULL )
ansond 0:137634ff4186 4995 return;
ansond 0:137634ff4186 4996
ansond 0:137634ff4186 4997 #if defined(POLARSSL_X509_CRT_PARSE_C)
ansond 0:137634ff4186 4998 if( session->peer_cert != NULL )
ansond 0:137634ff4186 4999 {
ansond 0:137634ff4186 5000 x509_crt_free( session->peer_cert );
ansond 0:137634ff4186 5001 polarssl_free( session->peer_cert );
ansond 0:137634ff4186 5002 }
ansond 0:137634ff4186 5003 #endif
ansond 0:137634ff4186 5004
ansond 0:137634ff4186 5005 #if defined(POLARSSL_SSL_SESSION_TICKETS)
ansond 0:137634ff4186 5006 polarssl_free( session->ticket );
ansond 0:137634ff4186 5007 #endif
ansond 0:137634ff4186 5008
ansond 0:137634ff4186 5009 polarssl_zeroize( session, sizeof( ssl_session ) );
ansond 0:137634ff4186 5010 }
ansond 0:137634ff4186 5011
ansond 0:137634ff4186 5012 /*
ansond 0:137634ff4186 5013 * Free an SSL context
ansond 0:137634ff4186 5014 */
ansond 0:137634ff4186 5015 void ssl_free( ssl_context *ssl )
ansond 0:137634ff4186 5016 {
ansond 0:137634ff4186 5017 if( ssl == NULL )
ansond 0:137634ff4186 5018 return;
ansond 0:137634ff4186 5019
ansond 0:137634ff4186 5020 SSL_DEBUG_MSG( 2, ( "=> free" ) );
ansond 0:137634ff4186 5021
ansond 0:137634ff4186 5022 if( ssl->out_ctr != NULL )
ansond 0:137634ff4186 5023 {
ansond 0:137634ff4186 5024 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
ansond 0:137634ff4186 5025 polarssl_free( ssl->out_ctr );
ansond 0:137634ff4186 5026 }
ansond 0:137634ff4186 5027
ansond 0:137634ff4186 5028 if( ssl->in_ctr != NULL )
ansond 0:137634ff4186 5029 {
ansond 0:137634ff4186 5030 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
ansond 0:137634ff4186 5031 polarssl_free( ssl->in_ctr );
ansond 0:137634ff4186 5032 }
ansond 0:137634ff4186 5033
ansond 0:137634ff4186 5034 #if defined(POLARSSL_ZLIB_SUPPORT)
ansond 0:137634ff4186 5035 if( ssl->compress_buf != NULL )
ansond 0:137634ff4186 5036 {
ansond 0:137634ff4186 5037 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
ansond 0:137634ff4186 5038 polarssl_free( ssl->compress_buf );
ansond 0:137634ff4186 5039 }
ansond 0:137634ff4186 5040 #endif
ansond 0:137634ff4186 5041
ansond 0:137634ff4186 5042 #if defined(POLARSSL_DHM_C)
ansond 0:137634ff4186 5043 mpi_free( &ssl->dhm_P );
ansond 0:137634ff4186 5044 mpi_free( &ssl->dhm_G );
ansond 0:137634ff4186 5045 #endif
ansond 0:137634ff4186 5046
ansond 0:137634ff4186 5047 if( ssl->transform )
ansond 0:137634ff4186 5048 {
ansond 0:137634ff4186 5049 ssl_transform_free( ssl->transform );
ansond 0:137634ff4186 5050 polarssl_free( ssl->transform );
ansond 0:137634ff4186 5051 }
ansond 0:137634ff4186 5052
ansond 0:137634ff4186 5053 if( ssl->handshake )
ansond 0:137634ff4186 5054 {
ansond 0:137634ff4186 5055 ssl_handshake_free( ssl->handshake );
ansond 0:137634ff4186 5056 ssl_transform_free( ssl->transform_negotiate );
ansond 0:137634ff4186 5057 ssl_session_free( ssl->session_negotiate );
ansond 0:137634ff4186 5058
ansond 0:137634ff4186 5059 polarssl_free( ssl->handshake );
ansond 0:137634ff4186 5060 polarssl_free( ssl->transform_negotiate );
ansond 0:137634ff4186 5061 polarssl_free( ssl->session_negotiate );
ansond 0:137634ff4186 5062 }
ansond 0:137634ff4186 5063
ansond 0:137634ff4186 5064 if( ssl->session )
ansond 0:137634ff4186 5065 {
ansond 0:137634ff4186 5066 ssl_session_free( ssl->session );
ansond 0:137634ff4186 5067 polarssl_free( ssl->session );
ansond 0:137634ff4186 5068 }
ansond 0:137634ff4186 5069
ansond 0:137634ff4186 5070 #if defined(POLARSSL_SSL_SESSION_TICKETS)
ansond 0:137634ff4186 5071 if( ssl->ticket_keys )
ansond 0:137634ff4186 5072 {
ansond 0:137634ff4186 5073 ssl_ticket_keys_free( ssl->ticket_keys );
ansond 0:137634ff4186 5074 polarssl_free( ssl->ticket_keys );
ansond 0:137634ff4186 5075 }
ansond 0:137634ff4186 5076 #endif
ansond 0:137634ff4186 5077
ansond 0:137634ff4186 5078 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
ansond 0:137634ff4186 5079 if( ssl->hostname != NULL )
ansond 0:137634ff4186 5080 {
ansond 0:137634ff4186 5081 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
ansond 0:137634ff4186 5082 polarssl_free( ssl->hostname );
ansond 0:137634ff4186 5083 ssl->hostname_len = 0;
ansond 0:137634ff4186 5084 }
ansond 0:137634ff4186 5085 #endif
ansond 0:137634ff4186 5086
ansond 0:137634ff4186 5087 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
ansond 0:137634ff4186 5088 if( ssl->psk != NULL )
ansond 0:137634ff4186 5089 {
ansond 0:137634ff4186 5090 polarssl_zeroize( ssl->psk, ssl->psk_len );
ansond 0:137634ff4186 5091 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
ansond 0:137634ff4186 5092 polarssl_free( ssl->psk );
ansond 0:137634ff4186 5093 polarssl_free( ssl->psk_identity );
ansond 0:137634ff4186 5094 ssl->psk_len = 0;
ansond 0:137634ff4186 5095 ssl->psk_identity_len = 0;
ansond 0:137634ff4186 5096 }
ansond 0:137634ff4186 5097 #endif
ansond 0:137634ff4186 5098
ansond 0:137634ff4186 5099 #if defined(POLARSSL_X509_CRT_PARSE_C)
ansond 0:137634ff4186 5100 ssl_key_cert_free( ssl->key_cert );
ansond 0:137634ff4186 5101 #endif
ansond 0:137634ff4186 5102
ansond 0:137634ff4186 5103 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
ansond 0:137634ff4186 5104 if( ssl_hw_record_finish != NULL )
ansond 0:137634ff4186 5105 {
ansond 0:137634ff4186 5106 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
ansond 0:137634ff4186 5107 ssl_hw_record_finish( ssl );
ansond 0:137634ff4186 5108 }
ansond 0:137634ff4186 5109 #endif
ansond 0:137634ff4186 5110
ansond 0:137634ff4186 5111 SSL_DEBUG_MSG( 2, ( "<= free" ) );
ansond 0:137634ff4186 5112
ansond 0:137634ff4186 5113 /* Actually clear after last debug message */
ansond 0:137634ff4186 5114 polarssl_zeroize( ssl, sizeof( ssl_context ) );
ansond 0:137634ff4186 5115 }
ansond 0:137634ff4186 5116
ansond 0:137634ff4186 5117 #if defined(POLARSSL_PK_C)
ansond 0:137634ff4186 5118 /*
ansond 0:137634ff4186 5119 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
ansond 0:137634ff4186 5120 */
ansond 0:137634ff4186 5121 unsigned char ssl_sig_from_pk( pk_context *pk )
ansond 0:137634ff4186 5122 {
ansond 0:137634ff4186 5123 #if defined(POLARSSL_RSA_C)
ansond 0:137634ff4186 5124 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
ansond 0:137634ff4186 5125 return( SSL_SIG_RSA );
ansond 0:137634ff4186 5126 #endif
ansond 0:137634ff4186 5127 #if defined(POLARSSL_ECDSA_C)
ansond 0:137634ff4186 5128 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
ansond 0:137634ff4186 5129 return( SSL_SIG_ECDSA );
ansond 0:137634ff4186 5130 #endif
ansond 0:137634ff4186 5131 return( SSL_SIG_ANON );
ansond 0:137634ff4186 5132 }
ansond 0:137634ff4186 5133
ansond 0:137634ff4186 5134 pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
ansond 0:137634ff4186 5135 {
ansond 0:137634ff4186 5136 switch( sig )
ansond 0:137634ff4186 5137 {
ansond 0:137634ff4186 5138 #if defined(POLARSSL_RSA_C)
ansond 0:137634ff4186 5139 case SSL_SIG_RSA:
ansond 0:137634ff4186 5140 return( POLARSSL_PK_RSA );
ansond 0:137634ff4186 5141 #endif
ansond 0:137634ff4186 5142 #if defined(POLARSSL_ECDSA_C)
ansond 0:137634ff4186 5143 case SSL_SIG_ECDSA:
ansond 0:137634ff4186 5144 return( POLARSSL_PK_ECDSA );
ansond 0:137634ff4186 5145 #endif
ansond 0:137634ff4186 5146 default:
ansond 0:137634ff4186 5147 return( POLARSSL_PK_NONE );
ansond 0:137634ff4186 5148 }
ansond 0:137634ff4186 5149 }
ansond 0:137634ff4186 5150 #endif /* POLARSSL_PK_C */
ansond 0:137634ff4186 5151
ansond 0:137634ff4186 5152 /*
ansond 0:137634ff4186 5153 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
ansond 0:137634ff4186 5154 */
ansond 0:137634ff4186 5155 md_type_t ssl_md_alg_from_hash( unsigned char hash )
ansond 0:137634ff4186 5156 {
ansond 0:137634ff4186 5157 switch( hash )
ansond 0:137634ff4186 5158 {
ansond 0:137634ff4186 5159 #if defined(POLARSSL_MD5_C)
ansond 0:137634ff4186 5160 case SSL_HASH_MD5:
ansond 0:137634ff4186 5161 return( POLARSSL_MD_MD5 );
ansond 0:137634ff4186 5162 #endif
ansond 0:137634ff4186 5163 #if defined(POLARSSL_SHA1_C)
ansond 0:137634ff4186 5164 case SSL_HASH_SHA1:
ansond 0:137634ff4186 5165 return( POLARSSL_MD_SHA1 );
ansond 0:137634ff4186 5166 #endif
ansond 0:137634ff4186 5167 #if defined(POLARSSL_SHA256_C)
ansond 0:137634ff4186 5168 case SSL_HASH_SHA224:
ansond 0:137634ff4186 5169 return( POLARSSL_MD_SHA224 );
ansond 0:137634ff4186 5170 case SSL_HASH_SHA256:
ansond 0:137634ff4186 5171 return( POLARSSL_MD_SHA256 );
ansond 0:137634ff4186 5172 #endif
ansond 0:137634ff4186 5173 #if defined(POLARSSL_SHA512_C)
ansond 0:137634ff4186 5174 case SSL_HASH_SHA384:
ansond 0:137634ff4186 5175 return( POLARSSL_MD_SHA384 );
ansond 0:137634ff4186 5176 case SSL_HASH_SHA512:
ansond 0:137634ff4186 5177 return( POLARSSL_MD_SHA512 );
ansond 0:137634ff4186 5178 #endif
ansond 0:137634ff4186 5179 default:
ansond 0:137634ff4186 5180 return( POLARSSL_MD_NONE );
ansond 0:137634ff4186 5181 }
ansond 0:137634ff4186 5182 }
ansond 0:137634ff4186 5183
ansond 0:137634ff4186 5184 #if defined(POLARSSL_SSL_SET_CURVES)
ansond 0:137634ff4186 5185 /*
ansond 0:137634ff4186 5186 * Check is a curve proposed by the peer is in our list.
ansond 0:137634ff4186 5187 * Return 1 if we're willing to use it, 0 otherwise.
ansond 0:137634ff4186 5188 */
ansond 0:137634ff4186 5189 int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
ansond 0:137634ff4186 5190 {
ansond 0:137634ff4186 5191 const ecp_group_id *gid;
ansond 0:137634ff4186 5192
ansond 0:137634ff4186 5193 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
ansond 0:137634ff4186 5194 if( *gid == grp_id )
ansond 0:137634ff4186 5195 return( 1 );
ansond 0:137634ff4186 5196
ansond 0:137634ff4186 5197 return( 0 );
ansond 0:137634ff4186 5198 }
ansond 0:137634ff4186 5199 #endif /* POLARSSL_SSL_SET_CURVES */
ansond 0:137634ff4186 5200
ansond 0:137634ff4186 5201 #if defined(POLARSSL_X509_CRT_PARSE_C)
ansond 0:137634ff4186 5202 int ssl_check_cert_usage( const x509_crt *cert,
ansond 0:137634ff4186 5203 const ssl_ciphersuite_t *ciphersuite,
ansond 0:137634ff4186 5204 int cert_endpoint,
ansond 0:137634ff4186 5205 int *flags )
ansond 0:137634ff4186 5206 {
ansond 0:137634ff4186 5207 int ret = 0;
ansond 0:137634ff4186 5208 #if defined(POLARSSL_X509_CHECK_KEY_USAGE)
ansond 0:137634ff4186 5209 int usage = 0;
ansond 0:137634ff4186 5210 #endif
ansond 0:137634ff4186 5211 #if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
ansond 0:137634ff4186 5212 const char *ext_oid;
ansond 0:137634ff4186 5213 size_t ext_len;
ansond 0:137634ff4186 5214 #endif
ansond 0:137634ff4186 5215
ansond 0:137634ff4186 5216 #if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
ansond 0:137634ff4186 5217 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
ansond 0:137634ff4186 5218 ((void) cert);
ansond 0:137634ff4186 5219 ((void) cert_endpoint);
ansond 0:137634ff4186 5220 ((void) flags);
ansond 0:137634ff4186 5221 #endif
ansond 0:137634ff4186 5222
ansond 0:137634ff4186 5223 #if defined(POLARSSL_X509_CHECK_KEY_USAGE)
ansond 0:137634ff4186 5224 if( cert_endpoint == SSL_IS_SERVER )
ansond 0:137634ff4186 5225 {
ansond 0:137634ff4186 5226 /* Server part of the key exchange */
ansond 0:137634ff4186 5227 switch( ciphersuite->key_exchange )
ansond 0:137634ff4186 5228 {
ansond 0:137634ff4186 5229 case POLARSSL_KEY_EXCHANGE_RSA:
ansond 0:137634ff4186 5230 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
ansond 0:137634ff4186 5231 usage = KU_KEY_ENCIPHERMENT;
ansond 0:137634ff4186 5232 break;
ansond 0:137634ff4186 5233
ansond 0:137634ff4186 5234 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
ansond 0:137634ff4186 5235 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
ansond 0:137634ff4186 5236 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
ansond 0:137634ff4186 5237 usage = KU_DIGITAL_SIGNATURE;
ansond 0:137634ff4186 5238 break;
ansond 0:137634ff4186 5239
ansond 0:137634ff4186 5240 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
ansond 0:137634ff4186 5241 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
ansond 0:137634ff4186 5242 usage = KU_KEY_AGREEMENT;
ansond 0:137634ff4186 5243 break;
ansond 0:137634ff4186 5244
ansond 0:137634ff4186 5245 /* Don't use default: we want warnings when adding new values */
ansond 0:137634ff4186 5246 case POLARSSL_KEY_EXCHANGE_NONE:
ansond 0:137634ff4186 5247 case POLARSSL_KEY_EXCHANGE_PSK:
ansond 0:137634ff4186 5248 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
ansond 0:137634ff4186 5249 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
ansond 0:137634ff4186 5250 usage = 0;
ansond 0:137634ff4186 5251 }
ansond 0:137634ff4186 5252 }
ansond 0:137634ff4186 5253 else
ansond 0:137634ff4186 5254 {
ansond 0:137634ff4186 5255 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
ansond 0:137634ff4186 5256 usage = KU_DIGITAL_SIGNATURE;
ansond 0:137634ff4186 5257 }
ansond 0:137634ff4186 5258
ansond 0:137634ff4186 5259 if( x509_crt_check_key_usage( cert, usage ) != 0 )
ansond 0:137634ff4186 5260 {
ansond 0:137634ff4186 5261 *flags |= BADCERT_KEY_USAGE;
ansond 0:137634ff4186 5262 ret = -1;
ansond 0:137634ff4186 5263 }
ansond 0:137634ff4186 5264 #else
ansond 0:137634ff4186 5265 ((void) ciphersuite);
ansond 0:137634ff4186 5266 #endif /* POLARSSL_X509_CHECK_KEY_USAGE */
ansond 0:137634ff4186 5267
ansond 0:137634ff4186 5268 #if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
ansond 0:137634ff4186 5269 if( cert_endpoint == SSL_IS_SERVER )
ansond 0:137634ff4186 5270 {
ansond 0:137634ff4186 5271 ext_oid = OID_SERVER_AUTH;
ansond 0:137634ff4186 5272 ext_len = OID_SIZE( OID_SERVER_AUTH );
ansond 0:137634ff4186 5273 }
ansond 0:137634ff4186 5274 else
ansond 0:137634ff4186 5275 {
ansond 0:137634ff4186 5276 ext_oid = OID_CLIENT_AUTH;
ansond 0:137634ff4186 5277 ext_len = OID_SIZE( OID_CLIENT_AUTH );
ansond 0:137634ff4186 5278 }
ansond 0:137634ff4186 5279
ansond 0:137634ff4186 5280 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
ansond 0:137634ff4186 5281 {
ansond 0:137634ff4186 5282 *flags |= BADCERT_EXT_KEY_USAGE;
ansond 0:137634ff4186 5283 ret = -1;
ansond 0:137634ff4186 5284 }
ansond 0:137634ff4186 5285 #endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
ansond 0:137634ff4186 5286
ansond 0:137634ff4186 5287 return( ret );
ansond 0:137634ff4186 5288 }
ansond 0:137634ff4186 5289 #endif /* POLARSSL_X509_CRT_PARSE_C */
ansond 0:137634ff4186 5290
ansond 0:137634ff4186 5291 #endif /* POLARSSL_SSL_TLS_C */
ansond 0:137634ff4186 5292