Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nexpaq 0:6c56fb4bc5f0 1 /*
nexpaq 0:6c56fb4bc5f0 2 * SSLv3/TLSv1 shared functions
nexpaq 0:6c56fb4bc5f0 3 *
nexpaq 0:6c56fb4bc5f0 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
nexpaq 0:6c56fb4bc5f0 5 * SPDX-License-Identifier: Apache-2.0
nexpaq 0:6c56fb4bc5f0 6 *
nexpaq 0:6c56fb4bc5f0 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
nexpaq 0:6c56fb4bc5f0 8 * not use this file except in compliance with the License.
nexpaq 0:6c56fb4bc5f0 9 * You may obtain a copy of the License at
nexpaq 0:6c56fb4bc5f0 10 *
nexpaq 0:6c56fb4bc5f0 11 * http://www.apache.org/licenses/LICENSE-2.0
nexpaq 0:6c56fb4bc5f0 12 *
nexpaq 0:6c56fb4bc5f0 13 * Unless required by applicable law or agreed to in writing, software
nexpaq 0:6c56fb4bc5f0 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
nexpaq 0:6c56fb4bc5f0 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
nexpaq 0:6c56fb4bc5f0 16 * See the License for the specific language governing permissions and
nexpaq 0:6c56fb4bc5f0 17 * limitations under the License.
nexpaq 0:6c56fb4bc5f0 18 *
nexpaq 0:6c56fb4bc5f0 19 * This file is part of mbed TLS (https://tls.mbed.org)
nexpaq 0:6c56fb4bc5f0 20 */
nexpaq 0:6c56fb4bc5f0 21 /*
nexpaq 0:6c56fb4bc5f0 22 * The SSL 3.0 specification was drafted by Netscape in 1996,
nexpaq 0:6c56fb4bc5f0 23 * and became an IETF standard in 1999.
nexpaq 0:6c56fb4bc5f0 24 *
nexpaq 0:6c56fb4bc5f0 25 * http://wp.netscape.com/eng/ssl3/
nexpaq 0:6c56fb4bc5f0 26 * http://www.ietf.org/rfc/rfc2246.txt
nexpaq 0:6c56fb4bc5f0 27 * http://www.ietf.org/rfc/rfc4346.txt
nexpaq 0:6c56fb4bc5f0 28 */
nexpaq 0:6c56fb4bc5f0 29
nexpaq 0:6c56fb4bc5f0 30 #if !defined(MBEDTLS_CONFIG_FILE)
nexpaq 0:6c56fb4bc5f0 31 #include "mbedtls/config.h"
nexpaq 0:6c56fb4bc5f0 32 #else
nexpaq 0:6c56fb4bc5f0 33 #include MBEDTLS_CONFIG_FILE
nexpaq 0:6c56fb4bc5f0 34 #endif
nexpaq 0:6c56fb4bc5f0 35
nexpaq 0:6c56fb4bc5f0 36 #if defined(MBEDTLS_SSL_TLS_C)
nexpaq 0:6c56fb4bc5f0 37
nexpaq 0:6c56fb4bc5f0 38 #if defined(MBEDTLS_PLATFORM_C)
nexpaq 0:6c56fb4bc5f0 39 #include "mbedtls/platform.h"
nexpaq 0:6c56fb4bc5f0 40 #else
nexpaq 0:6c56fb4bc5f0 41 #include <stdlib.h>
nexpaq 0:6c56fb4bc5f0 42 #define mbedtls_calloc calloc
nexpaq 0:6c56fb4bc5f0 43 #define mbedtls_free free
nexpaq 0:6c56fb4bc5f0 44 #define mbedtls_time_t time_t
nexpaq 0:6c56fb4bc5f0 45 #endif
nexpaq 0:6c56fb4bc5f0 46
nexpaq 0:6c56fb4bc5f0 47 #include "mbedtls/debug.h"
nexpaq 0:6c56fb4bc5f0 48 #include "mbedtls/ssl.h"
nexpaq 0:6c56fb4bc5f0 49 #include "mbedtls/ssl_internal.h"
nexpaq 0:6c56fb4bc5f0 50
nexpaq 0:6c56fb4bc5f0 51 #include <string.h>
nexpaq 0:6c56fb4bc5f0 52
nexpaq 0:6c56fb4bc5f0 53 #if defined(MBEDTLS_X509_CRT_PARSE_C) && \
nexpaq 0:6c56fb4bc5f0 54 defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
nexpaq 0:6c56fb4bc5f0 55 #include "mbedtls/oid.h"
nexpaq 0:6c56fb4bc5f0 56 #endif
nexpaq 0:6c56fb4bc5f0 57
nexpaq 0:6c56fb4bc5f0 58 /* Implementation that should never be optimized out by the compiler */
nexpaq 0:6c56fb4bc5f0 59 static void mbedtls_zeroize( void *v, size_t n ) {
nexpaq 0:6c56fb4bc5f0 60 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
nexpaq 0:6c56fb4bc5f0 61 }
nexpaq 0:6c56fb4bc5f0 62
nexpaq 0:6c56fb4bc5f0 63 /* Length of the "epoch" field in the record header */
nexpaq 0:6c56fb4bc5f0 64 static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 65 {
nexpaq 0:6c56fb4bc5f0 66 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 67 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
nexpaq 0:6c56fb4bc5f0 68 return( 2 );
nexpaq 0:6c56fb4bc5f0 69 #else
nexpaq 0:6c56fb4bc5f0 70 ((void) ssl);
nexpaq 0:6c56fb4bc5f0 71 #endif
nexpaq 0:6c56fb4bc5f0 72 return( 0 );
nexpaq 0:6c56fb4bc5f0 73 }
nexpaq 0:6c56fb4bc5f0 74
nexpaq 0:6c56fb4bc5f0 75 /*
nexpaq 0:6c56fb4bc5f0 76 * Start a timer.
nexpaq 0:6c56fb4bc5f0 77 * Passing millisecs = 0 cancels a running timer.
nexpaq 0:6c56fb4bc5f0 78 */
nexpaq 0:6c56fb4bc5f0 79 static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
nexpaq 0:6c56fb4bc5f0 80 {
nexpaq 0:6c56fb4bc5f0 81 if( ssl->f_set_timer == NULL )
nexpaq 0:6c56fb4bc5f0 82 return;
nexpaq 0:6c56fb4bc5f0 83
nexpaq 0:6c56fb4bc5f0 84 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
nexpaq 0:6c56fb4bc5f0 85 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
nexpaq 0:6c56fb4bc5f0 86 }
nexpaq 0:6c56fb4bc5f0 87
nexpaq 0:6c56fb4bc5f0 88 /*
nexpaq 0:6c56fb4bc5f0 89 * Return -1 is timer is expired, 0 if it isn't.
nexpaq 0:6c56fb4bc5f0 90 */
nexpaq 0:6c56fb4bc5f0 91 static int ssl_check_timer( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 92 {
nexpaq 0:6c56fb4bc5f0 93 if( ssl->f_get_timer == NULL )
nexpaq 0:6c56fb4bc5f0 94 return( 0 );
nexpaq 0:6c56fb4bc5f0 95
nexpaq 0:6c56fb4bc5f0 96 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
nexpaq 0:6c56fb4bc5f0 97 {
nexpaq 0:6c56fb4bc5f0 98 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
nexpaq 0:6c56fb4bc5f0 99 return( -1 );
nexpaq 0:6c56fb4bc5f0 100 }
nexpaq 0:6c56fb4bc5f0 101
nexpaq 0:6c56fb4bc5f0 102 return( 0 );
nexpaq 0:6c56fb4bc5f0 103 }
nexpaq 0:6c56fb4bc5f0 104
nexpaq 0:6c56fb4bc5f0 105 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 106 /*
nexpaq 0:6c56fb4bc5f0 107 * Double the retransmit timeout value, within the allowed range,
nexpaq 0:6c56fb4bc5f0 108 * returning -1 if the maximum value has already been reached.
nexpaq 0:6c56fb4bc5f0 109 */
nexpaq 0:6c56fb4bc5f0 110 static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 111 {
nexpaq 0:6c56fb4bc5f0 112 uint32_t new_timeout;
nexpaq 0:6c56fb4bc5f0 113
nexpaq 0:6c56fb4bc5f0 114 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
nexpaq 0:6c56fb4bc5f0 115 return( -1 );
nexpaq 0:6c56fb4bc5f0 116
nexpaq 0:6c56fb4bc5f0 117 new_timeout = 2 * ssl->handshake->retransmit_timeout;
nexpaq 0:6c56fb4bc5f0 118
nexpaq 0:6c56fb4bc5f0 119 /* Avoid arithmetic overflow and range overflow */
nexpaq 0:6c56fb4bc5f0 120 if( new_timeout < ssl->handshake->retransmit_timeout ||
nexpaq 0:6c56fb4bc5f0 121 new_timeout > ssl->conf->hs_timeout_max )
nexpaq 0:6c56fb4bc5f0 122 {
nexpaq 0:6c56fb4bc5f0 123 new_timeout = ssl->conf->hs_timeout_max;
nexpaq 0:6c56fb4bc5f0 124 }
nexpaq 0:6c56fb4bc5f0 125
nexpaq 0:6c56fb4bc5f0 126 ssl->handshake->retransmit_timeout = new_timeout;
nexpaq 0:6c56fb4bc5f0 127 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
nexpaq 0:6c56fb4bc5f0 128 ssl->handshake->retransmit_timeout ) );
nexpaq 0:6c56fb4bc5f0 129
nexpaq 0:6c56fb4bc5f0 130 return( 0 );
nexpaq 0:6c56fb4bc5f0 131 }
nexpaq 0:6c56fb4bc5f0 132
nexpaq 0:6c56fb4bc5f0 133 static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 134 {
nexpaq 0:6c56fb4bc5f0 135 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
nexpaq 0:6c56fb4bc5f0 136 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
nexpaq 0:6c56fb4bc5f0 137 ssl->handshake->retransmit_timeout ) );
nexpaq 0:6c56fb4bc5f0 138 }
nexpaq 0:6c56fb4bc5f0 139 #endif /* MBEDTLS_SSL_PROTO_DTLS */
nexpaq 0:6c56fb4bc5f0 140
nexpaq 0:6c56fb4bc5f0 141 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
nexpaq 0:6c56fb4bc5f0 142 /*
nexpaq 0:6c56fb4bc5f0 143 * Convert max_fragment_length codes to length.
nexpaq 0:6c56fb4bc5f0 144 * RFC 6066 says:
nexpaq 0:6c56fb4bc5f0 145 * enum{
nexpaq 0:6c56fb4bc5f0 146 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
nexpaq 0:6c56fb4bc5f0 147 * } MaxFragmentLength;
nexpaq 0:6c56fb4bc5f0 148 * and we add 0 -> extension unused
nexpaq 0:6c56fb4bc5f0 149 */
nexpaq 0:6c56fb4bc5f0 150 static unsigned int mfl_code_to_length[MBEDTLS_SSL_MAX_FRAG_LEN_INVALID] =
nexpaq 0:6c56fb4bc5f0 151 {
nexpaq 0:6c56fb4bc5f0 152 MBEDTLS_SSL_MAX_CONTENT_LEN, /* MBEDTLS_SSL_MAX_FRAG_LEN_NONE */
nexpaq 0:6c56fb4bc5f0 153 512, /* MBEDTLS_SSL_MAX_FRAG_LEN_512 */
nexpaq 0:6c56fb4bc5f0 154 1024, /* MBEDTLS_SSL_MAX_FRAG_LEN_1024 */
nexpaq 0:6c56fb4bc5f0 155 2048, /* MBEDTLS_SSL_MAX_FRAG_LEN_2048 */
nexpaq 0:6c56fb4bc5f0 156 4096, /* MBEDTLS_SSL_MAX_FRAG_LEN_4096 */
nexpaq 0:6c56fb4bc5f0 157 };
nexpaq 0:6c56fb4bc5f0 158 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
nexpaq 0:6c56fb4bc5f0 159
nexpaq 0:6c56fb4bc5f0 160 #if defined(MBEDTLS_SSL_CLI_C)
nexpaq 0:6c56fb4bc5f0 161 static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
nexpaq 0:6c56fb4bc5f0 162 {
nexpaq 0:6c56fb4bc5f0 163 mbedtls_ssl_session_free( dst );
nexpaq 0:6c56fb4bc5f0 164 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
nexpaq 0:6c56fb4bc5f0 165
nexpaq 0:6c56fb4bc5f0 166 #if defined(MBEDTLS_X509_CRT_PARSE_C)
nexpaq 0:6c56fb4bc5f0 167 if( src->peer_cert != NULL )
nexpaq 0:6c56fb4bc5f0 168 {
nexpaq 0:6c56fb4bc5f0 169 int ret;
nexpaq 0:6c56fb4bc5f0 170
nexpaq 0:6c56fb4bc5f0 171 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
nexpaq 0:6c56fb4bc5f0 172 if( dst->peer_cert == NULL )
nexpaq 0:6c56fb4bc5f0 173 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
nexpaq 0:6c56fb4bc5f0 174
nexpaq 0:6c56fb4bc5f0 175 mbedtls_x509_crt_init( dst->peer_cert );
nexpaq 0:6c56fb4bc5f0 176
nexpaq 0:6c56fb4bc5f0 177 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
nexpaq 0:6c56fb4bc5f0 178 src->peer_cert->raw.len ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 179 {
nexpaq 0:6c56fb4bc5f0 180 mbedtls_free( dst->peer_cert );
nexpaq 0:6c56fb4bc5f0 181 dst->peer_cert = NULL;
nexpaq 0:6c56fb4bc5f0 182 return( ret );
nexpaq 0:6c56fb4bc5f0 183 }
nexpaq 0:6c56fb4bc5f0 184 }
nexpaq 0:6c56fb4bc5f0 185 #endif /* MBEDTLS_X509_CRT_PARSE_C */
nexpaq 0:6c56fb4bc5f0 186
nexpaq 0:6c56fb4bc5f0 187 #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
nexpaq 0:6c56fb4bc5f0 188 if( src->ticket != NULL )
nexpaq 0:6c56fb4bc5f0 189 {
nexpaq 0:6c56fb4bc5f0 190 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
nexpaq 0:6c56fb4bc5f0 191 if( dst->ticket == NULL )
nexpaq 0:6c56fb4bc5f0 192 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
nexpaq 0:6c56fb4bc5f0 193
nexpaq 0:6c56fb4bc5f0 194 memcpy( dst->ticket, src->ticket, src->ticket_len );
nexpaq 0:6c56fb4bc5f0 195 }
nexpaq 0:6c56fb4bc5f0 196 #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
nexpaq 0:6c56fb4bc5f0 197
nexpaq 0:6c56fb4bc5f0 198 return( 0 );
nexpaq 0:6c56fb4bc5f0 199 }
nexpaq 0:6c56fb4bc5f0 200 #endif /* MBEDTLS_SSL_CLI_C */
nexpaq 0:6c56fb4bc5f0 201
nexpaq 0:6c56fb4bc5f0 202 #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
nexpaq 0:6c56fb4bc5f0 203 int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
nexpaq 0:6c56fb4bc5f0 204 const unsigned char *key_enc, const unsigned char *key_dec,
nexpaq 0:6c56fb4bc5f0 205 size_t keylen,
nexpaq 0:6c56fb4bc5f0 206 const unsigned char *iv_enc, const unsigned char *iv_dec,
nexpaq 0:6c56fb4bc5f0 207 size_t ivlen,
nexpaq 0:6c56fb4bc5f0 208 const unsigned char *mac_enc, const unsigned char *mac_dec,
nexpaq 0:6c56fb4bc5f0 209 size_t maclen ) = NULL;
nexpaq 0:6c56fb4bc5f0 210 int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
nexpaq 0:6c56fb4bc5f0 211 int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
nexpaq 0:6c56fb4bc5f0 212 int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
nexpaq 0:6c56fb4bc5f0 213 int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
nexpaq 0:6c56fb4bc5f0 214 int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
nexpaq 0:6c56fb4bc5f0 215 #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
nexpaq 0:6c56fb4bc5f0 216
nexpaq 0:6c56fb4bc5f0 217 /*
nexpaq 0:6c56fb4bc5f0 218 * Key material generation
nexpaq 0:6c56fb4bc5f0 219 */
nexpaq 0:6c56fb4bc5f0 220 #if defined(MBEDTLS_SSL_PROTO_SSL3)
nexpaq 0:6c56fb4bc5f0 221 static int ssl3_prf( const unsigned char *secret, size_t slen,
nexpaq 0:6c56fb4bc5f0 222 const char *label,
nexpaq 0:6c56fb4bc5f0 223 const unsigned char *random, size_t rlen,
nexpaq 0:6c56fb4bc5f0 224 unsigned char *dstbuf, size_t dlen )
nexpaq 0:6c56fb4bc5f0 225 {
nexpaq 0:6c56fb4bc5f0 226 size_t i;
nexpaq 0:6c56fb4bc5f0 227 mbedtls_md5_context md5;
nexpaq 0:6c56fb4bc5f0 228 mbedtls_sha1_context sha1;
nexpaq 0:6c56fb4bc5f0 229 unsigned char padding[16];
nexpaq 0:6c56fb4bc5f0 230 unsigned char sha1sum[20];
nexpaq 0:6c56fb4bc5f0 231 ((void)label);
nexpaq 0:6c56fb4bc5f0 232
nexpaq 0:6c56fb4bc5f0 233 mbedtls_md5_init( &md5 );
nexpaq 0:6c56fb4bc5f0 234 mbedtls_sha1_init( &sha1 );
nexpaq 0:6c56fb4bc5f0 235
nexpaq 0:6c56fb4bc5f0 236 /*
nexpaq 0:6c56fb4bc5f0 237 * SSLv3:
nexpaq 0:6c56fb4bc5f0 238 * block =
nexpaq 0:6c56fb4bc5f0 239 * MD5( secret + SHA1( 'A' + secret + random ) ) +
nexpaq 0:6c56fb4bc5f0 240 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
nexpaq 0:6c56fb4bc5f0 241 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
nexpaq 0:6c56fb4bc5f0 242 * ...
nexpaq 0:6c56fb4bc5f0 243 */
nexpaq 0:6c56fb4bc5f0 244 for( i = 0; i < dlen / 16; i++ )
nexpaq 0:6c56fb4bc5f0 245 {
nexpaq 0:6c56fb4bc5f0 246 memset( padding, (unsigned char) ('A' + i), 1 + i );
nexpaq 0:6c56fb4bc5f0 247
nexpaq 0:6c56fb4bc5f0 248 mbedtls_sha1_starts( &sha1 );
nexpaq 0:6c56fb4bc5f0 249 mbedtls_sha1_update( &sha1, padding, 1 + i );
nexpaq 0:6c56fb4bc5f0 250 mbedtls_sha1_update( &sha1, secret, slen );
nexpaq 0:6c56fb4bc5f0 251 mbedtls_sha1_update( &sha1, random, rlen );
nexpaq 0:6c56fb4bc5f0 252 mbedtls_sha1_finish( &sha1, sha1sum );
nexpaq 0:6c56fb4bc5f0 253
nexpaq 0:6c56fb4bc5f0 254 mbedtls_md5_starts( &md5 );
nexpaq 0:6c56fb4bc5f0 255 mbedtls_md5_update( &md5, secret, slen );
nexpaq 0:6c56fb4bc5f0 256 mbedtls_md5_update( &md5, sha1sum, 20 );
nexpaq 0:6c56fb4bc5f0 257 mbedtls_md5_finish( &md5, dstbuf + i * 16 );
nexpaq 0:6c56fb4bc5f0 258 }
nexpaq 0:6c56fb4bc5f0 259
nexpaq 0:6c56fb4bc5f0 260 mbedtls_md5_free( &md5 );
nexpaq 0:6c56fb4bc5f0 261 mbedtls_sha1_free( &sha1 );
nexpaq 0:6c56fb4bc5f0 262
nexpaq 0:6c56fb4bc5f0 263 mbedtls_zeroize( padding, sizeof( padding ) );
nexpaq 0:6c56fb4bc5f0 264 mbedtls_zeroize( sha1sum, sizeof( sha1sum ) );
nexpaq 0:6c56fb4bc5f0 265
nexpaq 0:6c56fb4bc5f0 266 return( 0 );
nexpaq 0:6c56fb4bc5f0 267 }
nexpaq 0:6c56fb4bc5f0 268 #endif /* MBEDTLS_SSL_PROTO_SSL3 */
nexpaq 0:6c56fb4bc5f0 269
nexpaq 0:6c56fb4bc5f0 270 #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
nexpaq 0:6c56fb4bc5f0 271 static int tls1_prf( const unsigned char *secret, size_t slen,
nexpaq 0:6c56fb4bc5f0 272 const char *label,
nexpaq 0:6c56fb4bc5f0 273 const unsigned char *random, size_t rlen,
nexpaq 0:6c56fb4bc5f0 274 unsigned char *dstbuf, size_t dlen )
nexpaq 0:6c56fb4bc5f0 275 {
nexpaq 0:6c56fb4bc5f0 276 size_t nb, hs;
nexpaq 0:6c56fb4bc5f0 277 size_t i, j, k;
nexpaq 0:6c56fb4bc5f0 278 const unsigned char *S1, *S2;
nexpaq 0:6c56fb4bc5f0 279 unsigned char tmp[128];
nexpaq 0:6c56fb4bc5f0 280 unsigned char h_i[20];
nexpaq 0:6c56fb4bc5f0 281 const mbedtls_md_info_t *md_info;
nexpaq 0:6c56fb4bc5f0 282 mbedtls_md_context_t md_ctx;
nexpaq 0:6c56fb4bc5f0 283 int ret;
nexpaq 0:6c56fb4bc5f0 284
nexpaq 0:6c56fb4bc5f0 285 mbedtls_md_init( &md_ctx );
nexpaq 0:6c56fb4bc5f0 286
nexpaq 0:6c56fb4bc5f0 287 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
nexpaq 0:6c56fb4bc5f0 288 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 289
nexpaq 0:6c56fb4bc5f0 290 hs = ( slen + 1 ) / 2;
nexpaq 0:6c56fb4bc5f0 291 S1 = secret;
nexpaq 0:6c56fb4bc5f0 292 S2 = secret + slen - hs;
nexpaq 0:6c56fb4bc5f0 293
nexpaq 0:6c56fb4bc5f0 294 nb = strlen( label );
nexpaq 0:6c56fb4bc5f0 295 memcpy( tmp + 20, label, nb );
nexpaq 0:6c56fb4bc5f0 296 memcpy( tmp + 20 + nb, random, rlen );
nexpaq 0:6c56fb4bc5f0 297 nb += rlen;
nexpaq 0:6c56fb4bc5f0 298
nexpaq 0:6c56fb4bc5f0 299 /*
nexpaq 0:6c56fb4bc5f0 300 * First compute P_md5(secret,label+random)[0..dlen]
nexpaq 0:6c56fb4bc5f0 301 */
nexpaq 0:6c56fb4bc5f0 302 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
nexpaq 0:6c56fb4bc5f0 303 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 304
nexpaq 0:6c56fb4bc5f0 305 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 306 return( ret );
nexpaq 0:6c56fb4bc5f0 307
nexpaq 0:6c56fb4bc5f0 308 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
nexpaq 0:6c56fb4bc5f0 309 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
nexpaq 0:6c56fb4bc5f0 310 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
nexpaq 0:6c56fb4bc5f0 311
nexpaq 0:6c56fb4bc5f0 312 for( i = 0; i < dlen; i += 16 )
nexpaq 0:6c56fb4bc5f0 313 {
nexpaq 0:6c56fb4bc5f0 314 mbedtls_md_hmac_reset ( &md_ctx );
nexpaq 0:6c56fb4bc5f0 315 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
nexpaq 0:6c56fb4bc5f0 316 mbedtls_md_hmac_finish( &md_ctx, h_i );
nexpaq 0:6c56fb4bc5f0 317
nexpaq 0:6c56fb4bc5f0 318 mbedtls_md_hmac_reset ( &md_ctx );
nexpaq 0:6c56fb4bc5f0 319 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
nexpaq 0:6c56fb4bc5f0 320 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
nexpaq 0:6c56fb4bc5f0 321
nexpaq 0:6c56fb4bc5f0 322 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
nexpaq 0:6c56fb4bc5f0 323
nexpaq 0:6c56fb4bc5f0 324 for( j = 0; j < k; j++ )
nexpaq 0:6c56fb4bc5f0 325 dstbuf[i + j] = h_i[j];
nexpaq 0:6c56fb4bc5f0 326 }
nexpaq 0:6c56fb4bc5f0 327
nexpaq 0:6c56fb4bc5f0 328 mbedtls_md_free( &md_ctx );
nexpaq 0:6c56fb4bc5f0 329
nexpaq 0:6c56fb4bc5f0 330 /*
nexpaq 0:6c56fb4bc5f0 331 * XOR out with P_sha1(secret,label+random)[0..dlen]
nexpaq 0:6c56fb4bc5f0 332 */
nexpaq 0:6c56fb4bc5f0 333 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
nexpaq 0:6c56fb4bc5f0 334 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 335
nexpaq 0:6c56fb4bc5f0 336 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 337 return( ret );
nexpaq 0:6c56fb4bc5f0 338
nexpaq 0:6c56fb4bc5f0 339 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
nexpaq 0:6c56fb4bc5f0 340 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
nexpaq 0:6c56fb4bc5f0 341 mbedtls_md_hmac_finish( &md_ctx, tmp );
nexpaq 0:6c56fb4bc5f0 342
nexpaq 0:6c56fb4bc5f0 343 for( i = 0; i < dlen; i += 20 )
nexpaq 0:6c56fb4bc5f0 344 {
nexpaq 0:6c56fb4bc5f0 345 mbedtls_md_hmac_reset ( &md_ctx );
nexpaq 0:6c56fb4bc5f0 346 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
nexpaq 0:6c56fb4bc5f0 347 mbedtls_md_hmac_finish( &md_ctx, h_i );
nexpaq 0:6c56fb4bc5f0 348
nexpaq 0:6c56fb4bc5f0 349 mbedtls_md_hmac_reset ( &md_ctx );
nexpaq 0:6c56fb4bc5f0 350 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
nexpaq 0:6c56fb4bc5f0 351 mbedtls_md_hmac_finish( &md_ctx, tmp );
nexpaq 0:6c56fb4bc5f0 352
nexpaq 0:6c56fb4bc5f0 353 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
nexpaq 0:6c56fb4bc5f0 354
nexpaq 0:6c56fb4bc5f0 355 for( j = 0; j < k; j++ )
nexpaq 0:6c56fb4bc5f0 356 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
nexpaq 0:6c56fb4bc5f0 357 }
nexpaq 0:6c56fb4bc5f0 358
nexpaq 0:6c56fb4bc5f0 359 mbedtls_md_free( &md_ctx );
nexpaq 0:6c56fb4bc5f0 360
nexpaq 0:6c56fb4bc5f0 361 mbedtls_zeroize( tmp, sizeof( tmp ) );
nexpaq 0:6c56fb4bc5f0 362 mbedtls_zeroize( h_i, sizeof( h_i ) );
nexpaq 0:6c56fb4bc5f0 363
nexpaq 0:6c56fb4bc5f0 364 return( 0 );
nexpaq 0:6c56fb4bc5f0 365 }
nexpaq 0:6c56fb4bc5f0 366 #endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
nexpaq 0:6c56fb4bc5f0 367
nexpaq 0:6c56fb4bc5f0 368 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
nexpaq 0:6c56fb4bc5f0 369 static int tls_prf_generic( mbedtls_md_type_t md_type,
nexpaq 0:6c56fb4bc5f0 370 const unsigned char *secret, size_t slen,
nexpaq 0:6c56fb4bc5f0 371 const char *label,
nexpaq 0:6c56fb4bc5f0 372 const unsigned char *random, size_t rlen,
nexpaq 0:6c56fb4bc5f0 373 unsigned char *dstbuf, size_t dlen )
nexpaq 0:6c56fb4bc5f0 374 {
nexpaq 0:6c56fb4bc5f0 375 size_t nb;
nexpaq 0:6c56fb4bc5f0 376 size_t i, j, k, md_len;
nexpaq 0:6c56fb4bc5f0 377 unsigned char tmp[128];
nexpaq 0:6c56fb4bc5f0 378 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
nexpaq 0:6c56fb4bc5f0 379 const mbedtls_md_info_t *md_info;
nexpaq 0:6c56fb4bc5f0 380 mbedtls_md_context_t md_ctx;
nexpaq 0:6c56fb4bc5f0 381 int ret;
nexpaq 0:6c56fb4bc5f0 382
nexpaq 0:6c56fb4bc5f0 383 mbedtls_md_init( &md_ctx );
nexpaq 0:6c56fb4bc5f0 384
nexpaq 0:6c56fb4bc5f0 385 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
nexpaq 0:6c56fb4bc5f0 386 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 387
nexpaq 0:6c56fb4bc5f0 388 md_len = mbedtls_md_get_size( md_info );
nexpaq 0:6c56fb4bc5f0 389
nexpaq 0:6c56fb4bc5f0 390 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
nexpaq 0:6c56fb4bc5f0 391 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 392
nexpaq 0:6c56fb4bc5f0 393 nb = strlen( label );
nexpaq 0:6c56fb4bc5f0 394 memcpy( tmp + md_len, label, nb );
nexpaq 0:6c56fb4bc5f0 395 memcpy( tmp + md_len + nb, random, rlen );
nexpaq 0:6c56fb4bc5f0 396 nb += rlen;
nexpaq 0:6c56fb4bc5f0 397
nexpaq 0:6c56fb4bc5f0 398 /*
nexpaq 0:6c56fb4bc5f0 399 * Compute P_<hash>(secret, label + random)[0..dlen]
nexpaq 0:6c56fb4bc5f0 400 */
nexpaq 0:6c56fb4bc5f0 401 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 402 return( ret );
nexpaq 0:6c56fb4bc5f0 403
nexpaq 0:6c56fb4bc5f0 404 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
nexpaq 0:6c56fb4bc5f0 405 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
nexpaq 0:6c56fb4bc5f0 406 mbedtls_md_hmac_finish( &md_ctx, tmp );
nexpaq 0:6c56fb4bc5f0 407
nexpaq 0:6c56fb4bc5f0 408 for( i = 0; i < dlen; i += md_len )
nexpaq 0:6c56fb4bc5f0 409 {
nexpaq 0:6c56fb4bc5f0 410 mbedtls_md_hmac_reset ( &md_ctx );
nexpaq 0:6c56fb4bc5f0 411 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
nexpaq 0:6c56fb4bc5f0 412 mbedtls_md_hmac_finish( &md_ctx, h_i );
nexpaq 0:6c56fb4bc5f0 413
nexpaq 0:6c56fb4bc5f0 414 mbedtls_md_hmac_reset ( &md_ctx );
nexpaq 0:6c56fb4bc5f0 415 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
nexpaq 0:6c56fb4bc5f0 416 mbedtls_md_hmac_finish( &md_ctx, tmp );
nexpaq 0:6c56fb4bc5f0 417
nexpaq 0:6c56fb4bc5f0 418 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
nexpaq 0:6c56fb4bc5f0 419
nexpaq 0:6c56fb4bc5f0 420 for( j = 0; j < k; j++ )
nexpaq 0:6c56fb4bc5f0 421 dstbuf[i + j] = h_i[j];
nexpaq 0:6c56fb4bc5f0 422 }
nexpaq 0:6c56fb4bc5f0 423
nexpaq 0:6c56fb4bc5f0 424 mbedtls_md_free( &md_ctx );
nexpaq 0:6c56fb4bc5f0 425
nexpaq 0:6c56fb4bc5f0 426 mbedtls_zeroize( tmp, sizeof( tmp ) );
nexpaq 0:6c56fb4bc5f0 427 mbedtls_zeroize( h_i, sizeof( h_i ) );
nexpaq 0:6c56fb4bc5f0 428
nexpaq 0:6c56fb4bc5f0 429 return( 0 );
nexpaq 0:6c56fb4bc5f0 430 }
nexpaq 0:6c56fb4bc5f0 431
nexpaq 0:6c56fb4bc5f0 432 #if defined(MBEDTLS_SHA256_C)
nexpaq 0:6c56fb4bc5f0 433 static int tls_prf_sha256( const unsigned char *secret, size_t slen,
nexpaq 0:6c56fb4bc5f0 434 const char *label,
nexpaq 0:6c56fb4bc5f0 435 const unsigned char *random, size_t rlen,
nexpaq 0:6c56fb4bc5f0 436 unsigned char *dstbuf, size_t dlen )
nexpaq 0:6c56fb4bc5f0 437 {
nexpaq 0:6c56fb4bc5f0 438 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
nexpaq 0:6c56fb4bc5f0 439 label, random, rlen, dstbuf, dlen ) );
nexpaq 0:6c56fb4bc5f0 440 }
nexpaq 0:6c56fb4bc5f0 441 #endif /* MBEDTLS_SHA256_C */
nexpaq 0:6c56fb4bc5f0 442
nexpaq 0:6c56fb4bc5f0 443 #if defined(MBEDTLS_SHA512_C)
nexpaq 0:6c56fb4bc5f0 444 static int tls_prf_sha384( const unsigned char *secret, size_t slen,
nexpaq 0:6c56fb4bc5f0 445 const char *label,
nexpaq 0:6c56fb4bc5f0 446 const unsigned char *random, size_t rlen,
nexpaq 0:6c56fb4bc5f0 447 unsigned char *dstbuf, size_t dlen )
nexpaq 0:6c56fb4bc5f0 448 {
nexpaq 0:6c56fb4bc5f0 449 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
nexpaq 0:6c56fb4bc5f0 450 label, random, rlen, dstbuf, dlen ) );
nexpaq 0:6c56fb4bc5f0 451 }
nexpaq 0:6c56fb4bc5f0 452 #endif /* MBEDTLS_SHA512_C */
nexpaq 0:6c56fb4bc5f0 453 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
nexpaq 0:6c56fb4bc5f0 454
nexpaq 0:6c56fb4bc5f0 455 static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
nexpaq 0:6c56fb4bc5f0 456
nexpaq 0:6c56fb4bc5f0 457 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
nexpaq 0:6c56fb4bc5f0 458 defined(MBEDTLS_SSL_PROTO_TLS1_1)
nexpaq 0:6c56fb4bc5f0 459 static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
nexpaq 0:6c56fb4bc5f0 460 #endif
nexpaq 0:6c56fb4bc5f0 461
nexpaq 0:6c56fb4bc5f0 462 #if defined(MBEDTLS_SSL_PROTO_SSL3)
nexpaq 0:6c56fb4bc5f0 463 static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
nexpaq 0:6c56fb4bc5f0 464 static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
nexpaq 0:6c56fb4bc5f0 465 #endif
nexpaq 0:6c56fb4bc5f0 466
nexpaq 0:6c56fb4bc5f0 467 #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
nexpaq 0:6c56fb4bc5f0 468 static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
nexpaq 0:6c56fb4bc5f0 469 static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
nexpaq 0:6c56fb4bc5f0 470 #endif
nexpaq 0:6c56fb4bc5f0 471
nexpaq 0:6c56fb4bc5f0 472 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
nexpaq 0:6c56fb4bc5f0 473 #if defined(MBEDTLS_SHA256_C)
nexpaq 0:6c56fb4bc5f0 474 static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
nexpaq 0:6c56fb4bc5f0 475 static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
nexpaq 0:6c56fb4bc5f0 476 static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
nexpaq 0:6c56fb4bc5f0 477 #endif
nexpaq 0:6c56fb4bc5f0 478
nexpaq 0:6c56fb4bc5f0 479 #if defined(MBEDTLS_SHA512_C)
nexpaq 0:6c56fb4bc5f0 480 static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
nexpaq 0:6c56fb4bc5f0 481 static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
nexpaq 0:6c56fb4bc5f0 482 static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
nexpaq 0:6c56fb4bc5f0 483 #endif
nexpaq 0:6c56fb4bc5f0 484 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
nexpaq 0:6c56fb4bc5f0 485
nexpaq 0:6c56fb4bc5f0 486 int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 487 {
nexpaq 0:6c56fb4bc5f0 488 int ret = 0;
nexpaq 0:6c56fb4bc5f0 489 unsigned char tmp[64];
nexpaq 0:6c56fb4bc5f0 490 unsigned char keyblk[256];
nexpaq 0:6c56fb4bc5f0 491 unsigned char *key1;
nexpaq 0:6c56fb4bc5f0 492 unsigned char *key2;
nexpaq 0:6c56fb4bc5f0 493 unsigned char *mac_enc;
nexpaq 0:6c56fb4bc5f0 494 unsigned char *mac_dec;
nexpaq 0:6c56fb4bc5f0 495 size_t iv_copy_len;
nexpaq 0:6c56fb4bc5f0 496 const mbedtls_cipher_info_t *cipher_info;
nexpaq 0:6c56fb4bc5f0 497 const mbedtls_md_info_t *md_info;
nexpaq 0:6c56fb4bc5f0 498
nexpaq 0:6c56fb4bc5f0 499 mbedtls_ssl_session *session = ssl->session_negotiate;
nexpaq 0:6c56fb4bc5f0 500 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
nexpaq 0:6c56fb4bc5f0 501 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
nexpaq 0:6c56fb4bc5f0 502
nexpaq 0:6c56fb4bc5f0 503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
nexpaq 0:6c56fb4bc5f0 504
nexpaq 0:6c56fb4bc5f0 505 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
nexpaq 0:6c56fb4bc5f0 506 if( cipher_info == NULL )
nexpaq 0:6c56fb4bc5f0 507 {
nexpaq 0:6c56fb4bc5f0 508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
nexpaq 0:6c56fb4bc5f0 509 transform->ciphersuite_info->cipher ) );
nexpaq 0:6c56fb4bc5f0 510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 511 }
nexpaq 0:6c56fb4bc5f0 512
nexpaq 0:6c56fb4bc5f0 513 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
nexpaq 0:6c56fb4bc5f0 514 if( md_info == NULL )
nexpaq 0:6c56fb4bc5f0 515 {
nexpaq 0:6c56fb4bc5f0 516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
nexpaq 0:6c56fb4bc5f0 517 transform->ciphersuite_info->mac ) );
nexpaq 0:6c56fb4bc5f0 518 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 519 }
nexpaq 0:6c56fb4bc5f0 520
nexpaq 0:6c56fb4bc5f0 521 /*
nexpaq 0:6c56fb4bc5f0 522 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
nexpaq 0:6c56fb4bc5f0 523 */
nexpaq 0:6c56fb4bc5f0 524 #if defined(MBEDTLS_SSL_PROTO_SSL3)
nexpaq 0:6c56fb4bc5f0 525 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
nexpaq 0:6c56fb4bc5f0 526 {
nexpaq 0:6c56fb4bc5f0 527 handshake->tls_prf = ssl3_prf;
nexpaq 0:6c56fb4bc5f0 528 handshake->calc_verify = ssl_calc_verify_ssl;
nexpaq 0:6c56fb4bc5f0 529 handshake->calc_finished = ssl_calc_finished_ssl;
nexpaq 0:6c56fb4bc5f0 530 }
nexpaq 0:6c56fb4bc5f0 531 else
nexpaq 0:6c56fb4bc5f0 532 #endif
nexpaq 0:6c56fb4bc5f0 533 #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
nexpaq 0:6c56fb4bc5f0 534 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
nexpaq 0:6c56fb4bc5f0 535 {
nexpaq 0:6c56fb4bc5f0 536 handshake->tls_prf = tls1_prf;
nexpaq 0:6c56fb4bc5f0 537 handshake->calc_verify = ssl_calc_verify_tls;
nexpaq 0:6c56fb4bc5f0 538 handshake->calc_finished = ssl_calc_finished_tls;
nexpaq 0:6c56fb4bc5f0 539 }
nexpaq 0:6c56fb4bc5f0 540 else
nexpaq 0:6c56fb4bc5f0 541 #endif
nexpaq 0:6c56fb4bc5f0 542 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
nexpaq 0:6c56fb4bc5f0 543 #if defined(MBEDTLS_SHA512_C)
nexpaq 0:6c56fb4bc5f0 544 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
nexpaq 0:6c56fb4bc5f0 545 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
nexpaq 0:6c56fb4bc5f0 546 {
nexpaq 0:6c56fb4bc5f0 547 handshake->tls_prf = tls_prf_sha384;
nexpaq 0:6c56fb4bc5f0 548 handshake->calc_verify = ssl_calc_verify_tls_sha384;
nexpaq 0:6c56fb4bc5f0 549 handshake->calc_finished = ssl_calc_finished_tls_sha384;
nexpaq 0:6c56fb4bc5f0 550 }
nexpaq 0:6c56fb4bc5f0 551 else
nexpaq 0:6c56fb4bc5f0 552 #endif
nexpaq 0:6c56fb4bc5f0 553 #if defined(MBEDTLS_SHA256_C)
nexpaq 0:6c56fb4bc5f0 554 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
nexpaq 0:6c56fb4bc5f0 555 {
nexpaq 0:6c56fb4bc5f0 556 handshake->tls_prf = tls_prf_sha256;
nexpaq 0:6c56fb4bc5f0 557 handshake->calc_verify = ssl_calc_verify_tls_sha256;
nexpaq 0:6c56fb4bc5f0 558 handshake->calc_finished = ssl_calc_finished_tls_sha256;
nexpaq 0:6c56fb4bc5f0 559 }
nexpaq 0:6c56fb4bc5f0 560 else
nexpaq 0:6c56fb4bc5f0 561 #endif
nexpaq 0:6c56fb4bc5f0 562 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
nexpaq 0:6c56fb4bc5f0 563 {
nexpaq 0:6c56fb4bc5f0 564 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 565 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 566 }
nexpaq 0:6c56fb4bc5f0 567
nexpaq 0:6c56fb4bc5f0 568 /*
nexpaq 0:6c56fb4bc5f0 569 * SSLv3:
nexpaq 0:6c56fb4bc5f0 570 * master =
nexpaq 0:6c56fb4bc5f0 571 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
nexpaq 0:6c56fb4bc5f0 572 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
nexpaq 0:6c56fb4bc5f0 573 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
nexpaq 0:6c56fb4bc5f0 574 *
nexpaq 0:6c56fb4bc5f0 575 * TLSv1+:
nexpaq 0:6c56fb4bc5f0 576 * master = PRF( premaster, "master secret", randbytes )[0..47]
nexpaq 0:6c56fb4bc5f0 577 */
nexpaq 0:6c56fb4bc5f0 578 if( handshake->resume == 0 )
nexpaq 0:6c56fb4bc5f0 579 {
nexpaq 0:6c56fb4bc5f0 580 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
nexpaq 0:6c56fb4bc5f0 581 handshake->pmslen );
nexpaq 0:6c56fb4bc5f0 582
nexpaq 0:6c56fb4bc5f0 583 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
nexpaq 0:6c56fb4bc5f0 584 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
nexpaq 0:6c56fb4bc5f0 585 {
nexpaq 0:6c56fb4bc5f0 586 unsigned char session_hash[48];
nexpaq 0:6c56fb4bc5f0 587 size_t hash_len;
nexpaq 0:6c56fb4bc5f0 588
nexpaq 0:6c56fb4bc5f0 589 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
nexpaq 0:6c56fb4bc5f0 590
nexpaq 0:6c56fb4bc5f0 591 ssl->handshake->calc_verify( ssl, session_hash );
nexpaq 0:6c56fb4bc5f0 592
nexpaq 0:6c56fb4bc5f0 593 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
nexpaq 0:6c56fb4bc5f0 594 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
nexpaq 0:6c56fb4bc5f0 595 {
nexpaq 0:6c56fb4bc5f0 596 #if defined(MBEDTLS_SHA512_C)
nexpaq 0:6c56fb4bc5f0 597 if( ssl->transform_negotiate->ciphersuite_info->mac ==
nexpaq 0:6c56fb4bc5f0 598 MBEDTLS_MD_SHA384 )
nexpaq 0:6c56fb4bc5f0 599 {
nexpaq 0:6c56fb4bc5f0 600 hash_len = 48;
nexpaq 0:6c56fb4bc5f0 601 }
nexpaq 0:6c56fb4bc5f0 602 else
nexpaq 0:6c56fb4bc5f0 603 #endif
nexpaq 0:6c56fb4bc5f0 604 hash_len = 32;
nexpaq 0:6c56fb4bc5f0 605 }
nexpaq 0:6c56fb4bc5f0 606 else
nexpaq 0:6c56fb4bc5f0 607 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
nexpaq 0:6c56fb4bc5f0 608 hash_len = 36;
nexpaq 0:6c56fb4bc5f0 609
nexpaq 0:6c56fb4bc5f0 610 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
nexpaq 0:6c56fb4bc5f0 611
nexpaq 0:6c56fb4bc5f0 612 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
nexpaq 0:6c56fb4bc5f0 613 "extended master secret",
nexpaq 0:6c56fb4bc5f0 614 session_hash, hash_len,
nexpaq 0:6c56fb4bc5f0 615 session->master, 48 );
nexpaq 0:6c56fb4bc5f0 616 if( ret != 0 )
nexpaq 0:6c56fb4bc5f0 617 {
nexpaq 0:6c56fb4bc5f0 618 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
nexpaq 0:6c56fb4bc5f0 619 return( ret );
nexpaq 0:6c56fb4bc5f0 620 }
nexpaq 0:6c56fb4bc5f0 621
nexpaq 0:6c56fb4bc5f0 622 }
nexpaq 0:6c56fb4bc5f0 623 else
nexpaq 0:6c56fb4bc5f0 624 #endif
nexpaq 0:6c56fb4bc5f0 625 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
nexpaq 0:6c56fb4bc5f0 626 "master secret",
nexpaq 0:6c56fb4bc5f0 627 handshake->randbytes, 64,
nexpaq 0:6c56fb4bc5f0 628 session->master, 48 );
nexpaq 0:6c56fb4bc5f0 629 if( ret != 0 )
nexpaq 0:6c56fb4bc5f0 630 {
nexpaq 0:6c56fb4bc5f0 631 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
nexpaq 0:6c56fb4bc5f0 632 return( ret );
nexpaq 0:6c56fb4bc5f0 633 }
nexpaq 0:6c56fb4bc5f0 634
nexpaq 0:6c56fb4bc5f0 635 mbedtls_zeroize( handshake->premaster, sizeof(handshake->premaster) );
nexpaq 0:6c56fb4bc5f0 636 }
nexpaq 0:6c56fb4bc5f0 637 else
nexpaq 0:6c56fb4bc5f0 638 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
nexpaq 0:6c56fb4bc5f0 639
nexpaq 0:6c56fb4bc5f0 640 /*
nexpaq 0:6c56fb4bc5f0 641 * Swap the client and server random values.
nexpaq 0:6c56fb4bc5f0 642 */
nexpaq 0:6c56fb4bc5f0 643 memcpy( tmp, handshake->randbytes, 64 );
nexpaq 0:6c56fb4bc5f0 644 memcpy( handshake->randbytes, tmp + 32, 32 );
nexpaq 0:6c56fb4bc5f0 645 memcpy( handshake->randbytes + 32, tmp, 32 );
nexpaq 0:6c56fb4bc5f0 646 mbedtls_zeroize( tmp, sizeof( tmp ) );
nexpaq 0:6c56fb4bc5f0 647
nexpaq 0:6c56fb4bc5f0 648 /*
nexpaq 0:6c56fb4bc5f0 649 * SSLv3:
nexpaq 0:6c56fb4bc5f0 650 * key block =
nexpaq 0:6c56fb4bc5f0 651 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
nexpaq 0:6c56fb4bc5f0 652 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
nexpaq 0:6c56fb4bc5f0 653 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
nexpaq 0:6c56fb4bc5f0 654 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
nexpaq 0:6c56fb4bc5f0 655 * ...
nexpaq 0:6c56fb4bc5f0 656 *
nexpaq 0:6c56fb4bc5f0 657 * TLSv1:
nexpaq 0:6c56fb4bc5f0 658 * key block = PRF( master, "key expansion", randbytes )
nexpaq 0:6c56fb4bc5f0 659 */
nexpaq 0:6c56fb4bc5f0 660 ret = handshake->tls_prf( session->master, 48, "key expansion",
nexpaq 0:6c56fb4bc5f0 661 handshake->randbytes, 64, keyblk, 256 );
nexpaq 0:6c56fb4bc5f0 662 if( ret != 0 )
nexpaq 0:6c56fb4bc5f0 663 {
nexpaq 0:6c56fb4bc5f0 664 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
nexpaq 0:6c56fb4bc5f0 665 return( ret );
nexpaq 0:6c56fb4bc5f0 666 }
nexpaq 0:6c56fb4bc5f0 667
nexpaq 0:6c56fb4bc5f0 668 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
nexpaq 0:6c56fb4bc5f0 669 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
nexpaq 0:6c56fb4bc5f0 670 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
nexpaq 0:6c56fb4bc5f0 671 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
nexpaq 0:6c56fb4bc5f0 672 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
nexpaq 0:6c56fb4bc5f0 673
nexpaq 0:6c56fb4bc5f0 674 mbedtls_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
nexpaq 0:6c56fb4bc5f0 675
nexpaq 0:6c56fb4bc5f0 676 /*
nexpaq 0:6c56fb4bc5f0 677 * Determine the appropriate key, IV and MAC length.
nexpaq 0:6c56fb4bc5f0 678 */
nexpaq 0:6c56fb4bc5f0 679
nexpaq 0:6c56fb4bc5f0 680 transform->keylen = cipher_info->key_bitlen / 8;
nexpaq 0:6c56fb4bc5f0 681
nexpaq 0:6c56fb4bc5f0 682 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
nexpaq 0:6c56fb4bc5f0 683 cipher_info->mode == MBEDTLS_MODE_CCM )
nexpaq 0:6c56fb4bc5f0 684 {
nexpaq 0:6c56fb4bc5f0 685 transform->maclen = 0;
nexpaq 0:6c56fb4bc5f0 686
nexpaq 0:6c56fb4bc5f0 687 transform->ivlen = 12;
nexpaq 0:6c56fb4bc5f0 688 transform->fixed_ivlen = 4;
nexpaq 0:6c56fb4bc5f0 689
nexpaq 0:6c56fb4bc5f0 690 /* Minimum length is expicit IV + tag */
nexpaq 0:6c56fb4bc5f0 691 transform->minlen = transform->ivlen - transform->fixed_ivlen
nexpaq 0:6c56fb4bc5f0 692 + ( transform->ciphersuite_info->flags &
nexpaq 0:6c56fb4bc5f0 693 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
nexpaq 0:6c56fb4bc5f0 694 }
nexpaq 0:6c56fb4bc5f0 695 else
nexpaq 0:6c56fb4bc5f0 696 {
nexpaq 0:6c56fb4bc5f0 697 /* Initialize HMAC contexts */
nexpaq 0:6c56fb4bc5f0 698 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
nexpaq 0:6c56fb4bc5f0 699 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 700 {
nexpaq 0:6c56fb4bc5f0 701 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
nexpaq 0:6c56fb4bc5f0 702 return( ret );
nexpaq 0:6c56fb4bc5f0 703 }
nexpaq 0:6c56fb4bc5f0 704
nexpaq 0:6c56fb4bc5f0 705 /* Get MAC length */
nexpaq 0:6c56fb4bc5f0 706 transform->maclen = mbedtls_md_get_size( md_info );
nexpaq 0:6c56fb4bc5f0 707
nexpaq 0:6c56fb4bc5f0 708 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
nexpaq 0:6c56fb4bc5f0 709 /*
nexpaq 0:6c56fb4bc5f0 710 * If HMAC is to be truncated, we shall keep the leftmost bytes,
nexpaq 0:6c56fb4bc5f0 711 * (rfc 6066 page 13 or rfc 2104 section 4),
nexpaq 0:6c56fb4bc5f0 712 * so we only need to adjust the length here.
nexpaq 0:6c56fb4bc5f0 713 */
nexpaq 0:6c56fb4bc5f0 714 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
nexpaq 0:6c56fb4bc5f0 715 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
nexpaq 0:6c56fb4bc5f0 716 #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
nexpaq 0:6c56fb4bc5f0 717
nexpaq 0:6c56fb4bc5f0 718 /* IV length */
nexpaq 0:6c56fb4bc5f0 719 transform->ivlen = cipher_info->iv_size;
nexpaq 0:6c56fb4bc5f0 720
nexpaq 0:6c56fb4bc5f0 721 /* Minimum length */
nexpaq 0:6c56fb4bc5f0 722 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
nexpaq 0:6c56fb4bc5f0 723 transform->minlen = transform->maclen;
nexpaq 0:6c56fb4bc5f0 724 else
nexpaq 0:6c56fb4bc5f0 725 {
nexpaq 0:6c56fb4bc5f0 726 /*
nexpaq 0:6c56fb4bc5f0 727 * GenericBlockCipher:
nexpaq 0:6c56fb4bc5f0 728 * 1. if EtM is in use: one block plus MAC
nexpaq 0:6c56fb4bc5f0 729 * otherwise: * first multiple of blocklen greater than maclen
nexpaq 0:6c56fb4bc5f0 730 * 2. IV except for SSL3 and TLS 1.0
nexpaq 0:6c56fb4bc5f0 731 */
nexpaq 0:6c56fb4bc5f0 732 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
nexpaq 0:6c56fb4bc5f0 733 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
nexpaq 0:6c56fb4bc5f0 734 {
nexpaq 0:6c56fb4bc5f0 735 transform->minlen = transform->maclen
nexpaq 0:6c56fb4bc5f0 736 + cipher_info->block_size;
nexpaq 0:6c56fb4bc5f0 737 }
nexpaq 0:6c56fb4bc5f0 738 else
nexpaq 0:6c56fb4bc5f0 739 #endif
nexpaq 0:6c56fb4bc5f0 740 {
nexpaq 0:6c56fb4bc5f0 741 transform->minlen = transform->maclen
nexpaq 0:6c56fb4bc5f0 742 + cipher_info->block_size
nexpaq 0:6c56fb4bc5f0 743 - transform->maclen % cipher_info->block_size;
nexpaq 0:6c56fb4bc5f0 744 }
nexpaq 0:6c56fb4bc5f0 745
nexpaq 0:6c56fb4bc5f0 746 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
nexpaq 0:6c56fb4bc5f0 747 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
nexpaq 0:6c56fb4bc5f0 748 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
nexpaq 0:6c56fb4bc5f0 749 ; /* No need to adjust minlen */
nexpaq 0:6c56fb4bc5f0 750 else
nexpaq 0:6c56fb4bc5f0 751 #endif
nexpaq 0:6c56fb4bc5f0 752 #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
nexpaq 0:6c56fb4bc5f0 753 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
nexpaq 0:6c56fb4bc5f0 754 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
nexpaq 0:6c56fb4bc5f0 755 {
nexpaq 0:6c56fb4bc5f0 756 transform->minlen += transform->ivlen;
nexpaq 0:6c56fb4bc5f0 757 }
nexpaq 0:6c56fb4bc5f0 758 else
nexpaq 0:6c56fb4bc5f0 759 #endif
nexpaq 0:6c56fb4bc5f0 760 {
nexpaq 0:6c56fb4bc5f0 761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 762 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 763 }
nexpaq 0:6c56fb4bc5f0 764 }
nexpaq 0:6c56fb4bc5f0 765 }
nexpaq 0:6c56fb4bc5f0 766
nexpaq 0:6c56fb4bc5f0 767 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
nexpaq 0:6c56fb4bc5f0 768 transform->keylen, transform->minlen, transform->ivlen,
nexpaq 0:6c56fb4bc5f0 769 transform->maclen ) );
nexpaq 0:6c56fb4bc5f0 770
nexpaq 0:6c56fb4bc5f0 771 /*
nexpaq 0:6c56fb4bc5f0 772 * Finally setup the cipher contexts, IVs and MAC secrets.
nexpaq 0:6c56fb4bc5f0 773 */
nexpaq 0:6c56fb4bc5f0 774 #if defined(MBEDTLS_SSL_CLI_C)
nexpaq 0:6c56fb4bc5f0 775 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
nexpaq 0:6c56fb4bc5f0 776 {
nexpaq 0:6c56fb4bc5f0 777 key1 = keyblk + transform->maclen * 2;
nexpaq 0:6c56fb4bc5f0 778 key2 = keyblk + transform->maclen * 2 + transform->keylen;
nexpaq 0:6c56fb4bc5f0 779
nexpaq 0:6c56fb4bc5f0 780 mac_enc = keyblk;
nexpaq 0:6c56fb4bc5f0 781 mac_dec = keyblk + transform->maclen;
nexpaq 0:6c56fb4bc5f0 782
nexpaq 0:6c56fb4bc5f0 783 /*
nexpaq 0:6c56fb4bc5f0 784 * This is not used in TLS v1.1.
nexpaq 0:6c56fb4bc5f0 785 */
nexpaq 0:6c56fb4bc5f0 786 iv_copy_len = ( transform->fixed_ivlen ) ?
nexpaq 0:6c56fb4bc5f0 787 transform->fixed_ivlen : transform->ivlen;
nexpaq 0:6c56fb4bc5f0 788 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
nexpaq 0:6c56fb4bc5f0 789 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
nexpaq 0:6c56fb4bc5f0 790 iv_copy_len );
nexpaq 0:6c56fb4bc5f0 791 }
nexpaq 0:6c56fb4bc5f0 792 else
nexpaq 0:6c56fb4bc5f0 793 #endif /* MBEDTLS_SSL_CLI_C */
nexpaq 0:6c56fb4bc5f0 794 #if defined(MBEDTLS_SSL_SRV_C)
nexpaq 0:6c56fb4bc5f0 795 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
nexpaq 0:6c56fb4bc5f0 796 {
nexpaq 0:6c56fb4bc5f0 797 key1 = keyblk + transform->maclen * 2 + transform->keylen;
nexpaq 0:6c56fb4bc5f0 798 key2 = keyblk + transform->maclen * 2;
nexpaq 0:6c56fb4bc5f0 799
nexpaq 0:6c56fb4bc5f0 800 mac_enc = keyblk + transform->maclen;
nexpaq 0:6c56fb4bc5f0 801 mac_dec = keyblk;
nexpaq 0:6c56fb4bc5f0 802
nexpaq 0:6c56fb4bc5f0 803 /*
nexpaq 0:6c56fb4bc5f0 804 * This is not used in TLS v1.1.
nexpaq 0:6c56fb4bc5f0 805 */
nexpaq 0:6c56fb4bc5f0 806 iv_copy_len = ( transform->fixed_ivlen ) ?
nexpaq 0:6c56fb4bc5f0 807 transform->fixed_ivlen : transform->ivlen;
nexpaq 0:6c56fb4bc5f0 808 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
nexpaq 0:6c56fb4bc5f0 809 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
nexpaq 0:6c56fb4bc5f0 810 iv_copy_len );
nexpaq 0:6c56fb4bc5f0 811 }
nexpaq 0:6c56fb4bc5f0 812 else
nexpaq 0:6c56fb4bc5f0 813 #endif /* MBEDTLS_SSL_SRV_C */
nexpaq 0:6c56fb4bc5f0 814 {
nexpaq 0:6c56fb4bc5f0 815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 816 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 817 }
nexpaq 0:6c56fb4bc5f0 818
nexpaq 0:6c56fb4bc5f0 819 #if defined(MBEDTLS_SSL_PROTO_SSL3)
nexpaq 0:6c56fb4bc5f0 820 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
nexpaq 0:6c56fb4bc5f0 821 {
nexpaq 0:6c56fb4bc5f0 822 if( transform->maclen > sizeof transform->mac_enc )
nexpaq 0:6c56fb4bc5f0 823 {
nexpaq 0:6c56fb4bc5f0 824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 825 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 826 }
nexpaq 0:6c56fb4bc5f0 827
nexpaq 0:6c56fb4bc5f0 828 memcpy( transform->mac_enc, mac_enc, transform->maclen );
nexpaq 0:6c56fb4bc5f0 829 memcpy( transform->mac_dec, mac_dec, transform->maclen );
nexpaq 0:6c56fb4bc5f0 830 }
nexpaq 0:6c56fb4bc5f0 831 else
nexpaq 0:6c56fb4bc5f0 832 #endif /* MBEDTLS_SSL_PROTO_SSL3 */
nexpaq 0:6c56fb4bc5f0 833 #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
nexpaq 0:6c56fb4bc5f0 834 defined(MBEDTLS_SSL_PROTO_TLS1_2)
nexpaq 0:6c56fb4bc5f0 835 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
nexpaq 0:6c56fb4bc5f0 836 {
nexpaq 0:6c56fb4bc5f0 837 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
nexpaq 0:6c56fb4bc5f0 838 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
nexpaq 0:6c56fb4bc5f0 839 }
nexpaq 0:6c56fb4bc5f0 840 else
nexpaq 0:6c56fb4bc5f0 841 #endif
nexpaq 0:6c56fb4bc5f0 842 {
nexpaq 0:6c56fb4bc5f0 843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 844 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 845 }
nexpaq 0:6c56fb4bc5f0 846
nexpaq 0:6c56fb4bc5f0 847 #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
nexpaq 0:6c56fb4bc5f0 848 if( mbedtls_ssl_hw_record_init != NULL )
nexpaq 0:6c56fb4bc5f0 849 {
nexpaq 0:6c56fb4bc5f0 850 int ret = 0;
nexpaq 0:6c56fb4bc5f0 851
nexpaq 0:6c56fb4bc5f0 852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
nexpaq 0:6c56fb4bc5f0 853
nexpaq 0:6c56fb4bc5f0 854 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
nexpaq 0:6c56fb4bc5f0 855 transform->iv_enc, transform->iv_dec,
nexpaq 0:6c56fb4bc5f0 856 iv_copy_len,
nexpaq 0:6c56fb4bc5f0 857 mac_enc, mac_dec,
nexpaq 0:6c56fb4bc5f0 858 transform->maclen ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 859 {
nexpaq 0:6c56fb4bc5f0 860 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
nexpaq 0:6c56fb4bc5f0 861 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
nexpaq 0:6c56fb4bc5f0 862 }
nexpaq 0:6c56fb4bc5f0 863 }
nexpaq 0:6c56fb4bc5f0 864 #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
nexpaq 0:6c56fb4bc5f0 865
nexpaq 0:6c56fb4bc5f0 866 #if defined(MBEDTLS_SSL_EXPORT_KEYS)
nexpaq 0:6c56fb4bc5f0 867 if( ssl->conf->f_export_keys != NULL )
nexpaq 0:6c56fb4bc5f0 868 {
nexpaq 0:6c56fb4bc5f0 869 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
nexpaq 0:6c56fb4bc5f0 870 session->master, keyblk,
nexpaq 0:6c56fb4bc5f0 871 transform->maclen, transform->keylen,
nexpaq 0:6c56fb4bc5f0 872 iv_copy_len );
nexpaq 0:6c56fb4bc5f0 873 }
nexpaq 0:6c56fb4bc5f0 874 #endif
nexpaq 0:6c56fb4bc5f0 875
nexpaq 0:6c56fb4bc5f0 876 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
nexpaq 0:6c56fb4bc5f0 877 cipher_info ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 878 {
nexpaq 0:6c56fb4bc5f0 879 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
nexpaq 0:6c56fb4bc5f0 880 return( ret );
nexpaq 0:6c56fb4bc5f0 881 }
nexpaq 0:6c56fb4bc5f0 882
nexpaq 0:6c56fb4bc5f0 883 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
nexpaq 0:6c56fb4bc5f0 884 cipher_info ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 885 {
nexpaq 0:6c56fb4bc5f0 886 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
nexpaq 0:6c56fb4bc5f0 887 return( ret );
nexpaq 0:6c56fb4bc5f0 888 }
nexpaq 0:6c56fb4bc5f0 889
nexpaq 0:6c56fb4bc5f0 890 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
nexpaq 0:6c56fb4bc5f0 891 cipher_info->key_bitlen,
nexpaq 0:6c56fb4bc5f0 892 MBEDTLS_ENCRYPT ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 893 {
nexpaq 0:6c56fb4bc5f0 894 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
nexpaq 0:6c56fb4bc5f0 895 return( ret );
nexpaq 0:6c56fb4bc5f0 896 }
nexpaq 0:6c56fb4bc5f0 897
nexpaq 0:6c56fb4bc5f0 898 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
nexpaq 0:6c56fb4bc5f0 899 cipher_info->key_bitlen,
nexpaq 0:6c56fb4bc5f0 900 MBEDTLS_DECRYPT ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 901 {
nexpaq 0:6c56fb4bc5f0 902 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
nexpaq 0:6c56fb4bc5f0 903 return( ret );
nexpaq 0:6c56fb4bc5f0 904 }
nexpaq 0:6c56fb4bc5f0 905
nexpaq 0:6c56fb4bc5f0 906 #if defined(MBEDTLS_CIPHER_MODE_CBC)
nexpaq 0:6c56fb4bc5f0 907 if( cipher_info->mode == MBEDTLS_MODE_CBC )
nexpaq 0:6c56fb4bc5f0 908 {
nexpaq 0:6c56fb4bc5f0 909 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
nexpaq 0:6c56fb4bc5f0 910 MBEDTLS_PADDING_NONE ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 911 {
nexpaq 0:6c56fb4bc5f0 912 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
nexpaq 0:6c56fb4bc5f0 913 return( ret );
nexpaq 0:6c56fb4bc5f0 914 }
nexpaq 0:6c56fb4bc5f0 915
nexpaq 0:6c56fb4bc5f0 916 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
nexpaq 0:6c56fb4bc5f0 917 MBEDTLS_PADDING_NONE ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 918 {
nexpaq 0:6c56fb4bc5f0 919 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
nexpaq 0:6c56fb4bc5f0 920 return( ret );
nexpaq 0:6c56fb4bc5f0 921 }
nexpaq 0:6c56fb4bc5f0 922 }
nexpaq 0:6c56fb4bc5f0 923 #endif /* MBEDTLS_CIPHER_MODE_CBC */
nexpaq 0:6c56fb4bc5f0 924
nexpaq 0:6c56fb4bc5f0 925 mbedtls_zeroize( keyblk, sizeof( keyblk ) );
nexpaq 0:6c56fb4bc5f0 926
nexpaq 0:6c56fb4bc5f0 927 #if defined(MBEDTLS_ZLIB_SUPPORT)
nexpaq 0:6c56fb4bc5f0 928 // Initialize compression
nexpaq 0:6c56fb4bc5f0 929 //
nexpaq 0:6c56fb4bc5f0 930 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
nexpaq 0:6c56fb4bc5f0 931 {
nexpaq 0:6c56fb4bc5f0 932 if( ssl->compress_buf == NULL )
nexpaq 0:6c56fb4bc5f0 933 {
nexpaq 0:6c56fb4bc5f0 934 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
nexpaq 0:6c56fb4bc5f0 935 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_BUFFER_LEN );
nexpaq 0:6c56fb4bc5f0 936 if( ssl->compress_buf == NULL )
nexpaq 0:6c56fb4bc5f0 937 {
nexpaq 0:6c56fb4bc5f0 938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
nexpaq 0:6c56fb4bc5f0 939 MBEDTLS_SSL_BUFFER_LEN ) );
nexpaq 0:6c56fb4bc5f0 940 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
nexpaq 0:6c56fb4bc5f0 941 }
nexpaq 0:6c56fb4bc5f0 942 }
nexpaq 0:6c56fb4bc5f0 943
nexpaq 0:6c56fb4bc5f0 944 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
nexpaq 0:6c56fb4bc5f0 945
nexpaq 0:6c56fb4bc5f0 946 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
nexpaq 0:6c56fb4bc5f0 947 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
nexpaq 0:6c56fb4bc5f0 948
nexpaq 0:6c56fb4bc5f0 949 if( deflateInit( &transform->ctx_deflate,
nexpaq 0:6c56fb4bc5f0 950 Z_DEFAULT_COMPRESSION ) != Z_OK ||
nexpaq 0:6c56fb4bc5f0 951 inflateInit( &transform->ctx_inflate ) != Z_OK )
nexpaq 0:6c56fb4bc5f0 952 {
nexpaq 0:6c56fb4bc5f0 953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
nexpaq 0:6c56fb4bc5f0 954 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
nexpaq 0:6c56fb4bc5f0 955 }
nexpaq 0:6c56fb4bc5f0 956 }
nexpaq 0:6c56fb4bc5f0 957 #endif /* MBEDTLS_ZLIB_SUPPORT */
nexpaq 0:6c56fb4bc5f0 958
nexpaq 0:6c56fb4bc5f0 959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
nexpaq 0:6c56fb4bc5f0 960
nexpaq 0:6c56fb4bc5f0 961 return( 0 );
nexpaq 0:6c56fb4bc5f0 962 }
nexpaq 0:6c56fb4bc5f0 963
nexpaq 0:6c56fb4bc5f0 964 #if defined(MBEDTLS_SSL_PROTO_SSL3)
nexpaq 0:6c56fb4bc5f0 965 void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
nexpaq 0:6c56fb4bc5f0 966 {
nexpaq 0:6c56fb4bc5f0 967 mbedtls_md5_context md5;
nexpaq 0:6c56fb4bc5f0 968 mbedtls_sha1_context sha1;
nexpaq 0:6c56fb4bc5f0 969 unsigned char pad_1[48];
nexpaq 0:6c56fb4bc5f0 970 unsigned char pad_2[48];
nexpaq 0:6c56fb4bc5f0 971
nexpaq 0:6c56fb4bc5f0 972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
nexpaq 0:6c56fb4bc5f0 973
nexpaq 0:6c56fb4bc5f0 974 mbedtls_md5_init( &md5 );
nexpaq 0:6c56fb4bc5f0 975 mbedtls_sha1_init( &sha1 );
nexpaq 0:6c56fb4bc5f0 976
nexpaq 0:6c56fb4bc5f0 977 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
nexpaq 0:6c56fb4bc5f0 978 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
nexpaq 0:6c56fb4bc5f0 979
nexpaq 0:6c56fb4bc5f0 980 memset( pad_1, 0x36, 48 );
nexpaq 0:6c56fb4bc5f0 981 memset( pad_2, 0x5C, 48 );
nexpaq 0:6c56fb4bc5f0 982
nexpaq 0:6c56fb4bc5f0 983 mbedtls_md5_update( &md5, ssl->session_negotiate->master, 48 );
nexpaq 0:6c56fb4bc5f0 984 mbedtls_md5_update( &md5, pad_1, 48 );
nexpaq 0:6c56fb4bc5f0 985 mbedtls_md5_finish( &md5, hash );
nexpaq 0:6c56fb4bc5f0 986
nexpaq 0:6c56fb4bc5f0 987 mbedtls_md5_starts( &md5 );
nexpaq 0:6c56fb4bc5f0 988 mbedtls_md5_update( &md5, ssl->session_negotiate->master, 48 );
nexpaq 0:6c56fb4bc5f0 989 mbedtls_md5_update( &md5, pad_2, 48 );
nexpaq 0:6c56fb4bc5f0 990 mbedtls_md5_update( &md5, hash, 16 );
nexpaq 0:6c56fb4bc5f0 991 mbedtls_md5_finish( &md5, hash );
nexpaq 0:6c56fb4bc5f0 992
nexpaq 0:6c56fb4bc5f0 993 mbedtls_sha1_update( &sha1, ssl->session_negotiate->master, 48 );
nexpaq 0:6c56fb4bc5f0 994 mbedtls_sha1_update( &sha1, pad_1, 40 );
nexpaq 0:6c56fb4bc5f0 995 mbedtls_sha1_finish( &sha1, hash + 16 );
nexpaq 0:6c56fb4bc5f0 996
nexpaq 0:6c56fb4bc5f0 997 mbedtls_sha1_starts( &sha1 );
nexpaq 0:6c56fb4bc5f0 998 mbedtls_sha1_update( &sha1, ssl->session_negotiate->master, 48 );
nexpaq 0:6c56fb4bc5f0 999 mbedtls_sha1_update( &sha1, pad_2, 40 );
nexpaq 0:6c56fb4bc5f0 1000 mbedtls_sha1_update( &sha1, hash + 16, 20 );
nexpaq 0:6c56fb4bc5f0 1001 mbedtls_sha1_finish( &sha1, hash + 16 );
nexpaq 0:6c56fb4bc5f0 1002
nexpaq 0:6c56fb4bc5f0 1003 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
nexpaq 0:6c56fb4bc5f0 1004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
nexpaq 0:6c56fb4bc5f0 1005
nexpaq 0:6c56fb4bc5f0 1006 mbedtls_md5_free( &md5 );
nexpaq 0:6c56fb4bc5f0 1007 mbedtls_sha1_free( &sha1 );
nexpaq 0:6c56fb4bc5f0 1008
nexpaq 0:6c56fb4bc5f0 1009 return;
nexpaq 0:6c56fb4bc5f0 1010 }
nexpaq 0:6c56fb4bc5f0 1011 #endif /* MBEDTLS_SSL_PROTO_SSL3 */
nexpaq 0:6c56fb4bc5f0 1012
nexpaq 0:6c56fb4bc5f0 1013 #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
nexpaq 0:6c56fb4bc5f0 1014 void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
nexpaq 0:6c56fb4bc5f0 1015 {
nexpaq 0:6c56fb4bc5f0 1016 mbedtls_md5_context md5;
nexpaq 0:6c56fb4bc5f0 1017 mbedtls_sha1_context sha1;
nexpaq 0:6c56fb4bc5f0 1018
nexpaq 0:6c56fb4bc5f0 1019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
nexpaq 0:6c56fb4bc5f0 1020
nexpaq 0:6c56fb4bc5f0 1021 mbedtls_md5_init( &md5 );
nexpaq 0:6c56fb4bc5f0 1022 mbedtls_sha1_init( &sha1 );
nexpaq 0:6c56fb4bc5f0 1023
nexpaq 0:6c56fb4bc5f0 1024 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
nexpaq 0:6c56fb4bc5f0 1025 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
nexpaq 0:6c56fb4bc5f0 1026
nexpaq 0:6c56fb4bc5f0 1027 mbedtls_md5_finish( &md5, hash );
nexpaq 0:6c56fb4bc5f0 1028 mbedtls_sha1_finish( &sha1, hash + 16 );
nexpaq 0:6c56fb4bc5f0 1029
nexpaq 0:6c56fb4bc5f0 1030 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
nexpaq 0:6c56fb4bc5f0 1031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
nexpaq 0:6c56fb4bc5f0 1032
nexpaq 0:6c56fb4bc5f0 1033 mbedtls_md5_free( &md5 );
nexpaq 0:6c56fb4bc5f0 1034 mbedtls_sha1_free( &sha1 );
nexpaq 0:6c56fb4bc5f0 1035
nexpaq 0:6c56fb4bc5f0 1036 return;
nexpaq 0:6c56fb4bc5f0 1037 }
nexpaq 0:6c56fb4bc5f0 1038 #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
nexpaq 0:6c56fb4bc5f0 1039
nexpaq 0:6c56fb4bc5f0 1040 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
nexpaq 0:6c56fb4bc5f0 1041 #if defined(MBEDTLS_SHA256_C)
nexpaq 0:6c56fb4bc5f0 1042 void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
nexpaq 0:6c56fb4bc5f0 1043 {
nexpaq 0:6c56fb4bc5f0 1044 mbedtls_sha256_context sha256;
nexpaq 0:6c56fb4bc5f0 1045
nexpaq 0:6c56fb4bc5f0 1046 mbedtls_sha256_init( &sha256 );
nexpaq 0:6c56fb4bc5f0 1047
nexpaq 0:6c56fb4bc5f0 1048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
nexpaq 0:6c56fb4bc5f0 1049
nexpaq 0:6c56fb4bc5f0 1050 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
nexpaq 0:6c56fb4bc5f0 1051 mbedtls_sha256_finish( &sha256, hash );
nexpaq 0:6c56fb4bc5f0 1052
nexpaq 0:6c56fb4bc5f0 1053 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
nexpaq 0:6c56fb4bc5f0 1054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
nexpaq 0:6c56fb4bc5f0 1055
nexpaq 0:6c56fb4bc5f0 1056 mbedtls_sha256_free( &sha256 );
nexpaq 0:6c56fb4bc5f0 1057
nexpaq 0:6c56fb4bc5f0 1058 return;
nexpaq 0:6c56fb4bc5f0 1059 }
nexpaq 0:6c56fb4bc5f0 1060 #endif /* MBEDTLS_SHA256_C */
nexpaq 0:6c56fb4bc5f0 1061
nexpaq 0:6c56fb4bc5f0 1062 #if defined(MBEDTLS_SHA512_C)
nexpaq 0:6c56fb4bc5f0 1063 void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
nexpaq 0:6c56fb4bc5f0 1064 {
nexpaq 0:6c56fb4bc5f0 1065 mbedtls_sha512_context sha512;
nexpaq 0:6c56fb4bc5f0 1066
nexpaq 0:6c56fb4bc5f0 1067 mbedtls_sha512_init( &sha512 );
nexpaq 0:6c56fb4bc5f0 1068
nexpaq 0:6c56fb4bc5f0 1069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
nexpaq 0:6c56fb4bc5f0 1070
nexpaq 0:6c56fb4bc5f0 1071 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
nexpaq 0:6c56fb4bc5f0 1072 mbedtls_sha512_finish( &sha512, hash );
nexpaq 0:6c56fb4bc5f0 1073
nexpaq 0:6c56fb4bc5f0 1074 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
nexpaq 0:6c56fb4bc5f0 1075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
nexpaq 0:6c56fb4bc5f0 1076
nexpaq 0:6c56fb4bc5f0 1077 mbedtls_sha512_free( &sha512 );
nexpaq 0:6c56fb4bc5f0 1078
nexpaq 0:6c56fb4bc5f0 1079 return;
nexpaq 0:6c56fb4bc5f0 1080 }
nexpaq 0:6c56fb4bc5f0 1081 #endif /* MBEDTLS_SHA512_C */
nexpaq 0:6c56fb4bc5f0 1082 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
nexpaq 0:6c56fb4bc5f0 1083
nexpaq 0:6c56fb4bc5f0 1084 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
nexpaq 0:6c56fb4bc5f0 1085 int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
nexpaq 0:6c56fb4bc5f0 1086 {
nexpaq 0:6c56fb4bc5f0 1087 unsigned char *p = ssl->handshake->premaster;
nexpaq 0:6c56fb4bc5f0 1088 unsigned char *end = p + sizeof( ssl->handshake->premaster );
nexpaq 0:6c56fb4bc5f0 1089 const unsigned char *psk = ssl->conf->psk;
nexpaq 0:6c56fb4bc5f0 1090 size_t psk_len = ssl->conf->psk_len;
nexpaq 0:6c56fb4bc5f0 1091
nexpaq 0:6c56fb4bc5f0 1092 /* If the psk callback was called, use its result */
nexpaq 0:6c56fb4bc5f0 1093 if( ssl->handshake->psk != NULL )
nexpaq 0:6c56fb4bc5f0 1094 {
nexpaq 0:6c56fb4bc5f0 1095 psk = ssl->handshake->psk;
nexpaq 0:6c56fb4bc5f0 1096 psk_len = ssl->handshake->psk_len;
nexpaq 0:6c56fb4bc5f0 1097 }
nexpaq 0:6c56fb4bc5f0 1098
nexpaq 0:6c56fb4bc5f0 1099 /*
nexpaq 0:6c56fb4bc5f0 1100 * PMS = struct {
nexpaq 0:6c56fb4bc5f0 1101 * opaque other_secret<0..2^16-1>;
nexpaq 0:6c56fb4bc5f0 1102 * opaque psk<0..2^16-1>;
nexpaq 0:6c56fb4bc5f0 1103 * };
nexpaq 0:6c56fb4bc5f0 1104 * with "other_secret" depending on the particular key exchange
nexpaq 0:6c56fb4bc5f0 1105 */
nexpaq 0:6c56fb4bc5f0 1106 #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
nexpaq 0:6c56fb4bc5f0 1107 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
nexpaq 0:6c56fb4bc5f0 1108 {
nexpaq 0:6c56fb4bc5f0 1109 if( end - p < 2 )
nexpaq 0:6c56fb4bc5f0 1110 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 1111
nexpaq 0:6c56fb4bc5f0 1112 *(p++) = (unsigned char)( psk_len >> 8 );
nexpaq 0:6c56fb4bc5f0 1113 *(p++) = (unsigned char)( psk_len );
nexpaq 0:6c56fb4bc5f0 1114
nexpaq 0:6c56fb4bc5f0 1115 if( end < p || (size_t)( end - p ) < psk_len )
nexpaq 0:6c56fb4bc5f0 1116 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 1117
nexpaq 0:6c56fb4bc5f0 1118 memset( p, 0, psk_len );
nexpaq 0:6c56fb4bc5f0 1119 p += psk_len;
nexpaq 0:6c56fb4bc5f0 1120 }
nexpaq 0:6c56fb4bc5f0 1121 else
nexpaq 0:6c56fb4bc5f0 1122 #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
nexpaq 0:6c56fb4bc5f0 1123 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
nexpaq 0:6c56fb4bc5f0 1124 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
nexpaq 0:6c56fb4bc5f0 1125 {
nexpaq 0:6c56fb4bc5f0 1126 /*
nexpaq 0:6c56fb4bc5f0 1127 * other_secret already set by the ClientKeyExchange message,
nexpaq 0:6c56fb4bc5f0 1128 * and is 48 bytes long
nexpaq 0:6c56fb4bc5f0 1129 */
nexpaq 0:6c56fb4bc5f0 1130 *p++ = 0;
nexpaq 0:6c56fb4bc5f0 1131 *p++ = 48;
nexpaq 0:6c56fb4bc5f0 1132 p += 48;
nexpaq 0:6c56fb4bc5f0 1133 }
nexpaq 0:6c56fb4bc5f0 1134 else
nexpaq 0:6c56fb4bc5f0 1135 #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
nexpaq 0:6c56fb4bc5f0 1136 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
nexpaq 0:6c56fb4bc5f0 1137 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
nexpaq 0:6c56fb4bc5f0 1138 {
nexpaq 0:6c56fb4bc5f0 1139 int ret;
nexpaq 0:6c56fb4bc5f0 1140 size_t len;
nexpaq 0:6c56fb4bc5f0 1141
nexpaq 0:6c56fb4bc5f0 1142 /* Write length only when we know the actual value */
nexpaq 0:6c56fb4bc5f0 1143 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
nexpaq 0:6c56fb4bc5f0 1144 p + 2, end - ( p + 2 ), &len,
nexpaq 0:6c56fb4bc5f0 1145 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 1146 {
nexpaq 0:6c56fb4bc5f0 1147 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
nexpaq 0:6c56fb4bc5f0 1148 return( ret );
nexpaq 0:6c56fb4bc5f0 1149 }
nexpaq 0:6c56fb4bc5f0 1150 *(p++) = (unsigned char)( len >> 8 );
nexpaq 0:6c56fb4bc5f0 1151 *(p++) = (unsigned char)( len );
nexpaq 0:6c56fb4bc5f0 1152 p += len;
nexpaq 0:6c56fb4bc5f0 1153
nexpaq 0:6c56fb4bc5f0 1154 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
nexpaq 0:6c56fb4bc5f0 1155 }
nexpaq 0:6c56fb4bc5f0 1156 else
nexpaq 0:6c56fb4bc5f0 1157 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
nexpaq 0:6c56fb4bc5f0 1158 #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
nexpaq 0:6c56fb4bc5f0 1159 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
nexpaq 0:6c56fb4bc5f0 1160 {
nexpaq 0:6c56fb4bc5f0 1161 int ret;
nexpaq 0:6c56fb4bc5f0 1162 size_t zlen;
nexpaq 0:6c56fb4bc5f0 1163
nexpaq 0:6c56fb4bc5f0 1164 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
nexpaq 0:6c56fb4bc5f0 1165 p + 2, end - ( p + 2 ),
nexpaq 0:6c56fb4bc5f0 1166 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 1167 {
nexpaq 0:6c56fb4bc5f0 1168 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
nexpaq 0:6c56fb4bc5f0 1169 return( ret );
nexpaq 0:6c56fb4bc5f0 1170 }
nexpaq 0:6c56fb4bc5f0 1171
nexpaq 0:6c56fb4bc5f0 1172 *(p++) = (unsigned char)( zlen >> 8 );
nexpaq 0:6c56fb4bc5f0 1173 *(p++) = (unsigned char)( zlen );
nexpaq 0:6c56fb4bc5f0 1174 p += zlen;
nexpaq 0:6c56fb4bc5f0 1175
nexpaq 0:6c56fb4bc5f0 1176 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
nexpaq 0:6c56fb4bc5f0 1177 }
nexpaq 0:6c56fb4bc5f0 1178 else
nexpaq 0:6c56fb4bc5f0 1179 #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
nexpaq 0:6c56fb4bc5f0 1180 {
nexpaq 0:6c56fb4bc5f0 1181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 1182 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 1183 }
nexpaq 0:6c56fb4bc5f0 1184
nexpaq 0:6c56fb4bc5f0 1185 /* opaque psk<0..2^16-1>; */
nexpaq 0:6c56fb4bc5f0 1186 if( end - p < 2 )
nexpaq 0:6c56fb4bc5f0 1187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 1188
nexpaq 0:6c56fb4bc5f0 1189 *(p++) = (unsigned char)( psk_len >> 8 );
nexpaq 0:6c56fb4bc5f0 1190 *(p++) = (unsigned char)( psk_len );
nexpaq 0:6c56fb4bc5f0 1191
nexpaq 0:6c56fb4bc5f0 1192 if( end < p || (size_t)( end - p ) < psk_len )
nexpaq 0:6c56fb4bc5f0 1193 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 1194
nexpaq 0:6c56fb4bc5f0 1195 memcpy( p, psk, psk_len );
nexpaq 0:6c56fb4bc5f0 1196 p += psk_len;
nexpaq 0:6c56fb4bc5f0 1197
nexpaq 0:6c56fb4bc5f0 1198 ssl->handshake->pmslen = p - ssl->handshake->premaster;
nexpaq 0:6c56fb4bc5f0 1199
nexpaq 0:6c56fb4bc5f0 1200 return( 0 );
nexpaq 0:6c56fb4bc5f0 1201 }
nexpaq 0:6c56fb4bc5f0 1202 #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
nexpaq 0:6c56fb4bc5f0 1203
nexpaq 0:6c56fb4bc5f0 1204 #if defined(MBEDTLS_SSL_PROTO_SSL3)
nexpaq 0:6c56fb4bc5f0 1205 /*
nexpaq 0:6c56fb4bc5f0 1206 * SSLv3.0 MAC functions
nexpaq 0:6c56fb4bc5f0 1207 */
nexpaq 0:6c56fb4bc5f0 1208 static void ssl_mac( mbedtls_md_context_t *md_ctx, unsigned char *secret,
nexpaq 0:6c56fb4bc5f0 1209 unsigned char *buf, size_t len,
nexpaq 0:6c56fb4bc5f0 1210 unsigned char *ctr, int type )
nexpaq 0:6c56fb4bc5f0 1211 {
nexpaq 0:6c56fb4bc5f0 1212 unsigned char header[11];
nexpaq 0:6c56fb4bc5f0 1213 unsigned char padding[48];
nexpaq 0:6c56fb4bc5f0 1214 int padlen;
nexpaq 0:6c56fb4bc5f0 1215 int md_size = mbedtls_md_get_size( md_ctx->md_info );
nexpaq 0:6c56fb4bc5f0 1216 int md_type = mbedtls_md_get_type( md_ctx->md_info );
nexpaq 0:6c56fb4bc5f0 1217
nexpaq 0:6c56fb4bc5f0 1218 /* Only MD5 and SHA-1 supported */
nexpaq 0:6c56fb4bc5f0 1219 if( md_type == MBEDTLS_MD_MD5 )
nexpaq 0:6c56fb4bc5f0 1220 padlen = 48;
nexpaq 0:6c56fb4bc5f0 1221 else
nexpaq 0:6c56fb4bc5f0 1222 padlen = 40;
nexpaq 0:6c56fb4bc5f0 1223
nexpaq 0:6c56fb4bc5f0 1224 memcpy( header, ctr, 8 );
nexpaq 0:6c56fb4bc5f0 1225 header[ 8] = (unsigned char) type;
nexpaq 0:6c56fb4bc5f0 1226 header[ 9] = (unsigned char)( len >> 8 );
nexpaq 0:6c56fb4bc5f0 1227 header[10] = (unsigned char)( len );
nexpaq 0:6c56fb4bc5f0 1228
nexpaq 0:6c56fb4bc5f0 1229 memset( padding, 0x36, padlen );
nexpaq 0:6c56fb4bc5f0 1230 mbedtls_md_starts( md_ctx );
nexpaq 0:6c56fb4bc5f0 1231 mbedtls_md_update( md_ctx, secret, md_size );
nexpaq 0:6c56fb4bc5f0 1232 mbedtls_md_update( md_ctx, padding, padlen );
nexpaq 0:6c56fb4bc5f0 1233 mbedtls_md_update( md_ctx, header, 11 );
nexpaq 0:6c56fb4bc5f0 1234 mbedtls_md_update( md_ctx, buf, len );
nexpaq 0:6c56fb4bc5f0 1235 mbedtls_md_finish( md_ctx, buf + len );
nexpaq 0:6c56fb4bc5f0 1236
nexpaq 0:6c56fb4bc5f0 1237 memset( padding, 0x5C, padlen );
nexpaq 0:6c56fb4bc5f0 1238 mbedtls_md_starts( md_ctx );
nexpaq 0:6c56fb4bc5f0 1239 mbedtls_md_update( md_ctx, secret, md_size );
nexpaq 0:6c56fb4bc5f0 1240 mbedtls_md_update( md_ctx, padding, padlen );
nexpaq 0:6c56fb4bc5f0 1241 mbedtls_md_update( md_ctx, buf + len, md_size );
nexpaq 0:6c56fb4bc5f0 1242 mbedtls_md_finish( md_ctx, buf + len );
nexpaq 0:6c56fb4bc5f0 1243 }
nexpaq 0:6c56fb4bc5f0 1244 #endif /* MBEDTLS_SSL_PROTO_SSL3 */
nexpaq 0:6c56fb4bc5f0 1245
nexpaq 0:6c56fb4bc5f0 1246 #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
nexpaq 0:6c56fb4bc5f0 1247 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
nexpaq 0:6c56fb4bc5f0 1248 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) ) )
nexpaq 0:6c56fb4bc5f0 1249 #define SSL_SOME_MODES_USE_MAC
nexpaq 0:6c56fb4bc5f0 1250 #endif
nexpaq 0:6c56fb4bc5f0 1251
nexpaq 0:6c56fb4bc5f0 1252 /*
nexpaq 0:6c56fb4bc5f0 1253 * Encryption/decryption functions
nexpaq 0:6c56fb4bc5f0 1254 */
nexpaq 0:6c56fb4bc5f0 1255 static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 1256 {
nexpaq 0:6c56fb4bc5f0 1257 mbedtls_cipher_mode_t mode;
nexpaq 0:6c56fb4bc5f0 1258 int auth_done = 0;
nexpaq 0:6c56fb4bc5f0 1259
nexpaq 0:6c56fb4bc5f0 1260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
nexpaq 0:6c56fb4bc5f0 1261
nexpaq 0:6c56fb4bc5f0 1262 if( ssl->session_out == NULL || ssl->transform_out == NULL )
nexpaq 0:6c56fb4bc5f0 1263 {
nexpaq 0:6c56fb4bc5f0 1264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 1265 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 1266 }
nexpaq 0:6c56fb4bc5f0 1267
nexpaq 0:6c56fb4bc5f0 1268 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
nexpaq 0:6c56fb4bc5f0 1269
nexpaq 0:6c56fb4bc5f0 1270 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
nexpaq 0:6c56fb4bc5f0 1271 ssl->out_msg, ssl->out_msglen );
nexpaq 0:6c56fb4bc5f0 1272
nexpaq 0:6c56fb4bc5f0 1273 /*
nexpaq 0:6c56fb4bc5f0 1274 * Add MAC before if needed
nexpaq 0:6c56fb4bc5f0 1275 */
nexpaq 0:6c56fb4bc5f0 1276 #if defined(SSL_SOME_MODES_USE_MAC)
nexpaq 0:6c56fb4bc5f0 1277 if( mode == MBEDTLS_MODE_STREAM ||
nexpaq 0:6c56fb4bc5f0 1278 ( mode == MBEDTLS_MODE_CBC
nexpaq 0:6c56fb4bc5f0 1279 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
nexpaq 0:6c56fb4bc5f0 1280 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
nexpaq 0:6c56fb4bc5f0 1281 #endif
nexpaq 0:6c56fb4bc5f0 1282 ) )
nexpaq 0:6c56fb4bc5f0 1283 {
nexpaq 0:6c56fb4bc5f0 1284 #if defined(MBEDTLS_SSL_PROTO_SSL3)
nexpaq 0:6c56fb4bc5f0 1285 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
nexpaq 0:6c56fb4bc5f0 1286 {
nexpaq 0:6c56fb4bc5f0 1287 ssl_mac( &ssl->transform_out->md_ctx_enc,
nexpaq 0:6c56fb4bc5f0 1288 ssl->transform_out->mac_enc,
nexpaq 0:6c56fb4bc5f0 1289 ssl->out_msg, ssl->out_msglen,
nexpaq 0:6c56fb4bc5f0 1290 ssl->out_ctr, ssl->out_msgtype );
nexpaq 0:6c56fb4bc5f0 1291 }
nexpaq 0:6c56fb4bc5f0 1292 else
nexpaq 0:6c56fb4bc5f0 1293 #endif
nexpaq 0:6c56fb4bc5f0 1294 #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
nexpaq 0:6c56fb4bc5f0 1295 defined(MBEDTLS_SSL_PROTO_TLS1_2)
nexpaq 0:6c56fb4bc5f0 1296 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
nexpaq 0:6c56fb4bc5f0 1297 {
nexpaq 0:6c56fb4bc5f0 1298 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
nexpaq 0:6c56fb4bc5f0 1299 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
nexpaq 0:6c56fb4bc5f0 1300 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
nexpaq 0:6c56fb4bc5f0 1301 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
nexpaq 0:6c56fb4bc5f0 1302 ssl->out_msg, ssl->out_msglen );
nexpaq 0:6c56fb4bc5f0 1303 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc,
nexpaq 0:6c56fb4bc5f0 1304 ssl->out_msg + ssl->out_msglen );
nexpaq 0:6c56fb4bc5f0 1305 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
nexpaq 0:6c56fb4bc5f0 1306 }
nexpaq 0:6c56fb4bc5f0 1307 else
nexpaq 0:6c56fb4bc5f0 1308 #endif
nexpaq 0:6c56fb4bc5f0 1309 {
nexpaq 0:6c56fb4bc5f0 1310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 1311 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 1312 }
nexpaq 0:6c56fb4bc5f0 1313
nexpaq 0:6c56fb4bc5f0 1314 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
nexpaq 0:6c56fb4bc5f0 1315 ssl->out_msg + ssl->out_msglen,
nexpaq 0:6c56fb4bc5f0 1316 ssl->transform_out->maclen );
nexpaq 0:6c56fb4bc5f0 1317
nexpaq 0:6c56fb4bc5f0 1318 ssl->out_msglen += ssl->transform_out->maclen;
nexpaq 0:6c56fb4bc5f0 1319 auth_done++;
nexpaq 0:6c56fb4bc5f0 1320 }
nexpaq 0:6c56fb4bc5f0 1321 #endif /* AEAD not the only option */
nexpaq 0:6c56fb4bc5f0 1322
nexpaq 0:6c56fb4bc5f0 1323 /*
nexpaq 0:6c56fb4bc5f0 1324 * Encrypt
nexpaq 0:6c56fb4bc5f0 1325 */
nexpaq 0:6c56fb4bc5f0 1326 #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
nexpaq 0:6c56fb4bc5f0 1327 if( mode == MBEDTLS_MODE_STREAM )
nexpaq 0:6c56fb4bc5f0 1328 {
nexpaq 0:6c56fb4bc5f0 1329 int ret;
nexpaq 0:6c56fb4bc5f0 1330 size_t olen = 0;
nexpaq 0:6c56fb4bc5f0 1331
nexpaq 0:6c56fb4bc5f0 1332 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
nexpaq 0:6c56fb4bc5f0 1333 "including %d bytes of padding",
nexpaq 0:6c56fb4bc5f0 1334 ssl->out_msglen, 0 ) );
nexpaq 0:6c56fb4bc5f0 1335
nexpaq 0:6c56fb4bc5f0 1336 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
nexpaq 0:6c56fb4bc5f0 1337 ssl->transform_out->iv_enc,
nexpaq 0:6c56fb4bc5f0 1338 ssl->transform_out->ivlen,
nexpaq 0:6c56fb4bc5f0 1339 ssl->out_msg, ssl->out_msglen,
nexpaq 0:6c56fb4bc5f0 1340 ssl->out_msg, &olen ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 1341 {
nexpaq 0:6c56fb4bc5f0 1342 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
nexpaq 0:6c56fb4bc5f0 1343 return( ret );
nexpaq 0:6c56fb4bc5f0 1344 }
nexpaq 0:6c56fb4bc5f0 1345
nexpaq 0:6c56fb4bc5f0 1346 if( ssl->out_msglen != olen )
nexpaq 0:6c56fb4bc5f0 1347 {
nexpaq 0:6c56fb4bc5f0 1348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 1349 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 1350 }
nexpaq 0:6c56fb4bc5f0 1351 }
nexpaq 0:6c56fb4bc5f0 1352 else
nexpaq 0:6c56fb4bc5f0 1353 #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
nexpaq 0:6c56fb4bc5f0 1354 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
nexpaq 0:6c56fb4bc5f0 1355 if( mode == MBEDTLS_MODE_GCM ||
nexpaq 0:6c56fb4bc5f0 1356 mode == MBEDTLS_MODE_CCM )
nexpaq 0:6c56fb4bc5f0 1357 {
nexpaq 0:6c56fb4bc5f0 1358 int ret;
nexpaq 0:6c56fb4bc5f0 1359 size_t enc_msglen, olen;
nexpaq 0:6c56fb4bc5f0 1360 unsigned char *enc_msg;
nexpaq 0:6c56fb4bc5f0 1361 unsigned char add_data[13];
nexpaq 0:6c56fb4bc5f0 1362 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
nexpaq 0:6c56fb4bc5f0 1363 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
nexpaq 0:6c56fb4bc5f0 1364
nexpaq 0:6c56fb4bc5f0 1365 memcpy( add_data, ssl->out_ctr, 8 );
nexpaq 0:6c56fb4bc5f0 1366 add_data[8] = ssl->out_msgtype;
nexpaq 0:6c56fb4bc5f0 1367 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
nexpaq 0:6c56fb4bc5f0 1368 ssl->conf->transport, add_data + 9 );
nexpaq 0:6c56fb4bc5f0 1369 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
nexpaq 0:6c56fb4bc5f0 1370 add_data[12] = ssl->out_msglen & 0xFF;
nexpaq 0:6c56fb4bc5f0 1371
nexpaq 0:6c56fb4bc5f0 1372 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
nexpaq 0:6c56fb4bc5f0 1373 add_data, 13 );
nexpaq 0:6c56fb4bc5f0 1374
nexpaq 0:6c56fb4bc5f0 1375 /*
nexpaq 0:6c56fb4bc5f0 1376 * Generate IV
nexpaq 0:6c56fb4bc5f0 1377 */
nexpaq 0:6c56fb4bc5f0 1378 #if defined(MBEDTLS_SSL_AEAD_RANDOM_IV)
nexpaq 0:6c56fb4bc5f0 1379 ret = ssl->conf->f_rng( ssl->conf->p_rng,
nexpaq 0:6c56fb4bc5f0 1380 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
nexpaq 0:6c56fb4bc5f0 1381 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
nexpaq 0:6c56fb4bc5f0 1382 if( ret != 0 )
nexpaq 0:6c56fb4bc5f0 1383 return( ret );
nexpaq 0:6c56fb4bc5f0 1384
nexpaq 0:6c56fb4bc5f0 1385 memcpy( ssl->out_iv,
nexpaq 0:6c56fb4bc5f0 1386 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
nexpaq 0:6c56fb4bc5f0 1387 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
nexpaq 0:6c56fb4bc5f0 1388 #else
nexpaq 0:6c56fb4bc5f0 1389 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
nexpaq 0:6c56fb4bc5f0 1390 {
nexpaq 0:6c56fb4bc5f0 1391 /* Reminder if we ever add an AEAD mode with a different size */
nexpaq 0:6c56fb4bc5f0 1392 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 1393 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 1394 }
nexpaq 0:6c56fb4bc5f0 1395
nexpaq 0:6c56fb4bc5f0 1396 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
nexpaq 0:6c56fb4bc5f0 1397 ssl->out_ctr, 8 );
nexpaq 0:6c56fb4bc5f0 1398 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
nexpaq 0:6c56fb4bc5f0 1399 #endif
nexpaq 0:6c56fb4bc5f0 1400
nexpaq 0:6c56fb4bc5f0 1401 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
nexpaq 0:6c56fb4bc5f0 1402 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
nexpaq 0:6c56fb4bc5f0 1403
nexpaq 0:6c56fb4bc5f0 1404 /*
nexpaq 0:6c56fb4bc5f0 1405 * Fix pointer positions and message length with added IV
nexpaq 0:6c56fb4bc5f0 1406 */
nexpaq 0:6c56fb4bc5f0 1407 enc_msg = ssl->out_msg;
nexpaq 0:6c56fb4bc5f0 1408 enc_msglen = ssl->out_msglen;
nexpaq 0:6c56fb4bc5f0 1409 ssl->out_msglen += ssl->transform_out->ivlen -
nexpaq 0:6c56fb4bc5f0 1410 ssl->transform_out->fixed_ivlen;
nexpaq 0:6c56fb4bc5f0 1411
nexpaq 0:6c56fb4bc5f0 1412 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
nexpaq 0:6c56fb4bc5f0 1413 "including %d bytes of padding",
nexpaq 0:6c56fb4bc5f0 1414 ssl->out_msglen, 0 ) );
nexpaq 0:6c56fb4bc5f0 1415
nexpaq 0:6c56fb4bc5f0 1416 /*
nexpaq 0:6c56fb4bc5f0 1417 * Encrypt and authenticate
nexpaq 0:6c56fb4bc5f0 1418 */
nexpaq 0:6c56fb4bc5f0 1419 if( ( ret = mbedtls_cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
nexpaq 0:6c56fb4bc5f0 1420 ssl->transform_out->iv_enc,
nexpaq 0:6c56fb4bc5f0 1421 ssl->transform_out->ivlen,
nexpaq 0:6c56fb4bc5f0 1422 add_data, 13,
nexpaq 0:6c56fb4bc5f0 1423 enc_msg, enc_msglen,
nexpaq 0:6c56fb4bc5f0 1424 enc_msg, &olen,
nexpaq 0:6c56fb4bc5f0 1425 enc_msg + enc_msglen, taglen ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 1426 {
nexpaq 0:6c56fb4bc5f0 1427 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
nexpaq 0:6c56fb4bc5f0 1428 return( ret );
nexpaq 0:6c56fb4bc5f0 1429 }
nexpaq 0:6c56fb4bc5f0 1430
nexpaq 0:6c56fb4bc5f0 1431 if( olen != enc_msglen )
nexpaq 0:6c56fb4bc5f0 1432 {
nexpaq 0:6c56fb4bc5f0 1433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 1434 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 1435 }
nexpaq 0:6c56fb4bc5f0 1436
nexpaq 0:6c56fb4bc5f0 1437 ssl->out_msglen += taglen;
nexpaq 0:6c56fb4bc5f0 1438 auth_done++;
nexpaq 0:6c56fb4bc5f0 1439
nexpaq 0:6c56fb4bc5f0 1440 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
nexpaq 0:6c56fb4bc5f0 1441 }
nexpaq 0:6c56fb4bc5f0 1442 else
nexpaq 0:6c56fb4bc5f0 1443 #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
nexpaq 0:6c56fb4bc5f0 1444 #if defined(MBEDTLS_CIPHER_MODE_CBC) && \
nexpaq 0:6c56fb4bc5f0 1445 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) )
nexpaq 0:6c56fb4bc5f0 1446 if( mode == MBEDTLS_MODE_CBC )
nexpaq 0:6c56fb4bc5f0 1447 {
nexpaq 0:6c56fb4bc5f0 1448 int ret;
nexpaq 0:6c56fb4bc5f0 1449 unsigned char *enc_msg;
nexpaq 0:6c56fb4bc5f0 1450 size_t enc_msglen, padlen, olen = 0, i;
nexpaq 0:6c56fb4bc5f0 1451
nexpaq 0:6c56fb4bc5f0 1452 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
nexpaq 0:6c56fb4bc5f0 1453 ssl->transform_out->ivlen;
nexpaq 0:6c56fb4bc5f0 1454 if( padlen == ssl->transform_out->ivlen )
nexpaq 0:6c56fb4bc5f0 1455 padlen = 0;
nexpaq 0:6c56fb4bc5f0 1456
nexpaq 0:6c56fb4bc5f0 1457 for( i = 0; i <= padlen; i++ )
nexpaq 0:6c56fb4bc5f0 1458 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
nexpaq 0:6c56fb4bc5f0 1459
nexpaq 0:6c56fb4bc5f0 1460 ssl->out_msglen += padlen + 1;
nexpaq 0:6c56fb4bc5f0 1461
nexpaq 0:6c56fb4bc5f0 1462 enc_msglen = ssl->out_msglen;
nexpaq 0:6c56fb4bc5f0 1463 enc_msg = ssl->out_msg;
nexpaq 0:6c56fb4bc5f0 1464
nexpaq 0:6c56fb4bc5f0 1465 #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
nexpaq 0:6c56fb4bc5f0 1466 /*
nexpaq 0:6c56fb4bc5f0 1467 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
nexpaq 0:6c56fb4bc5f0 1468 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
nexpaq 0:6c56fb4bc5f0 1469 */
nexpaq 0:6c56fb4bc5f0 1470 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
nexpaq 0:6c56fb4bc5f0 1471 {
nexpaq 0:6c56fb4bc5f0 1472 /*
nexpaq 0:6c56fb4bc5f0 1473 * Generate IV
nexpaq 0:6c56fb4bc5f0 1474 */
nexpaq 0:6c56fb4bc5f0 1475 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
nexpaq 0:6c56fb4bc5f0 1476 ssl->transform_out->ivlen );
nexpaq 0:6c56fb4bc5f0 1477 if( ret != 0 )
nexpaq 0:6c56fb4bc5f0 1478 return( ret );
nexpaq 0:6c56fb4bc5f0 1479
nexpaq 0:6c56fb4bc5f0 1480 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
nexpaq 0:6c56fb4bc5f0 1481 ssl->transform_out->ivlen );
nexpaq 0:6c56fb4bc5f0 1482
nexpaq 0:6c56fb4bc5f0 1483 /*
nexpaq 0:6c56fb4bc5f0 1484 * Fix pointer positions and message length with added IV
nexpaq 0:6c56fb4bc5f0 1485 */
nexpaq 0:6c56fb4bc5f0 1486 enc_msg = ssl->out_msg;
nexpaq 0:6c56fb4bc5f0 1487 enc_msglen = ssl->out_msglen;
nexpaq 0:6c56fb4bc5f0 1488 ssl->out_msglen += ssl->transform_out->ivlen;
nexpaq 0:6c56fb4bc5f0 1489 }
nexpaq 0:6c56fb4bc5f0 1490 #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
nexpaq 0:6c56fb4bc5f0 1491
nexpaq 0:6c56fb4bc5f0 1492 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
nexpaq 0:6c56fb4bc5f0 1493 "including %d bytes of IV and %d bytes of padding",
nexpaq 0:6c56fb4bc5f0 1494 ssl->out_msglen, ssl->transform_out->ivlen,
nexpaq 0:6c56fb4bc5f0 1495 padlen + 1 ) );
nexpaq 0:6c56fb4bc5f0 1496
nexpaq 0:6c56fb4bc5f0 1497 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
nexpaq 0:6c56fb4bc5f0 1498 ssl->transform_out->iv_enc,
nexpaq 0:6c56fb4bc5f0 1499 ssl->transform_out->ivlen,
nexpaq 0:6c56fb4bc5f0 1500 enc_msg, enc_msglen,
nexpaq 0:6c56fb4bc5f0 1501 enc_msg, &olen ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 1502 {
nexpaq 0:6c56fb4bc5f0 1503 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
nexpaq 0:6c56fb4bc5f0 1504 return( ret );
nexpaq 0:6c56fb4bc5f0 1505 }
nexpaq 0:6c56fb4bc5f0 1506
nexpaq 0:6c56fb4bc5f0 1507 if( enc_msglen != olen )
nexpaq 0:6c56fb4bc5f0 1508 {
nexpaq 0:6c56fb4bc5f0 1509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 1510 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 1511 }
nexpaq 0:6c56fb4bc5f0 1512
nexpaq 0:6c56fb4bc5f0 1513 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
nexpaq 0:6c56fb4bc5f0 1514 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
nexpaq 0:6c56fb4bc5f0 1515 {
nexpaq 0:6c56fb4bc5f0 1516 /*
nexpaq 0:6c56fb4bc5f0 1517 * Save IV in SSL3 and TLS1
nexpaq 0:6c56fb4bc5f0 1518 */
nexpaq 0:6c56fb4bc5f0 1519 memcpy( ssl->transform_out->iv_enc,
nexpaq 0:6c56fb4bc5f0 1520 ssl->transform_out->cipher_ctx_enc.iv,
nexpaq 0:6c56fb4bc5f0 1521 ssl->transform_out->ivlen );
nexpaq 0:6c56fb4bc5f0 1522 }
nexpaq 0:6c56fb4bc5f0 1523 #endif
nexpaq 0:6c56fb4bc5f0 1524
nexpaq 0:6c56fb4bc5f0 1525 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
nexpaq 0:6c56fb4bc5f0 1526 if( auth_done == 0 )
nexpaq 0:6c56fb4bc5f0 1527 {
nexpaq 0:6c56fb4bc5f0 1528 /*
nexpaq 0:6c56fb4bc5f0 1529 * MAC(MAC_write_key, seq_num +
nexpaq 0:6c56fb4bc5f0 1530 * TLSCipherText.type +
nexpaq 0:6c56fb4bc5f0 1531 * TLSCipherText.version +
nexpaq 0:6c56fb4bc5f0 1532 * length_of( (IV +) ENC(...) ) +
nexpaq 0:6c56fb4bc5f0 1533 * IV + // except for TLS 1.0
nexpaq 0:6c56fb4bc5f0 1534 * ENC(content + padding + padding_length));
nexpaq 0:6c56fb4bc5f0 1535 */
nexpaq 0:6c56fb4bc5f0 1536 unsigned char pseudo_hdr[13];
nexpaq 0:6c56fb4bc5f0 1537
nexpaq 0:6c56fb4bc5f0 1538 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
nexpaq 0:6c56fb4bc5f0 1539
nexpaq 0:6c56fb4bc5f0 1540 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
nexpaq 0:6c56fb4bc5f0 1541 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
nexpaq 0:6c56fb4bc5f0 1542 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
nexpaq 0:6c56fb4bc5f0 1543 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
nexpaq 0:6c56fb4bc5f0 1544
nexpaq 0:6c56fb4bc5f0 1545 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
nexpaq 0:6c56fb4bc5f0 1546
nexpaq 0:6c56fb4bc5f0 1547 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
nexpaq 0:6c56fb4bc5f0 1548 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
nexpaq 0:6c56fb4bc5f0 1549 ssl->out_iv, ssl->out_msglen );
nexpaq 0:6c56fb4bc5f0 1550 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc,
nexpaq 0:6c56fb4bc5f0 1551 ssl->out_iv + ssl->out_msglen );
nexpaq 0:6c56fb4bc5f0 1552 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
nexpaq 0:6c56fb4bc5f0 1553
nexpaq 0:6c56fb4bc5f0 1554 ssl->out_msglen += ssl->transform_out->maclen;
nexpaq 0:6c56fb4bc5f0 1555 auth_done++;
nexpaq 0:6c56fb4bc5f0 1556 }
nexpaq 0:6c56fb4bc5f0 1557 #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
nexpaq 0:6c56fb4bc5f0 1558 }
nexpaq 0:6c56fb4bc5f0 1559 else
nexpaq 0:6c56fb4bc5f0 1560 #endif /* MBEDTLS_CIPHER_MODE_CBC &&
nexpaq 0:6c56fb4bc5f0 1561 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C ) */
nexpaq 0:6c56fb4bc5f0 1562 {
nexpaq 0:6c56fb4bc5f0 1563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 1564 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 1565 }
nexpaq 0:6c56fb4bc5f0 1566
nexpaq 0:6c56fb4bc5f0 1567 /* Make extra sure authentication was performed, exactly once */
nexpaq 0:6c56fb4bc5f0 1568 if( auth_done != 1 )
nexpaq 0:6c56fb4bc5f0 1569 {
nexpaq 0:6c56fb4bc5f0 1570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 1571 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 1572 }
nexpaq 0:6c56fb4bc5f0 1573
nexpaq 0:6c56fb4bc5f0 1574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
nexpaq 0:6c56fb4bc5f0 1575
nexpaq 0:6c56fb4bc5f0 1576 return( 0 );
nexpaq 0:6c56fb4bc5f0 1577 }
nexpaq 0:6c56fb4bc5f0 1578
nexpaq 0:6c56fb4bc5f0 1579 #define SSL_MAX_MAC_SIZE 48
nexpaq 0:6c56fb4bc5f0 1580
nexpaq 0:6c56fb4bc5f0 1581 static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 1582 {
nexpaq 0:6c56fb4bc5f0 1583 size_t i;
nexpaq 0:6c56fb4bc5f0 1584 mbedtls_cipher_mode_t mode;
nexpaq 0:6c56fb4bc5f0 1585 int auth_done = 0;
nexpaq 0:6c56fb4bc5f0 1586 #if defined(SSL_SOME_MODES_USE_MAC)
nexpaq 0:6c56fb4bc5f0 1587 size_t padlen = 0, correct = 1;
nexpaq 0:6c56fb4bc5f0 1588 #endif
nexpaq 0:6c56fb4bc5f0 1589
nexpaq 0:6c56fb4bc5f0 1590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
nexpaq 0:6c56fb4bc5f0 1591
nexpaq 0:6c56fb4bc5f0 1592 if( ssl->session_in == NULL || ssl->transform_in == NULL )
nexpaq 0:6c56fb4bc5f0 1593 {
nexpaq 0:6c56fb4bc5f0 1594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 1595 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 1596 }
nexpaq 0:6c56fb4bc5f0 1597
nexpaq 0:6c56fb4bc5f0 1598 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
nexpaq 0:6c56fb4bc5f0 1599
nexpaq 0:6c56fb4bc5f0 1600 if( ssl->in_msglen < ssl->transform_in->minlen )
nexpaq 0:6c56fb4bc5f0 1601 {
nexpaq 0:6c56fb4bc5f0 1602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
nexpaq 0:6c56fb4bc5f0 1603 ssl->in_msglen, ssl->transform_in->minlen ) );
nexpaq 0:6c56fb4bc5f0 1604 return( MBEDTLS_ERR_SSL_INVALID_MAC );
nexpaq 0:6c56fb4bc5f0 1605 }
nexpaq 0:6c56fb4bc5f0 1606
nexpaq 0:6c56fb4bc5f0 1607 #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
nexpaq 0:6c56fb4bc5f0 1608 if( mode == MBEDTLS_MODE_STREAM )
nexpaq 0:6c56fb4bc5f0 1609 {
nexpaq 0:6c56fb4bc5f0 1610 int ret;
nexpaq 0:6c56fb4bc5f0 1611 size_t olen = 0;
nexpaq 0:6c56fb4bc5f0 1612
nexpaq 0:6c56fb4bc5f0 1613 padlen = 0;
nexpaq 0:6c56fb4bc5f0 1614
nexpaq 0:6c56fb4bc5f0 1615 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
nexpaq 0:6c56fb4bc5f0 1616 ssl->transform_in->iv_dec,
nexpaq 0:6c56fb4bc5f0 1617 ssl->transform_in->ivlen,
nexpaq 0:6c56fb4bc5f0 1618 ssl->in_msg, ssl->in_msglen,
nexpaq 0:6c56fb4bc5f0 1619 ssl->in_msg, &olen ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 1620 {
nexpaq 0:6c56fb4bc5f0 1621 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
nexpaq 0:6c56fb4bc5f0 1622 return( ret );
nexpaq 0:6c56fb4bc5f0 1623 }
nexpaq 0:6c56fb4bc5f0 1624
nexpaq 0:6c56fb4bc5f0 1625 if( ssl->in_msglen != olen )
nexpaq 0:6c56fb4bc5f0 1626 {
nexpaq 0:6c56fb4bc5f0 1627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 1628 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 1629 }
nexpaq 0:6c56fb4bc5f0 1630 }
nexpaq 0:6c56fb4bc5f0 1631 else
nexpaq 0:6c56fb4bc5f0 1632 #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
nexpaq 0:6c56fb4bc5f0 1633 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
nexpaq 0:6c56fb4bc5f0 1634 if( mode == MBEDTLS_MODE_GCM ||
nexpaq 0:6c56fb4bc5f0 1635 mode == MBEDTLS_MODE_CCM )
nexpaq 0:6c56fb4bc5f0 1636 {
nexpaq 0:6c56fb4bc5f0 1637 int ret;
nexpaq 0:6c56fb4bc5f0 1638 size_t dec_msglen, olen;
nexpaq 0:6c56fb4bc5f0 1639 unsigned char *dec_msg;
nexpaq 0:6c56fb4bc5f0 1640 unsigned char *dec_msg_result;
nexpaq 0:6c56fb4bc5f0 1641 unsigned char add_data[13];
nexpaq 0:6c56fb4bc5f0 1642 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
nexpaq 0:6c56fb4bc5f0 1643 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
nexpaq 0:6c56fb4bc5f0 1644 size_t explicit_iv_len = ssl->transform_in->ivlen -
nexpaq 0:6c56fb4bc5f0 1645 ssl->transform_in->fixed_ivlen;
nexpaq 0:6c56fb4bc5f0 1646
nexpaq 0:6c56fb4bc5f0 1647 if( ssl->in_msglen < explicit_iv_len + taglen )
nexpaq 0:6c56fb4bc5f0 1648 {
nexpaq 0:6c56fb4bc5f0 1649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
nexpaq 0:6c56fb4bc5f0 1650 "+ taglen (%d)", ssl->in_msglen,
nexpaq 0:6c56fb4bc5f0 1651 explicit_iv_len, taglen ) );
nexpaq 0:6c56fb4bc5f0 1652 return( MBEDTLS_ERR_SSL_INVALID_MAC );
nexpaq 0:6c56fb4bc5f0 1653 }
nexpaq 0:6c56fb4bc5f0 1654 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
nexpaq 0:6c56fb4bc5f0 1655
nexpaq 0:6c56fb4bc5f0 1656 dec_msg = ssl->in_msg;
nexpaq 0:6c56fb4bc5f0 1657 dec_msg_result = ssl->in_msg;
nexpaq 0:6c56fb4bc5f0 1658 ssl->in_msglen = dec_msglen;
nexpaq 0:6c56fb4bc5f0 1659
nexpaq 0:6c56fb4bc5f0 1660 memcpy( add_data, ssl->in_ctr, 8 );
nexpaq 0:6c56fb4bc5f0 1661 add_data[8] = ssl->in_msgtype;
nexpaq 0:6c56fb4bc5f0 1662 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
nexpaq 0:6c56fb4bc5f0 1663 ssl->conf->transport, add_data + 9 );
nexpaq 0:6c56fb4bc5f0 1664 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
nexpaq 0:6c56fb4bc5f0 1665 add_data[12] = ssl->in_msglen & 0xFF;
nexpaq 0:6c56fb4bc5f0 1666
nexpaq 0:6c56fb4bc5f0 1667 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
nexpaq 0:6c56fb4bc5f0 1668 add_data, 13 );
nexpaq 0:6c56fb4bc5f0 1669
nexpaq 0:6c56fb4bc5f0 1670 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
nexpaq 0:6c56fb4bc5f0 1671 ssl->in_iv,
nexpaq 0:6c56fb4bc5f0 1672 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
nexpaq 0:6c56fb4bc5f0 1673
nexpaq 0:6c56fb4bc5f0 1674 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
nexpaq 0:6c56fb4bc5f0 1675 ssl->transform_in->ivlen );
nexpaq 0:6c56fb4bc5f0 1676 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
nexpaq 0:6c56fb4bc5f0 1677
nexpaq 0:6c56fb4bc5f0 1678 /*
nexpaq 0:6c56fb4bc5f0 1679 * Decrypt and authenticate
nexpaq 0:6c56fb4bc5f0 1680 */
nexpaq 0:6c56fb4bc5f0 1681 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
nexpaq 0:6c56fb4bc5f0 1682 ssl->transform_in->iv_dec,
nexpaq 0:6c56fb4bc5f0 1683 ssl->transform_in->ivlen,
nexpaq 0:6c56fb4bc5f0 1684 add_data, 13,
nexpaq 0:6c56fb4bc5f0 1685 dec_msg, dec_msglen,
nexpaq 0:6c56fb4bc5f0 1686 dec_msg_result, &olen,
nexpaq 0:6c56fb4bc5f0 1687 dec_msg + dec_msglen, taglen ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 1688 {
nexpaq 0:6c56fb4bc5f0 1689 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
nexpaq 0:6c56fb4bc5f0 1690
nexpaq 0:6c56fb4bc5f0 1691 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
nexpaq 0:6c56fb4bc5f0 1692 return( MBEDTLS_ERR_SSL_INVALID_MAC );
nexpaq 0:6c56fb4bc5f0 1693
nexpaq 0:6c56fb4bc5f0 1694 return( ret );
nexpaq 0:6c56fb4bc5f0 1695 }
nexpaq 0:6c56fb4bc5f0 1696 auth_done++;
nexpaq 0:6c56fb4bc5f0 1697
nexpaq 0:6c56fb4bc5f0 1698 if( olen != dec_msglen )
nexpaq 0:6c56fb4bc5f0 1699 {
nexpaq 0:6c56fb4bc5f0 1700 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 1701 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 1702 }
nexpaq 0:6c56fb4bc5f0 1703 }
nexpaq 0:6c56fb4bc5f0 1704 else
nexpaq 0:6c56fb4bc5f0 1705 #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
nexpaq 0:6c56fb4bc5f0 1706 #if defined(MBEDTLS_CIPHER_MODE_CBC) && \
nexpaq 0:6c56fb4bc5f0 1707 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) )
nexpaq 0:6c56fb4bc5f0 1708 if( mode == MBEDTLS_MODE_CBC )
nexpaq 0:6c56fb4bc5f0 1709 {
nexpaq 0:6c56fb4bc5f0 1710 /*
nexpaq 0:6c56fb4bc5f0 1711 * Decrypt and check the padding
nexpaq 0:6c56fb4bc5f0 1712 */
nexpaq 0:6c56fb4bc5f0 1713 int ret;
nexpaq 0:6c56fb4bc5f0 1714 unsigned char *dec_msg;
nexpaq 0:6c56fb4bc5f0 1715 unsigned char *dec_msg_result;
nexpaq 0:6c56fb4bc5f0 1716 size_t dec_msglen;
nexpaq 0:6c56fb4bc5f0 1717 size_t minlen = 0;
nexpaq 0:6c56fb4bc5f0 1718 size_t olen = 0;
nexpaq 0:6c56fb4bc5f0 1719
nexpaq 0:6c56fb4bc5f0 1720 /*
nexpaq 0:6c56fb4bc5f0 1721 * Check immediate ciphertext sanity
nexpaq 0:6c56fb4bc5f0 1722 */
nexpaq 0:6c56fb4bc5f0 1723 #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
nexpaq 0:6c56fb4bc5f0 1724 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
nexpaq 0:6c56fb4bc5f0 1725 minlen += ssl->transform_in->ivlen;
nexpaq 0:6c56fb4bc5f0 1726 #endif
nexpaq 0:6c56fb4bc5f0 1727
nexpaq 0:6c56fb4bc5f0 1728 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
nexpaq 0:6c56fb4bc5f0 1729 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
nexpaq 0:6c56fb4bc5f0 1730 {
nexpaq 0:6c56fb4bc5f0 1731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
nexpaq 0:6c56fb4bc5f0 1732 "+ 1 ) ( + expl IV )", ssl->in_msglen,
nexpaq 0:6c56fb4bc5f0 1733 ssl->transform_in->ivlen,
nexpaq 0:6c56fb4bc5f0 1734 ssl->transform_in->maclen ) );
nexpaq 0:6c56fb4bc5f0 1735 return( MBEDTLS_ERR_SSL_INVALID_MAC );
nexpaq 0:6c56fb4bc5f0 1736 }
nexpaq 0:6c56fb4bc5f0 1737
nexpaq 0:6c56fb4bc5f0 1738 dec_msglen = ssl->in_msglen;
nexpaq 0:6c56fb4bc5f0 1739 dec_msg = ssl->in_msg;
nexpaq 0:6c56fb4bc5f0 1740 dec_msg_result = ssl->in_msg;
nexpaq 0:6c56fb4bc5f0 1741
nexpaq 0:6c56fb4bc5f0 1742 /*
nexpaq 0:6c56fb4bc5f0 1743 * Authenticate before decrypt if enabled
nexpaq 0:6c56fb4bc5f0 1744 */
nexpaq 0:6c56fb4bc5f0 1745 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
nexpaq 0:6c56fb4bc5f0 1746 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
nexpaq 0:6c56fb4bc5f0 1747 {
nexpaq 0:6c56fb4bc5f0 1748 unsigned char computed_mac[SSL_MAX_MAC_SIZE];
nexpaq 0:6c56fb4bc5f0 1749 unsigned char pseudo_hdr[13];
nexpaq 0:6c56fb4bc5f0 1750
nexpaq 0:6c56fb4bc5f0 1751 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
nexpaq 0:6c56fb4bc5f0 1752
nexpaq 0:6c56fb4bc5f0 1753 dec_msglen -= ssl->transform_in->maclen;
nexpaq 0:6c56fb4bc5f0 1754 ssl->in_msglen -= ssl->transform_in->maclen;
nexpaq 0:6c56fb4bc5f0 1755
nexpaq 0:6c56fb4bc5f0 1756 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
nexpaq 0:6c56fb4bc5f0 1757 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
nexpaq 0:6c56fb4bc5f0 1758 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
nexpaq 0:6c56fb4bc5f0 1759 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
nexpaq 0:6c56fb4bc5f0 1760
nexpaq 0:6c56fb4bc5f0 1761 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
nexpaq 0:6c56fb4bc5f0 1762
nexpaq 0:6c56fb4bc5f0 1763 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
nexpaq 0:6c56fb4bc5f0 1764 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
nexpaq 0:6c56fb4bc5f0 1765 ssl->in_iv, ssl->in_msglen );
nexpaq 0:6c56fb4bc5f0 1766 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
nexpaq 0:6c56fb4bc5f0 1767 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
nexpaq 0:6c56fb4bc5f0 1768
nexpaq 0:6c56fb4bc5f0 1769 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
nexpaq 0:6c56fb4bc5f0 1770 ssl->transform_in->maclen );
nexpaq 0:6c56fb4bc5f0 1771 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
nexpaq 0:6c56fb4bc5f0 1772 ssl->transform_in->maclen );
nexpaq 0:6c56fb4bc5f0 1773
nexpaq 0:6c56fb4bc5f0 1774 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
nexpaq 0:6c56fb4bc5f0 1775 ssl->transform_in->maclen ) != 0 )
nexpaq 0:6c56fb4bc5f0 1776 {
nexpaq 0:6c56fb4bc5f0 1777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
nexpaq 0:6c56fb4bc5f0 1778
nexpaq 0:6c56fb4bc5f0 1779 return( MBEDTLS_ERR_SSL_INVALID_MAC );
nexpaq 0:6c56fb4bc5f0 1780 }
nexpaq 0:6c56fb4bc5f0 1781 auth_done++;
nexpaq 0:6c56fb4bc5f0 1782 }
nexpaq 0:6c56fb4bc5f0 1783 #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
nexpaq 0:6c56fb4bc5f0 1784
nexpaq 0:6c56fb4bc5f0 1785 /*
nexpaq 0:6c56fb4bc5f0 1786 * Check length sanity
nexpaq 0:6c56fb4bc5f0 1787 */
nexpaq 0:6c56fb4bc5f0 1788 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
nexpaq 0:6c56fb4bc5f0 1789 {
nexpaq 0:6c56fb4bc5f0 1790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
nexpaq 0:6c56fb4bc5f0 1791 ssl->in_msglen, ssl->transform_in->ivlen ) );
nexpaq 0:6c56fb4bc5f0 1792 return( MBEDTLS_ERR_SSL_INVALID_MAC );
nexpaq 0:6c56fb4bc5f0 1793 }
nexpaq 0:6c56fb4bc5f0 1794
nexpaq 0:6c56fb4bc5f0 1795 #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
nexpaq 0:6c56fb4bc5f0 1796 /*
nexpaq 0:6c56fb4bc5f0 1797 * Initialize for prepended IV for block cipher in TLS v1.1 and up
nexpaq 0:6c56fb4bc5f0 1798 */
nexpaq 0:6c56fb4bc5f0 1799 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
nexpaq 0:6c56fb4bc5f0 1800 {
nexpaq 0:6c56fb4bc5f0 1801 dec_msglen -= ssl->transform_in->ivlen;
nexpaq 0:6c56fb4bc5f0 1802 ssl->in_msglen -= ssl->transform_in->ivlen;
nexpaq 0:6c56fb4bc5f0 1803
nexpaq 0:6c56fb4bc5f0 1804 for( i = 0; i < ssl->transform_in->ivlen; i++ )
nexpaq 0:6c56fb4bc5f0 1805 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
nexpaq 0:6c56fb4bc5f0 1806 }
nexpaq 0:6c56fb4bc5f0 1807 #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
nexpaq 0:6c56fb4bc5f0 1808
nexpaq 0:6c56fb4bc5f0 1809 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
nexpaq 0:6c56fb4bc5f0 1810 ssl->transform_in->iv_dec,
nexpaq 0:6c56fb4bc5f0 1811 ssl->transform_in->ivlen,
nexpaq 0:6c56fb4bc5f0 1812 dec_msg, dec_msglen,
nexpaq 0:6c56fb4bc5f0 1813 dec_msg_result, &olen ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 1814 {
nexpaq 0:6c56fb4bc5f0 1815 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
nexpaq 0:6c56fb4bc5f0 1816 return( ret );
nexpaq 0:6c56fb4bc5f0 1817 }
nexpaq 0:6c56fb4bc5f0 1818
nexpaq 0:6c56fb4bc5f0 1819 if( dec_msglen != olen )
nexpaq 0:6c56fb4bc5f0 1820 {
nexpaq 0:6c56fb4bc5f0 1821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 1822 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 1823 }
nexpaq 0:6c56fb4bc5f0 1824
nexpaq 0:6c56fb4bc5f0 1825 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
nexpaq 0:6c56fb4bc5f0 1826 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
nexpaq 0:6c56fb4bc5f0 1827 {
nexpaq 0:6c56fb4bc5f0 1828 /*
nexpaq 0:6c56fb4bc5f0 1829 * Save IV in SSL3 and TLS1
nexpaq 0:6c56fb4bc5f0 1830 */
nexpaq 0:6c56fb4bc5f0 1831 memcpy( ssl->transform_in->iv_dec,
nexpaq 0:6c56fb4bc5f0 1832 ssl->transform_in->cipher_ctx_dec.iv,
nexpaq 0:6c56fb4bc5f0 1833 ssl->transform_in->ivlen );
nexpaq 0:6c56fb4bc5f0 1834 }
nexpaq 0:6c56fb4bc5f0 1835 #endif
nexpaq 0:6c56fb4bc5f0 1836
nexpaq 0:6c56fb4bc5f0 1837 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
nexpaq 0:6c56fb4bc5f0 1838
nexpaq 0:6c56fb4bc5f0 1839 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
nexpaq 0:6c56fb4bc5f0 1840 auth_done == 0 )
nexpaq 0:6c56fb4bc5f0 1841 {
nexpaq 0:6c56fb4bc5f0 1842 #if defined(MBEDTLS_SSL_DEBUG_ALL)
nexpaq 0:6c56fb4bc5f0 1843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
nexpaq 0:6c56fb4bc5f0 1844 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
nexpaq 0:6c56fb4bc5f0 1845 #endif
nexpaq 0:6c56fb4bc5f0 1846 padlen = 0;
nexpaq 0:6c56fb4bc5f0 1847 correct = 0;
nexpaq 0:6c56fb4bc5f0 1848 }
nexpaq 0:6c56fb4bc5f0 1849
nexpaq 0:6c56fb4bc5f0 1850 #if defined(MBEDTLS_SSL_PROTO_SSL3)
nexpaq 0:6c56fb4bc5f0 1851 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
nexpaq 0:6c56fb4bc5f0 1852 {
nexpaq 0:6c56fb4bc5f0 1853 if( padlen > ssl->transform_in->ivlen )
nexpaq 0:6c56fb4bc5f0 1854 {
nexpaq 0:6c56fb4bc5f0 1855 #if defined(MBEDTLS_SSL_DEBUG_ALL)
nexpaq 0:6c56fb4bc5f0 1856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
nexpaq 0:6c56fb4bc5f0 1857 "should be no more than %d",
nexpaq 0:6c56fb4bc5f0 1858 padlen, ssl->transform_in->ivlen ) );
nexpaq 0:6c56fb4bc5f0 1859 #endif
nexpaq 0:6c56fb4bc5f0 1860 correct = 0;
nexpaq 0:6c56fb4bc5f0 1861 }
nexpaq 0:6c56fb4bc5f0 1862 }
nexpaq 0:6c56fb4bc5f0 1863 else
nexpaq 0:6c56fb4bc5f0 1864 #endif /* MBEDTLS_SSL_PROTO_SSL3 */
nexpaq 0:6c56fb4bc5f0 1865 #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
nexpaq 0:6c56fb4bc5f0 1866 defined(MBEDTLS_SSL_PROTO_TLS1_2)
nexpaq 0:6c56fb4bc5f0 1867 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
nexpaq 0:6c56fb4bc5f0 1868 {
nexpaq 0:6c56fb4bc5f0 1869 /*
nexpaq 0:6c56fb4bc5f0 1870 * TLSv1+: always check the padding up to the first failure
nexpaq 0:6c56fb4bc5f0 1871 * and fake check up to 256 bytes of padding
nexpaq 0:6c56fb4bc5f0 1872 */
nexpaq 0:6c56fb4bc5f0 1873 size_t pad_count = 0, real_count = 1;
nexpaq 0:6c56fb4bc5f0 1874 size_t padding_idx = ssl->in_msglen - padlen - 1;
nexpaq 0:6c56fb4bc5f0 1875
nexpaq 0:6c56fb4bc5f0 1876 /*
nexpaq 0:6c56fb4bc5f0 1877 * Padding is guaranteed to be incorrect if:
nexpaq 0:6c56fb4bc5f0 1878 * 1. padlen >= ssl->in_msglen
nexpaq 0:6c56fb4bc5f0 1879 *
nexpaq 0:6c56fb4bc5f0 1880 * 2. padding_idx >= MBEDTLS_SSL_MAX_CONTENT_LEN +
nexpaq 0:6c56fb4bc5f0 1881 * ssl->transform_in->maclen
nexpaq 0:6c56fb4bc5f0 1882 *
nexpaq 0:6c56fb4bc5f0 1883 * In both cases we reset padding_idx to a safe value (0) to
nexpaq 0:6c56fb4bc5f0 1884 * prevent out-of-buffer reads.
nexpaq 0:6c56fb4bc5f0 1885 */
nexpaq 0:6c56fb4bc5f0 1886 correct &= ( ssl->in_msglen >= padlen + 1 );
nexpaq 0:6c56fb4bc5f0 1887 correct &= ( padding_idx < MBEDTLS_SSL_MAX_CONTENT_LEN +
nexpaq 0:6c56fb4bc5f0 1888 ssl->transform_in->maclen );
nexpaq 0:6c56fb4bc5f0 1889
nexpaq 0:6c56fb4bc5f0 1890 padding_idx *= correct;
nexpaq 0:6c56fb4bc5f0 1891
nexpaq 0:6c56fb4bc5f0 1892 for( i = 1; i <= 256; i++ )
nexpaq 0:6c56fb4bc5f0 1893 {
nexpaq 0:6c56fb4bc5f0 1894 real_count &= ( i <= padlen );
nexpaq 0:6c56fb4bc5f0 1895 pad_count += real_count *
nexpaq 0:6c56fb4bc5f0 1896 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
nexpaq 0:6c56fb4bc5f0 1897 }
nexpaq 0:6c56fb4bc5f0 1898
nexpaq 0:6c56fb4bc5f0 1899 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
nexpaq 0:6c56fb4bc5f0 1900
nexpaq 0:6c56fb4bc5f0 1901 #if defined(MBEDTLS_SSL_DEBUG_ALL)
nexpaq 0:6c56fb4bc5f0 1902 if( padlen > 0 && correct == 0 )
nexpaq 0:6c56fb4bc5f0 1903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
nexpaq 0:6c56fb4bc5f0 1904 #endif
nexpaq 0:6c56fb4bc5f0 1905 padlen &= correct * 0x1FF;
nexpaq 0:6c56fb4bc5f0 1906 }
nexpaq 0:6c56fb4bc5f0 1907 else
nexpaq 0:6c56fb4bc5f0 1908 #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
nexpaq 0:6c56fb4bc5f0 1909 MBEDTLS_SSL_PROTO_TLS1_2 */
nexpaq 0:6c56fb4bc5f0 1910 {
nexpaq 0:6c56fb4bc5f0 1911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 1912 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 1913 }
nexpaq 0:6c56fb4bc5f0 1914
nexpaq 0:6c56fb4bc5f0 1915 ssl->in_msglen -= padlen;
nexpaq 0:6c56fb4bc5f0 1916 }
nexpaq 0:6c56fb4bc5f0 1917 else
nexpaq 0:6c56fb4bc5f0 1918 #endif /* MBEDTLS_CIPHER_MODE_CBC &&
nexpaq 0:6c56fb4bc5f0 1919 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C ) */
nexpaq 0:6c56fb4bc5f0 1920 {
nexpaq 0:6c56fb4bc5f0 1921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 1922 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 1923 }
nexpaq 0:6c56fb4bc5f0 1924
nexpaq 0:6c56fb4bc5f0 1925 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
nexpaq 0:6c56fb4bc5f0 1926 ssl->in_msg, ssl->in_msglen );
nexpaq 0:6c56fb4bc5f0 1927
nexpaq 0:6c56fb4bc5f0 1928 /*
nexpaq 0:6c56fb4bc5f0 1929 * Authenticate if not done yet.
nexpaq 0:6c56fb4bc5f0 1930 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
nexpaq 0:6c56fb4bc5f0 1931 */
nexpaq 0:6c56fb4bc5f0 1932 #if defined(SSL_SOME_MODES_USE_MAC)
nexpaq 0:6c56fb4bc5f0 1933 if( auth_done == 0 )
nexpaq 0:6c56fb4bc5f0 1934 {
nexpaq 0:6c56fb4bc5f0 1935 unsigned char tmp[SSL_MAX_MAC_SIZE];
nexpaq 0:6c56fb4bc5f0 1936
nexpaq 0:6c56fb4bc5f0 1937 ssl->in_msglen -= ssl->transform_in->maclen;
nexpaq 0:6c56fb4bc5f0 1938
nexpaq 0:6c56fb4bc5f0 1939 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
nexpaq 0:6c56fb4bc5f0 1940 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
nexpaq 0:6c56fb4bc5f0 1941
nexpaq 0:6c56fb4bc5f0 1942 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
nexpaq 0:6c56fb4bc5f0 1943
nexpaq 0:6c56fb4bc5f0 1944 #if defined(MBEDTLS_SSL_PROTO_SSL3)
nexpaq 0:6c56fb4bc5f0 1945 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
nexpaq 0:6c56fb4bc5f0 1946 {
nexpaq 0:6c56fb4bc5f0 1947 ssl_mac( &ssl->transform_in->md_ctx_dec,
nexpaq 0:6c56fb4bc5f0 1948 ssl->transform_in->mac_dec,
nexpaq 0:6c56fb4bc5f0 1949 ssl->in_msg, ssl->in_msglen,
nexpaq 0:6c56fb4bc5f0 1950 ssl->in_ctr, ssl->in_msgtype );
nexpaq 0:6c56fb4bc5f0 1951 }
nexpaq 0:6c56fb4bc5f0 1952 else
nexpaq 0:6c56fb4bc5f0 1953 #endif /* MBEDTLS_SSL_PROTO_SSL3 */
nexpaq 0:6c56fb4bc5f0 1954 #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
nexpaq 0:6c56fb4bc5f0 1955 defined(MBEDTLS_SSL_PROTO_TLS1_2)
nexpaq 0:6c56fb4bc5f0 1956 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
nexpaq 0:6c56fb4bc5f0 1957 {
nexpaq 0:6c56fb4bc5f0 1958 /*
nexpaq 0:6c56fb4bc5f0 1959 * Process MAC and always update for padlen afterwards to make
nexpaq 0:6c56fb4bc5f0 1960 * total time independent of padlen
nexpaq 0:6c56fb4bc5f0 1961 *
nexpaq 0:6c56fb4bc5f0 1962 * extra_run compensates MAC check for padlen
nexpaq 0:6c56fb4bc5f0 1963 *
nexpaq 0:6c56fb4bc5f0 1964 * Known timing attacks:
nexpaq 0:6c56fb4bc5f0 1965 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
nexpaq 0:6c56fb4bc5f0 1966 *
nexpaq 0:6c56fb4bc5f0 1967 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
nexpaq 0:6c56fb4bc5f0 1968 * correctly. (We round down instead of up, so -56 is the correct
nexpaq 0:6c56fb4bc5f0 1969 * value for our calculations instead of -55)
nexpaq 0:6c56fb4bc5f0 1970 */
nexpaq 0:6c56fb4bc5f0 1971 size_t j, extra_run = 0;
nexpaq 0:6c56fb4bc5f0 1972 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
nexpaq 0:6c56fb4bc5f0 1973 ( 13 + ssl->in_msglen + 8 ) / 64;
nexpaq 0:6c56fb4bc5f0 1974
nexpaq 0:6c56fb4bc5f0 1975 extra_run &= correct * 0xFF;
nexpaq 0:6c56fb4bc5f0 1976
nexpaq 0:6c56fb4bc5f0 1977 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
nexpaq 0:6c56fb4bc5f0 1978 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
nexpaq 0:6c56fb4bc5f0 1979 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
nexpaq 0:6c56fb4bc5f0 1980 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
nexpaq 0:6c56fb4bc5f0 1981 ssl->in_msglen );
nexpaq 0:6c56fb4bc5f0 1982 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec,
nexpaq 0:6c56fb4bc5f0 1983 ssl->in_msg + ssl->in_msglen );
nexpaq 0:6c56fb4bc5f0 1984 /* Call mbedtls_md_process at least once due to cache attacks */
nexpaq 0:6c56fb4bc5f0 1985 for( j = 0; j < extra_run + 1; j++ )
nexpaq 0:6c56fb4bc5f0 1986 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
nexpaq 0:6c56fb4bc5f0 1987
nexpaq 0:6c56fb4bc5f0 1988 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
nexpaq 0:6c56fb4bc5f0 1989 }
nexpaq 0:6c56fb4bc5f0 1990 else
nexpaq 0:6c56fb4bc5f0 1991 #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
nexpaq 0:6c56fb4bc5f0 1992 MBEDTLS_SSL_PROTO_TLS1_2 */
nexpaq 0:6c56fb4bc5f0 1993 {
nexpaq 0:6c56fb4bc5f0 1994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 1995 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 1996 }
nexpaq 0:6c56fb4bc5f0 1997
nexpaq 0:6c56fb4bc5f0 1998 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
nexpaq 0:6c56fb4bc5f0 1999 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
nexpaq 0:6c56fb4bc5f0 2000 ssl->transform_in->maclen );
nexpaq 0:6c56fb4bc5f0 2001
nexpaq 0:6c56fb4bc5f0 2002 if( mbedtls_ssl_safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
nexpaq 0:6c56fb4bc5f0 2003 ssl->transform_in->maclen ) != 0 )
nexpaq 0:6c56fb4bc5f0 2004 {
nexpaq 0:6c56fb4bc5f0 2005 #if defined(MBEDTLS_SSL_DEBUG_ALL)
nexpaq 0:6c56fb4bc5f0 2006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
nexpaq 0:6c56fb4bc5f0 2007 #endif
nexpaq 0:6c56fb4bc5f0 2008 correct = 0;
nexpaq 0:6c56fb4bc5f0 2009 }
nexpaq 0:6c56fb4bc5f0 2010 auth_done++;
nexpaq 0:6c56fb4bc5f0 2011
nexpaq 0:6c56fb4bc5f0 2012 /*
nexpaq 0:6c56fb4bc5f0 2013 * Finally check the correct flag
nexpaq 0:6c56fb4bc5f0 2014 */
nexpaq 0:6c56fb4bc5f0 2015 if( correct == 0 )
nexpaq 0:6c56fb4bc5f0 2016 return( MBEDTLS_ERR_SSL_INVALID_MAC );
nexpaq 0:6c56fb4bc5f0 2017 }
nexpaq 0:6c56fb4bc5f0 2018 #endif /* SSL_SOME_MODES_USE_MAC */
nexpaq 0:6c56fb4bc5f0 2019
nexpaq 0:6c56fb4bc5f0 2020 /* Make extra sure authentication was performed, exactly once */
nexpaq 0:6c56fb4bc5f0 2021 if( auth_done != 1 )
nexpaq 0:6c56fb4bc5f0 2022 {
nexpaq 0:6c56fb4bc5f0 2023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 2024 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 2025 }
nexpaq 0:6c56fb4bc5f0 2026
nexpaq 0:6c56fb4bc5f0 2027 if( ssl->in_msglen == 0 )
nexpaq 0:6c56fb4bc5f0 2028 {
nexpaq 0:6c56fb4bc5f0 2029 ssl->nb_zero++;
nexpaq 0:6c56fb4bc5f0 2030
nexpaq 0:6c56fb4bc5f0 2031 /*
nexpaq 0:6c56fb4bc5f0 2032 * Three or more empty messages may be a DoS attack
nexpaq 0:6c56fb4bc5f0 2033 * (excessive CPU consumption).
nexpaq 0:6c56fb4bc5f0 2034 */
nexpaq 0:6c56fb4bc5f0 2035 if( ssl->nb_zero > 3 )
nexpaq 0:6c56fb4bc5f0 2036 {
nexpaq 0:6c56fb4bc5f0 2037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
nexpaq 0:6c56fb4bc5f0 2038 "messages, possible DoS attack" ) );
nexpaq 0:6c56fb4bc5f0 2039 return( MBEDTLS_ERR_SSL_INVALID_MAC );
nexpaq 0:6c56fb4bc5f0 2040 }
nexpaq 0:6c56fb4bc5f0 2041 }
nexpaq 0:6c56fb4bc5f0 2042 else
nexpaq 0:6c56fb4bc5f0 2043 ssl->nb_zero = 0;
nexpaq 0:6c56fb4bc5f0 2044
nexpaq 0:6c56fb4bc5f0 2045 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 2046 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
nexpaq 0:6c56fb4bc5f0 2047 {
nexpaq 0:6c56fb4bc5f0 2048 ; /* in_ctr read from peer, not maintained internally */
nexpaq 0:6c56fb4bc5f0 2049 }
nexpaq 0:6c56fb4bc5f0 2050 else
nexpaq 0:6c56fb4bc5f0 2051 #endif
nexpaq 0:6c56fb4bc5f0 2052 {
nexpaq 0:6c56fb4bc5f0 2053 for( i = 8; i > ssl_ep_len( ssl ); i-- )
nexpaq 0:6c56fb4bc5f0 2054 if( ++ssl->in_ctr[i - 1] != 0 )
nexpaq 0:6c56fb4bc5f0 2055 break;
nexpaq 0:6c56fb4bc5f0 2056
nexpaq 0:6c56fb4bc5f0 2057 /* The loop goes to its end iff the counter is wrapping */
nexpaq 0:6c56fb4bc5f0 2058 if( i == ssl_ep_len( ssl ) )
nexpaq 0:6c56fb4bc5f0 2059 {
nexpaq 0:6c56fb4bc5f0 2060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
nexpaq 0:6c56fb4bc5f0 2061 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
nexpaq 0:6c56fb4bc5f0 2062 }
nexpaq 0:6c56fb4bc5f0 2063 }
nexpaq 0:6c56fb4bc5f0 2064
nexpaq 0:6c56fb4bc5f0 2065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
nexpaq 0:6c56fb4bc5f0 2066
nexpaq 0:6c56fb4bc5f0 2067 return( 0 );
nexpaq 0:6c56fb4bc5f0 2068 }
nexpaq 0:6c56fb4bc5f0 2069
nexpaq 0:6c56fb4bc5f0 2070 #undef MAC_NONE
nexpaq 0:6c56fb4bc5f0 2071 #undef MAC_PLAINTEXT
nexpaq 0:6c56fb4bc5f0 2072 #undef MAC_CIPHERTEXT
nexpaq 0:6c56fb4bc5f0 2073
nexpaq 0:6c56fb4bc5f0 2074 #if defined(MBEDTLS_ZLIB_SUPPORT)
nexpaq 0:6c56fb4bc5f0 2075 /*
nexpaq 0:6c56fb4bc5f0 2076 * Compression/decompression functions
nexpaq 0:6c56fb4bc5f0 2077 */
nexpaq 0:6c56fb4bc5f0 2078 static int ssl_compress_buf( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 2079 {
nexpaq 0:6c56fb4bc5f0 2080 int ret;
nexpaq 0:6c56fb4bc5f0 2081 unsigned char *msg_post = ssl->out_msg;
nexpaq 0:6c56fb4bc5f0 2082 size_t len_pre = ssl->out_msglen;
nexpaq 0:6c56fb4bc5f0 2083 unsigned char *msg_pre = ssl->compress_buf;
nexpaq 0:6c56fb4bc5f0 2084
nexpaq 0:6c56fb4bc5f0 2085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
nexpaq 0:6c56fb4bc5f0 2086
nexpaq 0:6c56fb4bc5f0 2087 if( len_pre == 0 )
nexpaq 0:6c56fb4bc5f0 2088 return( 0 );
nexpaq 0:6c56fb4bc5f0 2089
nexpaq 0:6c56fb4bc5f0 2090 memcpy( msg_pre, ssl->out_msg, len_pre );
nexpaq 0:6c56fb4bc5f0 2091
nexpaq 0:6c56fb4bc5f0 2092 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
nexpaq 0:6c56fb4bc5f0 2093 ssl->out_msglen ) );
nexpaq 0:6c56fb4bc5f0 2094
nexpaq 0:6c56fb4bc5f0 2095 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
nexpaq 0:6c56fb4bc5f0 2096 ssl->out_msg, ssl->out_msglen );
nexpaq 0:6c56fb4bc5f0 2097
nexpaq 0:6c56fb4bc5f0 2098 ssl->transform_out->ctx_deflate.next_in = msg_pre;
nexpaq 0:6c56fb4bc5f0 2099 ssl->transform_out->ctx_deflate.avail_in = len_pre;
nexpaq 0:6c56fb4bc5f0 2100 ssl->transform_out->ctx_deflate.next_out = msg_post;
nexpaq 0:6c56fb4bc5f0 2101 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_BUFFER_LEN;
nexpaq 0:6c56fb4bc5f0 2102
nexpaq 0:6c56fb4bc5f0 2103 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
nexpaq 0:6c56fb4bc5f0 2104 if( ret != Z_OK )
nexpaq 0:6c56fb4bc5f0 2105 {
nexpaq 0:6c56fb4bc5f0 2106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
nexpaq 0:6c56fb4bc5f0 2107 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
nexpaq 0:6c56fb4bc5f0 2108 }
nexpaq 0:6c56fb4bc5f0 2109
nexpaq 0:6c56fb4bc5f0 2110 ssl->out_msglen = MBEDTLS_SSL_BUFFER_LEN -
nexpaq 0:6c56fb4bc5f0 2111 ssl->transform_out->ctx_deflate.avail_out;
nexpaq 0:6c56fb4bc5f0 2112
nexpaq 0:6c56fb4bc5f0 2113 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
nexpaq 0:6c56fb4bc5f0 2114 ssl->out_msglen ) );
nexpaq 0:6c56fb4bc5f0 2115
nexpaq 0:6c56fb4bc5f0 2116 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
nexpaq 0:6c56fb4bc5f0 2117 ssl->out_msg, ssl->out_msglen );
nexpaq 0:6c56fb4bc5f0 2118
nexpaq 0:6c56fb4bc5f0 2119 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
nexpaq 0:6c56fb4bc5f0 2120
nexpaq 0:6c56fb4bc5f0 2121 return( 0 );
nexpaq 0:6c56fb4bc5f0 2122 }
nexpaq 0:6c56fb4bc5f0 2123
nexpaq 0:6c56fb4bc5f0 2124 static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 2125 {
nexpaq 0:6c56fb4bc5f0 2126 int ret;
nexpaq 0:6c56fb4bc5f0 2127 unsigned char *msg_post = ssl->in_msg;
nexpaq 0:6c56fb4bc5f0 2128 size_t len_pre = ssl->in_msglen;
nexpaq 0:6c56fb4bc5f0 2129 unsigned char *msg_pre = ssl->compress_buf;
nexpaq 0:6c56fb4bc5f0 2130
nexpaq 0:6c56fb4bc5f0 2131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
nexpaq 0:6c56fb4bc5f0 2132
nexpaq 0:6c56fb4bc5f0 2133 if( len_pre == 0 )
nexpaq 0:6c56fb4bc5f0 2134 return( 0 );
nexpaq 0:6c56fb4bc5f0 2135
nexpaq 0:6c56fb4bc5f0 2136 memcpy( msg_pre, ssl->in_msg, len_pre );
nexpaq 0:6c56fb4bc5f0 2137
nexpaq 0:6c56fb4bc5f0 2138 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
nexpaq 0:6c56fb4bc5f0 2139 ssl->in_msglen ) );
nexpaq 0:6c56fb4bc5f0 2140
nexpaq 0:6c56fb4bc5f0 2141 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
nexpaq 0:6c56fb4bc5f0 2142 ssl->in_msg, ssl->in_msglen );
nexpaq 0:6c56fb4bc5f0 2143
nexpaq 0:6c56fb4bc5f0 2144 ssl->transform_in->ctx_inflate.next_in = msg_pre;
nexpaq 0:6c56fb4bc5f0 2145 ssl->transform_in->ctx_inflate.avail_in = len_pre;
nexpaq 0:6c56fb4bc5f0 2146 ssl->transform_in->ctx_inflate.next_out = msg_post;
nexpaq 0:6c56fb4bc5f0 2147 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_MAX_CONTENT_LEN;
nexpaq 0:6c56fb4bc5f0 2148
nexpaq 0:6c56fb4bc5f0 2149 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
nexpaq 0:6c56fb4bc5f0 2150 if( ret != Z_OK )
nexpaq 0:6c56fb4bc5f0 2151 {
nexpaq 0:6c56fb4bc5f0 2152 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
nexpaq 0:6c56fb4bc5f0 2153 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
nexpaq 0:6c56fb4bc5f0 2154 }
nexpaq 0:6c56fb4bc5f0 2155
nexpaq 0:6c56fb4bc5f0 2156 ssl->in_msglen = MBEDTLS_SSL_MAX_CONTENT_LEN -
nexpaq 0:6c56fb4bc5f0 2157 ssl->transform_in->ctx_inflate.avail_out;
nexpaq 0:6c56fb4bc5f0 2158
nexpaq 0:6c56fb4bc5f0 2159 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
nexpaq 0:6c56fb4bc5f0 2160 ssl->in_msglen ) );
nexpaq 0:6c56fb4bc5f0 2161
nexpaq 0:6c56fb4bc5f0 2162 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
nexpaq 0:6c56fb4bc5f0 2163 ssl->in_msg, ssl->in_msglen );
nexpaq 0:6c56fb4bc5f0 2164
nexpaq 0:6c56fb4bc5f0 2165 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
nexpaq 0:6c56fb4bc5f0 2166
nexpaq 0:6c56fb4bc5f0 2167 return( 0 );
nexpaq 0:6c56fb4bc5f0 2168 }
nexpaq 0:6c56fb4bc5f0 2169 #endif /* MBEDTLS_ZLIB_SUPPORT */
nexpaq 0:6c56fb4bc5f0 2170
nexpaq 0:6c56fb4bc5f0 2171 #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
nexpaq 0:6c56fb4bc5f0 2172 static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
nexpaq 0:6c56fb4bc5f0 2173
nexpaq 0:6c56fb4bc5f0 2174 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 2175 static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 2176 {
nexpaq 0:6c56fb4bc5f0 2177 /* If renegotiation is not enforced, retransmit until we would reach max
nexpaq 0:6c56fb4bc5f0 2178 * timeout if we were using the usual handshake doubling scheme */
nexpaq 0:6c56fb4bc5f0 2179 if( ssl->conf->renego_max_records < 0 )
nexpaq 0:6c56fb4bc5f0 2180 {
nexpaq 0:6c56fb4bc5f0 2181 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
nexpaq 0:6c56fb4bc5f0 2182 unsigned char doublings = 1;
nexpaq 0:6c56fb4bc5f0 2183
nexpaq 0:6c56fb4bc5f0 2184 while( ratio != 0 )
nexpaq 0:6c56fb4bc5f0 2185 {
nexpaq 0:6c56fb4bc5f0 2186 ++doublings;
nexpaq 0:6c56fb4bc5f0 2187 ratio >>= 1;
nexpaq 0:6c56fb4bc5f0 2188 }
nexpaq 0:6c56fb4bc5f0 2189
nexpaq 0:6c56fb4bc5f0 2190 if( ++ssl->renego_records_seen > doublings )
nexpaq 0:6c56fb4bc5f0 2191 {
nexpaq 0:6c56fb4bc5f0 2192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
nexpaq 0:6c56fb4bc5f0 2193 return( 0 );
nexpaq 0:6c56fb4bc5f0 2194 }
nexpaq 0:6c56fb4bc5f0 2195 }
nexpaq 0:6c56fb4bc5f0 2196
nexpaq 0:6c56fb4bc5f0 2197 return( ssl_write_hello_request( ssl ) );
nexpaq 0:6c56fb4bc5f0 2198 }
nexpaq 0:6c56fb4bc5f0 2199 #endif
nexpaq 0:6c56fb4bc5f0 2200 #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
nexpaq 0:6c56fb4bc5f0 2201
nexpaq 0:6c56fb4bc5f0 2202 /*
nexpaq 0:6c56fb4bc5f0 2203 * Fill the input message buffer by appending data to it.
nexpaq 0:6c56fb4bc5f0 2204 * The amount of data already fetched is in ssl->in_left.
nexpaq 0:6c56fb4bc5f0 2205 *
nexpaq 0:6c56fb4bc5f0 2206 * If we return 0, is it guaranteed that (at least) nb_want bytes are
nexpaq 0:6c56fb4bc5f0 2207 * available (from this read and/or a previous one). Otherwise, an error code
nexpaq 0:6c56fb4bc5f0 2208 * is returned (possibly EOF or WANT_READ).
nexpaq 0:6c56fb4bc5f0 2209 *
nexpaq 0:6c56fb4bc5f0 2210 * With stream transport (TLS) on success ssl->in_left == nb_want, but
nexpaq 0:6c56fb4bc5f0 2211 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
nexpaq 0:6c56fb4bc5f0 2212 * since we always read a whole datagram at once.
nexpaq 0:6c56fb4bc5f0 2213 *
nexpaq 0:6c56fb4bc5f0 2214 * For DTLS, it is up to the caller to set ssl->next_record_offset when
nexpaq 0:6c56fb4bc5f0 2215 * they're done reading a record.
nexpaq 0:6c56fb4bc5f0 2216 */
nexpaq 0:6c56fb4bc5f0 2217 int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
nexpaq 0:6c56fb4bc5f0 2218 {
nexpaq 0:6c56fb4bc5f0 2219 int ret;
nexpaq 0:6c56fb4bc5f0 2220 size_t len;
nexpaq 0:6c56fb4bc5f0 2221
nexpaq 0:6c56fb4bc5f0 2222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
nexpaq 0:6c56fb4bc5f0 2223
nexpaq 0:6c56fb4bc5f0 2224 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
nexpaq 0:6c56fb4bc5f0 2225 {
nexpaq 0:6c56fb4bc5f0 2226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
nexpaq 0:6c56fb4bc5f0 2227 "or mbedtls_ssl_set_bio()" ) );
nexpaq 0:6c56fb4bc5f0 2228 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 2229 }
nexpaq 0:6c56fb4bc5f0 2230
nexpaq 0:6c56fb4bc5f0 2231 if( nb_want > MBEDTLS_SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
nexpaq 0:6c56fb4bc5f0 2232 {
nexpaq 0:6c56fb4bc5f0 2233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
nexpaq 0:6c56fb4bc5f0 2234 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 2235 }
nexpaq 0:6c56fb4bc5f0 2236
nexpaq 0:6c56fb4bc5f0 2237 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 2238 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
nexpaq 0:6c56fb4bc5f0 2239 {
nexpaq 0:6c56fb4bc5f0 2240 uint32_t timeout;
nexpaq 0:6c56fb4bc5f0 2241
nexpaq 0:6c56fb4bc5f0 2242 /* Just to be sure */
nexpaq 0:6c56fb4bc5f0 2243 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
nexpaq 0:6c56fb4bc5f0 2244 {
nexpaq 0:6c56fb4bc5f0 2245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
nexpaq 0:6c56fb4bc5f0 2246 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
nexpaq 0:6c56fb4bc5f0 2247 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 2248 }
nexpaq 0:6c56fb4bc5f0 2249
nexpaq 0:6c56fb4bc5f0 2250 /*
nexpaq 0:6c56fb4bc5f0 2251 * The point is, we need to always read a full datagram at once, so we
nexpaq 0:6c56fb4bc5f0 2252 * sometimes read more then requested, and handle the additional data.
nexpaq 0:6c56fb4bc5f0 2253 * It could be the rest of the current record (while fetching the
nexpaq 0:6c56fb4bc5f0 2254 * header) and/or some other records in the same datagram.
nexpaq 0:6c56fb4bc5f0 2255 */
nexpaq 0:6c56fb4bc5f0 2256
nexpaq 0:6c56fb4bc5f0 2257 /*
nexpaq 0:6c56fb4bc5f0 2258 * Move to the next record in the already read datagram if applicable
nexpaq 0:6c56fb4bc5f0 2259 */
nexpaq 0:6c56fb4bc5f0 2260 if( ssl->next_record_offset != 0 )
nexpaq 0:6c56fb4bc5f0 2261 {
nexpaq 0:6c56fb4bc5f0 2262 if( ssl->in_left < ssl->next_record_offset )
nexpaq 0:6c56fb4bc5f0 2263 {
nexpaq 0:6c56fb4bc5f0 2264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 2265 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 2266 }
nexpaq 0:6c56fb4bc5f0 2267
nexpaq 0:6c56fb4bc5f0 2268 ssl->in_left -= ssl->next_record_offset;
nexpaq 0:6c56fb4bc5f0 2269
nexpaq 0:6c56fb4bc5f0 2270 if( ssl->in_left != 0 )
nexpaq 0:6c56fb4bc5f0 2271 {
nexpaq 0:6c56fb4bc5f0 2272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
nexpaq 0:6c56fb4bc5f0 2273 ssl->next_record_offset ) );
nexpaq 0:6c56fb4bc5f0 2274 memmove( ssl->in_hdr,
nexpaq 0:6c56fb4bc5f0 2275 ssl->in_hdr + ssl->next_record_offset,
nexpaq 0:6c56fb4bc5f0 2276 ssl->in_left );
nexpaq 0:6c56fb4bc5f0 2277 }
nexpaq 0:6c56fb4bc5f0 2278
nexpaq 0:6c56fb4bc5f0 2279 ssl->next_record_offset = 0;
nexpaq 0:6c56fb4bc5f0 2280 }
nexpaq 0:6c56fb4bc5f0 2281
nexpaq 0:6c56fb4bc5f0 2282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
nexpaq 0:6c56fb4bc5f0 2283 ssl->in_left, nb_want ) );
nexpaq 0:6c56fb4bc5f0 2284
nexpaq 0:6c56fb4bc5f0 2285 /*
nexpaq 0:6c56fb4bc5f0 2286 * Done if we already have enough data.
nexpaq 0:6c56fb4bc5f0 2287 */
nexpaq 0:6c56fb4bc5f0 2288 if( nb_want <= ssl->in_left)
nexpaq 0:6c56fb4bc5f0 2289 {
nexpaq 0:6c56fb4bc5f0 2290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
nexpaq 0:6c56fb4bc5f0 2291 return( 0 );
nexpaq 0:6c56fb4bc5f0 2292 }
nexpaq 0:6c56fb4bc5f0 2293
nexpaq 0:6c56fb4bc5f0 2294 /*
nexpaq 0:6c56fb4bc5f0 2295 * A record can't be split accross datagrams. If we need to read but
nexpaq 0:6c56fb4bc5f0 2296 * are not at the beginning of a new record, the caller did something
nexpaq 0:6c56fb4bc5f0 2297 * wrong.
nexpaq 0:6c56fb4bc5f0 2298 */
nexpaq 0:6c56fb4bc5f0 2299 if( ssl->in_left != 0 )
nexpaq 0:6c56fb4bc5f0 2300 {
nexpaq 0:6c56fb4bc5f0 2301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 2302 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 2303 }
nexpaq 0:6c56fb4bc5f0 2304
nexpaq 0:6c56fb4bc5f0 2305 /*
nexpaq 0:6c56fb4bc5f0 2306 * Don't even try to read if time's out already.
nexpaq 0:6c56fb4bc5f0 2307 * This avoids by-passing the timer when repeatedly receiving messages
nexpaq 0:6c56fb4bc5f0 2308 * that will end up being dropped.
nexpaq 0:6c56fb4bc5f0 2309 */
nexpaq 0:6c56fb4bc5f0 2310 if( ssl_check_timer( ssl ) != 0 )
nexpaq 0:6c56fb4bc5f0 2311 ret = MBEDTLS_ERR_SSL_TIMEOUT;
nexpaq 0:6c56fb4bc5f0 2312 else
nexpaq 0:6c56fb4bc5f0 2313 {
nexpaq 0:6c56fb4bc5f0 2314 len = MBEDTLS_SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
nexpaq 0:6c56fb4bc5f0 2315
nexpaq 0:6c56fb4bc5f0 2316 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
nexpaq 0:6c56fb4bc5f0 2317 timeout = ssl->handshake->retransmit_timeout;
nexpaq 0:6c56fb4bc5f0 2318 else
nexpaq 0:6c56fb4bc5f0 2319 timeout = ssl->conf->read_timeout;
nexpaq 0:6c56fb4bc5f0 2320
nexpaq 0:6c56fb4bc5f0 2321 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
nexpaq 0:6c56fb4bc5f0 2322
nexpaq 0:6c56fb4bc5f0 2323 if( ssl->f_recv_timeout != NULL )
nexpaq 0:6c56fb4bc5f0 2324 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
nexpaq 0:6c56fb4bc5f0 2325 timeout );
nexpaq 0:6c56fb4bc5f0 2326 else
nexpaq 0:6c56fb4bc5f0 2327 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
nexpaq 0:6c56fb4bc5f0 2328
nexpaq 0:6c56fb4bc5f0 2329 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
nexpaq 0:6c56fb4bc5f0 2330
nexpaq 0:6c56fb4bc5f0 2331 if( ret == 0 )
nexpaq 0:6c56fb4bc5f0 2332 return( MBEDTLS_ERR_SSL_CONN_EOF );
nexpaq 0:6c56fb4bc5f0 2333 }
nexpaq 0:6c56fb4bc5f0 2334
nexpaq 0:6c56fb4bc5f0 2335 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
nexpaq 0:6c56fb4bc5f0 2336 {
nexpaq 0:6c56fb4bc5f0 2337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
nexpaq 0:6c56fb4bc5f0 2338 ssl_set_timer( ssl, 0 );
nexpaq 0:6c56fb4bc5f0 2339
nexpaq 0:6c56fb4bc5f0 2340 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
nexpaq 0:6c56fb4bc5f0 2341 {
nexpaq 0:6c56fb4bc5f0 2342 if( ssl_double_retransmit_timeout( ssl ) != 0 )
nexpaq 0:6c56fb4bc5f0 2343 {
nexpaq 0:6c56fb4bc5f0 2344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
nexpaq 0:6c56fb4bc5f0 2345 return( MBEDTLS_ERR_SSL_TIMEOUT );
nexpaq 0:6c56fb4bc5f0 2346 }
nexpaq 0:6c56fb4bc5f0 2347
nexpaq 0:6c56fb4bc5f0 2348 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 2349 {
nexpaq 0:6c56fb4bc5f0 2350 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
nexpaq 0:6c56fb4bc5f0 2351 return( ret );
nexpaq 0:6c56fb4bc5f0 2352 }
nexpaq 0:6c56fb4bc5f0 2353
nexpaq 0:6c56fb4bc5f0 2354 return( MBEDTLS_ERR_SSL_WANT_READ );
nexpaq 0:6c56fb4bc5f0 2355 }
nexpaq 0:6c56fb4bc5f0 2356 #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
nexpaq 0:6c56fb4bc5f0 2357 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
nexpaq 0:6c56fb4bc5f0 2358 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
nexpaq 0:6c56fb4bc5f0 2359 {
nexpaq 0:6c56fb4bc5f0 2360 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 2361 {
nexpaq 0:6c56fb4bc5f0 2362 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
nexpaq 0:6c56fb4bc5f0 2363 return( ret );
nexpaq 0:6c56fb4bc5f0 2364 }
nexpaq 0:6c56fb4bc5f0 2365
nexpaq 0:6c56fb4bc5f0 2366 return( MBEDTLS_ERR_SSL_WANT_READ );
nexpaq 0:6c56fb4bc5f0 2367 }
nexpaq 0:6c56fb4bc5f0 2368 #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
nexpaq 0:6c56fb4bc5f0 2369 }
nexpaq 0:6c56fb4bc5f0 2370
nexpaq 0:6c56fb4bc5f0 2371 if( ret < 0 )
nexpaq 0:6c56fb4bc5f0 2372 return( ret );
nexpaq 0:6c56fb4bc5f0 2373
nexpaq 0:6c56fb4bc5f0 2374 ssl->in_left = ret;
nexpaq 0:6c56fb4bc5f0 2375 }
nexpaq 0:6c56fb4bc5f0 2376 else
nexpaq 0:6c56fb4bc5f0 2377 #endif
nexpaq 0:6c56fb4bc5f0 2378 {
nexpaq 0:6c56fb4bc5f0 2379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
nexpaq 0:6c56fb4bc5f0 2380 ssl->in_left, nb_want ) );
nexpaq 0:6c56fb4bc5f0 2381
nexpaq 0:6c56fb4bc5f0 2382 while( ssl->in_left < nb_want )
nexpaq 0:6c56fb4bc5f0 2383 {
nexpaq 0:6c56fb4bc5f0 2384 len = nb_want - ssl->in_left;
nexpaq 0:6c56fb4bc5f0 2385
nexpaq 0:6c56fb4bc5f0 2386 if( ssl_check_timer( ssl ) != 0 )
nexpaq 0:6c56fb4bc5f0 2387 ret = MBEDTLS_ERR_SSL_TIMEOUT;
nexpaq 0:6c56fb4bc5f0 2388 else
nexpaq 0:6c56fb4bc5f0 2389 {
nexpaq 0:6c56fb4bc5f0 2390 if( ssl->f_recv_timeout != NULL )
nexpaq 0:6c56fb4bc5f0 2391 {
nexpaq 0:6c56fb4bc5f0 2392 ret = ssl->f_recv_timeout( ssl->p_bio,
nexpaq 0:6c56fb4bc5f0 2393 ssl->in_hdr + ssl->in_left, len,
nexpaq 0:6c56fb4bc5f0 2394 ssl->conf->read_timeout );
nexpaq 0:6c56fb4bc5f0 2395 }
nexpaq 0:6c56fb4bc5f0 2396 else
nexpaq 0:6c56fb4bc5f0 2397 {
nexpaq 0:6c56fb4bc5f0 2398 ret = ssl->f_recv( ssl->p_bio,
nexpaq 0:6c56fb4bc5f0 2399 ssl->in_hdr + ssl->in_left, len );
nexpaq 0:6c56fb4bc5f0 2400 }
nexpaq 0:6c56fb4bc5f0 2401 }
nexpaq 0:6c56fb4bc5f0 2402
nexpaq 0:6c56fb4bc5f0 2403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
nexpaq 0:6c56fb4bc5f0 2404 ssl->in_left, nb_want ) );
nexpaq 0:6c56fb4bc5f0 2405 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
nexpaq 0:6c56fb4bc5f0 2406
nexpaq 0:6c56fb4bc5f0 2407 if( ret == 0 )
nexpaq 0:6c56fb4bc5f0 2408 return( MBEDTLS_ERR_SSL_CONN_EOF );
nexpaq 0:6c56fb4bc5f0 2409
nexpaq 0:6c56fb4bc5f0 2410 if( ret < 0 )
nexpaq 0:6c56fb4bc5f0 2411 return( ret );
nexpaq 0:6c56fb4bc5f0 2412
nexpaq 0:6c56fb4bc5f0 2413 ssl->in_left += ret;
nexpaq 0:6c56fb4bc5f0 2414 }
nexpaq 0:6c56fb4bc5f0 2415 }
nexpaq 0:6c56fb4bc5f0 2416
nexpaq 0:6c56fb4bc5f0 2417 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
nexpaq 0:6c56fb4bc5f0 2418
nexpaq 0:6c56fb4bc5f0 2419 return( 0 );
nexpaq 0:6c56fb4bc5f0 2420 }
nexpaq 0:6c56fb4bc5f0 2421
nexpaq 0:6c56fb4bc5f0 2422 /*
nexpaq 0:6c56fb4bc5f0 2423 * Flush any data not yet written
nexpaq 0:6c56fb4bc5f0 2424 */
nexpaq 0:6c56fb4bc5f0 2425 int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 2426 {
nexpaq 0:6c56fb4bc5f0 2427 int ret;
nexpaq 0:6c56fb4bc5f0 2428 unsigned char *buf, i;
nexpaq 0:6c56fb4bc5f0 2429
nexpaq 0:6c56fb4bc5f0 2430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
nexpaq 0:6c56fb4bc5f0 2431
nexpaq 0:6c56fb4bc5f0 2432 if( ssl->f_send == NULL )
nexpaq 0:6c56fb4bc5f0 2433 {
nexpaq 0:6c56fb4bc5f0 2434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
nexpaq 0:6c56fb4bc5f0 2435 "or mbedtls_ssl_set_bio()" ) );
nexpaq 0:6c56fb4bc5f0 2436 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 2437 }
nexpaq 0:6c56fb4bc5f0 2438
nexpaq 0:6c56fb4bc5f0 2439 /* Avoid incrementing counter if data is flushed */
nexpaq 0:6c56fb4bc5f0 2440 if( ssl->out_left == 0 )
nexpaq 0:6c56fb4bc5f0 2441 {
nexpaq 0:6c56fb4bc5f0 2442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
nexpaq 0:6c56fb4bc5f0 2443 return( 0 );
nexpaq 0:6c56fb4bc5f0 2444 }
nexpaq 0:6c56fb4bc5f0 2445
nexpaq 0:6c56fb4bc5f0 2446 while( ssl->out_left > 0 )
nexpaq 0:6c56fb4bc5f0 2447 {
nexpaq 0:6c56fb4bc5f0 2448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
nexpaq 0:6c56fb4bc5f0 2449 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
nexpaq 0:6c56fb4bc5f0 2450
nexpaq 0:6c56fb4bc5f0 2451 buf = ssl->out_hdr + mbedtls_ssl_hdr_len( ssl ) +
nexpaq 0:6c56fb4bc5f0 2452 ssl->out_msglen - ssl->out_left;
nexpaq 0:6c56fb4bc5f0 2453 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
nexpaq 0:6c56fb4bc5f0 2454
nexpaq 0:6c56fb4bc5f0 2455 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
nexpaq 0:6c56fb4bc5f0 2456
nexpaq 0:6c56fb4bc5f0 2457 if( ret <= 0 )
nexpaq 0:6c56fb4bc5f0 2458 return( ret );
nexpaq 0:6c56fb4bc5f0 2459
nexpaq 0:6c56fb4bc5f0 2460 ssl->out_left -= ret;
nexpaq 0:6c56fb4bc5f0 2461 }
nexpaq 0:6c56fb4bc5f0 2462
nexpaq 0:6c56fb4bc5f0 2463 for( i = 8; i > ssl_ep_len( ssl ); i-- )
nexpaq 0:6c56fb4bc5f0 2464 if( ++ssl->out_ctr[i - 1] != 0 )
nexpaq 0:6c56fb4bc5f0 2465 break;
nexpaq 0:6c56fb4bc5f0 2466
nexpaq 0:6c56fb4bc5f0 2467 /* The loop goes to its end iff the counter is wrapping */
nexpaq 0:6c56fb4bc5f0 2468 if( i == ssl_ep_len( ssl ) )
nexpaq 0:6c56fb4bc5f0 2469 {
nexpaq 0:6c56fb4bc5f0 2470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
nexpaq 0:6c56fb4bc5f0 2471 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
nexpaq 0:6c56fb4bc5f0 2472 }
nexpaq 0:6c56fb4bc5f0 2473
nexpaq 0:6c56fb4bc5f0 2474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
nexpaq 0:6c56fb4bc5f0 2475
nexpaq 0:6c56fb4bc5f0 2476 return( 0 );
nexpaq 0:6c56fb4bc5f0 2477 }
nexpaq 0:6c56fb4bc5f0 2478
nexpaq 0:6c56fb4bc5f0 2479 /*
nexpaq 0:6c56fb4bc5f0 2480 * Functions to handle the DTLS retransmission state machine
nexpaq 0:6c56fb4bc5f0 2481 */
nexpaq 0:6c56fb4bc5f0 2482 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 2483 /*
nexpaq 0:6c56fb4bc5f0 2484 * Append current handshake message to current outgoing flight
nexpaq 0:6c56fb4bc5f0 2485 */
nexpaq 0:6c56fb4bc5f0 2486 static int ssl_flight_append( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 2487 {
nexpaq 0:6c56fb4bc5f0 2488 mbedtls_ssl_flight_item *msg;
nexpaq 0:6c56fb4bc5f0 2489
nexpaq 0:6c56fb4bc5f0 2490 /* Allocate space for current message */
nexpaq 0:6c56fb4bc5f0 2491 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
nexpaq 0:6c56fb4bc5f0 2492 {
nexpaq 0:6c56fb4bc5f0 2493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
nexpaq 0:6c56fb4bc5f0 2494 sizeof( mbedtls_ssl_flight_item ) ) );
nexpaq 0:6c56fb4bc5f0 2495 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
nexpaq 0:6c56fb4bc5f0 2496 }
nexpaq 0:6c56fb4bc5f0 2497
nexpaq 0:6c56fb4bc5f0 2498 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
nexpaq 0:6c56fb4bc5f0 2499 {
nexpaq 0:6c56fb4bc5f0 2500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
nexpaq 0:6c56fb4bc5f0 2501 mbedtls_free( msg );
nexpaq 0:6c56fb4bc5f0 2502 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
nexpaq 0:6c56fb4bc5f0 2503 }
nexpaq 0:6c56fb4bc5f0 2504
nexpaq 0:6c56fb4bc5f0 2505 /* Copy current handshake message with headers */
nexpaq 0:6c56fb4bc5f0 2506 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
nexpaq 0:6c56fb4bc5f0 2507 msg->len = ssl->out_msglen;
nexpaq 0:6c56fb4bc5f0 2508 msg->type = ssl->out_msgtype;
nexpaq 0:6c56fb4bc5f0 2509 msg->next = NULL;
nexpaq 0:6c56fb4bc5f0 2510
nexpaq 0:6c56fb4bc5f0 2511 /* Append to the current flight */
nexpaq 0:6c56fb4bc5f0 2512 if( ssl->handshake->flight == NULL )
nexpaq 0:6c56fb4bc5f0 2513 ssl->handshake->flight = msg;
nexpaq 0:6c56fb4bc5f0 2514 else
nexpaq 0:6c56fb4bc5f0 2515 {
nexpaq 0:6c56fb4bc5f0 2516 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
nexpaq 0:6c56fb4bc5f0 2517 while( cur->next != NULL )
nexpaq 0:6c56fb4bc5f0 2518 cur = cur->next;
nexpaq 0:6c56fb4bc5f0 2519 cur->next = msg;
nexpaq 0:6c56fb4bc5f0 2520 }
nexpaq 0:6c56fb4bc5f0 2521
nexpaq 0:6c56fb4bc5f0 2522 return( 0 );
nexpaq 0:6c56fb4bc5f0 2523 }
nexpaq 0:6c56fb4bc5f0 2524
nexpaq 0:6c56fb4bc5f0 2525 /*
nexpaq 0:6c56fb4bc5f0 2526 * Free the current flight of handshake messages
nexpaq 0:6c56fb4bc5f0 2527 */
nexpaq 0:6c56fb4bc5f0 2528 static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
nexpaq 0:6c56fb4bc5f0 2529 {
nexpaq 0:6c56fb4bc5f0 2530 mbedtls_ssl_flight_item *cur = flight;
nexpaq 0:6c56fb4bc5f0 2531 mbedtls_ssl_flight_item *next;
nexpaq 0:6c56fb4bc5f0 2532
nexpaq 0:6c56fb4bc5f0 2533 while( cur != NULL )
nexpaq 0:6c56fb4bc5f0 2534 {
nexpaq 0:6c56fb4bc5f0 2535 next = cur->next;
nexpaq 0:6c56fb4bc5f0 2536
nexpaq 0:6c56fb4bc5f0 2537 mbedtls_free( cur->p );
nexpaq 0:6c56fb4bc5f0 2538 mbedtls_free( cur );
nexpaq 0:6c56fb4bc5f0 2539
nexpaq 0:6c56fb4bc5f0 2540 cur = next;
nexpaq 0:6c56fb4bc5f0 2541 }
nexpaq 0:6c56fb4bc5f0 2542 }
nexpaq 0:6c56fb4bc5f0 2543
nexpaq 0:6c56fb4bc5f0 2544 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
nexpaq 0:6c56fb4bc5f0 2545 static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
nexpaq 0:6c56fb4bc5f0 2546 #endif
nexpaq 0:6c56fb4bc5f0 2547
nexpaq 0:6c56fb4bc5f0 2548 /*
nexpaq 0:6c56fb4bc5f0 2549 * Swap transform_out and out_ctr with the alternative ones
nexpaq 0:6c56fb4bc5f0 2550 */
nexpaq 0:6c56fb4bc5f0 2551 static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 2552 {
nexpaq 0:6c56fb4bc5f0 2553 mbedtls_ssl_transform *tmp_transform;
nexpaq 0:6c56fb4bc5f0 2554 unsigned char tmp_out_ctr[8];
nexpaq 0:6c56fb4bc5f0 2555
nexpaq 0:6c56fb4bc5f0 2556 if( ssl->transform_out == ssl->handshake->alt_transform_out )
nexpaq 0:6c56fb4bc5f0 2557 {
nexpaq 0:6c56fb4bc5f0 2558 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
nexpaq 0:6c56fb4bc5f0 2559 return;
nexpaq 0:6c56fb4bc5f0 2560 }
nexpaq 0:6c56fb4bc5f0 2561
nexpaq 0:6c56fb4bc5f0 2562 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
nexpaq 0:6c56fb4bc5f0 2563
nexpaq 0:6c56fb4bc5f0 2564 /* Swap transforms */
nexpaq 0:6c56fb4bc5f0 2565 tmp_transform = ssl->transform_out;
nexpaq 0:6c56fb4bc5f0 2566 ssl->transform_out = ssl->handshake->alt_transform_out;
nexpaq 0:6c56fb4bc5f0 2567 ssl->handshake->alt_transform_out = tmp_transform;
nexpaq 0:6c56fb4bc5f0 2568
nexpaq 0:6c56fb4bc5f0 2569 /* Swap epoch + sequence_number */
nexpaq 0:6c56fb4bc5f0 2570 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
nexpaq 0:6c56fb4bc5f0 2571 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
nexpaq 0:6c56fb4bc5f0 2572 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
nexpaq 0:6c56fb4bc5f0 2573
nexpaq 0:6c56fb4bc5f0 2574 /* Adjust to the newly activated transform */
nexpaq 0:6c56fb4bc5f0 2575 if( ssl->transform_out != NULL &&
nexpaq 0:6c56fb4bc5f0 2576 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
nexpaq 0:6c56fb4bc5f0 2577 {
nexpaq 0:6c56fb4bc5f0 2578 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
nexpaq 0:6c56fb4bc5f0 2579 ssl->transform_out->fixed_ivlen;
nexpaq 0:6c56fb4bc5f0 2580 }
nexpaq 0:6c56fb4bc5f0 2581 else
nexpaq 0:6c56fb4bc5f0 2582 ssl->out_msg = ssl->out_iv;
nexpaq 0:6c56fb4bc5f0 2583
nexpaq 0:6c56fb4bc5f0 2584 #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
nexpaq 0:6c56fb4bc5f0 2585 if( mbedtls_ssl_hw_record_activate != NULL )
nexpaq 0:6c56fb4bc5f0 2586 {
nexpaq 0:6c56fb4bc5f0 2587 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 2588 {
nexpaq 0:6c56fb4bc5f0 2589 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
nexpaq 0:6c56fb4bc5f0 2590 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
nexpaq 0:6c56fb4bc5f0 2591 }
nexpaq 0:6c56fb4bc5f0 2592 }
nexpaq 0:6c56fb4bc5f0 2593 #endif
nexpaq 0:6c56fb4bc5f0 2594 }
nexpaq 0:6c56fb4bc5f0 2595
nexpaq 0:6c56fb4bc5f0 2596 /*
nexpaq 0:6c56fb4bc5f0 2597 * Retransmit the current flight of messages.
nexpaq 0:6c56fb4bc5f0 2598 *
nexpaq 0:6c56fb4bc5f0 2599 * Need to remember the current message in case flush_output returns
nexpaq 0:6c56fb4bc5f0 2600 * WANT_WRITE, causing us to exit this function and come back later.
nexpaq 0:6c56fb4bc5f0 2601 * This function must be called until state is no longer SENDING.
nexpaq 0:6c56fb4bc5f0 2602 */
nexpaq 0:6c56fb4bc5f0 2603 int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 2604 {
nexpaq 0:6c56fb4bc5f0 2605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
nexpaq 0:6c56fb4bc5f0 2606
nexpaq 0:6c56fb4bc5f0 2607 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
nexpaq 0:6c56fb4bc5f0 2608 {
nexpaq 0:6c56fb4bc5f0 2609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
nexpaq 0:6c56fb4bc5f0 2610
nexpaq 0:6c56fb4bc5f0 2611 ssl->handshake->cur_msg = ssl->handshake->flight;
nexpaq 0:6c56fb4bc5f0 2612 ssl_swap_epochs( ssl );
nexpaq 0:6c56fb4bc5f0 2613
nexpaq 0:6c56fb4bc5f0 2614 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
nexpaq 0:6c56fb4bc5f0 2615 }
nexpaq 0:6c56fb4bc5f0 2616
nexpaq 0:6c56fb4bc5f0 2617 while( ssl->handshake->cur_msg != NULL )
nexpaq 0:6c56fb4bc5f0 2618 {
nexpaq 0:6c56fb4bc5f0 2619 int ret;
nexpaq 0:6c56fb4bc5f0 2620 mbedtls_ssl_flight_item *cur = ssl->handshake->cur_msg;
nexpaq 0:6c56fb4bc5f0 2621
nexpaq 0:6c56fb4bc5f0 2622 /* Swap epochs before sending Finished: we can't do it after
nexpaq 0:6c56fb4bc5f0 2623 * sending ChangeCipherSpec, in case write returns WANT_READ.
nexpaq 0:6c56fb4bc5f0 2624 * Must be done before copying, may change out_msg pointer */
nexpaq 0:6c56fb4bc5f0 2625 if( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
nexpaq 0:6c56fb4bc5f0 2626 cur->p[0] == MBEDTLS_SSL_HS_FINISHED )
nexpaq 0:6c56fb4bc5f0 2627 {
nexpaq 0:6c56fb4bc5f0 2628 ssl_swap_epochs( ssl );
nexpaq 0:6c56fb4bc5f0 2629 }
nexpaq 0:6c56fb4bc5f0 2630
nexpaq 0:6c56fb4bc5f0 2631 memcpy( ssl->out_msg, cur->p, cur->len );
nexpaq 0:6c56fb4bc5f0 2632 ssl->out_msglen = cur->len;
nexpaq 0:6c56fb4bc5f0 2633 ssl->out_msgtype = cur->type;
nexpaq 0:6c56fb4bc5f0 2634
nexpaq 0:6c56fb4bc5f0 2635 ssl->handshake->cur_msg = cur->next;
nexpaq 0:6c56fb4bc5f0 2636
nexpaq 0:6c56fb4bc5f0 2637 MBEDTLS_SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
nexpaq 0:6c56fb4bc5f0 2638
nexpaq 0:6c56fb4bc5f0 2639 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 2640 {
nexpaq 0:6c56fb4bc5f0 2641 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
nexpaq 0:6c56fb4bc5f0 2642 return( ret );
nexpaq 0:6c56fb4bc5f0 2643 }
nexpaq 0:6c56fb4bc5f0 2644 }
nexpaq 0:6c56fb4bc5f0 2645
nexpaq 0:6c56fb4bc5f0 2646 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
nexpaq 0:6c56fb4bc5f0 2647 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
nexpaq 0:6c56fb4bc5f0 2648 else
nexpaq 0:6c56fb4bc5f0 2649 {
nexpaq 0:6c56fb4bc5f0 2650 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
nexpaq 0:6c56fb4bc5f0 2651 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
nexpaq 0:6c56fb4bc5f0 2652 }
nexpaq 0:6c56fb4bc5f0 2653
nexpaq 0:6c56fb4bc5f0 2654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
nexpaq 0:6c56fb4bc5f0 2655
nexpaq 0:6c56fb4bc5f0 2656 return( 0 );
nexpaq 0:6c56fb4bc5f0 2657 }
nexpaq 0:6c56fb4bc5f0 2658
nexpaq 0:6c56fb4bc5f0 2659 /*
nexpaq 0:6c56fb4bc5f0 2660 * To be called when the last message of an incoming flight is received.
nexpaq 0:6c56fb4bc5f0 2661 */
nexpaq 0:6c56fb4bc5f0 2662 void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 2663 {
nexpaq 0:6c56fb4bc5f0 2664 /* We won't need to resend that one any more */
nexpaq 0:6c56fb4bc5f0 2665 ssl_flight_free( ssl->handshake->flight );
nexpaq 0:6c56fb4bc5f0 2666 ssl->handshake->flight = NULL;
nexpaq 0:6c56fb4bc5f0 2667 ssl->handshake->cur_msg = NULL;
nexpaq 0:6c56fb4bc5f0 2668
nexpaq 0:6c56fb4bc5f0 2669 /* The next incoming flight will start with this msg_seq */
nexpaq 0:6c56fb4bc5f0 2670 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
nexpaq 0:6c56fb4bc5f0 2671
nexpaq 0:6c56fb4bc5f0 2672 /* Cancel timer */
nexpaq 0:6c56fb4bc5f0 2673 ssl_set_timer( ssl, 0 );
nexpaq 0:6c56fb4bc5f0 2674
nexpaq 0:6c56fb4bc5f0 2675 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
nexpaq 0:6c56fb4bc5f0 2676 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
nexpaq 0:6c56fb4bc5f0 2677 {
nexpaq 0:6c56fb4bc5f0 2678 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
nexpaq 0:6c56fb4bc5f0 2679 }
nexpaq 0:6c56fb4bc5f0 2680 else
nexpaq 0:6c56fb4bc5f0 2681 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
nexpaq 0:6c56fb4bc5f0 2682 }
nexpaq 0:6c56fb4bc5f0 2683
nexpaq 0:6c56fb4bc5f0 2684 /*
nexpaq 0:6c56fb4bc5f0 2685 * To be called when the last message of an outgoing flight is send.
nexpaq 0:6c56fb4bc5f0 2686 */
nexpaq 0:6c56fb4bc5f0 2687 void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 2688 {
nexpaq 0:6c56fb4bc5f0 2689 ssl_reset_retransmit_timeout( ssl );
nexpaq 0:6c56fb4bc5f0 2690 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
nexpaq 0:6c56fb4bc5f0 2691
nexpaq 0:6c56fb4bc5f0 2692 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
nexpaq 0:6c56fb4bc5f0 2693 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
nexpaq 0:6c56fb4bc5f0 2694 {
nexpaq 0:6c56fb4bc5f0 2695 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
nexpaq 0:6c56fb4bc5f0 2696 }
nexpaq 0:6c56fb4bc5f0 2697 else
nexpaq 0:6c56fb4bc5f0 2698 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
nexpaq 0:6c56fb4bc5f0 2699 }
nexpaq 0:6c56fb4bc5f0 2700 #endif /* MBEDTLS_SSL_PROTO_DTLS */
nexpaq 0:6c56fb4bc5f0 2701
nexpaq 0:6c56fb4bc5f0 2702 /*
nexpaq 0:6c56fb4bc5f0 2703 * Record layer functions
nexpaq 0:6c56fb4bc5f0 2704 */
nexpaq 0:6c56fb4bc5f0 2705
nexpaq 0:6c56fb4bc5f0 2706 /*
nexpaq 0:6c56fb4bc5f0 2707 * Write current record.
nexpaq 0:6c56fb4bc5f0 2708 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
nexpaq 0:6c56fb4bc5f0 2709 */
nexpaq 0:6c56fb4bc5f0 2710 int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 2711 {
nexpaq 0:6c56fb4bc5f0 2712 int ret, done = 0, out_msg_type;
nexpaq 0:6c56fb4bc5f0 2713 size_t len = ssl->out_msglen;
nexpaq 0:6c56fb4bc5f0 2714
nexpaq 0:6c56fb4bc5f0 2715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
nexpaq 0:6c56fb4bc5f0 2716
nexpaq 0:6c56fb4bc5f0 2717 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 2718 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
nexpaq 0:6c56fb4bc5f0 2719 ssl->handshake != NULL &&
nexpaq 0:6c56fb4bc5f0 2720 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
nexpaq 0:6c56fb4bc5f0 2721 {
nexpaq 0:6c56fb4bc5f0 2722 ; /* Skip special handshake treatment when resending */
nexpaq 0:6c56fb4bc5f0 2723 }
nexpaq 0:6c56fb4bc5f0 2724 else
nexpaq 0:6c56fb4bc5f0 2725 #endif
nexpaq 0:6c56fb4bc5f0 2726 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
nexpaq 0:6c56fb4bc5f0 2727 {
nexpaq 0:6c56fb4bc5f0 2728 out_msg_type = ssl->out_msg[0];
nexpaq 0:6c56fb4bc5f0 2729
nexpaq 0:6c56fb4bc5f0 2730 if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
nexpaq 0:6c56fb4bc5f0 2731 ssl->handshake == NULL )
nexpaq 0:6c56fb4bc5f0 2732 {
nexpaq 0:6c56fb4bc5f0 2733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 2734 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 2735 }
nexpaq 0:6c56fb4bc5f0 2736
nexpaq 0:6c56fb4bc5f0 2737 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
nexpaq 0:6c56fb4bc5f0 2738 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
nexpaq 0:6c56fb4bc5f0 2739 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
nexpaq 0:6c56fb4bc5f0 2740
nexpaq 0:6c56fb4bc5f0 2741 /*
nexpaq 0:6c56fb4bc5f0 2742 * DTLS has additional fields in the Handshake layer,
nexpaq 0:6c56fb4bc5f0 2743 * between the length field and the actual payload:
nexpaq 0:6c56fb4bc5f0 2744 * uint16 message_seq;
nexpaq 0:6c56fb4bc5f0 2745 * uint24 fragment_offset;
nexpaq 0:6c56fb4bc5f0 2746 * uint24 fragment_length;
nexpaq 0:6c56fb4bc5f0 2747 */
nexpaq 0:6c56fb4bc5f0 2748 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 2749 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
nexpaq 0:6c56fb4bc5f0 2750 {
nexpaq 0:6c56fb4bc5f0 2751 /* Make room for the additional DTLS fields */
nexpaq 0:6c56fb4bc5f0 2752 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
nexpaq 0:6c56fb4bc5f0 2753 ssl->out_msglen += 8;
nexpaq 0:6c56fb4bc5f0 2754 len += 8;
nexpaq 0:6c56fb4bc5f0 2755
nexpaq 0:6c56fb4bc5f0 2756 /* Write message_seq and update it, except for HelloRequest */
nexpaq 0:6c56fb4bc5f0 2757 if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
nexpaq 0:6c56fb4bc5f0 2758 {
nexpaq 0:6c56fb4bc5f0 2759 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
nexpaq 0:6c56fb4bc5f0 2760 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
nexpaq 0:6c56fb4bc5f0 2761 ++( ssl->handshake->out_msg_seq );
nexpaq 0:6c56fb4bc5f0 2762 }
nexpaq 0:6c56fb4bc5f0 2763 else
nexpaq 0:6c56fb4bc5f0 2764 {
nexpaq 0:6c56fb4bc5f0 2765 ssl->out_msg[4] = 0;
nexpaq 0:6c56fb4bc5f0 2766 ssl->out_msg[5] = 0;
nexpaq 0:6c56fb4bc5f0 2767 }
nexpaq 0:6c56fb4bc5f0 2768
nexpaq 0:6c56fb4bc5f0 2769 /* We don't fragment, so frag_offset = 0 and frag_len = len */
nexpaq 0:6c56fb4bc5f0 2770 memset( ssl->out_msg + 6, 0x00, 3 );
nexpaq 0:6c56fb4bc5f0 2771 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
nexpaq 0:6c56fb4bc5f0 2772 }
nexpaq 0:6c56fb4bc5f0 2773 #endif /* MBEDTLS_SSL_PROTO_DTLS */
nexpaq 0:6c56fb4bc5f0 2774
nexpaq 0:6c56fb4bc5f0 2775 if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
nexpaq 0:6c56fb4bc5f0 2776 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
nexpaq 0:6c56fb4bc5f0 2777 }
nexpaq 0:6c56fb4bc5f0 2778
nexpaq 0:6c56fb4bc5f0 2779 /* Save handshake and CCS messages for resending */
nexpaq 0:6c56fb4bc5f0 2780 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 2781 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
nexpaq 0:6c56fb4bc5f0 2782 ssl->handshake != NULL &&
nexpaq 0:6c56fb4bc5f0 2783 ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING &&
nexpaq 0:6c56fb4bc5f0 2784 ( ssl->out_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ||
nexpaq 0:6c56fb4bc5f0 2785 ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) )
nexpaq 0:6c56fb4bc5f0 2786 {
nexpaq 0:6c56fb4bc5f0 2787 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 2788 {
nexpaq 0:6c56fb4bc5f0 2789 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
nexpaq 0:6c56fb4bc5f0 2790 return( ret );
nexpaq 0:6c56fb4bc5f0 2791 }
nexpaq 0:6c56fb4bc5f0 2792 }
nexpaq 0:6c56fb4bc5f0 2793 #endif
nexpaq 0:6c56fb4bc5f0 2794
nexpaq 0:6c56fb4bc5f0 2795 #if defined(MBEDTLS_ZLIB_SUPPORT)
nexpaq 0:6c56fb4bc5f0 2796 if( ssl->transform_out != NULL &&
nexpaq 0:6c56fb4bc5f0 2797 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
nexpaq 0:6c56fb4bc5f0 2798 {
nexpaq 0:6c56fb4bc5f0 2799 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 2800 {
nexpaq 0:6c56fb4bc5f0 2801 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
nexpaq 0:6c56fb4bc5f0 2802 return( ret );
nexpaq 0:6c56fb4bc5f0 2803 }
nexpaq 0:6c56fb4bc5f0 2804
nexpaq 0:6c56fb4bc5f0 2805 len = ssl->out_msglen;
nexpaq 0:6c56fb4bc5f0 2806 }
nexpaq 0:6c56fb4bc5f0 2807 #endif /*MBEDTLS_ZLIB_SUPPORT */
nexpaq 0:6c56fb4bc5f0 2808
nexpaq 0:6c56fb4bc5f0 2809 #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
nexpaq 0:6c56fb4bc5f0 2810 if( mbedtls_ssl_hw_record_write != NULL )
nexpaq 0:6c56fb4bc5f0 2811 {
nexpaq 0:6c56fb4bc5f0 2812 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
nexpaq 0:6c56fb4bc5f0 2813
nexpaq 0:6c56fb4bc5f0 2814 ret = mbedtls_ssl_hw_record_write( ssl );
nexpaq 0:6c56fb4bc5f0 2815 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
nexpaq 0:6c56fb4bc5f0 2816 {
nexpaq 0:6c56fb4bc5f0 2817 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
nexpaq 0:6c56fb4bc5f0 2818 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
nexpaq 0:6c56fb4bc5f0 2819 }
nexpaq 0:6c56fb4bc5f0 2820
nexpaq 0:6c56fb4bc5f0 2821 if( ret == 0 )
nexpaq 0:6c56fb4bc5f0 2822 done = 1;
nexpaq 0:6c56fb4bc5f0 2823 }
nexpaq 0:6c56fb4bc5f0 2824 #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
nexpaq 0:6c56fb4bc5f0 2825 if( !done )
nexpaq 0:6c56fb4bc5f0 2826 {
nexpaq 0:6c56fb4bc5f0 2827 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
nexpaq 0:6c56fb4bc5f0 2828 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
nexpaq 0:6c56fb4bc5f0 2829 ssl->conf->transport, ssl->out_hdr + 1 );
nexpaq 0:6c56fb4bc5f0 2830
nexpaq 0:6c56fb4bc5f0 2831 ssl->out_len[0] = (unsigned char)( len >> 8 );
nexpaq 0:6c56fb4bc5f0 2832 ssl->out_len[1] = (unsigned char)( len );
nexpaq 0:6c56fb4bc5f0 2833
nexpaq 0:6c56fb4bc5f0 2834 if( ssl->transform_out != NULL )
nexpaq 0:6c56fb4bc5f0 2835 {
nexpaq 0:6c56fb4bc5f0 2836 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 2837 {
nexpaq 0:6c56fb4bc5f0 2838 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
nexpaq 0:6c56fb4bc5f0 2839 return( ret );
nexpaq 0:6c56fb4bc5f0 2840 }
nexpaq 0:6c56fb4bc5f0 2841
nexpaq 0:6c56fb4bc5f0 2842 len = ssl->out_msglen;
nexpaq 0:6c56fb4bc5f0 2843 ssl->out_len[0] = (unsigned char)( len >> 8 );
nexpaq 0:6c56fb4bc5f0 2844 ssl->out_len[1] = (unsigned char)( len );
nexpaq 0:6c56fb4bc5f0 2845 }
nexpaq 0:6c56fb4bc5f0 2846
nexpaq 0:6c56fb4bc5f0 2847 ssl->out_left = mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen;
nexpaq 0:6c56fb4bc5f0 2848
nexpaq 0:6c56fb4bc5f0 2849 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
nexpaq 0:6c56fb4bc5f0 2850 "version = [%d:%d], msglen = %d",
nexpaq 0:6c56fb4bc5f0 2851 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
nexpaq 0:6c56fb4bc5f0 2852 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
nexpaq 0:6c56fb4bc5f0 2853
nexpaq 0:6c56fb4bc5f0 2854 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
nexpaq 0:6c56fb4bc5f0 2855 ssl->out_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen );
nexpaq 0:6c56fb4bc5f0 2856 }
nexpaq 0:6c56fb4bc5f0 2857
nexpaq 0:6c56fb4bc5f0 2858 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 2859 {
nexpaq 0:6c56fb4bc5f0 2860 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
nexpaq 0:6c56fb4bc5f0 2861 return( ret );
nexpaq 0:6c56fb4bc5f0 2862 }
nexpaq 0:6c56fb4bc5f0 2863
nexpaq 0:6c56fb4bc5f0 2864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
nexpaq 0:6c56fb4bc5f0 2865
nexpaq 0:6c56fb4bc5f0 2866 return( 0 );
nexpaq 0:6c56fb4bc5f0 2867 }
nexpaq 0:6c56fb4bc5f0 2868
nexpaq 0:6c56fb4bc5f0 2869 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 2870 /*
nexpaq 0:6c56fb4bc5f0 2871 * Mark bits in bitmask (used for DTLS HS reassembly)
nexpaq 0:6c56fb4bc5f0 2872 */
nexpaq 0:6c56fb4bc5f0 2873 static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
nexpaq 0:6c56fb4bc5f0 2874 {
nexpaq 0:6c56fb4bc5f0 2875 unsigned int start_bits, end_bits;
nexpaq 0:6c56fb4bc5f0 2876
nexpaq 0:6c56fb4bc5f0 2877 start_bits = 8 - ( offset % 8 );
nexpaq 0:6c56fb4bc5f0 2878 if( start_bits != 8 )
nexpaq 0:6c56fb4bc5f0 2879 {
nexpaq 0:6c56fb4bc5f0 2880 size_t first_byte_idx = offset / 8;
nexpaq 0:6c56fb4bc5f0 2881
nexpaq 0:6c56fb4bc5f0 2882 /* Special case */
nexpaq 0:6c56fb4bc5f0 2883 if( len <= start_bits )
nexpaq 0:6c56fb4bc5f0 2884 {
nexpaq 0:6c56fb4bc5f0 2885 for( ; len != 0; len-- )
nexpaq 0:6c56fb4bc5f0 2886 mask[first_byte_idx] |= 1 << ( start_bits - len );
nexpaq 0:6c56fb4bc5f0 2887
nexpaq 0:6c56fb4bc5f0 2888 /* Avoid potential issues with offset or len becoming invalid */
nexpaq 0:6c56fb4bc5f0 2889 return;
nexpaq 0:6c56fb4bc5f0 2890 }
nexpaq 0:6c56fb4bc5f0 2891
nexpaq 0:6c56fb4bc5f0 2892 offset += start_bits; /* Now offset % 8 == 0 */
nexpaq 0:6c56fb4bc5f0 2893 len -= start_bits;
nexpaq 0:6c56fb4bc5f0 2894
nexpaq 0:6c56fb4bc5f0 2895 for( ; start_bits != 0; start_bits-- )
nexpaq 0:6c56fb4bc5f0 2896 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
nexpaq 0:6c56fb4bc5f0 2897 }
nexpaq 0:6c56fb4bc5f0 2898
nexpaq 0:6c56fb4bc5f0 2899 end_bits = len % 8;
nexpaq 0:6c56fb4bc5f0 2900 if( end_bits != 0 )
nexpaq 0:6c56fb4bc5f0 2901 {
nexpaq 0:6c56fb4bc5f0 2902 size_t last_byte_idx = ( offset + len ) / 8;
nexpaq 0:6c56fb4bc5f0 2903
nexpaq 0:6c56fb4bc5f0 2904 len -= end_bits; /* Now len % 8 == 0 */
nexpaq 0:6c56fb4bc5f0 2905
nexpaq 0:6c56fb4bc5f0 2906 for( ; end_bits != 0; end_bits-- )
nexpaq 0:6c56fb4bc5f0 2907 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
nexpaq 0:6c56fb4bc5f0 2908 }
nexpaq 0:6c56fb4bc5f0 2909
nexpaq 0:6c56fb4bc5f0 2910 memset( mask + offset / 8, 0xFF, len / 8 );
nexpaq 0:6c56fb4bc5f0 2911 }
nexpaq 0:6c56fb4bc5f0 2912
nexpaq 0:6c56fb4bc5f0 2913 /*
nexpaq 0:6c56fb4bc5f0 2914 * Check that bitmask is full
nexpaq 0:6c56fb4bc5f0 2915 */
nexpaq 0:6c56fb4bc5f0 2916 static int ssl_bitmask_check( unsigned char *mask, size_t len )
nexpaq 0:6c56fb4bc5f0 2917 {
nexpaq 0:6c56fb4bc5f0 2918 size_t i;
nexpaq 0:6c56fb4bc5f0 2919
nexpaq 0:6c56fb4bc5f0 2920 for( i = 0; i < len / 8; i++ )
nexpaq 0:6c56fb4bc5f0 2921 if( mask[i] != 0xFF )
nexpaq 0:6c56fb4bc5f0 2922 return( -1 );
nexpaq 0:6c56fb4bc5f0 2923
nexpaq 0:6c56fb4bc5f0 2924 for( i = 0; i < len % 8; i++ )
nexpaq 0:6c56fb4bc5f0 2925 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
nexpaq 0:6c56fb4bc5f0 2926 return( -1 );
nexpaq 0:6c56fb4bc5f0 2927
nexpaq 0:6c56fb4bc5f0 2928 return( 0 );
nexpaq 0:6c56fb4bc5f0 2929 }
nexpaq 0:6c56fb4bc5f0 2930
nexpaq 0:6c56fb4bc5f0 2931 /*
nexpaq 0:6c56fb4bc5f0 2932 * Reassemble fragmented DTLS handshake messages.
nexpaq 0:6c56fb4bc5f0 2933 *
nexpaq 0:6c56fb4bc5f0 2934 * Use a temporary buffer for reassembly, divided in two parts:
nexpaq 0:6c56fb4bc5f0 2935 * - the first holds the reassembled message (including handshake header),
nexpaq 0:6c56fb4bc5f0 2936 * - the second holds a bitmask indicating which parts of the message
nexpaq 0:6c56fb4bc5f0 2937 * (excluding headers) have been received so far.
nexpaq 0:6c56fb4bc5f0 2938 */
nexpaq 0:6c56fb4bc5f0 2939 static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 2940 {
nexpaq 0:6c56fb4bc5f0 2941 unsigned char *msg, *bitmask;
nexpaq 0:6c56fb4bc5f0 2942 size_t frag_len, frag_off;
nexpaq 0:6c56fb4bc5f0 2943 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
nexpaq 0:6c56fb4bc5f0 2944
nexpaq 0:6c56fb4bc5f0 2945 if( ssl->handshake == NULL )
nexpaq 0:6c56fb4bc5f0 2946 {
nexpaq 0:6c56fb4bc5f0 2947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
nexpaq 0:6c56fb4bc5f0 2948 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
nexpaq 0:6c56fb4bc5f0 2949 }
nexpaq 0:6c56fb4bc5f0 2950
nexpaq 0:6c56fb4bc5f0 2951 /*
nexpaq 0:6c56fb4bc5f0 2952 * For first fragment, check size and allocate buffer
nexpaq 0:6c56fb4bc5f0 2953 */
nexpaq 0:6c56fb4bc5f0 2954 if( ssl->handshake->hs_msg == NULL )
nexpaq 0:6c56fb4bc5f0 2955 {
nexpaq 0:6c56fb4bc5f0 2956 size_t alloc_len;
nexpaq 0:6c56fb4bc5f0 2957
nexpaq 0:6c56fb4bc5f0 2958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
nexpaq 0:6c56fb4bc5f0 2959 msg_len ) );
nexpaq 0:6c56fb4bc5f0 2960
nexpaq 0:6c56fb4bc5f0 2961 if( ssl->in_hslen > MBEDTLS_SSL_MAX_CONTENT_LEN )
nexpaq 0:6c56fb4bc5f0 2962 {
nexpaq 0:6c56fb4bc5f0 2963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
nexpaq 0:6c56fb4bc5f0 2964 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
nexpaq 0:6c56fb4bc5f0 2965 }
nexpaq 0:6c56fb4bc5f0 2966
nexpaq 0:6c56fb4bc5f0 2967 /* The bitmask needs one bit per byte of message excluding header */
nexpaq 0:6c56fb4bc5f0 2968 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
nexpaq 0:6c56fb4bc5f0 2969
nexpaq 0:6c56fb4bc5f0 2970 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
nexpaq 0:6c56fb4bc5f0 2971 if( ssl->handshake->hs_msg == NULL )
nexpaq 0:6c56fb4bc5f0 2972 {
nexpaq 0:6c56fb4bc5f0 2973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
nexpaq 0:6c56fb4bc5f0 2974 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
nexpaq 0:6c56fb4bc5f0 2975 }
nexpaq 0:6c56fb4bc5f0 2976
nexpaq 0:6c56fb4bc5f0 2977 /* Prepare final header: copy msg_type, length and message_seq,
nexpaq 0:6c56fb4bc5f0 2978 * then add standardised fragment_offset and fragment_length */
nexpaq 0:6c56fb4bc5f0 2979 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
nexpaq 0:6c56fb4bc5f0 2980 memset( ssl->handshake->hs_msg + 6, 0, 3 );
nexpaq 0:6c56fb4bc5f0 2981 memcpy( ssl->handshake->hs_msg + 9,
nexpaq 0:6c56fb4bc5f0 2982 ssl->handshake->hs_msg + 1, 3 );
nexpaq 0:6c56fb4bc5f0 2983 }
nexpaq 0:6c56fb4bc5f0 2984 else
nexpaq 0:6c56fb4bc5f0 2985 {
nexpaq 0:6c56fb4bc5f0 2986 /* Make sure msg_type and length are consistent */
nexpaq 0:6c56fb4bc5f0 2987 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
nexpaq 0:6c56fb4bc5f0 2988 {
nexpaq 0:6c56fb4bc5f0 2989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
nexpaq 0:6c56fb4bc5f0 2990 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
nexpaq 0:6c56fb4bc5f0 2991 }
nexpaq 0:6c56fb4bc5f0 2992 }
nexpaq 0:6c56fb4bc5f0 2993
nexpaq 0:6c56fb4bc5f0 2994 msg = ssl->handshake->hs_msg + 12;
nexpaq 0:6c56fb4bc5f0 2995 bitmask = msg + msg_len;
nexpaq 0:6c56fb4bc5f0 2996
nexpaq 0:6c56fb4bc5f0 2997 /*
nexpaq 0:6c56fb4bc5f0 2998 * Check and copy current fragment
nexpaq 0:6c56fb4bc5f0 2999 */
nexpaq 0:6c56fb4bc5f0 3000 frag_off = ( ssl->in_msg[6] << 16 ) |
nexpaq 0:6c56fb4bc5f0 3001 ( ssl->in_msg[7] << 8 ) |
nexpaq 0:6c56fb4bc5f0 3002 ssl->in_msg[8];
nexpaq 0:6c56fb4bc5f0 3003 frag_len = ( ssl->in_msg[9] << 16 ) |
nexpaq 0:6c56fb4bc5f0 3004 ( ssl->in_msg[10] << 8 ) |
nexpaq 0:6c56fb4bc5f0 3005 ssl->in_msg[11];
nexpaq 0:6c56fb4bc5f0 3006
nexpaq 0:6c56fb4bc5f0 3007 if( frag_off + frag_len > msg_len )
nexpaq 0:6c56fb4bc5f0 3008 {
nexpaq 0:6c56fb4bc5f0 3009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
nexpaq 0:6c56fb4bc5f0 3010 frag_off, frag_len, msg_len ) );
nexpaq 0:6c56fb4bc5f0 3011 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
nexpaq 0:6c56fb4bc5f0 3012 }
nexpaq 0:6c56fb4bc5f0 3013
nexpaq 0:6c56fb4bc5f0 3014 if( frag_len + 12 > ssl->in_msglen )
nexpaq 0:6c56fb4bc5f0 3015 {
nexpaq 0:6c56fb4bc5f0 3016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
nexpaq 0:6c56fb4bc5f0 3017 frag_len, ssl->in_msglen ) );
nexpaq 0:6c56fb4bc5f0 3018 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
nexpaq 0:6c56fb4bc5f0 3019 }
nexpaq 0:6c56fb4bc5f0 3020
nexpaq 0:6c56fb4bc5f0 3021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
nexpaq 0:6c56fb4bc5f0 3022 frag_off, frag_len ) );
nexpaq 0:6c56fb4bc5f0 3023
nexpaq 0:6c56fb4bc5f0 3024 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
nexpaq 0:6c56fb4bc5f0 3025 ssl_bitmask_set( bitmask, frag_off, frag_len );
nexpaq 0:6c56fb4bc5f0 3026
nexpaq 0:6c56fb4bc5f0 3027 /*
nexpaq 0:6c56fb4bc5f0 3028 * Do we have the complete message by now?
nexpaq 0:6c56fb4bc5f0 3029 * If yes, finalize it, else ask to read the next record.
nexpaq 0:6c56fb4bc5f0 3030 */
nexpaq 0:6c56fb4bc5f0 3031 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
nexpaq 0:6c56fb4bc5f0 3032 {
nexpaq 0:6c56fb4bc5f0 3033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
nexpaq 0:6c56fb4bc5f0 3034 return( MBEDTLS_ERR_SSL_WANT_READ );
nexpaq 0:6c56fb4bc5f0 3035 }
nexpaq 0:6c56fb4bc5f0 3036
nexpaq 0:6c56fb4bc5f0 3037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
nexpaq 0:6c56fb4bc5f0 3038
nexpaq 0:6c56fb4bc5f0 3039 if( frag_len + 12 < ssl->in_msglen )
nexpaq 0:6c56fb4bc5f0 3040 {
nexpaq 0:6c56fb4bc5f0 3041 /*
nexpaq 0:6c56fb4bc5f0 3042 * We'got more handshake messages in the same record.
nexpaq 0:6c56fb4bc5f0 3043 * This case is not handled now because no know implementation does
nexpaq 0:6c56fb4bc5f0 3044 * that and it's hard to test, so we prefer to fail cleanly for now.
nexpaq 0:6c56fb4bc5f0 3045 */
nexpaq 0:6c56fb4bc5f0 3046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
nexpaq 0:6c56fb4bc5f0 3047 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
nexpaq 0:6c56fb4bc5f0 3048 }
nexpaq 0:6c56fb4bc5f0 3049
nexpaq 0:6c56fb4bc5f0 3050 if( ssl->in_left > ssl->next_record_offset )
nexpaq 0:6c56fb4bc5f0 3051 {
nexpaq 0:6c56fb4bc5f0 3052 /*
nexpaq 0:6c56fb4bc5f0 3053 * We've got more data in the buffer after the current record,
nexpaq 0:6c56fb4bc5f0 3054 * that we don't want to overwrite. Move it before writing the
nexpaq 0:6c56fb4bc5f0 3055 * reassembled message, and adjust in_left and next_record_offset.
nexpaq 0:6c56fb4bc5f0 3056 */
nexpaq 0:6c56fb4bc5f0 3057 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
nexpaq 0:6c56fb4bc5f0 3058 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
nexpaq 0:6c56fb4bc5f0 3059 size_t remain_len = ssl->in_left - ssl->next_record_offset;
nexpaq 0:6c56fb4bc5f0 3060
nexpaq 0:6c56fb4bc5f0 3061 /* First compute and check new lengths */
nexpaq 0:6c56fb4bc5f0 3062 ssl->next_record_offset = new_remain - ssl->in_hdr;
nexpaq 0:6c56fb4bc5f0 3063 ssl->in_left = ssl->next_record_offset + remain_len;
nexpaq 0:6c56fb4bc5f0 3064
nexpaq 0:6c56fb4bc5f0 3065 if( ssl->in_left > MBEDTLS_SSL_BUFFER_LEN -
nexpaq 0:6c56fb4bc5f0 3066 (size_t)( ssl->in_hdr - ssl->in_buf ) )
nexpaq 0:6c56fb4bc5f0 3067 {
nexpaq 0:6c56fb4bc5f0 3068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
nexpaq 0:6c56fb4bc5f0 3069 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
nexpaq 0:6c56fb4bc5f0 3070 }
nexpaq 0:6c56fb4bc5f0 3071
nexpaq 0:6c56fb4bc5f0 3072 memmove( new_remain, cur_remain, remain_len );
nexpaq 0:6c56fb4bc5f0 3073 }
nexpaq 0:6c56fb4bc5f0 3074
nexpaq 0:6c56fb4bc5f0 3075 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
nexpaq 0:6c56fb4bc5f0 3076
nexpaq 0:6c56fb4bc5f0 3077 mbedtls_free( ssl->handshake->hs_msg );
nexpaq 0:6c56fb4bc5f0 3078 ssl->handshake->hs_msg = NULL;
nexpaq 0:6c56fb4bc5f0 3079
nexpaq 0:6c56fb4bc5f0 3080 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
nexpaq 0:6c56fb4bc5f0 3081 ssl->in_msg, ssl->in_hslen );
nexpaq 0:6c56fb4bc5f0 3082
nexpaq 0:6c56fb4bc5f0 3083 return( 0 );
nexpaq 0:6c56fb4bc5f0 3084 }
nexpaq 0:6c56fb4bc5f0 3085 #endif /* MBEDTLS_SSL_PROTO_DTLS */
nexpaq 0:6c56fb4bc5f0 3086
nexpaq 0:6c56fb4bc5f0 3087 static int ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 3088 {
nexpaq 0:6c56fb4bc5f0 3089 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
nexpaq 0:6c56fb4bc5f0 3090 {
nexpaq 0:6c56fb4bc5f0 3091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
nexpaq 0:6c56fb4bc5f0 3092 ssl->in_msglen ) );
nexpaq 0:6c56fb4bc5f0 3093 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
nexpaq 0:6c56fb4bc5f0 3094 }
nexpaq 0:6c56fb4bc5f0 3095
nexpaq 0:6c56fb4bc5f0 3096 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
nexpaq 0:6c56fb4bc5f0 3097 ( ssl->in_msg[1] << 16 ) |
nexpaq 0:6c56fb4bc5f0 3098 ( ssl->in_msg[2] << 8 ) |
nexpaq 0:6c56fb4bc5f0 3099 ssl->in_msg[3] );
nexpaq 0:6c56fb4bc5f0 3100
nexpaq 0:6c56fb4bc5f0 3101 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
nexpaq 0:6c56fb4bc5f0 3102 " %d, type = %d, hslen = %d",
nexpaq 0:6c56fb4bc5f0 3103 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
nexpaq 0:6c56fb4bc5f0 3104
nexpaq 0:6c56fb4bc5f0 3105 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 3106 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
nexpaq 0:6c56fb4bc5f0 3107 {
nexpaq 0:6c56fb4bc5f0 3108 int ret;
nexpaq 0:6c56fb4bc5f0 3109 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
nexpaq 0:6c56fb4bc5f0 3110
nexpaq 0:6c56fb4bc5f0 3111 /* ssl->handshake is NULL when receiving ClientHello for renego */
nexpaq 0:6c56fb4bc5f0 3112 if( ssl->handshake != NULL &&
nexpaq 0:6c56fb4bc5f0 3113 recv_msg_seq != ssl->handshake->in_msg_seq )
nexpaq 0:6c56fb4bc5f0 3114 {
nexpaq 0:6c56fb4bc5f0 3115 /* Retransmit only on last message from previous flight, to avoid
nexpaq 0:6c56fb4bc5f0 3116 * too many retransmissions.
nexpaq 0:6c56fb4bc5f0 3117 * Besides, No sane server ever retransmits HelloVerifyRequest */
nexpaq 0:6c56fb4bc5f0 3118 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
nexpaq 0:6c56fb4bc5f0 3119 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
nexpaq 0:6c56fb4bc5f0 3120 {
nexpaq 0:6c56fb4bc5f0 3121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
nexpaq 0:6c56fb4bc5f0 3122 "message_seq = %d, start_of_flight = %d",
nexpaq 0:6c56fb4bc5f0 3123 recv_msg_seq,
nexpaq 0:6c56fb4bc5f0 3124 ssl->handshake->in_flight_start_seq ) );
nexpaq 0:6c56fb4bc5f0 3125
nexpaq 0:6c56fb4bc5f0 3126 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 3127 {
nexpaq 0:6c56fb4bc5f0 3128 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
nexpaq 0:6c56fb4bc5f0 3129 return( ret );
nexpaq 0:6c56fb4bc5f0 3130 }
nexpaq 0:6c56fb4bc5f0 3131 }
nexpaq 0:6c56fb4bc5f0 3132 else
nexpaq 0:6c56fb4bc5f0 3133 {
nexpaq 0:6c56fb4bc5f0 3134 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
nexpaq 0:6c56fb4bc5f0 3135 "message_seq = %d, expected = %d",
nexpaq 0:6c56fb4bc5f0 3136 recv_msg_seq,
nexpaq 0:6c56fb4bc5f0 3137 ssl->handshake->in_msg_seq ) );
nexpaq 0:6c56fb4bc5f0 3138 }
nexpaq 0:6c56fb4bc5f0 3139
nexpaq 0:6c56fb4bc5f0 3140 return( MBEDTLS_ERR_SSL_WANT_READ );
nexpaq 0:6c56fb4bc5f0 3141 }
nexpaq 0:6c56fb4bc5f0 3142 /* Wait until message completion to increment in_msg_seq */
nexpaq 0:6c56fb4bc5f0 3143
nexpaq 0:6c56fb4bc5f0 3144 /* Reassemble if current message is fragmented or reassembly is
nexpaq 0:6c56fb4bc5f0 3145 * already in progress */
nexpaq 0:6c56fb4bc5f0 3146 if( ssl->in_msglen < ssl->in_hslen ||
nexpaq 0:6c56fb4bc5f0 3147 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
nexpaq 0:6c56fb4bc5f0 3148 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
nexpaq 0:6c56fb4bc5f0 3149 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
nexpaq 0:6c56fb4bc5f0 3150 {
nexpaq 0:6c56fb4bc5f0 3151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
nexpaq 0:6c56fb4bc5f0 3152
nexpaq 0:6c56fb4bc5f0 3153 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 3154 {
nexpaq 0:6c56fb4bc5f0 3155 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
nexpaq 0:6c56fb4bc5f0 3156 return( ret );
nexpaq 0:6c56fb4bc5f0 3157 }
nexpaq 0:6c56fb4bc5f0 3158 }
nexpaq 0:6c56fb4bc5f0 3159 }
nexpaq 0:6c56fb4bc5f0 3160 else
nexpaq 0:6c56fb4bc5f0 3161 #endif /* MBEDTLS_SSL_PROTO_DTLS */
nexpaq 0:6c56fb4bc5f0 3162 /* With TLS we don't handle fragmentation (for now) */
nexpaq 0:6c56fb4bc5f0 3163 if( ssl->in_msglen < ssl->in_hslen )
nexpaq 0:6c56fb4bc5f0 3164 {
nexpaq 0:6c56fb4bc5f0 3165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
nexpaq 0:6c56fb4bc5f0 3166 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
nexpaq 0:6c56fb4bc5f0 3167 }
nexpaq 0:6c56fb4bc5f0 3168
nexpaq 0:6c56fb4bc5f0 3169 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
nexpaq 0:6c56fb4bc5f0 3170 ssl->handshake != NULL )
nexpaq 0:6c56fb4bc5f0 3171 {
nexpaq 0:6c56fb4bc5f0 3172 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
nexpaq 0:6c56fb4bc5f0 3173 }
nexpaq 0:6c56fb4bc5f0 3174
nexpaq 0:6c56fb4bc5f0 3175 /* Handshake message is complete, increment counter */
nexpaq 0:6c56fb4bc5f0 3176 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 3177 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
nexpaq 0:6c56fb4bc5f0 3178 ssl->handshake != NULL )
nexpaq 0:6c56fb4bc5f0 3179 {
nexpaq 0:6c56fb4bc5f0 3180 ssl->handshake->in_msg_seq++;
nexpaq 0:6c56fb4bc5f0 3181 }
nexpaq 0:6c56fb4bc5f0 3182 #endif
nexpaq 0:6c56fb4bc5f0 3183
nexpaq 0:6c56fb4bc5f0 3184 return( 0 );
nexpaq 0:6c56fb4bc5f0 3185 }
nexpaq 0:6c56fb4bc5f0 3186
nexpaq 0:6c56fb4bc5f0 3187 /*
nexpaq 0:6c56fb4bc5f0 3188 * DTLS anti-replay: RFC 6347 4.1.2.6
nexpaq 0:6c56fb4bc5f0 3189 *
nexpaq 0:6c56fb4bc5f0 3190 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
nexpaq 0:6c56fb4bc5f0 3191 * Bit n is set iff record number in_window_top - n has been seen.
nexpaq 0:6c56fb4bc5f0 3192 *
nexpaq 0:6c56fb4bc5f0 3193 * Usually, in_window_top is the last record number seen and the lsb of
nexpaq 0:6c56fb4bc5f0 3194 * in_window is set. The only exception is the initial state (record number 0
nexpaq 0:6c56fb4bc5f0 3195 * not seen yet).
nexpaq 0:6c56fb4bc5f0 3196 */
nexpaq 0:6c56fb4bc5f0 3197 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
nexpaq 0:6c56fb4bc5f0 3198 static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 3199 {
nexpaq 0:6c56fb4bc5f0 3200 ssl->in_window_top = 0;
nexpaq 0:6c56fb4bc5f0 3201 ssl->in_window = 0;
nexpaq 0:6c56fb4bc5f0 3202 }
nexpaq 0:6c56fb4bc5f0 3203
nexpaq 0:6c56fb4bc5f0 3204 static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
nexpaq 0:6c56fb4bc5f0 3205 {
nexpaq 0:6c56fb4bc5f0 3206 return( ( (uint64_t) buf[0] << 40 ) |
nexpaq 0:6c56fb4bc5f0 3207 ( (uint64_t) buf[1] << 32 ) |
nexpaq 0:6c56fb4bc5f0 3208 ( (uint64_t) buf[2] << 24 ) |
nexpaq 0:6c56fb4bc5f0 3209 ( (uint64_t) buf[3] << 16 ) |
nexpaq 0:6c56fb4bc5f0 3210 ( (uint64_t) buf[4] << 8 ) |
nexpaq 0:6c56fb4bc5f0 3211 ( (uint64_t) buf[5] ) );
nexpaq 0:6c56fb4bc5f0 3212 }
nexpaq 0:6c56fb4bc5f0 3213
nexpaq 0:6c56fb4bc5f0 3214 /*
nexpaq 0:6c56fb4bc5f0 3215 * Return 0 if sequence number is acceptable, -1 otherwise
nexpaq 0:6c56fb4bc5f0 3216 */
nexpaq 0:6c56fb4bc5f0 3217 int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 3218 {
nexpaq 0:6c56fb4bc5f0 3219 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
nexpaq 0:6c56fb4bc5f0 3220 uint64_t bit;
nexpaq 0:6c56fb4bc5f0 3221
nexpaq 0:6c56fb4bc5f0 3222 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
nexpaq 0:6c56fb4bc5f0 3223 return( 0 );
nexpaq 0:6c56fb4bc5f0 3224
nexpaq 0:6c56fb4bc5f0 3225 if( rec_seqnum > ssl->in_window_top )
nexpaq 0:6c56fb4bc5f0 3226 return( 0 );
nexpaq 0:6c56fb4bc5f0 3227
nexpaq 0:6c56fb4bc5f0 3228 bit = ssl->in_window_top - rec_seqnum;
nexpaq 0:6c56fb4bc5f0 3229
nexpaq 0:6c56fb4bc5f0 3230 if( bit >= 64 )
nexpaq 0:6c56fb4bc5f0 3231 return( -1 );
nexpaq 0:6c56fb4bc5f0 3232
nexpaq 0:6c56fb4bc5f0 3233 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 3234 return( -1 );
nexpaq 0:6c56fb4bc5f0 3235
nexpaq 0:6c56fb4bc5f0 3236 return( 0 );
nexpaq 0:6c56fb4bc5f0 3237 }
nexpaq 0:6c56fb4bc5f0 3238
nexpaq 0:6c56fb4bc5f0 3239 /*
nexpaq 0:6c56fb4bc5f0 3240 * Update replay window on new validated record
nexpaq 0:6c56fb4bc5f0 3241 */
nexpaq 0:6c56fb4bc5f0 3242 void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 3243 {
nexpaq 0:6c56fb4bc5f0 3244 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
nexpaq 0:6c56fb4bc5f0 3245
nexpaq 0:6c56fb4bc5f0 3246 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
nexpaq 0:6c56fb4bc5f0 3247 return;
nexpaq 0:6c56fb4bc5f0 3248
nexpaq 0:6c56fb4bc5f0 3249 if( rec_seqnum > ssl->in_window_top )
nexpaq 0:6c56fb4bc5f0 3250 {
nexpaq 0:6c56fb4bc5f0 3251 /* Update window_top and the contents of the window */
nexpaq 0:6c56fb4bc5f0 3252 uint64_t shift = rec_seqnum - ssl->in_window_top;
nexpaq 0:6c56fb4bc5f0 3253
nexpaq 0:6c56fb4bc5f0 3254 if( shift >= 64 )
nexpaq 0:6c56fb4bc5f0 3255 ssl->in_window = 1;
nexpaq 0:6c56fb4bc5f0 3256 else
nexpaq 0:6c56fb4bc5f0 3257 {
nexpaq 0:6c56fb4bc5f0 3258 ssl->in_window <<= shift;
nexpaq 0:6c56fb4bc5f0 3259 ssl->in_window |= 1;
nexpaq 0:6c56fb4bc5f0 3260 }
nexpaq 0:6c56fb4bc5f0 3261
nexpaq 0:6c56fb4bc5f0 3262 ssl->in_window_top = rec_seqnum;
nexpaq 0:6c56fb4bc5f0 3263 }
nexpaq 0:6c56fb4bc5f0 3264 else
nexpaq 0:6c56fb4bc5f0 3265 {
nexpaq 0:6c56fb4bc5f0 3266 /* Mark that number as seen in the current window */
nexpaq 0:6c56fb4bc5f0 3267 uint64_t bit = ssl->in_window_top - rec_seqnum;
nexpaq 0:6c56fb4bc5f0 3268
nexpaq 0:6c56fb4bc5f0 3269 if( bit < 64 ) /* Always true, but be extra sure */
nexpaq 0:6c56fb4bc5f0 3270 ssl->in_window |= (uint64_t) 1 << bit;
nexpaq 0:6c56fb4bc5f0 3271 }
nexpaq 0:6c56fb4bc5f0 3272 }
nexpaq 0:6c56fb4bc5f0 3273 #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
nexpaq 0:6c56fb4bc5f0 3274
nexpaq 0:6c56fb4bc5f0 3275 #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
nexpaq 0:6c56fb4bc5f0 3276 /* Forward declaration */
nexpaq 0:6c56fb4bc5f0 3277 static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
nexpaq 0:6c56fb4bc5f0 3278
nexpaq 0:6c56fb4bc5f0 3279 /*
nexpaq 0:6c56fb4bc5f0 3280 * Without any SSL context, check if a datagram looks like a ClientHello with
nexpaq 0:6c56fb4bc5f0 3281 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
nexpaq 0:6c56fb4bc5f0 3282 * Both input and output include full DTLS headers.
nexpaq 0:6c56fb4bc5f0 3283 *
nexpaq 0:6c56fb4bc5f0 3284 * - if cookie is valid, return 0
nexpaq 0:6c56fb4bc5f0 3285 * - if ClientHello looks superficially valid but cookie is not,
nexpaq 0:6c56fb4bc5f0 3286 * fill obuf and set olen, then
nexpaq 0:6c56fb4bc5f0 3287 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
nexpaq 0:6c56fb4bc5f0 3288 * - otherwise return a specific error code
nexpaq 0:6c56fb4bc5f0 3289 */
nexpaq 0:6c56fb4bc5f0 3290 static int ssl_check_dtls_clihlo_cookie(
nexpaq 0:6c56fb4bc5f0 3291 mbedtls_ssl_cookie_write_t *f_cookie_write,
nexpaq 0:6c56fb4bc5f0 3292 mbedtls_ssl_cookie_check_t *f_cookie_check,
nexpaq 0:6c56fb4bc5f0 3293 void *p_cookie,
nexpaq 0:6c56fb4bc5f0 3294 const unsigned char *cli_id, size_t cli_id_len,
nexpaq 0:6c56fb4bc5f0 3295 const unsigned char *in, size_t in_len,
nexpaq 0:6c56fb4bc5f0 3296 unsigned char *obuf, size_t buf_len, size_t *olen )
nexpaq 0:6c56fb4bc5f0 3297 {
nexpaq 0:6c56fb4bc5f0 3298 size_t sid_len, cookie_len;
nexpaq 0:6c56fb4bc5f0 3299 unsigned char *p;
nexpaq 0:6c56fb4bc5f0 3300
nexpaq 0:6c56fb4bc5f0 3301 if( f_cookie_write == NULL || f_cookie_check == NULL )
nexpaq 0:6c56fb4bc5f0 3302 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 3303
nexpaq 0:6c56fb4bc5f0 3304 /*
nexpaq 0:6c56fb4bc5f0 3305 * Structure of ClientHello with record and handshake headers,
nexpaq 0:6c56fb4bc5f0 3306 * and expected values. We don't need to check a lot, more checks will be
nexpaq 0:6c56fb4bc5f0 3307 * done when actually parsing the ClientHello - skipping those checks
nexpaq 0:6c56fb4bc5f0 3308 * avoids code duplication and does not make cookie forging any easier.
nexpaq 0:6c56fb4bc5f0 3309 *
nexpaq 0:6c56fb4bc5f0 3310 * 0-0 ContentType type; copied, must be handshake
nexpaq 0:6c56fb4bc5f0 3311 * 1-2 ProtocolVersion version; copied
nexpaq 0:6c56fb4bc5f0 3312 * 3-4 uint16 epoch; copied, must be 0
nexpaq 0:6c56fb4bc5f0 3313 * 5-10 uint48 sequence_number; copied
nexpaq 0:6c56fb4bc5f0 3314 * 11-12 uint16 length; (ignored)
nexpaq 0:6c56fb4bc5f0 3315 *
nexpaq 0:6c56fb4bc5f0 3316 * 13-13 HandshakeType msg_type; (ignored)
nexpaq 0:6c56fb4bc5f0 3317 * 14-16 uint24 length; (ignored)
nexpaq 0:6c56fb4bc5f0 3318 * 17-18 uint16 message_seq; copied
nexpaq 0:6c56fb4bc5f0 3319 * 19-21 uint24 fragment_offset; copied, must be 0
nexpaq 0:6c56fb4bc5f0 3320 * 22-24 uint24 fragment_length; (ignored)
nexpaq 0:6c56fb4bc5f0 3321 *
nexpaq 0:6c56fb4bc5f0 3322 * 25-26 ProtocolVersion client_version; (ignored)
nexpaq 0:6c56fb4bc5f0 3323 * 27-58 Random random; (ignored)
nexpaq 0:6c56fb4bc5f0 3324 * 59-xx SessionID session_id; 1 byte len + sid_len content
nexpaq 0:6c56fb4bc5f0 3325 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
nexpaq 0:6c56fb4bc5f0 3326 * ...
nexpaq 0:6c56fb4bc5f0 3327 *
nexpaq 0:6c56fb4bc5f0 3328 * Minimum length is 61 bytes.
nexpaq 0:6c56fb4bc5f0 3329 */
nexpaq 0:6c56fb4bc5f0 3330 if( in_len < 61 ||
nexpaq 0:6c56fb4bc5f0 3331 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
nexpaq 0:6c56fb4bc5f0 3332 in[3] != 0 || in[4] != 0 ||
nexpaq 0:6c56fb4bc5f0 3333 in[19] != 0 || in[20] != 0 || in[21] != 0 )
nexpaq 0:6c56fb4bc5f0 3334 {
nexpaq 0:6c56fb4bc5f0 3335 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
nexpaq 0:6c56fb4bc5f0 3336 }
nexpaq 0:6c56fb4bc5f0 3337
nexpaq 0:6c56fb4bc5f0 3338 sid_len = in[59];
nexpaq 0:6c56fb4bc5f0 3339 if( sid_len > in_len - 61 )
nexpaq 0:6c56fb4bc5f0 3340 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
nexpaq 0:6c56fb4bc5f0 3341
nexpaq 0:6c56fb4bc5f0 3342 cookie_len = in[60 + sid_len];
nexpaq 0:6c56fb4bc5f0 3343 if( cookie_len > in_len - 60 )
nexpaq 0:6c56fb4bc5f0 3344 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
nexpaq 0:6c56fb4bc5f0 3345
nexpaq 0:6c56fb4bc5f0 3346 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
nexpaq 0:6c56fb4bc5f0 3347 cli_id, cli_id_len ) == 0 )
nexpaq 0:6c56fb4bc5f0 3348 {
nexpaq 0:6c56fb4bc5f0 3349 /* Valid cookie */
nexpaq 0:6c56fb4bc5f0 3350 return( 0 );
nexpaq 0:6c56fb4bc5f0 3351 }
nexpaq 0:6c56fb4bc5f0 3352
nexpaq 0:6c56fb4bc5f0 3353 /*
nexpaq 0:6c56fb4bc5f0 3354 * If we get here, we've got an invalid cookie, let's prepare HVR.
nexpaq 0:6c56fb4bc5f0 3355 *
nexpaq 0:6c56fb4bc5f0 3356 * 0-0 ContentType type; copied
nexpaq 0:6c56fb4bc5f0 3357 * 1-2 ProtocolVersion version; copied
nexpaq 0:6c56fb4bc5f0 3358 * 3-4 uint16 epoch; copied
nexpaq 0:6c56fb4bc5f0 3359 * 5-10 uint48 sequence_number; copied
nexpaq 0:6c56fb4bc5f0 3360 * 11-12 uint16 length; olen - 13
nexpaq 0:6c56fb4bc5f0 3361 *
nexpaq 0:6c56fb4bc5f0 3362 * 13-13 HandshakeType msg_type; hello_verify_request
nexpaq 0:6c56fb4bc5f0 3363 * 14-16 uint24 length; olen - 25
nexpaq 0:6c56fb4bc5f0 3364 * 17-18 uint16 message_seq; copied
nexpaq 0:6c56fb4bc5f0 3365 * 19-21 uint24 fragment_offset; copied
nexpaq 0:6c56fb4bc5f0 3366 * 22-24 uint24 fragment_length; olen - 25
nexpaq 0:6c56fb4bc5f0 3367 *
nexpaq 0:6c56fb4bc5f0 3368 * 25-26 ProtocolVersion server_version; 0xfe 0xff
nexpaq 0:6c56fb4bc5f0 3369 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
nexpaq 0:6c56fb4bc5f0 3370 *
nexpaq 0:6c56fb4bc5f0 3371 * Minimum length is 28.
nexpaq 0:6c56fb4bc5f0 3372 */
nexpaq 0:6c56fb4bc5f0 3373 if( buf_len < 28 )
nexpaq 0:6c56fb4bc5f0 3374 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
nexpaq 0:6c56fb4bc5f0 3375
nexpaq 0:6c56fb4bc5f0 3376 /* Copy most fields and adapt others */
nexpaq 0:6c56fb4bc5f0 3377 memcpy( obuf, in, 25 );
nexpaq 0:6c56fb4bc5f0 3378 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
nexpaq 0:6c56fb4bc5f0 3379 obuf[25] = 0xfe;
nexpaq 0:6c56fb4bc5f0 3380 obuf[26] = 0xff;
nexpaq 0:6c56fb4bc5f0 3381
nexpaq 0:6c56fb4bc5f0 3382 /* Generate and write actual cookie */
nexpaq 0:6c56fb4bc5f0 3383 p = obuf + 28;
nexpaq 0:6c56fb4bc5f0 3384 if( f_cookie_write( p_cookie,
nexpaq 0:6c56fb4bc5f0 3385 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
nexpaq 0:6c56fb4bc5f0 3386 {
nexpaq 0:6c56fb4bc5f0 3387 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 3388 }
nexpaq 0:6c56fb4bc5f0 3389
nexpaq 0:6c56fb4bc5f0 3390 *olen = p - obuf;
nexpaq 0:6c56fb4bc5f0 3391
nexpaq 0:6c56fb4bc5f0 3392 /* Go back and fill length fields */
nexpaq 0:6c56fb4bc5f0 3393 obuf[27] = (unsigned char)( *olen - 28 );
nexpaq 0:6c56fb4bc5f0 3394
nexpaq 0:6c56fb4bc5f0 3395 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
nexpaq 0:6c56fb4bc5f0 3396 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
nexpaq 0:6c56fb4bc5f0 3397 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
nexpaq 0:6c56fb4bc5f0 3398
nexpaq 0:6c56fb4bc5f0 3399 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
nexpaq 0:6c56fb4bc5f0 3400 obuf[12] = (unsigned char)( ( *olen - 13 ) );
nexpaq 0:6c56fb4bc5f0 3401
nexpaq 0:6c56fb4bc5f0 3402 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
nexpaq 0:6c56fb4bc5f0 3403 }
nexpaq 0:6c56fb4bc5f0 3404
nexpaq 0:6c56fb4bc5f0 3405 /*
nexpaq 0:6c56fb4bc5f0 3406 * Handle possible client reconnect with the same UDP quadruplet
nexpaq 0:6c56fb4bc5f0 3407 * (RFC 6347 Section 4.2.8).
nexpaq 0:6c56fb4bc5f0 3408 *
nexpaq 0:6c56fb4bc5f0 3409 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
nexpaq 0:6c56fb4bc5f0 3410 * that looks like a ClientHello.
nexpaq 0:6c56fb4bc5f0 3411 *
nexpaq 0:6c56fb4bc5f0 3412 * - if the input looks like a ClientHello without cookies,
nexpaq 0:6c56fb4bc5f0 3413 * send back HelloVerifyRequest, then
nexpaq 0:6c56fb4bc5f0 3414 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
nexpaq 0:6c56fb4bc5f0 3415 * - if the input looks like a ClientHello with a valid cookie,
nexpaq 0:6c56fb4bc5f0 3416 * reset the session of the current context, and
nexpaq 0:6c56fb4bc5f0 3417 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
nexpaq 0:6c56fb4bc5f0 3418 * - if anything goes wrong, return a specific error code
nexpaq 0:6c56fb4bc5f0 3419 *
nexpaq 0:6c56fb4bc5f0 3420 * mbedtls_ssl_read_record() will ignore the record if anything else than
nexpaq 0:6c56fb4bc5f0 3421 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
nexpaq 0:6c56fb4bc5f0 3422 * cannot not return 0.
nexpaq 0:6c56fb4bc5f0 3423 */
nexpaq 0:6c56fb4bc5f0 3424 static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 3425 {
nexpaq 0:6c56fb4bc5f0 3426 int ret;
nexpaq 0:6c56fb4bc5f0 3427 size_t len;
nexpaq 0:6c56fb4bc5f0 3428
nexpaq 0:6c56fb4bc5f0 3429 ret = ssl_check_dtls_clihlo_cookie(
nexpaq 0:6c56fb4bc5f0 3430 ssl->conf->f_cookie_write,
nexpaq 0:6c56fb4bc5f0 3431 ssl->conf->f_cookie_check,
nexpaq 0:6c56fb4bc5f0 3432 ssl->conf->p_cookie,
nexpaq 0:6c56fb4bc5f0 3433 ssl->cli_id, ssl->cli_id_len,
nexpaq 0:6c56fb4bc5f0 3434 ssl->in_buf, ssl->in_left,
nexpaq 0:6c56fb4bc5f0 3435 ssl->out_buf, MBEDTLS_SSL_MAX_CONTENT_LEN, &len );
nexpaq 0:6c56fb4bc5f0 3436
nexpaq 0:6c56fb4bc5f0 3437 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
nexpaq 0:6c56fb4bc5f0 3438
nexpaq 0:6c56fb4bc5f0 3439 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
nexpaq 0:6c56fb4bc5f0 3440 {
nexpaq 0:6c56fb4bc5f0 3441 /* Dont check write errors as we can't do anything here.
nexpaq 0:6c56fb4bc5f0 3442 * If the error is permanent we'll catch it later,
nexpaq 0:6c56fb4bc5f0 3443 * if it's not, then hopefully it'll work next time. */
nexpaq 0:6c56fb4bc5f0 3444 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
nexpaq 0:6c56fb4bc5f0 3445
nexpaq 0:6c56fb4bc5f0 3446 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
nexpaq 0:6c56fb4bc5f0 3447 }
nexpaq 0:6c56fb4bc5f0 3448
nexpaq 0:6c56fb4bc5f0 3449 if( ret == 0 )
nexpaq 0:6c56fb4bc5f0 3450 {
nexpaq 0:6c56fb4bc5f0 3451 /* Got a valid cookie, partially reset context */
nexpaq 0:6c56fb4bc5f0 3452 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 3453 {
nexpaq 0:6c56fb4bc5f0 3454 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
nexpaq 0:6c56fb4bc5f0 3455 return( ret );
nexpaq 0:6c56fb4bc5f0 3456 }
nexpaq 0:6c56fb4bc5f0 3457
nexpaq 0:6c56fb4bc5f0 3458 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
nexpaq 0:6c56fb4bc5f0 3459 }
nexpaq 0:6c56fb4bc5f0 3460
nexpaq 0:6c56fb4bc5f0 3461 return( ret );
nexpaq 0:6c56fb4bc5f0 3462 }
nexpaq 0:6c56fb4bc5f0 3463 #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
nexpaq 0:6c56fb4bc5f0 3464
nexpaq 0:6c56fb4bc5f0 3465 /*
nexpaq 0:6c56fb4bc5f0 3466 * ContentType type;
nexpaq 0:6c56fb4bc5f0 3467 * ProtocolVersion version;
nexpaq 0:6c56fb4bc5f0 3468 * uint16 epoch; // DTLS only
nexpaq 0:6c56fb4bc5f0 3469 * uint48 sequence_number; // DTLS only
nexpaq 0:6c56fb4bc5f0 3470 * uint16 length;
nexpaq 0:6c56fb4bc5f0 3471 *
nexpaq 0:6c56fb4bc5f0 3472 * Return 0 if header looks sane (and, for DTLS, the record is expected)
nexpaq 0:6c56fb4bc5f0 3473 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
nexpaq 0:6c56fb4bc5f0 3474 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
nexpaq 0:6c56fb4bc5f0 3475 *
nexpaq 0:6c56fb4bc5f0 3476 * With DTLS, mbedtls_ssl_read_record() will:
nexpaq 0:6c56fb4bc5f0 3477 * 1. proceed with the record if this function returns 0
nexpaq 0:6c56fb4bc5f0 3478 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
nexpaq 0:6c56fb4bc5f0 3479 * 3. return CLIENT_RECONNECT if this function return that value
nexpaq 0:6c56fb4bc5f0 3480 * 4. drop the whole datagram if this function returns anything else.
nexpaq 0:6c56fb4bc5f0 3481 * Point 2 is needed when the peer is resending, and we have already received
nexpaq 0:6c56fb4bc5f0 3482 * the first record from a datagram but are still waiting for the others.
nexpaq 0:6c56fb4bc5f0 3483 */
nexpaq 0:6c56fb4bc5f0 3484 static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 3485 {
nexpaq 0:6c56fb4bc5f0 3486 int ret;
nexpaq 0:6c56fb4bc5f0 3487 int major_ver, minor_ver;
nexpaq 0:6c56fb4bc5f0 3488
nexpaq 0:6c56fb4bc5f0 3489 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) );
nexpaq 0:6c56fb4bc5f0 3490
nexpaq 0:6c56fb4bc5f0 3491 ssl->in_msgtype = ssl->in_hdr[0];
nexpaq 0:6c56fb4bc5f0 3492 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
nexpaq 0:6c56fb4bc5f0 3493 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
nexpaq 0:6c56fb4bc5f0 3494
nexpaq 0:6c56fb4bc5f0 3495 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
nexpaq 0:6c56fb4bc5f0 3496 "version = [%d:%d], msglen = %d",
nexpaq 0:6c56fb4bc5f0 3497 ssl->in_msgtype,
nexpaq 0:6c56fb4bc5f0 3498 major_ver, minor_ver, ssl->in_msglen ) );
nexpaq 0:6c56fb4bc5f0 3499
nexpaq 0:6c56fb4bc5f0 3500 /* Check record type */
nexpaq 0:6c56fb4bc5f0 3501 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
nexpaq 0:6c56fb4bc5f0 3502 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
nexpaq 0:6c56fb4bc5f0 3503 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
nexpaq 0:6c56fb4bc5f0 3504 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
nexpaq 0:6c56fb4bc5f0 3505 {
nexpaq 0:6c56fb4bc5f0 3506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
nexpaq 0:6c56fb4bc5f0 3507
nexpaq 0:6c56fb4bc5f0 3508 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
nexpaq 0:6c56fb4bc5f0 3509 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
nexpaq 0:6c56fb4bc5f0 3510 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 3511 {
nexpaq 0:6c56fb4bc5f0 3512 return( ret );
nexpaq 0:6c56fb4bc5f0 3513 }
nexpaq 0:6c56fb4bc5f0 3514
nexpaq 0:6c56fb4bc5f0 3515 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
nexpaq 0:6c56fb4bc5f0 3516 }
nexpaq 0:6c56fb4bc5f0 3517
nexpaq 0:6c56fb4bc5f0 3518 /* Check version */
nexpaq 0:6c56fb4bc5f0 3519 if( major_ver != ssl->major_ver )
nexpaq 0:6c56fb4bc5f0 3520 {
nexpaq 0:6c56fb4bc5f0 3521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
nexpaq 0:6c56fb4bc5f0 3522 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
nexpaq 0:6c56fb4bc5f0 3523 }
nexpaq 0:6c56fb4bc5f0 3524
nexpaq 0:6c56fb4bc5f0 3525 if( minor_ver > ssl->conf->max_minor_ver )
nexpaq 0:6c56fb4bc5f0 3526 {
nexpaq 0:6c56fb4bc5f0 3527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
nexpaq 0:6c56fb4bc5f0 3528 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
nexpaq 0:6c56fb4bc5f0 3529 }
nexpaq 0:6c56fb4bc5f0 3530
nexpaq 0:6c56fb4bc5f0 3531 /* Check length against the size of our buffer */
nexpaq 0:6c56fb4bc5f0 3532 if( ssl->in_msglen > MBEDTLS_SSL_BUFFER_LEN
nexpaq 0:6c56fb4bc5f0 3533 - (size_t)( ssl->in_msg - ssl->in_buf ) )
nexpaq 0:6c56fb4bc5f0 3534 {
nexpaq 0:6c56fb4bc5f0 3535 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
nexpaq 0:6c56fb4bc5f0 3536 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
nexpaq 0:6c56fb4bc5f0 3537 }
nexpaq 0:6c56fb4bc5f0 3538
nexpaq 0:6c56fb4bc5f0 3539 /* Check length against bounds of the current transform and version */
nexpaq 0:6c56fb4bc5f0 3540 if( ssl->transform_in == NULL )
nexpaq 0:6c56fb4bc5f0 3541 {
nexpaq 0:6c56fb4bc5f0 3542 if( ssl->in_msglen < 1 ||
nexpaq 0:6c56fb4bc5f0 3543 ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
nexpaq 0:6c56fb4bc5f0 3544 {
nexpaq 0:6c56fb4bc5f0 3545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
nexpaq 0:6c56fb4bc5f0 3546 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
nexpaq 0:6c56fb4bc5f0 3547 }
nexpaq 0:6c56fb4bc5f0 3548 }
nexpaq 0:6c56fb4bc5f0 3549 else
nexpaq 0:6c56fb4bc5f0 3550 {
nexpaq 0:6c56fb4bc5f0 3551 if( ssl->in_msglen < ssl->transform_in->minlen )
nexpaq 0:6c56fb4bc5f0 3552 {
nexpaq 0:6c56fb4bc5f0 3553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
nexpaq 0:6c56fb4bc5f0 3554 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
nexpaq 0:6c56fb4bc5f0 3555 }
nexpaq 0:6c56fb4bc5f0 3556
nexpaq 0:6c56fb4bc5f0 3557 #if defined(MBEDTLS_SSL_PROTO_SSL3)
nexpaq 0:6c56fb4bc5f0 3558 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
nexpaq 0:6c56fb4bc5f0 3559 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_MAX_CONTENT_LEN )
nexpaq 0:6c56fb4bc5f0 3560 {
nexpaq 0:6c56fb4bc5f0 3561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
nexpaq 0:6c56fb4bc5f0 3562 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
nexpaq 0:6c56fb4bc5f0 3563 }
nexpaq 0:6c56fb4bc5f0 3564 #endif
nexpaq 0:6c56fb4bc5f0 3565 #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
nexpaq 0:6c56fb4bc5f0 3566 defined(MBEDTLS_SSL_PROTO_TLS1_2)
nexpaq 0:6c56fb4bc5f0 3567 /*
nexpaq 0:6c56fb4bc5f0 3568 * TLS encrypted messages can have up to 256 bytes of padding
nexpaq 0:6c56fb4bc5f0 3569 */
nexpaq 0:6c56fb4bc5f0 3570 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
nexpaq 0:6c56fb4bc5f0 3571 ssl->in_msglen > ssl->transform_in->minlen +
nexpaq 0:6c56fb4bc5f0 3572 MBEDTLS_SSL_MAX_CONTENT_LEN + 256 )
nexpaq 0:6c56fb4bc5f0 3573 {
nexpaq 0:6c56fb4bc5f0 3574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
nexpaq 0:6c56fb4bc5f0 3575 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
nexpaq 0:6c56fb4bc5f0 3576 }
nexpaq 0:6c56fb4bc5f0 3577 #endif
nexpaq 0:6c56fb4bc5f0 3578 }
nexpaq 0:6c56fb4bc5f0 3579
nexpaq 0:6c56fb4bc5f0 3580 /*
nexpaq 0:6c56fb4bc5f0 3581 * DTLS-related tests done last, because most of them may result in
nexpaq 0:6c56fb4bc5f0 3582 * silently dropping the record (but not the whole datagram), and we only
nexpaq 0:6c56fb4bc5f0 3583 * want to consider that after ensuring that the "basic" fields (type,
nexpaq 0:6c56fb4bc5f0 3584 * version, length) are sane.
nexpaq 0:6c56fb4bc5f0 3585 */
nexpaq 0:6c56fb4bc5f0 3586 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 3587 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
nexpaq 0:6c56fb4bc5f0 3588 {
nexpaq 0:6c56fb4bc5f0 3589 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
nexpaq 0:6c56fb4bc5f0 3590
nexpaq 0:6c56fb4bc5f0 3591 /* Drop unexpected ChangeCipherSpec messages */
nexpaq 0:6c56fb4bc5f0 3592 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
nexpaq 0:6c56fb4bc5f0 3593 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
nexpaq 0:6c56fb4bc5f0 3594 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
nexpaq 0:6c56fb4bc5f0 3595 {
nexpaq 0:6c56fb4bc5f0 3596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
nexpaq 0:6c56fb4bc5f0 3597 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
nexpaq 0:6c56fb4bc5f0 3598 }
nexpaq 0:6c56fb4bc5f0 3599
nexpaq 0:6c56fb4bc5f0 3600 /* Drop unexpected ApplicationData records,
nexpaq 0:6c56fb4bc5f0 3601 * except at the beginning of renegotiations */
nexpaq 0:6c56fb4bc5f0 3602 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
nexpaq 0:6c56fb4bc5f0 3603 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
nexpaq 0:6c56fb4bc5f0 3604 #if defined(MBEDTLS_SSL_RENEGOTIATION)
nexpaq 0:6c56fb4bc5f0 3605 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
nexpaq 0:6c56fb4bc5f0 3606 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
nexpaq 0:6c56fb4bc5f0 3607 #endif
nexpaq 0:6c56fb4bc5f0 3608 )
nexpaq 0:6c56fb4bc5f0 3609 {
nexpaq 0:6c56fb4bc5f0 3610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
nexpaq 0:6c56fb4bc5f0 3611 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
nexpaq 0:6c56fb4bc5f0 3612 }
nexpaq 0:6c56fb4bc5f0 3613
nexpaq 0:6c56fb4bc5f0 3614 /* Check epoch (and sequence number) with DTLS */
nexpaq 0:6c56fb4bc5f0 3615 if( rec_epoch != ssl->in_epoch )
nexpaq 0:6c56fb4bc5f0 3616 {
nexpaq 0:6c56fb4bc5f0 3617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
nexpaq 0:6c56fb4bc5f0 3618 "expected %d, received %d",
nexpaq 0:6c56fb4bc5f0 3619 ssl->in_epoch, rec_epoch ) );
nexpaq 0:6c56fb4bc5f0 3620
nexpaq 0:6c56fb4bc5f0 3621 #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
nexpaq 0:6c56fb4bc5f0 3622 /*
nexpaq 0:6c56fb4bc5f0 3623 * Check for an epoch 0 ClientHello. We can't use in_msg here to
nexpaq 0:6c56fb4bc5f0 3624 * access the first byte of record content (handshake type), as we
nexpaq 0:6c56fb4bc5f0 3625 * have an active transform (possibly iv_len != 0), so use the
nexpaq 0:6c56fb4bc5f0 3626 * fact that the record header len is 13 instead.
nexpaq 0:6c56fb4bc5f0 3627 */
nexpaq 0:6c56fb4bc5f0 3628 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
nexpaq 0:6c56fb4bc5f0 3629 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
nexpaq 0:6c56fb4bc5f0 3630 rec_epoch == 0 &&
nexpaq 0:6c56fb4bc5f0 3631 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
nexpaq 0:6c56fb4bc5f0 3632 ssl->in_left > 13 &&
nexpaq 0:6c56fb4bc5f0 3633 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
nexpaq 0:6c56fb4bc5f0 3634 {
nexpaq 0:6c56fb4bc5f0 3635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
nexpaq 0:6c56fb4bc5f0 3636 "from the same port" ) );
nexpaq 0:6c56fb4bc5f0 3637 return( ssl_handle_possible_reconnect( ssl ) );
nexpaq 0:6c56fb4bc5f0 3638 }
nexpaq 0:6c56fb4bc5f0 3639 else
nexpaq 0:6c56fb4bc5f0 3640 #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
nexpaq 0:6c56fb4bc5f0 3641 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
nexpaq 0:6c56fb4bc5f0 3642 }
nexpaq 0:6c56fb4bc5f0 3643
nexpaq 0:6c56fb4bc5f0 3644 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
nexpaq 0:6c56fb4bc5f0 3645 /* Replay detection only works for the current epoch */
nexpaq 0:6c56fb4bc5f0 3646 if( rec_epoch == ssl->in_epoch &&
nexpaq 0:6c56fb4bc5f0 3647 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
nexpaq 0:6c56fb4bc5f0 3648 {
nexpaq 0:6c56fb4bc5f0 3649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
nexpaq 0:6c56fb4bc5f0 3650 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
nexpaq 0:6c56fb4bc5f0 3651 }
nexpaq 0:6c56fb4bc5f0 3652 #endif
nexpaq 0:6c56fb4bc5f0 3653 }
nexpaq 0:6c56fb4bc5f0 3654 #endif /* MBEDTLS_SSL_PROTO_DTLS */
nexpaq 0:6c56fb4bc5f0 3655
nexpaq 0:6c56fb4bc5f0 3656 return( 0 );
nexpaq 0:6c56fb4bc5f0 3657 }
nexpaq 0:6c56fb4bc5f0 3658
nexpaq 0:6c56fb4bc5f0 3659 /*
nexpaq 0:6c56fb4bc5f0 3660 * If applicable, decrypt (and decompress) record content
nexpaq 0:6c56fb4bc5f0 3661 */
nexpaq 0:6c56fb4bc5f0 3662 static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 3663 {
nexpaq 0:6c56fb4bc5f0 3664 int ret, done = 0;
nexpaq 0:6c56fb4bc5f0 3665
nexpaq 0:6c56fb4bc5f0 3666 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
nexpaq 0:6c56fb4bc5f0 3667 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
nexpaq 0:6c56fb4bc5f0 3668
nexpaq 0:6c56fb4bc5f0 3669 #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
nexpaq 0:6c56fb4bc5f0 3670 if( mbedtls_ssl_hw_record_read != NULL )
nexpaq 0:6c56fb4bc5f0 3671 {
nexpaq 0:6c56fb4bc5f0 3672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
nexpaq 0:6c56fb4bc5f0 3673
nexpaq 0:6c56fb4bc5f0 3674 ret = mbedtls_ssl_hw_record_read( ssl );
nexpaq 0:6c56fb4bc5f0 3675 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
nexpaq 0:6c56fb4bc5f0 3676 {
nexpaq 0:6c56fb4bc5f0 3677 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
nexpaq 0:6c56fb4bc5f0 3678 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
nexpaq 0:6c56fb4bc5f0 3679 }
nexpaq 0:6c56fb4bc5f0 3680
nexpaq 0:6c56fb4bc5f0 3681 if( ret == 0 )
nexpaq 0:6c56fb4bc5f0 3682 done = 1;
nexpaq 0:6c56fb4bc5f0 3683 }
nexpaq 0:6c56fb4bc5f0 3684 #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
nexpaq 0:6c56fb4bc5f0 3685 if( !done && ssl->transform_in != NULL )
nexpaq 0:6c56fb4bc5f0 3686 {
nexpaq 0:6c56fb4bc5f0 3687 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 3688 {
nexpaq 0:6c56fb4bc5f0 3689 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
nexpaq 0:6c56fb4bc5f0 3690 return( ret );
nexpaq 0:6c56fb4bc5f0 3691 }
nexpaq 0:6c56fb4bc5f0 3692
nexpaq 0:6c56fb4bc5f0 3693 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
nexpaq 0:6c56fb4bc5f0 3694 ssl->in_msg, ssl->in_msglen );
nexpaq 0:6c56fb4bc5f0 3695
nexpaq 0:6c56fb4bc5f0 3696 if( ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
nexpaq 0:6c56fb4bc5f0 3697 {
nexpaq 0:6c56fb4bc5f0 3698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
nexpaq 0:6c56fb4bc5f0 3699 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
nexpaq 0:6c56fb4bc5f0 3700 }
nexpaq 0:6c56fb4bc5f0 3701 }
nexpaq 0:6c56fb4bc5f0 3702
nexpaq 0:6c56fb4bc5f0 3703 #if defined(MBEDTLS_ZLIB_SUPPORT)
nexpaq 0:6c56fb4bc5f0 3704 if( ssl->transform_in != NULL &&
nexpaq 0:6c56fb4bc5f0 3705 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
nexpaq 0:6c56fb4bc5f0 3706 {
nexpaq 0:6c56fb4bc5f0 3707 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 3708 {
nexpaq 0:6c56fb4bc5f0 3709 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
nexpaq 0:6c56fb4bc5f0 3710 return( ret );
nexpaq 0:6c56fb4bc5f0 3711 }
nexpaq 0:6c56fb4bc5f0 3712 }
nexpaq 0:6c56fb4bc5f0 3713 #endif /* MBEDTLS_ZLIB_SUPPORT */
nexpaq 0:6c56fb4bc5f0 3714
nexpaq 0:6c56fb4bc5f0 3715 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
nexpaq 0:6c56fb4bc5f0 3716 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
nexpaq 0:6c56fb4bc5f0 3717 {
nexpaq 0:6c56fb4bc5f0 3718 mbedtls_ssl_dtls_replay_update( ssl );
nexpaq 0:6c56fb4bc5f0 3719 }
nexpaq 0:6c56fb4bc5f0 3720 #endif
nexpaq 0:6c56fb4bc5f0 3721
nexpaq 0:6c56fb4bc5f0 3722 return( 0 );
nexpaq 0:6c56fb4bc5f0 3723 }
nexpaq 0:6c56fb4bc5f0 3724
nexpaq 0:6c56fb4bc5f0 3725 static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
nexpaq 0:6c56fb4bc5f0 3726
nexpaq 0:6c56fb4bc5f0 3727 /*
nexpaq 0:6c56fb4bc5f0 3728 * Read a record.
nexpaq 0:6c56fb4bc5f0 3729 *
nexpaq 0:6c56fb4bc5f0 3730 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
nexpaq 0:6c56fb4bc5f0 3731 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
nexpaq 0:6c56fb4bc5f0 3732 *
nexpaq 0:6c56fb4bc5f0 3733 */
nexpaq 0:6c56fb4bc5f0 3734 int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 3735 {
nexpaq 0:6c56fb4bc5f0 3736 int ret;
nexpaq 0:6c56fb4bc5f0 3737
nexpaq 0:6c56fb4bc5f0 3738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
nexpaq 0:6c56fb4bc5f0 3739
nexpaq 0:6c56fb4bc5f0 3740 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
nexpaq 0:6c56fb4bc5f0 3741 {
nexpaq 0:6c56fb4bc5f0 3742 /*
nexpaq 0:6c56fb4bc5f0 3743 * Get next Handshake message in the current record
nexpaq 0:6c56fb4bc5f0 3744 */
nexpaq 0:6c56fb4bc5f0 3745 ssl->in_msglen -= ssl->in_hslen;
nexpaq 0:6c56fb4bc5f0 3746
nexpaq 0:6c56fb4bc5f0 3747 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
nexpaq 0:6c56fb4bc5f0 3748 ssl->in_msglen );
nexpaq 0:6c56fb4bc5f0 3749
nexpaq 0:6c56fb4bc5f0 3750 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
nexpaq 0:6c56fb4bc5f0 3751 ssl->in_msg, ssl->in_msglen );
nexpaq 0:6c56fb4bc5f0 3752
nexpaq 0:6c56fb4bc5f0 3753 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 3754 return( ret );
nexpaq 0:6c56fb4bc5f0 3755
nexpaq 0:6c56fb4bc5f0 3756 return( 0 );
nexpaq 0:6c56fb4bc5f0 3757 }
nexpaq 0:6c56fb4bc5f0 3758
nexpaq 0:6c56fb4bc5f0 3759 ssl->in_hslen = 0;
nexpaq 0:6c56fb4bc5f0 3760
nexpaq 0:6c56fb4bc5f0 3761 /*
nexpaq 0:6c56fb4bc5f0 3762 * Read the record header and parse it
nexpaq 0:6c56fb4bc5f0 3763 */
nexpaq 0:6c56fb4bc5f0 3764 read_record_header:
nexpaq 0:6c56fb4bc5f0 3765 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 3766 {
nexpaq 0:6c56fb4bc5f0 3767 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
nexpaq 0:6c56fb4bc5f0 3768 return( ret );
nexpaq 0:6c56fb4bc5f0 3769 }
nexpaq 0:6c56fb4bc5f0 3770
nexpaq 0:6c56fb4bc5f0 3771 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 3772 {
nexpaq 0:6c56fb4bc5f0 3773 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 3774 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
nexpaq 0:6c56fb4bc5f0 3775 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
nexpaq 0:6c56fb4bc5f0 3776 {
nexpaq 0:6c56fb4bc5f0 3777 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
nexpaq 0:6c56fb4bc5f0 3778 {
nexpaq 0:6c56fb4bc5f0 3779 /* Skip unexpected record (but not whole datagram) */
nexpaq 0:6c56fb4bc5f0 3780 ssl->next_record_offset = ssl->in_msglen
nexpaq 0:6c56fb4bc5f0 3781 + mbedtls_ssl_hdr_len( ssl );
nexpaq 0:6c56fb4bc5f0 3782
nexpaq 0:6c56fb4bc5f0 3783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
nexpaq 0:6c56fb4bc5f0 3784 "(header)" ) );
nexpaq 0:6c56fb4bc5f0 3785 }
nexpaq 0:6c56fb4bc5f0 3786 else
nexpaq 0:6c56fb4bc5f0 3787 {
nexpaq 0:6c56fb4bc5f0 3788 /* Skip invalid record and the rest of the datagram */
nexpaq 0:6c56fb4bc5f0 3789 ssl->next_record_offset = 0;
nexpaq 0:6c56fb4bc5f0 3790 ssl->in_left = 0;
nexpaq 0:6c56fb4bc5f0 3791
nexpaq 0:6c56fb4bc5f0 3792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
nexpaq 0:6c56fb4bc5f0 3793 "(header)" ) );
nexpaq 0:6c56fb4bc5f0 3794 }
nexpaq 0:6c56fb4bc5f0 3795
nexpaq 0:6c56fb4bc5f0 3796 /* Get next record */
nexpaq 0:6c56fb4bc5f0 3797 goto read_record_header;
nexpaq 0:6c56fb4bc5f0 3798 }
nexpaq 0:6c56fb4bc5f0 3799 #endif
nexpaq 0:6c56fb4bc5f0 3800 return( ret );
nexpaq 0:6c56fb4bc5f0 3801 }
nexpaq 0:6c56fb4bc5f0 3802
nexpaq 0:6c56fb4bc5f0 3803 /*
nexpaq 0:6c56fb4bc5f0 3804 * Read and optionally decrypt the message contents
nexpaq 0:6c56fb4bc5f0 3805 */
nexpaq 0:6c56fb4bc5f0 3806 if( ( ret = mbedtls_ssl_fetch_input( ssl,
nexpaq 0:6c56fb4bc5f0 3807 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 3808 {
nexpaq 0:6c56fb4bc5f0 3809 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
nexpaq 0:6c56fb4bc5f0 3810 return( ret );
nexpaq 0:6c56fb4bc5f0 3811 }
nexpaq 0:6c56fb4bc5f0 3812
nexpaq 0:6c56fb4bc5f0 3813 /* Done reading this record, get ready for the next one */
nexpaq 0:6c56fb4bc5f0 3814 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 3815 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
nexpaq 0:6c56fb4bc5f0 3816 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
nexpaq 0:6c56fb4bc5f0 3817 else
nexpaq 0:6c56fb4bc5f0 3818 #endif
nexpaq 0:6c56fb4bc5f0 3819 ssl->in_left = 0;
nexpaq 0:6c56fb4bc5f0 3820
nexpaq 0:6c56fb4bc5f0 3821 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 3822 {
nexpaq 0:6c56fb4bc5f0 3823 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 3824 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
nexpaq 0:6c56fb4bc5f0 3825 {
nexpaq 0:6c56fb4bc5f0 3826 /* Silently discard invalid records */
nexpaq 0:6c56fb4bc5f0 3827 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
nexpaq 0:6c56fb4bc5f0 3828 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
nexpaq 0:6c56fb4bc5f0 3829 {
nexpaq 0:6c56fb4bc5f0 3830 /* Except when waiting for Finished as a bad mac here
nexpaq 0:6c56fb4bc5f0 3831 * probably means something went wrong in the handshake
nexpaq 0:6c56fb4bc5f0 3832 * (eg wrong psk used, mitm downgrade attempt, etc.) */
nexpaq 0:6c56fb4bc5f0 3833 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
nexpaq 0:6c56fb4bc5f0 3834 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
nexpaq 0:6c56fb4bc5f0 3835 {
nexpaq 0:6c56fb4bc5f0 3836 #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
nexpaq 0:6c56fb4bc5f0 3837 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
nexpaq 0:6c56fb4bc5f0 3838 {
nexpaq 0:6c56fb4bc5f0 3839 mbedtls_ssl_send_alert_message( ssl,
nexpaq 0:6c56fb4bc5f0 3840 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
nexpaq 0:6c56fb4bc5f0 3841 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
nexpaq 0:6c56fb4bc5f0 3842 }
nexpaq 0:6c56fb4bc5f0 3843 #endif
nexpaq 0:6c56fb4bc5f0 3844 return( ret );
nexpaq 0:6c56fb4bc5f0 3845 }
nexpaq 0:6c56fb4bc5f0 3846
nexpaq 0:6c56fb4bc5f0 3847 #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
nexpaq 0:6c56fb4bc5f0 3848 if( ssl->conf->badmac_limit != 0 &&
nexpaq 0:6c56fb4bc5f0 3849 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
nexpaq 0:6c56fb4bc5f0 3850 {
nexpaq 0:6c56fb4bc5f0 3851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
nexpaq 0:6c56fb4bc5f0 3852 return( MBEDTLS_ERR_SSL_INVALID_MAC );
nexpaq 0:6c56fb4bc5f0 3853 }
nexpaq 0:6c56fb4bc5f0 3854 #endif
nexpaq 0:6c56fb4bc5f0 3855
nexpaq 0:6c56fb4bc5f0 3856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
nexpaq 0:6c56fb4bc5f0 3857 goto read_record_header;
nexpaq 0:6c56fb4bc5f0 3858 }
nexpaq 0:6c56fb4bc5f0 3859
nexpaq 0:6c56fb4bc5f0 3860 return( ret );
nexpaq 0:6c56fb4bc5f0 3861 }
nexpaq 0:6c56fb4bc5f0 3862 else
nexpaq 0:6c56fb4bc5f0 3863 #endif
nexpaq 0:6c56fb4bc5f0 3864 {
nexpaq 0:6c56fb4bc5f0 3865 /* Error out (and send alert) on invalid records */
nexpaq 0:6c56fb4bc5f0 3866 #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
nexpaq 0:6c56fb4bc5f0 3867 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
nexpaq 0:6c56fb4bc5f0 3868 {
nexpaq 0:6c56fb4bc5f0 3869 mbedtls_ssl_send_alert_message( ssl,
nexpaq 0:6c56fb4bc5f0 3870 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
nexpaq 0:6c56fb4bc5f0 3871 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
nexpaq 0:6c56fb4bc5f0 3872 }
nexpaq 0:6c56fb4bc5f0 3873 #endif
nexpaq 0:6c56fb4bc5f0 3874 return( ret );
nexpaq 0:6c56fb4bc5f0 3875 }
nexpaq 0:6c56fb4bc5f0 3876 }
nexpaq 0:6c56fb4bc5f0 3877
nexpaq 0:6c56fb4bc5f0 3878 /*
nexpaq 0:6c56fb4bc5f0 3879 * When we sent the last flight of the handshake, we MUST respond to a
nexpaq 0:6c56fb4bc5f0 3880 * retransmit of the peer's previous flight with a retransmit. (In
nexpaq 0:6c56fb4bc5f0 3881 * practice, only the Finished message will make it, other messages
nexpaq 0:6c56fb4bc5f0 3882 * including CCS use the old transform so they're dropped as invalid.)
nexpaq 0:6c56fb4bc5f0 3883 *
nexpaq 0:6c56fb4bc5f0 3884 * If the record we received is not a handshake message, however, it
nexpaq 0:6c56fb4bc5f0 3885 * means the peer received our last flight so we can clean up
nexpaq 0:6c56fb4bc5f0 3886 * handshake info.
nexpaq 0:6c56fb4bc5f0 3887 *
nexpaq 0:6c56fb4bc5f0 3888 * This check needs to be done before prepare_handshake() due to an edge
nexpaq 0:6c56fb4bc5f0 3889 * case: if the client immediately requests renegotiation, this
nexpaq 0:6c56fb4bc5f0 3890 * finishes the current handshake first, avoiding the new ClientHello
nexpaq 0:6c56fb4bc5f0 3891 * being mistaken for an ancient message in the current handshake.
nexpaq 0:6c56fb4bc5f0 3892 */
nexpaq 0:6c56fb4bc5f0 3893 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 3894 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
nexpaq 0:6c56fb4bc5f0 3895 ssl->handshake != NULL &&
nexpaq 0:6c56fb4bc5f0 3896 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
nexpaq 0:6c56fb4bc5f0 3897 {
nexpaq 0:6c56fb4bc5f0 3898 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
nexpaq 0:6c56fb4bc5f0 3899 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
nexpaq 0:6c56fb4bc5f0 3900 {
nexpaq 0:6c56fb4bc5f0 3901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
nexpaq 0:6c56fb4bc5f0 3902
nexpaq 0:6c56fb4bc5f0 3903 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 3904 {
nexpaq 0:6c56fb4bc5f0 3905 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
nexpaq 0:6c56fb4bc5f0 3906 return( ret );
nexpaq 0:6c56fb4bc5f0 3907 }
nexpaq 0:6c56fb4bc5f0 3908
nexpaq 0:6c56fb4bc5f0 3909 return( MBEDTLS_ERR_SSL_WANT_READ );
nexpaq 0:6c56fb4bc5f0 3910 }
nexpaq 0:6c56fb4bc5f0 3911 else
nexpaq 0:6c56fb4bc5f0 3912 {
nexpaq 0:6c56fb4bc5f0 3913 ssl_handshake_wrapup_free_hs_transform( ssl );
nexpaq 0:6c56fb4bc5f0 3914 }
nexpaq 0:6c56fb4bc5f0 3915 }
nexpaq 0:6c56fb4bc5f0 3916 #endif
nexpaq 0:6c56fb4bc5f0 3917
nexpaq 0:6c56fb4bc5f0 3918 /*
nexpaq 0:6c56fb4bc5f0 3919 * Handle particular types of records
nexpaq 0:6c56fb4bc5f0 3920 */
nexpaq 0:6c56fb4bc5f0 3921 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
nexpaq 0:6c56fb4bc5f0 3922 {
nexpaq 0:6c56fb4bc5f0 3923 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 3924 return( ret );
nexpaq 0:6c56fb4bc5f0 3925 }
nexpaq 0:6c56fb4bc5f0 3926
nexpaq 0:6c56fb4bc5f0 3927 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
nexpaq 0:6c56fb4bc5f0 3928 {
nexpaq 0:6c56fb4bc5f0 3929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
nexpaq 0:6c56fb4bc5f0 3930 ssl->in_msg[0], ssl->in_msg[1] ) );
nexpaq 0:6c56fb4bc5f0 3931
nexpaq 0:6c56fb4bc5f0 3932 /*
nexpaq 0:6c56fb4bc5f0 3933 * Ignore non-fatal alerts, except close_notify and no_renegotiation
nexpaq 0:6c56fb4bc5f0 3934 */
nexpaq 0:6c56fb4bc5f0 3935 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
nexpaq 0:6c56fb4bc5f0 3936 {
nexpaq 0:6c56fb4bc5f0 3937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
nexpaq 0:6c56fb4bc5f0 3938 ssl->in_msg[1] ) );
nexpaq 0:6c56fb4bc5f0 3939 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
nexpaq 0:6c56fb4bc5f0 3940 }
nexpaq 0:6c56fb4bc5f0 3941
nexpaq 0:6c56fb4bc5f0 3942 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
nexpaq 0:6c56fb4bc5f0 3943 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
nexpaq 0:6c56fb4bc5f0 3944 {
nexpaq 0:6c56fb4bc5f0 3945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
nexpaq 0:6c56fb4bc5f0 3946 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
nexpaq 0:6c56fb4bc5f0 3947 }
nexpaq 0:6c56fb4bc5f0 3948
nexpaq 0:6c56fb4bc5f0 3949 #if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
nexpaq 0:6c56fb4bc5f0 3950 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
nexpaq 0:6c56fb4bc5f0 3951 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
nexpaq 0:6c56fb4bc5f0 3952 {
nexpaq 0:6c56fb4bc5f0 3953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
nexpaq 0:6c56fb4bc5f0 3954 /* Will be handled when trying to parse ServerHello */
nexpaq 0:6c56fb4bc5f0 3955 return( 0 );
nexpaq 0:6c56fb4bc5f0 3956 }
nexpaq 0:6c56fb4bc5f0 3957 #endif
nexpaq 0:6c56fb4bc5f0 3958
nexpaq 0:6c56fb4bc5f0 3959 #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
nexpaq 0:6c56fb4bc5f0 3960 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
nexpaq 0:6c56fb4bc5f0 3961 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
nexpaq 0:6c56fb4bc5f0 3962 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
nexpaq 0:6c56fb4bc5f0 3963 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
nexpaq 0:6c56fb4bc5f0 3964 {
nexpaq 0:6c56fb4bc5f0 3965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
nexpaq 0:6c56fb4bc5f0 3966 /* Will be handled in mbedtls_ssl_parse_certificate() */
nexpaq 0:6c56fb4bc5f0 3967 return( 0 );
nexpaq 0:6c56fb4bc5f0 3968 }
nexpaq 0:6c56fb4bc5f0 3969 #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
nexpaq 0:6c56fb4bc5f0 3970
nexpaq 0:6c56fb4bc5f0 3971 /* Silently ignore: fetch new message */
nexpaq 0:6c56fb4bc5f0 3972 goto read_record_header;
nexpaq 0:6c56fb4bc5f0 3973 }
nexpaq 0:6c56fb4bc5f0 3974
nexpaq 0:6c56fb4bc5f0 3975 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
nexpaq 0:6c56fb4bc5f0 3976
nexpaq 0:6c56fb4bc5f0 3977 return( 0 );
nexpaq 0:6c56fb4bc5f0 3978 }
nexpaq 0:6c56fb4bc5f0 3979
nexpaq 0:6c56fb4bc5f0 3980 int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 3981 {
nexpaq 0:6c56fb4bc5f0 3982 int ret;
nexpaq 0:6c56fb4bc5f0 3983
nexpaq 0:6c56fb4bc5f0 3984 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
nexpaq 0:6c56fb4bc5f0 3985 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
nexpaq 0:6c56fb4bc5f0 3986 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 3987 {
nexpaq 0:6c56fb4bc5f0 3988 return( ret );
nexpaq 0:6c56fb4bc5f0 3989 }
nexpaq 0:6c56fb4bc5f0 3990
nexpaq 0:6c56fb4bc5f0 3991 return( 0 );
nexpaq 0:6c56fb4bc5f0 3992 }
nexpaq 0:6c56fb4bc5f0 3993
nexpaq 0:6c56fb4bc5f0 3994 int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
nexpaq 0:6c56fb4bc5f0 3995 unsigned char level,
nexpaq 0:6c56fb4bc5f0 3996 unsigned char message )
nexpaq 0:6c56fb4bc5f0 3997 {
nexpaq 0:6c56fb4bc5f0 3998 int ret;
nexpaq 0:6c56fb4bc5f0 3999
nexpaq 0:6c56fb4bc5f0 4000 if( ssl == NULL || ssl->conf == NULL )
nexpaq 0:6c56fb4bc5f0 4001 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 4002
nexpaq 0:6c56fb4bc5f0 4003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
nexpaq 0:6c56fb4bc5f0 4004
nexpaq 0:6c56fb4bc5f0 4005 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
nexpaq 0:6c56fb4bc5f0 4006 ssl->out_msglen = 2;
nexpaq 0:6c56fb4bc5f0 4007 ssl->out_msg[0] = level;
nexpaq 0:6c56fb4bc5f0 4008 ssl->out_msg[1] = message;
nexpaq 0:6c56fb4bc5f0 4009
nexpaq 0:6c56fb4bc5f0 4010 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 4011 {
nexpaq 0:6c56fb4bc5f0 4012 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
nexpaq 0:6c56fb4bc5f0 4013 return( ret );
nexpaq 0:6c56fb4bc5f0 4014 }
nexpaq 0:6c56fb4bc5f0 4015
nexpaq 0:6c56fb4bc5f0 4016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
nexpaq 0:6c56fb4bc5f0 4017
nexpaq 0:6c56fb4bc5f0 4018 return( 0 );
nexpaq 0:6c56fb4bc5f0 4019 }
nexpaq 0:6c56fb4bc5f0 4020
nexpaq 0:6c56fb4bc5f0 4021 /*
nexpaq 0:6c56fb4bc5f0 4022 * Handshake functions
nexpaq 0:6c56fb4bc5f0 4023 */
nexpaq 0:6c56fb4bc5f0 4024 #if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
nexpaq 0:6c56fb4bc5f0 4025 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
nexpaq 0:6c56fb4bc5f0 4026 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
nexpaq 0:6c56fb4bc5f0 4027 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
nexpaq 0:6c56fb4bc5f0 4028 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
nexpaq 0:6c56fb4bc5f0 4029 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
nexpaq 0:6c56fb4bc5f0 4030 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
nexpaq 0:6c56fb4bc5f0 4031 int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 4032 {
nexpaq 0:6c56fb4bc5f0 4033 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
nexpaq 0:6c56fb4bc5f0 4034
nexpaq 0:6c56fb4bc5f0 4035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
nexpaq 0:6c56fb4bc5f0 4036
nexpaq 0:6c56fb4bc5f0 4037 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
nexpaq 0:6c56fb4bc5f0 4038 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
nexpaq 0:6c56fb4bc5f0 4039 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
nexpaq 0:6c56fb4bc5f0 4040 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
nexpaq 0:6c56fb4bc5f0 4041 {
nexpaq 0:6c56fb4bc5f0 4042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
nexpaq 0:6c56fb4bc5f0 4043 ssl->state++;
nexpaq 0:6c56fb4bc5f0 4044 return( 0 );
nexpaq 0:6c56fb4bc5f0 4045 }
nexpaq 0:6c56fb4bc5f0 4046
nexpaq 0:6c56fb4bc5f0 4047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 4048 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 4049 }
nexpaq 0:6c56fb4bc5f0 4050
nexpaq 0:6c56fb4bc5f0 4051 int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 4052 {
nexpaq 0:6c56fb4bc5f0 4053 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
nexpaq 0:6c56fb4bc5f0 4054
nexpaq 0:6c56fb4bc5f0 4055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
nexpaq 0:6c56fb4bc5f0 4056
nexpaq 0:6c56fb4bc5f0 4057 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
nexpaq 0:6c56fb4bc5f0 4058 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
nexpaq 0:6c56fb4bc5f0 4059 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
nexpaq 0:6c56fb4bc5f0 4060 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
nexpaq 0:6c56fb4bc5f0 4061 {
nexpaq 0:6c56fb4bc5f0 4062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
nexpaq 0:6c56fb4bc5f0 4063 ssl->state++;
nexpaq 0:6c56fb4bc5f0 4064 return( 0 );
nexpaq 0:6c56fb4bc5f0 4065 }
nexpaq 0:6c56fb4bc5f0 4066
nexpaq 0:6c56fb4bc5f0 4067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 4068 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 4069 }
nexpaq 0:6c56fb4bc5f0 4070 #else
nexpaq 0:6c56fb4bc5f0 4071 int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 4072 {
nexpaq 0:6c56fb4bc5f0 4073 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
nexpaq 0:6c56fb4bc5f0 4074 size_t i, n;
nexpaq 0:6c56fb4bc5f0 4075 const mbedtls_x509_crt *crt;
nexpaq 0:6c56fb4bc5f0 4076 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
nexpaq 0:6c56fb4bc5f0 4077
nexpaq 0:6c56fb4bc5f0 4078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
nexpaq 0:6c56fb4bc5f0 4079
nexpaq 0:6c56fb4bc5f0 4080 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
nexpaq 0:6c56fb4bc5f0 4081 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
nexpaq 0:6c56fb4bc5f0 4082 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
nexpaq 0:6c56fb4bc5f0 4083 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
nexpaq 0:6c56fb4bc5f0 4084 {
nexpaq 0:6c56fb4bc5f0 4085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
nexpaq 0:6c56fb4bc5f0 4086 ssl->state++;
nexpaq 0:6c56fb4bc5f0 4087 return( 0 );
nexpaq 0:6c56fb4bc5f0 4088 }
nexpaq 0:6c56fb4bc5f0 4089
nexpaq 0:6c56fb4bc5f0 4090 #if defined(MBEDTLS_SSL_CLI_C)
nexpaq 0:6c56fb4bc5f0 4091 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
nexpaq 0:6c56fb4bc5f0 4092 {
nexpaq 0:6c56fb4bc5f0 4093 if( ssl->client_auth == 0 )
nexpaq 0:6c56fb4bc5f0 4094 {
nexpaq 0:6c56fb4bc5f0 4095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
nexpaq 0:6c56fb4bc5f0 4096 ssl->state++;
nexpaq 0:6c56fb4bc5f0 4097 return( 0 );
nexpaq 0:6c56fb4bc5f0 4098 }
nexpaq 0:6c56fb4bc5f0 4099
nexpaq 0:6c56fb4bc5f0 4100 #if defined(MBEDTLS_SSL_PROTO_SSL3)
nexpaq 0:6c56fb4bc5f0 4101 /*
nexpaq 0:6c56fb4bc5f0 4102 * If using SSLv3 and got no cert, send an Alert message
nexpaq 0:6c56fb4bc5f0 4103 * (otherwise an empty Certificate message will be sent).
nexpaq 0:6c56fb4bc5f0 4104 */
nexpaq 0:6c56fb4bc5f0 4105 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
nexpaq 0:6c56fb4bc5f0 4106 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
nexpaq 0:6c56fb4bc5f0 4107 {
nexpaq 0:6c56fb4bc5f0 4108 ssl->out_msglen = 2;
nexpaq 0:6c56fb4bc5f0 4109 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
nexpaq 0:6c56fb4bc5f0 4110 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
nexpaq 0:6c56fb4bc5f0 4111 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
nexpaq 0:6c56fb4bc5f0 4112
nexpaq 0:6c56fb4bc5f0 4113 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
nexpaq 0:6c56fb4bc5f0 4114 goto write_msg;
nexpaq 0:6c56fb4bc5f0 4115 }
nexpaq 0:6c56fb4bc5f0 4116 #endif /* MBEDTLS_SSL_PROTO_SSL3 */
nexpaq 0:6c56fb4bc5f0 4117 }
nexpaq 0:6c56fb4bc5f0 4118 #endif /* MBEDTLS_SSL_CLI_C */
nexpaq 0:6c56fb4bc5f0 4119 #if defined(MBEDTLS_SSL_SRV_C)
nexpaq 0:6c56fb4bc5f0 4120 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
nexpaq 0:6c56fb4bc5f0 4121 {
nexpaq 0:6c56fb4bc5f0 4122 if( mbedtls_ssl_own_cert( ssl ) == NULL )
nexpaq 0:6c56fb4bc5f0 4123 {
nexpaq 0:6c56fb4bc5f0 4124 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
nexpaq 0:6c56fb4bc5f0 4125 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
nexpaq 0:6c56fb4bc5f0 4126 }
nexpaq 0:6c56fb4bc5f0 4127 }
nexpaq 0:6c56fb4bc5f0 4128 #endif
nexpaq 0:6c56fb4bc5f0 4129
nexpaq 0:6c56fb4bc5f0 4130 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
nexpaq 0:6c56fb4bc5f0 4131
nexpaq 0:6c56fb4bc5f0 4132 /*
nexpaq 0:6c56fb4bc5f0 4133 * 0 . 0 handshake type
nexpaq 0:6c56fb4bc5f0 4134 * 1 . 3 handshake length
nexpaq 0:6c56fb4bc5f0 4135 * 4 . 6 length of all certs
nexpaq 0:6c56fb4bc5f0 4136 * 7 . 9 length of cert. 1
nexpaq 0:6c56fb4bc5f0 4137 * 10 . n-1 peer certificate
nexpaq 0:6c56fb4bc5f0 4138 * n . n+2 length of cert. 2
nexpaq 0:6c56fb4bc5f0 4139 * n+3 . ... upper level cert, etc.
nexpaq 0:6c56fb4bc5f0 4140 */
nexpaq 0:6c56fb4bc5f0 4141 i = 7;
nexpaq 0:6c56fb4bc5f0 4142 crt = mbedtls_ssl_own_cert( ssl );
nexpaq 0:6c56fb4bc5f0 4143
nexpaq 0:6c56fb4bc5f0 4144 while( crt != NULL )
nexpaq 0:6c56fb4bc5f0 4145 {
nexpaq 0:6c56fb4bc5f0 4146 n = crt->raw.len;
nexpaq 0:6c56fb4bc5f0 4147 if( n > MBEDTLS_SSL_MAX_CONTENT_LEN - 3 - i )
nexpaq 0:6c56fb4bc5f0 4148 {
nexpaq 0:6c56fb4bc5f0 4149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
nexpaq 0:6c56fb4bc5f0 4150 i + 3 + n, MBEDTLS_SSL_MAX_CONTENT_LEN ) );
nexpaq 0:6c56fb4bc5f0 4151 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
nexpaq 0:6c56fb4bc5f0 4152 }
nexpaq 0:6c56fb4bc5f0 4153
nexpaq 0:6c56fb4bc5f0 4154 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
nexpaq 0:6c56fb4bc5f0 4155 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
nexpaq 0:6c56fb4bc5f0 4156 ssl->out_msg[i + 2] = (unsigned char)( n );
nexpaq 0:6c56fb4bc5f0 4157
nexpaq 0:6c56fb4bc5f0 4158 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
nexpaq 0:6c56fb4bc5f0 4159 i += n; crt = crt->next;
nexpaq 0:6c56fb4bc5f0 4160 }
nexpaq 0:6c56fb4bc5f0 4161
nexpaq 0:6c56fb4bc5f0 4162 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
nexpaq 0:6c56fb4bc5f0 4163 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
nexpaq 0:6c56fb4bc5f0 4164 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
nexpaq 0:6c56fb4bc5f0 4165
nexpaq 0:6c56fb4bc5f0 4166 ssl->out_msglen = i;
nexpaq 0:6c56fb4bc5f0 4167 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
nexpaq 0:6c56fb4bc5f0 4168 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
nexpaq 0:6c56fb4bc5f0 4169
nexpaq 0:6c56fb4bc5f0 4170 #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
nexpaq 0:6c56fb4bc5f0 4171 write_msg:
nexpaq 0:6c56fb4bc5f0 4172 #endif
nexpaq 0:6c56fb4bc5f0 4173
nexpaq 0:6c56fb4bc5f0 4174 ssl->state++;
nexpaq 0:6c56fb4bc5f0 4175
nexpaq 0:6c56fb4bc5f0 4176 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 4177 {
nexpaq 0:6c56fb4bc5f0 4178 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
nexpaq 0:6c56fb4bc5f0 4179 return( ret );
nexpaq 0:6c56fb4bc5f0 4180 }
nexpaq 0:6c56fb4bc5f0 4181
nexpaq 0:6c56fb4bc5f0 4182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
nexpaq 0:6c56fb4bc5f0 4183
nexpaq 0:6c56fb4bc5f0 4184 return( ret );
nexpaq 0:6c56fb4bc5f0 4185 }
nexpaq 0:6c56fb4bc5f0 4186
nexpaq 0:6c56fb4bc5f0 4187 int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 4188 {
nexpaq 0:6c56fb4bc5f0 4189 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
nexpaq 0:6c56fb4bc5f0 4190 size_t i, n;
nexpaq 0:6c56fb4bc5f0 4191 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
nexpaq 0:6c56fb4bc5f0 4192 int authmode = ssl->conf->authmode;
nexpaq 0:6c56fb4bc5f0 4193
nexpaq 0:6c56fb4bc5f0 4194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
nexpaq 0:6c56fb4bc5f0 4195
nexpaq 0:6c56fb4bc5f0 4196 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
nexpaq 0:6c56fb4bc5f0 4197 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
nexpaq 0:6c56fb4bc5f0 4198 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
nexpaq 0:6c56fb4bc5f0 4199 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
nexpaq 0:6c56fb4bc5f0 4200 {
nexpaq 0:6c56fb4bc5f0 4201 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
nexpaq 0:6c56fb4bc5f0 4202 ssl->state++;
nexpaq 0:6c56fb4bc5f0 4203 return( 0 );
nexpaq 0:6c56fb4bc5f0 4204 }
nexpaq 0:6c56fb4bc5f0 4205
nexpaq 0:6c56fb4bc5f0 4206 #if defined(MBEDTLS_SSL_SRV_C)
nexpaq 0:6c56fb4bc5f0 4207 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
nexpaq 0:6c56fb4bc5f0 4208 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
nexpaq 0:6c56fb4bc5f0 4209 {
nexpaq 0:6c56fb4bc5f0 4210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
nexpaq 0:6c56fb4bc5f0 4211 ssl->state++;
nexpaq 0:6c56fb4bc5f0 4212 return( 0 );
nexpaq 0:6c56fb4bc5f0 4213 }
nexpaq 0:6c56fb4bc5f0 4214
nexpaq 0:6c56fb4bc5f0 4215 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
nexpaq 0:6c56fb4bc5f0 4216 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
nexpaq 0:6c56fb4bc5f0 4217 authmode = ssl->handshake->sni_authmode;
nexpaq 0:6c56fb4bc5f0 4218 #endif
nexpaq 0:6c56fb4bc5f0 4219
nexpaq 0:6c56fb4bc5f0 4220 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
nexpaq 0:6c56fb4bc5f0 4221 authmode == MBEDTLS_SSL_VERIFY_NONE )
nexpaq 0:6c56fb4bc5f0 4222 {
nexpaq 0:6c56fb4bc5f0 4223 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
nexpaq 0:6c56fb4bc5f0 4224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
nexpaq 0:6c56fb4bc5f0 4225 ssl->state++;
nexpaq 0:6c56fb4bc5f0 4226 return( 0 );
nexpaq 0:6c56fb4bc5f0 4227 }
nexpaq 0:6c56fb4bc5f0 4228 #endif
nexpaq 0:6c56fb4bc5f0 4229
nexpaq 0:6c56fb4bc5f0 4230 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 4231 {
nexpaq 0:6c56fb4bc5f0 4232 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
nexpaq 0:6c56fb4bc5f0 4233 return( ret );
nexpaq 0:6c56fb4bc5f0 4234 }
nexpaq 0:6c56fb4bc5f0 4235
nexpaq 0:6c56fb4bc5f0 4236 ssl->state++;
nexpaq 0:6c56fb4bc5f0 4237
nexpaq 0:6c56fb4bc5f0 4238 #if defined(MBEDTLS_SSL_SRV_C)
nexpaq 0:6c56fb4bc5f0 4239 #if defined(MBEDTLS_SSL_PROTO_SSL3)
nexpaq 0:6c56fb4bc5f0 4240 /*
nexpaq 0:6c56fb4bc5f0 4241 * Check if the client sent an empty certificate
nexpaq 0:6c56fb4bc5f0 4242 */
nexpaq 0:6c56fb4bc5f0 4243 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
nexpaq 0:6c56fb4bc5f0 4244 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
nexpaq 0:6c56fb4bc5f0 4245 {
nexpaq 0:6c56fb4bc5f0 4246 if( ssl->in_msglen == 2 &&
nexpaq 0:6c56fb4bc5f0 4247 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
nexpaq 0:6c56fb4bc5f0 4248 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
nexpaq 0:6c56fb4bc5f0 4249 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
nexpaq 0:6c56fb4bc5f0 4250 {
nexpaq 0:6c56fb4bc5f0 4251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
nexpaq 0:6c56fb4bc5f0 4252
nexpaq 0:6c56fb4bc5f0 4253 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
nexpaq 0:6c56fb4bc5f0 4254 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
nexpaq 0:6c56fb4bc5f0 4255 return( 0 );
nexpaq 0:6c56fb4bc5f0 4256 else
nexpaq 0:6c56fb4bc5f0 4257 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
nexpaq 0:6c56fb4bc5f0 4258 }
nexpaq 0:6c56fb4bc5f0 4259 }
nexpaq 0:6c56fb4bc5f0 4260 #endif /* MBEDTLS_SSL_PROTO_SSL3 */
nexpaq 0:6c56fb4bc5f0 4261
nexpaq 0:6c56fb4bc5f0 4262 #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
nexpaq 0:6c56fb4bc5f0 4263 defined(MBEDTLS_SSL_PROTO_TLS1_2)
nexpaq 0:6c56fb4bc5f0 4264 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
nexpaq 0:6c56fb4bc5f0 4265 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
nexpaq 0:6c56fb4bc5f0 4266 {
nexpaq 0:6c56fb4bc5f0 4267 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
nexpaq 0:6c56fb4bc5f0 4268 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
nexpaq 0:6c56fb4bc5f0 4269 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
nexpaq 0:6c56fb4bc5f0 4270 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
nexpaq 0:6c56fb4bc5f0 4271 {
nexpaq 0:6c56fb4bc5f0 4272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
nexpaq 0:6c56fb4bc5f0 4273
nexpaq 0:6c56fb4bc5f0 4274 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
nexpaq 0:6c56fb4bc5f0 4275 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
nexpaq 0:6c56fb4bc5f0 4276 return( 0 );
nexpaq 0:6c56fb4bc5f0 4277 else
nexpaq 0:6c56fb4bc5f0 4278 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
nexpaq 0:6c56fb4bc5f0 4279 }
nexpaq 0:6c56fb4bc5f0 4280 }
nexpaq 0:6c56fb4bc5f0 4281 #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
nexpaq 0:6c56fb4bc5f0 4282 MBEDTLS_SSL_PROTO_TLS1_2 */
nexpaq 0:6c56fb4bc5f0 4283 #endif /* MBEDTLS_SSL_SRV_C */
nexpaq 0:6c56fb4bc5f0 4284
nexpaq 0:6c56fb4bc5f0 4285 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
nexpaq 0:6c56fb4bc5f0 4286 {
nexpaq 0:6c56fb4bc5f0 4287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
nexpaq 0:6c56fb4bc5f0 4288 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
nexpaq 0:6c56fb4bc5f0 4289 }
nexpaq 0:6c56fb4bc5f0 4290
nexpaq 0:6c56fb4bc5f0 4291 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
nexpaq 0:6c56fb4bc5f0 4292 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
nexpaq 0:6c56fb4bc5f0 4293 {
nexpaq 0:6c56fb4bc5f0 4294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
nexpaq 0:6c56fb4bc5f0 4295 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
nexpaq 0:6c56fb4bc5f0 4296 }
nexpaq 0:6c56fb4bc5f0 4297
nexpaq 0:6c56fb4bc5f0 4298 i = mbedtls_ssl_hs_hdr_len( ssl );
nexpaq 0:6c56fb4bc5f0 4299
nexpaq 0:6c56fb4bc5f0 4300 /*
nexpaq 0:6c56fb4bc5f0 4301 * Same message structure as in mbedtls_ssl_write_certificate()
nexpaq 0:6c56fb4bc5f0 4302 */
nexpaq 0:6c56fb4bc5f0 4303 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
nexpaq 0:6c56fb4bc5f0 4304
nexpaq 0:6c56fb4bc5f0 4305 if( ssl->in_msg[i] != 0 ||
nexpaq 0:6c56fb4bc5f0 4306 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
nexpaq 0:6c56fb4bc5f0 4307 {
nexpaq 0:6c56fb4bc5f0 4308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
nexpaq 0:6c56fb4bc5f0 4309 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
nexpaq 0:6c56fb4bc5f0 4310 }
nexpaq 0:6c56fb4bc5f0 4311
nexpaq 0:6c56fb4bc5f0 4312 /* In case we tried to reuse a session but it failed */
nexpaq 0:6c56fb4bc5f0 4313 if( ssl->session_negotiate->peer_cert != NULL )
nexpaq 0:6c56fb4bc5f0 4314 {
nexpaq 0:6c56fb4bc5f0 4315 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
nexpaq 0:6c56fb4bc5f0 4316 mbedtls_free( ssl->session_negotiate->peer_cert );
nexpaq 0:6c56fb4bc5f0 4317 }
nexpaq 0:6c56fb4bc5f0 4318
nexpaq 0:6c56fb4bc5f0 4319 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
nexpaq 0:6c56fb4bc5f0 4320 sizeof( mbedtls_x509_crt ) ) ) == NULL )
nexpaq 0:6c56fb4bc5f0 4321 {
nexpaq 0:6c56fb4bc5f0 4322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
nexpaq 0:6c56fb4bc5f0 4323 sizeof( mbedtls_x509_crt ) ) );
nexpaq 0:6c56fb4bc5f0 4324 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
nexpaq 0:6c56fb4bc5f0 4325 }
nexpaq 0:6c56fb4bc5f0 4326
nexpaq 0:6c56fb4bc5f0 4327 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
nexpaq 0:6c56fb4bc5f0 4328
nexpaq 0:6c56fb4bc5f0 4329 i += 3;
nexpaq 0:6c56fb4bc5f0 4330
nexpaq 0:6c56fb4bc5f0 4331 while( i < ssl->in_hslen )
nexpaq 0:6c56fb4bc5f0 4332 {
nexpaq 0:6c56fb4bc5f0 4333 if( ssl->in_msg[i] != 0 )
nexpaq 0:6c56fb4bc5f0 4334 {
nexpaq 0:6c56fb4bc5f0 4335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
nexpaq 0:6c56fb4bc5f0 4336 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
nexpaq 0:6c56fb4bc5f0 4337 }
nexpaq 0:6c56fb4bc5f0 4338
nexpaq 0:6c56fb4bc5f0 4339 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
nexpaq 0:6c56fb4bc5f0 4340 | (unsigned int) ssl->in_msg[i + 2];
nexpaq 0:6c56fb4bc5f0 4341 i += 3;
nexpaq 0:6c56fb4bc5f0 4342
nexpaq 0:6c56fb4bc5f0 4343 if( n < 128 || i + n > ssl->in_hslen )
nexpaq 0:6c56fb4bc5f0 4344 {
nexpaq 0:6c56fb4bc5f0 4345 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
nexpaq 0:6c56fb4bc5f0 4346 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
nexpaq 0:6c56fb4bc5f0 4347 }
nexpaq 0:6c56fb4bc5f0 4348
nexpaq 0:6c56fb4bc5f0 4349 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
nexpaq 0:6c56fb4bc5f0 4350 ssl->in_msg + i, n );
nexpaq 0:6c56fb4bc5f0 4351 if( ret != 0 )
nexpaq 0:6c56fb4bc5f0 4352 {
nexpaq 0:6c56fb4bc5f0 4353 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
nexpaq 0:6c56fb4bc5f0 4354 return( ret );
nexpaq 0:6c56fb4bc5f0 4355 }
nexpaq 0:6c56fb4bc5f0 4356
nexpaq 0:6c56fb4bc5f0 4357 i += n;
nexpaq 0:6c56fb4bc5f0 4358 }
nexpaq 0:6c56fb4bc5f0 4359
nexpaq 0:6c56fb4bc5f0 4360 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
nexpaq 0:6c56fb4bc5f0 4361
nexpaq 0:6c56fb4bc5f0 4362 /*
nexpaq 0:6c56fb4bc5f0 4363 * On client, make sure the server cert doesn't change during renego to
nexpaq 0:6c56fb4bc5f0 4364 * avoid "triple handshake" attack: https://secure-resumption.com/
nexpaq 0:6c56fb4bc5f0 4365 */
nexpaq 0:6c56fb4bc5f0 4366 #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
nexpaq 0:6c56fb4bc5f0 4367 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
nexpaq 0:6c56fb4bc5f0 4368 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
nexpaq 0:6c56fb4bc5f0 4369 {
nexpaq 0:6c56fb4bc5f0 4370 if( ssl->session->peer_cert == NULL )
nexpaq 0:6c56fb4bc5f0 4371 {
nexpaq 0:6c56fb4bc5f0 4372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
nexpaq 0:6c56fb4bc5f0 4373 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
nexpaq 0:6c56fb4bc5f0 4374 }
nexpaq 0:6c56fb4bc5f0 4375
nexpaq 0:6c56fb4bc5f0 4376 if( ssl->session->peer_cert->raw.len !=
nexpaq 0:6c56fb4bc5f0 4377 ssl->session_negotiate->peer_cert->raw.len ||
nexpaq 0:6c56fb4bc5f0 4378 memcmp( ssl->session->peer_cert->raw.p,
nexpaq 0:6c56fb4bc5f0 4379 ssl->session_negotiate->peer_cert->raw.p,
nexpaq 0:6c56fb4bc5f0 4380 ssl->session->peer_cert->raw.len ) != 0 )
nexpaq 0:6c56fb4bc5f0 4381 {
nexpaq 0:6c56fb4bc5f0 4382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
nexpaq 0:6c56fb4bc5f0 4383 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
nexpaq 0:6c56fb4bc5f0 4384 }
nexpaq 0:6c56fb4bc5f0 4385 }
nexpaq 0:6c56fb4bc5f0 4386 #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
nexpaq 0:6c56fb4bc5f0 4387
nexpaq 0:6c56fb4bc5f0 4388 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
nexpaq 0:6c56fb4bc5f0 4389 {
nexpaq 0:6c56fb4bc5f0 4390 mbedtls_x509_crt *ca_chain;
nexpaq 0:6c56fb4bc5f0 4391 mbedtls_x509_crl *ca_crl;
nexpaq 0:6c56fb4bc5f0 4392
nexpaq 0:6c56fb4bc5f0 4393 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
nexpaq 0:6c56fb4bc5f0 4394 if( ssl->handshake->sni_ca_chain != NULL )
nexpaq 0:6c56fb4bc5f0 4395 {
nexpaq 0:6c56fb4bc5f0 4396 ca_chain = ssl->handshake->sni_ca_chain;
nexpaq 0:6c56fb4bc5f0 4397 ca_crl = ssl->handshake->sni_ca_crl;
nexpaq 0:6c56fb4bc5f0 4398 }
nexpaq 0:6c56fb4bc5f0 4399 else
nexpaq 0:6c56fb4bc5f0 4400 #endif
nexpaq 0:6c56fb4bc5f0 4401 {
nexpaq 0:6c56fb4bc5f0 4402 ca_chain = ssl->conf->ca_chain;
nexpaq 0:6c56fb4bc5f0 4403 ca_crl = ssl->conf->ca_crl;
nexpaq 0:6c56fb4bc5f0 4404 }
nexpaq 0:6c56fb4bc5f0 4405
nexpaq 0:6c56fb4bc5f0 4406 if( ca_chain == NULL )
nexpaq 0:6c56fb4bc5f0 4407 {
nexpaq 0:6c56fb4bc5f0 4408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
nexpaq 0:6c56fb4bc5f0 4409 return( MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED );
nexpaq 0:6c56fb4bc5f0 4410 }
nexpaq 0:6c56fb4bc5f0 4411
nexpaq 0:6c56fb4bc5f0 4412 /*
nexpaq 0:6c56fb4bc5f0 4413 * Main check: verify certificate
nexpaq 0:6c56fb4bc5f0 4414 */
nexpaq 0:6c56fb4bc5f0 4415 ret = mbedtls_x509_crt_verify_with_profile(
nexpaq 0:6c56fb4bc5f0 4416 ssl->session_negotiate->peer_cert,
nexpaq 0:6c56fb4bc5f0 4417 ca_chain, ca_crl,
nexpaq 0:6c56fb4bc5f0 4418 ssl->conf->cert_profile,
nexpaq 0:6c56fb4bc5f0 4419 ssl->hostname,
nexpaq 0:6c56fb4bc5f0 4420 &ssl->session_negotiate->verify_result,
nexpaq 0:6c56fb4bc5f0 4421 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
nexpaq 0:6c56fb4bc5f0 4422
nexpaq 0:6c56fb4bc5f0 4423 if( ret != 0 )
nexpaq 0:6c56fb4bc5f0 4424 {
nexpaq 0:6c56fb4bc5f0 4425 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
nexpaq 0:6c56fb4bc5f0 4426 }
nexpaq 0:6c56fb4bc5f0 4427
nexpaq 0:6c56fb4bc5f0 4428 /*
nexpaq 0:6c56fb4bc5f0 4429 * Secondary checks: always done, but change 'ret' only if it was 0
nexpaq 0:6c56fb4bc5f0 4430 */
nexpaq 0:6c56fb4bc5f0 4431
nexpaq 0:6c56fb4bc5f0 4432 #if defined(MBEDTLS_ECP_C)
nexpaq 0:6c56fb4bc5f0 4433 {
nexpaq 0:6c56fb4bc5f0 4434 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
nexpaq 0:6c56fb4bc5f0 4435
nexpaq 0:6c56fb4bc5f0 4436 /* If certificate uses an EC key, make sure the curve is OK */
nexpaq 0:6c56fb4bc5f0 4437 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
nexpaq 0:6c56fb4bc5f0 4438 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
nexpaq 0:6c56fb4bc5f0 4439 {
nexpaq 0:6c56fb4bc5f0 4440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
nexpaq 0:6c56fb4bc5f0 4441 if( ret == 0 )
nexpaq 0:6c56fb4bc5f0 4442 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
nexpaq 0:6c56fb4bc5f0 4443 }
nexpaq 0:6c56fb4bc5f0 4444 }
nexpaq 0:6c56fb4bc5f0 4445 #endif /* MBEDTLS_ECP_C */
nexpaq 0:6c56fb4bc5f0 4446
nexpaq 0:6c56fb4bc5f0 4447 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
nexpaq 0:6c56fb4bc5f0 4448 ciphersuite_info,
nexpaq 0:6c56fb4bc5f0 4449 ! ssl->conf->endpoint,
nexpaq 0:6c56fb4bc5f0 4450 &ssl->session_negotiate->verify_result ) != 0 )
nexpaq 0:6c56fb4bc5f0 4451 {
nexpaq 0:6c56fb4bc5f0 4452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
nexpaq 0:6c56fb4bc5f0 4453 if( ret == 0 )
nexpaq 0:6c56fb4bc5f0 4454 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
nexpaq 0:6c56fb4bc5f0 4455 }
nexpaq 0:6c56fb4bc5f0 4456
nexpaq 0:6c56fb4bc5f0 4457 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
nexpaq 0:6c56fb4bc5f0 4458 ret = 0;
nexpaq 0:6c56fb4bc5f0 4459 }
nexpaq 0:6c56fb4bc5f0 4460
nexpaq 0:6c56fb4bc5f0 4461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
nexpaq 0:6c56fb4bc5f0 4462
nexpaq 0:6c56fb4bc5f0 4463 return( ret );
nexpaq 0:6c56fb4bc5f0 4464 }
nexpaq 0:6c56fb4bc5f0 4465 #endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
nexpaq 0:6c56fb4bc5f0 4466 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
nexpaq 0:6c56fb4bc5f0 4467 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
nexpaq 0:6c56fb4bc5f0 4468 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
nexpaq 0:6c56fb4bc5f0 4469 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
nexpaq 0:6c56fb4bc5f0 4470 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
nexpaq 0:6c56fb4bc5f0 4471 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
nexpaq 0:6c56fb4bc5f0 4472
nexpaq 0:6c56fb4bc5f0 4473 int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 4474 {
nexpaq 0:6c56fb4bc5f0 4475 int ret;
nexpaq 0:6c56fb4bc5f0 4476
nexpaq 0:6c56fb4bc5f0 4477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
nexpaq 0:6c56fb4bc5f0 4478
nexpaq 0:6c56fb4bc5f0 4479 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
nexpaq 0:6c56fb4bc5f0 4480 ssl->out_msglen = 1;
nexpaq 0:6c56fb4bc5f0 4481 ssl->out_msg[0] = 1;
nexpaq 0:6c56fb4bc5f0 4482
nexpaq 0:6c56fb4bc5f0 4483 ssl->state++;
nexpaq 0:6c56fb4bc5f0 4484
nexpaq 0:6c56fb4bc5f0 4485 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 4486 {
nexpaq 0:6c56fb4bc5f0 4487 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
nexpaq 0:6c56fb4bc5f0 4488 return( ret );
nexpaq 0:6c56fb4bc5f0 4489 }
nexpaq 0:6c56fb4bc5f0 4490
nexpaq 0:6c56fb4bc5f0 4491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
nexpaq 0:6c56fb4bc5f0 4492
nexpaq 0:6c56fb4bc5f0 4493 return( 0 );
nexpaq 0:6c56fb4bc5f0 4494 }
nexpaq 0:6c56fb4bc5f0 4495
nexpaq 0:6c56fb4bc5f0 4496 int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 4497 {
nexpaq 0:6c56fb4bc5f0 4498 int ret;
nexpaq 0:6c56fb4bc5f0 4499
nexpaq 0:6c56fb4bc5f0 4500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
nexpaq 0:6c56fb4bc5f0 4501
nexpaq 0:6c56fb4bc5f0 4502 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 4503 {
nexpaq 0:6c56fb4bc5f0 4504 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
nexpaq 0:6c56fb4bc5f0 4505 return( ret );
nexpaq 0:6c56fb4bc5f0 4506 }
nexpaq 0:6c56fb4bc5f0 4507
nexpaq 0:6c56fb4bc5f0 4508 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
nexpaq 0:6c56fb4bc5f0 4509 {
nexpaq 0:6c56fb4bc5f0 4510 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
nexpaq 0:6c56fb4bc5f0 4511 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
nexpaq 0:6c56fb4bc5f0 4512 }
nexpaq 0:6c56fb4bc5f0 4513
nexpaq 0:6c56fb4bc5f0 4514 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
nexpaq 0:6c56fb4bc5f0 4515 {
nexpaq 0:6c56fb4bc5f0 4516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
nexpaq 0:6c56fb4bc5f0 4517 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
nexpaq 0:6c56fb4bc5f0 4518 }
nexpaq 0:6c56fb4bc5f0 4519
nexpaq 0:6c56fb4bc5f0 4520 /*
nexpaq 0:6c56fb4bc5f0 4521 * Switch to our negotiated transform and session parameters for inbound
nexpaq 0:6c56fb4bc5f0 4522 * data.
nexpaq 0:6c56fb4bc5f0 4523 */
nexpaq 0:6c56fb4bc5f0 4524 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
nexpaq 0:6c56fb4bc5f0 4525 ssl->transform_in = ssl->transform_negotiate;
nexpaq 0:6c56fb4bc5f0 4526 ssl->session_in = ssl->session_negotiate;
nexpaq 0:6c56fb4bc5f0 4527
nexpaq 0:6c56fb4bc5f0 4528 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 4529 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
nexpaq 0:6c56fb4bc5f0 4530 {
nexpaq 0:6c56fb4bc5f0 4531 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
nexpaq 0:6c56fb4bc5f0 4532 ssl_dtls_replay_reset( ssl );
nexpaq 0:6c56fb4bc5f0 4533 #endif
nexpaq 0:6c56fb4bc5f0 4534
nexpaq 0:6c56fb4bc5f0 4535 /* Increment epoch */
nexpaq 0:6c56fb4bc5f0 4536 if( ++ssl->in_epoch == 0 )
nexpaq 0:6c56fb4bc5f0 4537 {
nexpaq 0:6c56fb4bc5f0 4538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
nexpaq 0:6c56fb4bc5f0 4539 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
nexpaq 0:6c56fb4bc5f0 4540 }
nexpaq 0:6c56fb4bc5f0 4541 }
nexpaq 0:6c56fb4bc5f0 4542 else
nexpaq 0:6c56fb4bc5f0 4543 #endif /* MBEDTLS_SSL_PROTO_DTLS */
nexpaq 0:6c56fb4bc5f0 4544 memset( ssl->in_ctr, 0, 8 );
nexpaq 0:6c56fb4bc5f0 4545
nexpaq 0:6c56fb4bc5f0 4546 /*
nexpaq 0:6c56fb4bc5f0 4547 * Set the in_msg pointer to the correct location based on IV length
nexpaq 0:6c56fb4bc5f0 4548 */
nexpaq 0:6c56fb4bc5f0 4549 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
nexpaq 0:6c56fb4bc5f0 4550 {
nexpaq 0:6c56fb4bc5f0 4551 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
nexpaq 0:6c56fb4bc5f0 4552 ssl->transform_negotiate->fixed_ivlen;
nexpaq 0:6c56fb4bc5f0 4553 }
nexpaq 0:6c56fb4bc5f0 4554 else
nexpaq 0:6c56fb4bc5f0 4555 ssl->in_msg = ssl->in_iv;
nexpaq 0:6c56fb4bc5f0 4556
nexpaq 0:6c56fb4bc5f0 4557 #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
nexpaq 0:6c56fb4bc5f0 4558 if( mbedtls_ssl_hw_record_activate != NULL )
nexpaq 0:6c56fb4bc5f0 4559 {
nexpaq 0:6c56fb4bc5f0 4560 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 4561 {
nexpaq 0:6c56fb4bc5f0 4562 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
nexpaq 0:6c56fb4bc5f0 4563 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
nexpaq 0:6c56fb4bc5f0 4564 }
nexpaq 0:6c56fb4bc5f0 4565 }
nexpaq 0:6c56fb4bc5f0 4566 #endif
nexpaq 0:6c56fb4bc5f0 4567
nexpaq 0:6c56fb4bc5f0 4568 ssl->state++;
nexpaq 0:6c56fb4bc5f0 4569
nexpaq 0:6c56fb4bc5f0 4570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
nexpaq 0:6c56fb4bc5f0 4571
nexpaq 0:6c56fb4bc5f0 4572 return( 0 );
nexpaq 0:6c56fb4bc5f0 4573 }
nexpaq 0:6c56fb4bc5f0 4574
nexpaq 0:6c56fb4bc5f0 4575 void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
nexpaq 0:6c56fb4bc5f0 4576 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
nexpaq 0:6c56fb4bc5f0 4577 {
nexpaq 0:6c56fb4bc5f0 4578 ((void) ciphersuite_info);
nexpaq 0:6c56fb4bc5f0 4579
nexpaq 0:6c56fb4bc5f0 4580 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
nexpaq 0:6c56fb4bc5f0 4581 defined(MBEDTLS_SSL_PROTO_TLS1_1)
nexpaq 0:6c56fb4bc5f0 4582 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
nexpaq 0:6c56fb4bc5f0 4583 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
nexpaq 0:6c56fb4bc5f0 4584 else
nexpaq 0:6c56fb4bc5f0 4585 #endif
nexpaq 0:6c56fb4bc5f0 4586 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
nexpaq 0:6c56fb4bc5f0 4587 #if defined(MBEDTLS_SHA512_C)
nexpaq 0:6c56fb4bc5f0 4588 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
nexpaq 0:6c56fb4bc5f0 4589 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
nexpaq 0:6c56fb4bc5f0 4590 else
nexpaq 0:6c56fb4bc5f0 4591 #endif
nexpaq 0:6c56fb4bc5f0 4592 #if defined(MBEDTLS_SHA256_C)
nexpaq 0:6c56fb4bc5f0 4593 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
nexpaq 0:6c56fb4bc5f0 4594 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
nexpaq 0:6c56fb4bc5f0 4595 else
nexpaq 0:6c56fb4bc5f0 4596 #endif
nexpaq 0:6c56fb4bc5f0 4597 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
nexpaq 0:6c56fb4bc5f0 4598 {
nexpaq 0:6c56fb4bc5f0 4599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 4600 return;
nexpaq 0:6c56fb4bc5f0 4601 }
nexpaq 0:6c56fb4bc5f0 4602 }
nexpaq 0:6c56fb4bc5f0 4603
nexpaq 0:6c56fb4bc5f0 4604 void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 4605 {
nexpaq 0:6c56fb4bc5f0 4606 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
nexpaq 0:6c56fb4bc5f0 4607 defined(MBEDTLS_SSL_PROTO_TLS1_1)
nexpaq 0:6c56fb4bc5f0 4608 mbedtls_md5_starts( &ssl->handshake->fin_md5 );
nexpaq 0:6c56fb4bc5f0 4609 mbedtls_sha1_starts( &ssl->handshake->fin_sha1 );
nexpaq 0:6c56fb4bc5f0 4610 #endif
nexpaq 0:6c56fb4bc5f0 4611 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
nexpaq 0:6c56fb4bc5f0 4612 #if defined(MBEDTLS_SHA256_C)
nexpaq 0:6c56fb4bc5f0 4613 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
nexpaq 0:6c56fb4bc5f0 4614 #endif
nexpaq 0:6c56fb4bc5f0 4615 #if defined(MBEDTLS_SHA512_C)
nexpaq 0:6c56fb4bc5f0 4616 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
nexpaq 0:6c56fb4bc5f0 4617 #endif
nexpaq 0:6c56fb4bc5f0 4618 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
nexpaq 0:6c56fb4bc5f0 4619 }
nexpaq 0:6c56fb4bc5f0 4620
nexpaq 0:6c56fb4bc5f0 4621 static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
nexpaq 0:6c56fb4bc5f0 4622 const unsigned char *buf, size_t len )
nexpaq 0:6c56fb4bc5f0 4623 {
nexpaq 0:6c56fb4bc5f0 4624 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
nexpaq 0:6c56fb4bc5f0 4625 defined(MBEDTLS_SSL_PROTO_TLS1_1)
nexpaq 0:6c56fb4bc5f0 4626 mbedtls_md5_update( &ssl->handshake->fin_md5 , buf, len );
nexpaq 0:6c56fb4bc5f0 4627 mbedtls_sha1_update( &ssl->handshake->fin_sha1, buf, len );
nexpaq 0:6c56fb4bc5f0 4628 #endif
nexpaq 0:6c56fb4bc5f0 4629 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
nexpaq 0:6c56fb4bc5f0 4630 #if defined(MBEDTLS_SHA256_C)
nexpaq 0:6c56fb4bc5f0 4631 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
nexpaq 0:6c56fb4bc5f0 4632 #endif
nexpaq 0:6c56fb4bc5f0 4633 #if defined(MBEDTLS_SHA512_C)
nexpaq 0:6c56fb4bc5f0 4634 mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len );
nexpaq 0:6c56fb4bc5f0 4635 #endif
nexpaq 0:6c56fb4bc5f0 4636 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
nexpaq 0:6c56fb4bc5f0 4637 }
nexpaq 0:6c56fb4bc5f0 4638
nexpaq 0:6c56fb4bc5f0 4639 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
nexpaq 0:6c56fb4bc5f0 4640 defined(MBEDTLS_SSL_PROTO_TLS1_1)
nexpaq 0:6c56fb4bc5f0 4641 static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
nexpaq 0:6c56fb4bc5f0 4642 const unsigned char *buf, size_t len )
nexpaq 0:6c56fb4bc5f0 4643 {
nexpaq 0:6c56fb4bc5f0 4644 mbedtls_md5_update( &ssl->handshake->fin_md5 , buf, len );
nexpaq 0:6c56fb4bc5f0 4645 mbedtls_sha1_update( &ssl->handshake->fin_sha1, buf, len );
nexpaq 0:6c56fb4bc5f0 4646 }
nexpaq 0:6c56fb4bc5f0 4647 #endif
nexpaq 0:6c56fb4bc5f0 4648
nexpaq 0:6c56fb4bc5f0 4649 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
nexpaq 0:6c56fb4bc5f0 4650 #if defined(MBEDTLS_SHA256_C)
nexpaq 0:6c56fb4bc5f0 4651 static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
nexpaq 0:6c56fb4bc5f0 4652 const unsigned char *buf, size_t len )
nexpaq 0:6c56fb4bc5f0 4653 {
nexpaq 0:6c56fb4bc5f0 4654 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
nexpaq 0:6c56fb4bc5f0 4655 }
nexpaq 0:6c56fb4bc5f0 4656 #endif
nexpaq 0:6c56fb4bc5f0 4657
nexpaq 0:6c56fb4bc5f0 4658 #if defined(MBEDTLS_SHA512_C)
nexpaq 0:6c56fb4bc5f0 4659 static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
nexpaq 0:6c56fb4bc5f0 4660 const unsigned char *buf, size_t len )
nexpaq 0:6c56fb4bc5f0 4661 {
nexpaq 0:6c56fb4bc5f0 4662 mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len );
nexpaq 0:6c56fb4bc5f0 4663 }
nexpaq 0:6c56fb4bc5f0 4664 #endif
nexpaq 0:6c56fb4bc5f0 4665 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
nexpaq 0:6c56fb4bc5f0 4666
nexpaq 0:6c56fb4bc5f0 4667 #if defined(MBEDTLS_SSL_PROTO_SSL3)
nexpaq 0:6c56fb4bc5f0 4668 static void ssl_calc_finished_ssl(
nexpaq 0:6c56fb4bc5f0 4669 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
nexpaq 0:6c56fb4bc5f0 4670 {
nexpaq 0:6c56fb4bc5f0 4671 const char *sender;
nexpaq 0:6c56fb4bc5f0 4672 mbedtls_md5_context md5;
nexpaq 0:6c56fb4bc5f0 4673 mbedtls_sha1_context sha1;
nexpaq 0:6c56fb4bc5f0 4674
nexpaq 0:6c56fb4bc5f0 4675 unsigned char padbuf[48];
nexpaq 0:6c56fb4bc5f0 4676 unsigned char md5sum[16];
nexpaq 0:6c56fb4bc5f0 4677 unsigned char sha1sum[20];
nexpaq 0:6c56fb4bc5f0 4678
nexpaq 0:6c56fb4bc5f0 4679 mbedtls_ssl_session *session = ssl->session_negotiate;
nexpaq 0:6c56fb4bc5f0 4680 if( !session )
nexpaq 0:6c56fb4bc5f0 4681 session = ssl->session;
nexpaq 0:6c56fb4bc5f0 4682
nexpaq 0:6c56fb4bc5f0 4683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
nexpaq 0:6c56fb4bc5f0 4684
nexpaq 0:6c56fb4bc5f0 4685 mbedtls_md5_init( &md5 );
nexpaq 0:6c56fb4bc5f0 4686 mbedtls_sha1_init( &sha1 );
nexpaq 0:6c56fb4bc5f0 4687
nexpaq 0:6c56fb4bc5f0 4688 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
nexpaq 0:6c56fb4bc5f0 4689 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
nexpaq 0:6c56fb4bc5f0 4690
nexpaq 0:6c56fb4bc5f0 4691 /*
nexpaq 0:6c56fb4bc5f0 4692 * SSLv3:
nexpaq 0:6c56fb4bc5f0 4693 * hash =
nexpaq 0:6c56fb4bc5f0 4694 * MD5( master + pad2 +
nexpaq 0:6c56fb4bc5f0 4695 * MD5( handshake + sender + master + pad1 ) )
nexpaq 0:6c56fb4bc5f0 4696 * + SHA1( master + pad2 +
nexpaq 0:6c56fb4bc5f0 4697 * SHA1( handshake + sender + master + pad1 ) )
nexpaq 0:6c56fb4bc5f0 4698 */
nexpaq 0:6c56fb4bc5f0 4699
nexpaq 0:6c56fb4bc5f0 4700 #if !defined(MBEDTLS_MD5_ALT)
nexpaq 0:6c56fb4bc5f0 4701 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
nexpaq 0:6c56fb4bc5f0 4702 md5.state, sizeof( md5.state ) );
nexpaq 0:6c56fb4bc5f0 4703 #endif
nexpaq 0:6c56fb4bc5f0 4704
nexpaq 0:6c56fb4bc5f0 4705 #if !defined(MBEDTLS_SHA1_ALT)
nexpaq 0:6c56fb4bc5f0 4706 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
nexpaq 0:6c56fb4bc5f0 4707 sha1.state, sizeof( sha1.state ) );
nexpaq 0:6c56fb4bc5f0 4708 #endif
nexpaq 0:6c56fb4bc5f0 4709
nexpaq 0:6c56fb4bc5f0 4710 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
nexpaq 0:6c56fb4bc5f0 4711 : "SRVR";
nexpaq 0:6c56fb4bc5f0 4712
nexpaq 0:6c56fb4bc5f0 4713 memset( padbuf, 0x36, 48 );
nexpaq 0:6c56fb4bc5f0 4714
nexpaq 0:6c56fb4bc5f0 4715 mbedtls_md5_update( &md5, (const unsigned char *) sender, 4 );
nexpaq 0:6c56fb4bc5f0 4716 mbedtls_md5_update( &md5, session->master, 48 );
nexpaq 0:6c56fb4bc5f0 4717 mbedtls_md5_update( &md5, padbuf, 48 );
nexpaq 0:6c56fb4bc5f0 4718 mbedtls_md5_finish( &md5, md5sum );
nexpaq 0:6c56fb4bc5f0 4719
nexpaq 0:6c56fb4bc5f0 4720 mbedtls_sha1_update( &sha1, (const unsigned char *) sender, 4 );
nexpaq 0:6c56fb4bc5f0 4721 mbedtls_sha1_update( &sha1, session->master, 48 );
nexpaq 0:6c56fb4bc5f0 4722 mbedtls_sha1_update( &sha1, padbuf, 40 );
nexpaq 0:6c56fb4bc5f0 4723 mbedtls_sha1_finish( &sha1, sha1sum );
nexpaq 0:6c56fb4bc5f0 4724
nexpaq 0:6c56fb4bc5f0 4725 memset( padbuf, 0x5C, 48 );
nexpaq 0:6c56fb4bc5f0 4726
nexpaq 0:6c56fb4bc5f0 4727 mbedtls_md5_starts( &md5 );
nexpaq 0:6c56fb4bc5f0 4728 mbedtls_md5_update( &md5, session->master, 48 );
nexpaq 0:6c56fb4bc5f0 4729 mbedtls_md5_update( &md5, padbuf, 48 );
nexpaq 0:6c56fb4bc5f0 4730 mbedtls_md5_update( &md5, md5sum, 16 );
nexpaq 0:6c56fb4bc5f0 4731 mbedtls_md5_finish( &md5, buf );
nexpaq 0:6c56fb4bc5f0 4732
nexpaq 0:6c56fb4bc5f0 4733 mbedtls_sha1_starts( &sha1 );
nexpaq 0:6c56fb4bc5f0 4734 mbedtls_sha1_update( &sha1, session->master, 48 );
nexpaq 0:6c56fb4bc5f0 4735 mbedtls_sha1_update( &sha1, padbuf , 40 );
nexpaq 0:6c56fb4bc5f0 4736 mbedtls_sha1_update( &sha1, sha1sum, 20 );
nexpaq 0:6c56fb4bc5f0 4737 mbedtls_sha1_finish( &sha1, buf + 16 );
nexpaq 0:6c56fb4bc5f0 4738
nexpaq 0:6c56fb4bc5f0 4739 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
nexpaq 0:6c56fb4bc5f0 4740
nexpaq 0:6c56fb4bc5f0 4741 mbedtls_md5_free( &md5 );
nexpaq 0:6c56fb4bc5f0 4742 mbedtls_sha1_free( &sha1 );
nexpaq 0:6c56fb4bc5f0 4743
nexpaq 0:6c56fb4bc5f0 4744 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
nexpaq 0:6c56fb4bc5f0 4745 mbedtls_zeroize( md5sum, sizeof( md5sum ) );
nexpaq 0:6c56fb4bc5f0 4746 mbedtls_zeroize( sha1sum, sizeof( sha1sum ) );
nexpaq 0:6c56fb4bc5f0 4747
nexpaq 0:6c56fb4bc5f0 4748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
nexpaq 0:6c56fb4bc5f0 4749 }
nexpaq 0:6c56fb4bc5f0 4750 #endif /* MBEDTLS_SSL_PROTO_SSL3 */
nexpaq 0:6c56fb4bc5f0 4751
nexpaq 0:6c56fb4bc5f0 4752 #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
nexpaq 0:6c56fb4bc5f0 4753 static void ssl_calc_finished_tls(
nexpaq 0:6c56fb4bc5f0 4754 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
nexpaq 0:6c56fb4bc5f0 4755 {
nexpaq 0:6c56fb4bc5f0 4756 int len = 12;
nexpaq 0:6c56fb4bc5f0 4757 const char *sender;
nexpaq 0:6c56fb4bc5f0 4758 mbedtls_md5_context md5;
nexpaq 0:6c56fb4bc5f0 4759 mbedtls_sha1_context sha1;
nexpaq 0:6c56fb4bc5f0 4760 unsigned char padbuf[36];
nexpaq 0:6c56fb4bc5f0 4761
nexpaq 0:6c56fb4bc5f0 4762 mbedtls_ssl_session *session = ssl->session_negotiate;
nexpaq 0:6c56fb4bc5f0 4763 if( !session )
nexpaq 0:6c56fb4bc5f0 4764 session = ssl->session;
nexpaq 0:6c56fb4bc5f0 4765
nexpaq 0:6c56fb4bc5f0 4766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
nexpaq 0:6c56fb4bc5f0 4767
nexpaq 0:6c56fb4bc5f0 4768 mbedtls_md5_init( &md5 );
nexpaq 0:6c56fb4bc5f0 4769 mbedtls_sha1_init( &sha1 );
nexpaq 0:6c56fb4bc5f0 4770
nexpaq 0:6c56fb4bc5f0 4771 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
nexpaq 0:6c56fb4bc5f0 4772 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
nexpaq 0:6c56fb4bc5f0 4773
nexpaq 0:6c56fb4bc5f0 4774 /*
nexpaq 0:6c56fb4bc5f0 4775 * TLSv1:
nexpaq 0:6c56fb4bc5f0 4776 * hash = PRF( master, finished_label,
nexpaq 0:6c56fb4bc5f0 4777 * MD5( handshake ) + SHA1( handshake ) )[0..11]
nexpaq 0:6c56fb4bc5f0 4778 */
nexpaq 0:6c56fb4bc5f0 4779
nexpaq 0:6c56fb4bc5f0 4780 #if !defined(MBEDTLS_MD5_ALT)
nexpaq 0:6c56fb4bc5f0 4781 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
nexpaq 0:6c56fb4bc5f0 4782 md5.state, sizeof( md5.state ) );
nexpaq 0:6c56fb4bc5f0 4783 #endif
nexpaq 0:6c56fb4bc5f0 4784
nexpaq 0:6c56fb4bc5f0 4785 #if !defined(MBEDTLS_SHA1_ALT)
nexpaq 0:6c56fb4bc5f0 4786 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
nexpaq 0:6c56fb4bc5f0 4787 sha1.state, sizeof( sha1.state ) );
nexpaq 0:6c56fb4bc5f0 4788 #endif
nexpaq 0:6c56fb4bc5f0 4789
nexpaq 0:6c56fb4bc5f0 4790 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
nexpaq 0:6c56fb4bc5f0 4791 ? "client finished"
nexpaq 0:6c56fb4bc5f0 4792 : "server finished";
nexpaq 0:6c56fb4bc5f0 4793
nexpaq 0:6c56fb4bc5f0 4794 mbedtls_md5_finish( &md5, padbuf );
nexpaq 0:6c56fb4bc5f0 4795 mbedtls_sha1_finish( &sha1, padbuf + 16 );
nexpaq 0:6c56fb4bc5f0 4796
nexpaq 0:6c56fb4bc5f0 4797 ssl->handshake->tls_prf( session->master, 48, sender,
nexpaq 0:6c56fb4bc5f0 4798 padbuf, 36, buf, len );
nexpaq 0:6c56fb4bc5f0 4799
nexpaq 0:6c56fb4bc5f0 4800 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
nexpaq 0:6c56fb4bc5f0 4801
nexpaq 0:6c56fb4bc5f0 4802 mbedtls_md5_free( &md5 );
nexpaq 0:6c56fb4bc5f0 4803 mbedtls_sha1_free( &sha1 );
nexpaq 0:6c56fb4bc5f0 4804
nexpaq 0:6c56fb4bc5f0 4805 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
nexpaq 0:6c56fb4bc5f0 4806
nexpaq 0:6c56fb4bc5f0 4807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
nexpaq 0:6c56fb4bc5f0 4808 }
nexpaq 0:6c56fb4bc5f0 4809 #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
nexpaq 0:6c56fb4bc5f0 4810
nexpaq 0:6c56fb4bc5f0 4811 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
nexpaq 0:6c56fb4bc5f0 4812 #if defined(MBEDTLS_SHA256_C)
nexpaq 0:6c56fb4bc5f0 4813 static void ssl_calc_finished_tls_sha256(
nexpaq 0:6c56fb4bc5f0 4814 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
nexpaq 0:6c56fb4bc5f0 4815 {
nexpaq 0:6c56fb4bc5f0 4816 int len = 12;
nexpaq 0:6c56fb4bc5f0 4817 const char *sender;
nexpaq 0:6c56fb4bc5f0 4818 mbedtls_sha256_context sha256;
nexpaq 0:6c56fb4bc5f0 4819 unsigned char padbuf[32];
nexpaq 0:6c56fb4bc5f0 4820
nexpaq 0:6c56fb4bc5f0 4821 mbedtls_ssl_session *session = ssl->session_negotiate;
nexpaq 0:6c56fb4bc5f0 4822 if( !session )
nexpaq 0:6c56fb4bc5f0 4823 session = ssl->session;
nexpaq 0:6c56fb4bc5f0 4824
nexpaq 0:6c56fb4bc5f0 4825 mbedtls_sha256_init( &sha256 );
nexpaq 0:6c56fb4bc5f0 4826
nexpaq 0:6c56fb4bc5f0 4827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
nexpaq 0:6c56fb4bc5f0 4828
nexpaq 0:6c56fb4bc5f0 4829 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
nexpaq 0:6c56fb4bc5f0 4830
nexpaq 0:6c56fb4bc5f0 4831 /*
nexpaq 0:6c56fb4bc5f0 4832 * TLSv1.2:
nexpaq 0:6c56fb4bc5f0 4833 * hash = PRF( master, finished_label,
nexpaq 0:6c56fb4bc5f0 4834 * Hash( handshake ) )[0.11]
nexpaq 0:6c56fb4bc5f0 4835 */
nexpaq 0:6c56fb4bc5f0 4836
nexpaq 0:6c56fb4bc5f0 4837 #if !defined(MBEDTLS_SHA256_ALT)
nexpaq 0:6c56fb4bc5f0 4838 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
nexpaq 0:6c56fb4bc5f0 4839 sha256.state, sizeof( sha256.state ) );
nexpaq 0:6c56fb4bc5f0 4840 #endif
nexpaq 0:6c56fb4bc5f0 4841
nexpaq 0:6c56fb4bc5f0 4842 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
nexpaq 0:6c56fb4bc5f0 4843 ? "client finished"
nexpaq 0:6c56fb4bc5f0 4844 : "server finished";
nexpaq 0:6c56fb4bc5f0 4845
nexpaq 0:6c56fb4bc5f0 4846 mbedtls_sha256_finish( &sha256, padbuf );
nexpaq 0:6c56fb4bc5f0 4847
nexpaq 0:6c56fb4bc5f0 4848 ssl->handshake->tls_prf( session->master, 48, sender,
nexpaq 0:6c56fb4bc5f0 4849 padbuf, 32, buf, len );
nexpaq 0:6c56fb4bc5f0 4850
nexpaq 0:6c56fb4bc5f0 4851 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
nexpaq 0:6c56fb4bc5f0 4852
nexpaq 0:6c56fb4bc5f0 4853 mbedtls_sha256_free( &sha256 );
nexpaq 0:6c56fb4bc5f0 4854
nexpaq 0:6c56fb4bc5f0 4855 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
nexpaq 0:6c56fb4bc5f0 4856
nexpaq 0:6c56fb4bc5f0 4857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
nexpaq 0:6c56fb4bc5f0 4858 }
nexpaq 0:6c56fb4bc5f0 4859 #endif /* MBEDTLS_SHA256_C */
nexpaq 0:6c56fb4bc5f0 4860
nexpaq 0:6c56fb4bc5f0 4861 #if defined(MBEDTLS_SHA512_C)
nexpaq 0:6c56fb4bc5f0 4862 static void ssl_calc_finished_tls_sha384(
nexpaq 0:6c56fb4bc5f0 4863 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
nexpaq 0:6c56fb4bc5f0 4864 {
nexpaq 0:6c56fb4bc5f0 4865 int len = 12;
nexpaq 0:6c56fb4bc5f0 4866 const char *sender;
nexpaq 0:6c56fb4bc5f0 4867 mbedtls_sha512_context sha512;
nexpaq 0:6c56fb4bc5f0 4868 unsigned char padbuf[48];
nexpaq 0:6c56fb4bc5f0 4869
nexpaq 0:6c56fb4bc5f0 4870 mbedtls_ssl_session *session = ssl->session_negotiate;
nexpaq 0:6c56fb4bc5f0 4871 if( !session )
nexpaq 0:6c56fb4bc5f0 4872 session = ssl->session;
nexpaq 0:6c56fb4bc5f0 4873
nexpaq 0:6c56fb4bc5f0 4874 mbedtls_sha512_init( &sha512 );
nexpaq 0:6c56fb4bc5f0 4875
nexpaq 0:6c56fb4bc5f0 4876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
nexpaq 0:6c56fb4bc5f0 4877
nexpaq 0:6c56fb4bc5f0 4878 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
nexpaq 0:6c56fb4bc5f0 4879
nexpaq 0:6c56fb4bc5f0 4880 /*
nexpaq 0:6c56fb4bc5f0 4881 * TLSv1.2:
nexpaq 0:6c56fb4bc5f0 4882 * hash = PRF( master, finished_label,
nexpaq 0:6c56fb4bc5f0 4883 * Hash( handshake ) )[0.11]
nexpaq 0:6c56fb4bc5f0 4884 */
nexpaq 0:6c56fb4bc5f0 4885
nexpaq 0:6c56fb4bc5f0 4886 #if !defined(MBEDTLS_SHA512_ALT)
nexpaq 0:6c56fb4bc5f0 4887 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
nexpaq 0:6c56fb4bc5f0 4888 sha512.state, sizeof( sha512.state ) );
nexpaq 0:6c56fb4bc5f0 4889 #endif
nexpaq 0:6c56fb4bc5f0 4890
nexpaq 0:6c56fb4bc5f0 4891 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
nexpaq 0:6c56fb4bc5f0 4892 ? "client finished"
nexpaq 0:6c56fb4bc5f0 4893 : "server finished";
nexpaq 0:6c56fb4bc5f0 4894
nexpaq 0:6c56fb4bc5f0 4895 mbedtls_sha512_finish( &sha512, padbuf );
nexpaq 0:6c56fb4bc5f0 4896
nexpaq 0:6c56fb4bc5f0 4897 ssl->handshake->tls_prf( session->master, 48, sender,
nexpaq 0:6c56fb4bc5f0 4898 padbuf, 48, buf, len );
nexpaq 0:6c56fb4bc5f0 4899
nexpaq 0:6c56fb4bc5f0 4900 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
nexpaq 0:6c56fb4bc5f0 4901
nexpaq 0:6c56fb4bc5f0 4902 mbedtls_sha512_free( &sha512 );
nexpaq 0:6c56fb4bc5f0 4903
nexpaq 0:6c56fb4bc5f0 4904 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
nexpaq 0:6c56fb4bc5f0 4905
nexpaq 0:6c56fb4bc5f0 4906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
nexpaq 0:6c56fb4bc5f0 4907 }
nexpaq 0:6c56fb4bc5f0 4908 #endif /* MBEDTLS_SHA512_C */
nexpaq 0:6c56fb4bc5f0 4909 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
nexpaq 0:6c56fb4bc5f0 4910
nexpaq 0:6c56fb4bc5f0 4911 static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 4912 {
nexpaq 0:6c56fb4bc5f0 4913 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
nexpaq 0:6c56fb4bc5f0 4914
nexpaq 0:6c56fb4bc5f0 4915 /*
nexpaq 0:6c56fb4bc5f0 4916 * Free our handshake params
nexpaq 0:6c56fb4bc5f0 4917 */
nexpaq 0:6c56fb4bc5f0 4918 mbedtls_ssl_handshake_free( ssl->handshake );
nexpaq 0:6c56fb4bc5f0 4919 mbedtls_free( ssl->handshake );
nexpaq 0:6c56fb4bc5f0 4920 ssl->handshake = NULL;
nexpaq 0:6c56fb4bc5f0 4921
nexpaq 0:6c56fb4bc5f0 4922 /*
nexpaq 0:6c56fb4bc5f0 4923 * Free the previous transform and swith in the current one
nexpaq 0:6c56fb4bc5f0 4924 */
nexpaq 0:6c56fb4bc5f0 4925 if( ssl->transform )
nexpaq 0:6c56fb4bc5f0 4926 {
nexpaq 0:6c56fb4bc5f0 4927 mbedtls_ssl_transform_free( ssl->transform );
nexpaq 0:6c56fb4bc5f0 4928 mbedtls_free( ssl->transform );
nexpaq 0:6c56fb4bc5f0 4929 }
nexpaq 0:6c56fb4bc5f0 4930 ssl->transform = ssl->transform_negotiate;
nexpaq 0:6c56fb4bc5f0 4931 ssl->transform_negotiate = NULL;
nexpaq 0:6c56fb4bc5f0 4932
nexpaq 0:6c56fb4bc5f0 4933 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
nexpaq 0:6c56fb4bc5f0 4934 }
nexpaq 0:6c56fb4bc5f0 4935
nexpaq 0:6c56fb4bc5f0 4936 void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 4937 {
nexpaq 0:6c56fb4bc5f0 4938 int resume = ssl->handshake->resume;
nexpaq 0:6c56fb4bc5f0 4939
nexpaq 0:6c56fb4bc5f0 4940 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
nexpaq 0:6c56fb4bc5f0 4941
nexpaq 0:6c56fb4bc5f0 4942 #if defined(MBEDTLS_SSL_RENEGOTIATION)
nexpaq 0:6c56fb4bc5f0 4943 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
nexpaq 0:6c56fb4bc5f0 4944 {
nexpaq 0:6c56fb4bc5f0 4945 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
nexpaq 0:6c56fb4bc5f0 4946 ssl->renego_records_seen = 0;
nexpaq 0:6c56fb4bc5f0 4947 }
nexpaq 0:6c56fb4bc5f0 4948 #endif
nexpaq 0:6c56fb4bc5f0 4949
nexpaq 0:6c56fb4bc5f0 4950 /*
nexpaq 0:6c56fb4bc5f0 4951 * Free the previous session and switch in the current one
nexpaq 0:6c56fb4bc5f0 4952 */
nexpaq 0:6c56fb4bc5f0 4953 if( ssl->session )
nexpaq 0:6c56fb4bc5f0 4954 {
nexpaq 0:6c56fb4bc5f0 4955 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
nexpaq 0:6c56fb4bc5f0 4956 /* RFC 7366 3.1: keep the EtM state */
nexpaq 0:6c56fb4bc5f0 4957 ssl->session_negotiate->encrypt_then_mac =
nexpaq 0:6c56fb4bc5f0 4958 ssl->session->encrypt_then_mac;
nexpaq 0:6c56fb4bc5f0 4959 #endif
nexpaq 0:6c56fb4bc5f0 4960
nexpaq 0:6c56fb4bc5f0 4961 mbedtls_ssl_session_free( ssl->session );
nexpaq 0:6c56fb4bc5f0 4962 mbedtls_free( ssl->session );
nexpaq 0:6c56fb4bc5f0 4963 }
nexpaq 0:6c56fb4bc5f0 4964 ssl->session = ssl->session_negotiate;
nexpaq 0:6c56fb4bc5f0 4965 ssl->session_negotiate = NULL;
nexpaq 0:6c56fb4bc5f0 4966
nexpaq 0:6c56fb4bc5f0 4967 /*
nexpaq 0:6c56fb4bc5f0 4968 * Add cache entry
nexpaq 0:6c56fb4bc5f0 4969 */
nexpaq 0:6c56fb4bc5f0 4970 if( ssl->conf->f_set_cache != NULL &&
nexpaq 0:6c56fb4bc5f0 4971 ssl->session->id_len != 0 &&
nexpaq 0:6c56fb4bc5f0 4972 resume == 0 )
nexpaq 0:6c56fb4bc5f0 4973 {
nexpaq 0:6c56fb4bc5f0 4974 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
nexpaq 0:6c56fb4bc5f0 4975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
nexpaq 0:6c56fb4bc5f0 4976 }
nexpaq 0:6c56fb4bc5f0 4977
nexpaq 0:6c56fb4bc5f0 4978 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 4979 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
nexpaq 0:6c56fb4bc5f0 4980 ssl->handshake->flight != NULL )
nexpaq 0:6c56fb4bc5f0 4981 {
nexpaq 0:6c56fb4bc5f0 4982 /* Cancel handshake timer */
nexpaq 0:6c56fb4bc5f0 4983 ssl_set_timer( ssl, 0 );
nexpaq 0:6c56fb4bc5f0 4984
nexpaq 0:6c56fb4bc5f0 4985 /* Keep last flight around in case we need to resend it:
nexpaq 0:6c56fb4bc5f0 4986 * we need the handshake and transform structures for that */
nexpaq 0:6c56fb4bc5f0 4987 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
nexpaq 0:6c56fb4bc5f0 4988 }
nexpaq 0:6c56fb4bc5f0 4989 else
nexpaq 0:6c56fb4bc5f0 4990 #endif
nexpaq 0:6c56fb4bc5f0 4991 ssl_handshake_wrapup_free_hs_transform( ssl );
nexpaq 0:6c56fb4bc5f0 4992
nexpaq 0:6c56fb4bc5f0 4993 ssl->state++;
nexpaq 0:6c56fb4bc5f0 4994
nexpaq 0:6c56fb4bc5f0 4995 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
nexpaq 0:6c56fb4bc5f0 4996 }
nexpaq 0:6c56fb4bc5f0 4997
nexpaq 0:6c56fb4bc5f0 4998 int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 4999 {
nexpaq 0:6c56fb4bc5f0 5000 int ret, hash_len;
nexpaq 0:6c56fb4bc5f0 5001
nexpaq 0:6c56fb4bc5f0 5002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
nexpaq 0:6c56fb4bc5f0 5003
nexpaq 0:6c56fb4bc5f0 5004 /*
nexpaq 0:6c56fb4bc5f0 5005 * Set the out_msg pointer to the correct location based on IV length
nexpaq 0:6c56fb4bc5f0 5006 */
nexpaq 0:6c56fb4bc5f0 5007 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
nexpaq 0:6c56fb4bc5f0 5008 {
nexpaq 0:6c56fb4bc5f0 5009 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
nexpaq 0:6c56fb4bc5f0 5010 ssl->transform_negotiate->fixed_ivlen;
nexpaq 0:6c56fb4bc5f0 5011 }
nexpaq 0:6c56fb4bc5f0 5012 else
nexpaq 0:6c56fb4bc5f0 5013 ssl->out_msg = ssl->out_iv;
nexpaq 0:6c56fb4bc5f0 5014
nexpaq 0:6c56fb4bc5f0 5015 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
nexpaq 0:6c56fb4bc5f0 5016
nexpaq 0:6c56fb4bc5f0 5017 /*
nexpaq 0:6c56fb4bc5f0 5018 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
nexpaq 0:6c56fb4bc5f0 5019 * may define some other value. Currently (early 2016), no defined
nexpaq 0:6c56fb4bc5f0 5020 * ciphersuite does this (and this is unlikely to change as activity has
nexpaq 0:6c56fb4bc5f0 5021 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
nexpaq 0:6c56fb4bc5f0 5022 */
nexpaq 0:6c56fb4bc5f0 5023 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
nexpaq 0:6c56fb4bc5f0 5024
nexpaq 0:6c56fb4bc5f0 5025 #if defined(MBEDTLS_SSL_RENEGOTIATION)
nexpaq 0:6c56fb4bc5f0 5026 ssl->verify_data_len = hash_len;
nexpaq 0:6c56fb4bc5f0 5027 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
nexpaq 0:6c56fb4bc5f0 5028 #endif
nexpaq 0:6c56fb4bc5f0 5029
nexpaq 0:6c56fb4bc5f0 5030 ssl->out_msglen = 4 + hash_len;
nexpaq 0:6c56fb4bc5f0 5031 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
nexpaq 0:6c56fb4bc5f0 5032 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
nexpaq 0:6c56fb4bc5f0 5033
nexpaq 0:6c56fb4bc5f0 5034 /*
nexpaq 0:6c56fb4bc5f0 5035 * In case of session resuming, invert the client and server
nexpaq 0:6c56fb4bc5f0 5036 * ChangeCipherSpec messages order.
nexpaq 0:6c56fb4bc5f0 5037 */
nexpaq 0:6c56fb4bc5f0 5038 if( ssl->handshake->resume != 0 )
nexpaq 0:6c56fb4bc5f0 5039 {
nexpaq 0:6c56fb4bc5f0 5040 #if defined(MBEDTLS_SSL_CLI_C)
nexpaq 0:6c56fb4bc5f0 5041 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
nexpaq 0:6c56fb4bc5f0 5042 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
nexpaq 0:6c56fb4bc5f0 5043 #endif
nexpaq 0:6c56fb4bc5f0 5044 #if defined(MBEDTLS_SSL_SRV_C)
nexpaq 0:6c56fb4bc5f0 5045 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
nexpaq 0:6c56fb4bc5f0 5046 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
nexpaq 0:6c56fb4bc5f0 5047 #endif
nexpaq 0:6c56fb4bc5f0 5048 }
nexpaq 0:6c56fb4bc5f0 5049 else
nexpaq 0:6c56fb4bc5f0 5050 ssl->state++;
nexpaq 0:6c56fb4bc5f0 5051
nexpaq 0:6c56fb4bc5f0 5052 /*
nexpaq 0:6c56fb4bc5f0 5053 * Switch to our negotiated transform and session parameters for outbound
nexpaq 0:6c56fb4bc5f0 5054 * data.
nexpaq 0:6c56fb4bc5f0 5055 */
nexpaq 0:6c56fb4bc5f0 5056 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
nexpaq 0:6c56fb4bc5f0 5057
nexpaq 0:6c56fb4bc5f0 5058 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 5059 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
nexpaq 0:6c56fb4bc5f0 5060 {
nexpaq 0:6c56fb4bc5f0 5061 unsigned char i;
nexpaq 0:6c56fb4bc5f0 5062
nexpaq 0:6c56fb4bc5f0 5063 /* Remember current epoch settings for resending */
nexpaq 0:6c56fb4bc5f0 5064 ssl->handshake->alt_transform_out = ssl->transform_out;
nexpaq 0:6c56fb4bc5f0 5065 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
nexpaq 0:6c56fb4bc5f0 5066
nexpaq 0:6c56fb4bc5f0 5067 /* Set sequence_number to zero */
nexpaq 0:6c56fb4bc5f0 5068 memset( ssl->out_ctr + 2, 0, 6 );
nexpaq 0:6c56fb4bc5f0 5069
nexpaq 0:6c56fb4bc5f0 5070 /* Increment epoch */
nexpaq 0:6c56fb4bc5f0 5071 for( i = 2; i > 0; i-- )
nexpaq 0:6c56fb4bc5f0 5072 if( ++ssl->out_ctr[i - 1] != 0 )
nexpaq 0:6c56fb4bc5f0 5073 break;
nexpaq 0:6c56fb4bc5f0 5074
nexpaq 0:6c56fb4bc5f0 5075 /* The loop goes to its end iff the counter is wrapping */
nexpaq 0:6c56fb4bc5f0 5076 if( i == 0 )
nexpaq 0:6c56fb4bc5f0 5077 {
nexpaq 0:6c56fb4bc5f0 5078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
nexpaq 0:6c56fb4bc5f0 5079 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
nexpaq 0:6c56fb4bc5f0 5080 }
nexpaq 0:6c56fb4bc5f0 5081 }
nexpaq 0:6c56fb4bc5f0 5082 else
nexpaq 0:6c56fb4bc5f0 5083 #endif /* MBEDTLS_SSL_PROTO_DTLS */
nexpaq 0:6c56fb4bc5f0 5084 memset( ssl->out_ctr, 0, 8 );
nexpaq 0:6c56fb4bc5f0 5085
nexpaq 0:6c56fb4bc5f0 5086 ssl->transform_out = ssl->transform_negotiate;
nexpaq 0:6c56fb4bc5f0 5087 ssl->session_out = ssl->session_negotiate;
nexpaq 0:6c56fb4bc5f0 5088
nexpaq 0:6c56fb4bc5f0 5089 #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
nexpaq 0:6c56fb4bc5f0 5090 if( mbedtls_ssl_hw_record_activate != NULL )
nexpaq 0:6c56fb4bc5f0 5091 {
nexpaq 0:6c56fb4bc5f0 5092 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 5093 {
nexpaq 0:6c56fb4bc5f0 5094 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
nexpaq 0:6c56fb4bc5f0 5095 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
nexpaq 0:6c56fb4bc5f0 5096 }
nexpaq 0:6c56fb4bc5f0 5097 }
nexpaq 0:6c56fb4bc5f0 5098 #endif
nexpaq 0:6c56fb4bc5f0 5099
nexpaq 0:6c56fb4bc5f0 5100 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 5101 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
nexpaq 0:6c56fb4bc5f0 5102 mbedtls_ssl_send_flight_completed( ssl );
nexpaq 0:6c56fb4bc5f0 5103 #endif
nexpaq 0:6c56fb4bc5f0 5104
nexpaq 0:6c56fb4bc5f0 5105 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 5106 {
nexpaq 0:6c56fb4bc5f0 5107 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
nexpaq 0:6c56fb4bc5f0 5108 return( ret );
nexpaq 0:6c56fb4bc5f0 5109 }
nexpaq 0:6c56fb4bc5f0 5110
nexpaq 0:6c56fb4bc5f0 5111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
nexpaq 0:6c56fb4bc5f0 5112
nexpaq 0:6c56fb4bc5f0 5113 return( 0 );
nexpaq 0:6c56fb4bc5f0 5114 }
nexpaq 0:6c56fb4bc5f0 5115
nexpaq 0:6c56fb4bc5f0 5116 #if defined(MBEDTLS_SSL_PROTO_SSL3)
nexpaq 0:6c56fb4bc5f0 5117 #define SSL_MAX_HASH_LEN 36
nexpaq 0:6c56fb4bc5f0 5118 #else
nexpaq 0:6c56fb4bc5f0 5119 #define SSL_MAX_HASH_LEN 12
nexpaq 0:6c56fb4bc5f0 5120 #endif
nexpaq 0:6c56fb4bc5f0 5121
nexpaq 0:6c56fb4bc5f0 5122 int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 5123 {
nexpaq 0:6c56fb4bc5f0 5124 int ret;
nexpaq 0:6c56fb4bc5f0 5125 unsigned int hash_len;
nexpaq 0:6c56fb4bc5f0 5126 unsigned char buf[SSL_MAX_HASH_LEN];
nexpaq 0:6c56fb4bc5f0 5127
nexpaq 0:6c56fb4bc5f0 5128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
nexpaq 0:6c56fb4bc5f0 5129
nexpaq 0:6c56fb4bc5f0 5130 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
nexpaq 0:6c56fb4bc5f0 5131
nexpaq 0:6c56fb4bc5f0 5132 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 5133 {
nexpaq 0:6c56fb4bc5f0 5134 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
nexpaq 0:6c56fb4bc5f0 5135 return( ret );
nexpaq 0:6c56fb4bc5f0 5136 }
nexpaq 0:6c56fb4bc5f0 5137
nexpaq 0:6c56fb4bc5f0 5138 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
nexpaq 0:6c56fb4bc5f0 5139 {
nexpaq 0:6c56fb4bc5f0 5140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
nexpaq 0:6c56fb4bc5f0 5141 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
nexpaq 0:6c56fb4bc5f0 5142 }
nexpaq 0:6c56fb4bc5f0 5143
nexpaq 0:6c56fb4bc5f0 5144 /* There is currently no ciphersuite using another length with TLS 1.2 */
nexpaq 0:6c56fb4bc5f0 5145 #if defined(MBEDTLS_SSL_PROTO_SSL3)
nexpaq 0:6c56fb4bc5f0 5146 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
nexpaq 0:6c56fb4bc5f0 5147 hash_len = 36;
nexpaq 0:6c56fb4bc5f0 5148 else
nexpaq 0:6c56fb4bc5f0 5149 #endif
nexpaq 0:6c56fb4bc5f0 5150 hash_len = 12;
nexpaq 0:6c56fb4bc5f0 5151
nexpaq 0:6c56fb4bc5f0 5152 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
nexpaq 0:6c56fb4bc5f0 5153 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
nexpaq 0:6c56fb4bc5f0 5154 {
nexpaq 0:6c56fb4bc5f0 5155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
nexpaq 0:6c56fb4bc5f0 5156 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
nexpaq 0:6c56fb4bc5f0 5157 }
nexpaq 0:6c56fb4bc5f0 5158
nexpaq 0:6c56fb4bc5f0 5159 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
nexpaq 0:6c56fb4bc5f0 5160 buf, hash_len ) != 0 )
nexpaq 0:6c56fb4bc5f0 5161 {
nexpaq 0:6c56fb4bc5f0 5162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
nexpaq 0:6c56fb4bc5f0 5163 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
nexpaq 0:6c56fb4bc5f0 5164 }
nexpaq 0:6c56fb4bc5f0 5165
nexpaq 0:6c56fb4bc5f0 5166 #if defined(MBEDTLS_SSL_RENEGOTIATION)
nexpaq 0:6c56fb4bc5f0 5167 ssl->verify_data_len = hash_len;
nexpaq 0:6c56fb4bc5f0 5168 memcpy( ssl->peer_verify_data, buf, hash_len );
nexpaq 0:6c56fb4bc5f0 5169 #endif
nexpaq 0:6c56fb4bc5f0 5170
nexpaq 0:6c56fb4bc5f0 5171 if( ssl->handshake->resume != 0 )
nexpaq 0:6c56fb4bc5f0 5172 {
nexpaq 0:6c56fb4bc5f0 5173 #if defined(MBEDTLS_SSL_CLI_C)
nexpaq 0:6c56fb4bc5f0 5174 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
nexpaq 0:6c56fb4bc5f0 5175 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
nexpaq 0:6c56fb4bc5f0 5176 #endif
nexpaq 0:6c56fb4bc5f0 5177 #if defined(MBEDTLS_SSL_SRV_C)
nexpaq 0:6c56fb4bc5f0 5178 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
nexpaq 0:6c56fb4bc5f0 5179 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
nexpaq 0:6c56fb4bc5f0 5180 #endif
nexpaq 0:6c56fb4bc5f0 5181 }
nexpaq 0:6c56fb4bc5f0 5182 else
nexpaq 0:6c56fb4bc5f0 5183 ssl->state++;
nexpaq 0:6c56fb4bc5f0 5184
nexpaq 0:6c56fb4bc5f0 5185 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 5186 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
nexpaq 0:6c56fb4bc5f0 5187 mbedtls_ssl_recv_flight_completed( ssl );
nexpaq 0:6c56fb4bc5f0 5188 #endif
nexpaq 0:6c56fb4bc5f0 5189
nexpaq 0:6c56fb4bc5f0 5190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
nexpaq 0:6c56fb4bc5f0 5191
nexpaq 0:6c56fb4bc5f0 5192 return( 0 );
nexpaq 0:6c56fb4bc5f0 5193 }
nexpaq 0:6c56fb4bc5f0 5194
nexpaq 0:6c56fb4bc5f0 5195 static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
nexpaq 0:6c56fb4bc5f0 5196 {
nexpaq 0:6c56fb4bc5f0 5197 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
nexpaq 0:6c56fb4bc5f0 5198
nexpaq 0:6c56fb4bc5f0 5199 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
nexpaq 0:6c56fb4bc5f0 5200 defined(MBEDTLS_SSL_PROTO_TLS1_1)
nexpaq 0:6c56fb4bc5f0 5201 mbedtls_md5_init( &handshake->fin_md5 );
nexpaq 0:6c56fb4bc5f0 5202 mbedtls_sha1_init( &handshake->fin_sha1 );
nexpaq 0:6c56fb4bc5f0 5203 mbedtls_md5_starts( &handshake->fin_md5 );
nexpaq 0:6c56fb4bc5f0 5204 mbedtls_sha1_starts( &handshake->fin_sha1 );
nexpaq 0:6c56fb4bc5f0 5205 #endif
nexpaq 0:6c56fb4bc5f0 5206 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
nexpaq 0:6c56fb4bc5f0 5207 #if defined(MBEDTLS_SHA256_C)
nexpaq 0:6c56fb4bc5f0 5208 mbedtls_sha256_init( &handshake->fin_sha256 );
nexpaq 0:6c56fb4bc5f0 5209 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
nexpaq 0:6c56fb4bc5f0 5210 #endif
nexpaq 0:6c56fb4bc5f0 5211 #if defined(MBEDTLS_SHA512_C)
nexpaq 0:6c56fb4bc5f0 5212 mbedtls_sha512_init( &handshake->fin_sha512 );
nexpaq 0:6c56fb4bc5f0 5213 mbedtls_sha512_starts( &handshake->fin_sha512, 1 );
nexpaq 0:6c56fb4bc5f0 5214 #endif
nexpaq 0:6c56fb4bc5f0 5215 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
nexpaq 0:6c56fb4bc5f0 5216
nexpaq 0:6c56fb4bc5f0 5217 handshake->update_checksum = ssl_update_checksum_start;
nexpaq 0:6c56fb4bc5f0 5218 handshake->sig_alg = MBEDTLS_SSL_HASH_SHA1;
nexpaq 0:6c56fb4bc5f0 5219
nexpaq 0:6c56fb4bc5f0 5220 #if defined(MBEDTLS_DHM_C)
nexpaq 0:6c56fb4bc5f0 5221 mbedtls_dhm_init( &handshake->dhm_ctx );
nexpaq 0:6c56fb4bc5f0 5222 #endif
nexpaq 0:6c56fb4bc5f0 5223 #if defined(MBEDTLS_ECDH_C)
nexpaq 0:6c56fb4bc5f0 5224 mbedtls_ecdh_init( &handshake->ecdh_ctx );
nexpaq 0:6c56fb4bc5f0 5225 #endif
nexpaq 0:6c56fb4bc5f0 5226 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
nexpaq 0:6c56fb4bc5f0 5227 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
nexpaq 0:6c56fb4bc5f0 5228 #if defined(MBEDTLS_SSL_CLI_C)
nexpaq 0:6c56fb4bc5f0 5229 handshake->ecjpake_cache = NULL;
nexpaq 0:6c56fb4bc5f0 5230 handshake->ecjpake_cache_len = 0;
nexpaq 0:6c56fb4bc5f0 5231 #endif
nexpaq 0:6c56fb4bc5f0 5232 #endif
nexpaq 0:6c56fb4bc5f0 5233
nexpaq 0:6c56fb4bc5f0 5234 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
nexpaq 0:6c56fb4bc5f0 5235 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
nexpaq 0:6c56fb4bc5f0 5236 #endif
nexpaq 0:6c56fb4bc5f0 5237 }
nexpaq 0:6c56fb4bc5f0 5238
nexpaq 0:6c56fb4bc5f0 5239 static void ssl_transform_init( mbedtls_ssl_transform *transform )
nexpaq 0:6c56fb4bc5f0 5240 {
nexpaq 0:6c56fb4bc5f0 5241 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
nexpaq 0:6c56fb4bc5f0 5242
nexpaq 0:6c56fb4bc5f0 5243 mbedtls_cipher_init( &transform->cipher_ctx_enc );
nexpaq 0:6c56fb4bc5f0 5244 mbedtls_cipher_init( &transform->cipher_ctx_dec );
nexpaq 0:6c56fb4bc5f0 5245
nexpaq 0:6c56fb4bc5f0 5246 mbedtls_md_init( &transform->md_ctx_enc );
nexpaq 0:6c56fb4bc5f0 5247 mbedtls_md_init( &transform->md_ctx_dec );
nexpaq 0:6c56fb4bc5f0 5248 }
nexpaq 0:6c56fb4bc5f0 5249
nexpaq 0:6c56fb4bc5f0 5250 void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
nexpaq 0:6c56fb4bc5f0 5251 {
nexpaq 0:6c56fb4bc5f0 5252 memset( session, 0, sizeof(mbedtls_ssl_session) );
nexpaq 0:6c56fb4bc5f0 5253 }
nexpaq 0:6c56fb4bc5f0 5254
nexpaq 0:6c56fb4bc5f0 5255 static int ssl_handshake_init( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 5256 {
nexpaq 0:6c56fb4bc5f0 5257 /* Clear old handshake information if present */
nexpaq 0:6c56fb4bc5f0 5258 if( ssl->transform_negotiate )
nexpaq 0:6c56fb4bc5f0 5259 mbedtls_ssl_transform_free( ssl->transform_negotiate );
nexpaq 0:6c56fb4bc5f0 5260 if( ssl->session_negotiate )
nexpaq 0:6c56fb4bc5f0 5261 mbedtls_ssl_session_free( ssl->session_negotiate );
nexpaq 0:6c56fb4bc5f0 5262 if( ssl->handshake )
nexpaq 0:6c56fb4bc5f0 5263 mbedtls_ssl_handshake_free( ssl->handshake );
nexpaq 0:6c56fb4bc5f0 5264
nexpaq 0:6c56fb4bc5f0 5265 /*
nexpaq 0:6c56fb4bc5f0 5266 * Either the pointers are now NULL or cleared properly and can be freed.
nexpaq 0:6c56fb4bc5f0 5267 * Now allocate missing structures.
nexpaq 0:6c56fb4bc5f0 5268 */
nexpaq 0:6c56fb4bc5f0 5269 if( ssl->transform_negotiate == NULL )
nexpaq 0:6c56fb4bc5f0 5270 {
nexpaq 0:6c56fb4bc5f0 5271 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
nexpaq 0:6c56fb4bc5f0 5272 }
nexpaq 0:6c56fb4bc5f0 5273
nexpaq 0:6c56fb4bc5f0 5274 if( ssl->session_negotiate == NULL )
nexpaq 0:6c56fb4bc5f0 5275 {
nexpaq 0:6c56fb4bc5f0 5276 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
nexpaq 0:6c56fb4bc5f0 5277 }
nexpaq 0:6c56fb4bc5f0 5278
nexpaq 0:6c56fb4bc5f0 5279 if( ssl->handshake == NULL )
nexpaq 0:6c56fb4bc5f0 5280 {
nexpaq 0:6c56fb4bc5f0 5281 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
nexpaq 0:6c56fb4bc5f0 5282 }
nexpaq 0:6c56fb4bc5f0 5283
nexpaq 0:6c56fb4bc5f0 5284 /* All pointers should exist and can be directly freed without issue */
nexpaq 0:6c56fb4bc5f0 5285 if( ssl->handshake == NULL ||
nexpaq 0:6c56fb4bc5f0 5286 ssl->transform_negotiate == NULL ||
nexpaq 0:6c56fb4bc5f0 5287 ssl->session_negotiate == NULL )
nexpaq 0:6c56fb4bc5f0 5288 {
nexpaq 0:6c56fb4bc5f0 5289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
nexpaq 0:6c56fb4bc5f0 5290
nexpaq 0:6c56fb4bc5f0 5291 mbedtls_free( ssl->handshake );
nexpaq 0:6c56fb4bc5f0 5292 mbedtls_free( ssl->transform_negotiate );
nexpaq 0:6c56fb4bc5f0 5293 mbedtls_free( ssl->session_negotiate );
nexpaq 0:6c56fb4bc5f0 5294
nexpaq 0:6c56fb4bc5f0 5295 ssl->handshake = NULL;
nexpaq 0:6c56fb4bc5f0 5296 ssl->transform_negotiate = NULL;
nexpaq 0:6c56fb4bc5f0 5297 ssl->session_negotiate = NULL;
nexpaq 0:6c56fb4bc5f0 5298
nexpaq 0:6c56fb4bc5f0 5299 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
nexpaq 0:6c56fb4bc5f0 5300 }
nexpaq 0:6c56fb4bc5f0 5301
nexpaq 0:6c56fb4bc5f0 5302 /* Initialize structures */
nexpaq 0:6c56fb4bc5f0 5303 mbedtls_ssl_session_init( ssl->session_negotiate );
nexpaq 0:6c56fb4bc5f0 5304 ssl_transform_init( ssl->transform_negotiate );
nexpaq 0:6c56fb4bc5f0 5305 ssl_handshake_params_init( ssl->handshake );
nexpaq 0:6c56fb4bc5f0 5306
nexpaq 0:6c56fb4bc5f0 5307 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 5308 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
nexpaq 0:6c56fb4bc5f0 5309 {
nexpaq 0:6c56fb4bc5f0 5310 ssl->handshake->alt_transform_out = ssl->transform_out;
nexpaq 0:6c56fb4bc5f0 5311
nexpaq 0:6c56fb4bc5f0 5312 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
nexpaq 0:6c56fb4bc5f0 5313 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
nexpaq 0:6c56fb4bc5f0 5314 else
nexpaq 0:6c56fb4bc5f0 5315 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
nexpaq 0:6c56fb4bc5f0 5316
nexpaq 0:6c56fb4bc5f0 5317 ssl_set_timer( ssl, 0 );
nexpaq 0:6c56fb4bc5f0 5318 }
nexpaq 0:6c56fb4bc5f0 5319 #endif
nexpaq 0:6c56fb4bc5f0 5320
nexpaq 0:6c56fb4bc5f0 5321 return( 0 );
nexpaq 0:6c56fb4bc5f0 5322 }
nexpaq 0:6c56fb4bc5f0 5323
nexpaq 0:6c56fb4bc5f0 5324 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
nexpaq 0:6c56fb4bc5f0 5325 /* Dummy cookie callbacks for defaults */
nexpaq 0:6c56fb4bc5f0 5326 static int ssl_cookie_write_dummy( void *ctx,
nexpaq 0:6c56fb4bc5f0 5327 unsigned char **p, unsigned char *end,
nexpaq 0:6c56fb4bc5f0 5328 const unsigned char *cli_id, size_t cli_id_len )
nexpaq 0:6c56fb4bc5f0 5329 {
nexpaq 0:6c56fb4bc5f0 5330 ((void) ctx);
nexpaq 0:6c56fb4bc5f0 5331 ((void) p);
nexpaq 0:6c56fb4bc5f0 5332 ((void) end);
nexpaq 0:6c56fb4bc5f0 5333 ((void) cli_id);
nexpaq 0:6c56fb4bc5f0 5334 ((void) cli_id_len);
nexpaq 0:6c56fb4bc5f0 5335
nexpaq 0:6c56fb4bc5f0 5336 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
nexpaq 0:6c56fb4bc5f0 5337 }
nexpaq 0:6c56fb4bc5f0 5338
nexpaq 0:6c56fb4bc5f0 5339 static int ssl_cookie_check_dummy( void *ctx,
nexpaq 0:6c56fb4bc5f0 5340 const unsigned char *cookie, size_t cookie_len,
nexpaq 0:6c56fb4bc5f0 5341 const unsigned char *cli_id, size_t cli_id_len )
nexpaq 0:6c56fb4bc5f0 5342 {
nexpaq 0:6c56fb4bc5f0 5343 ((void) ctx);
nexpaq 0:6c56fb4bc5f0 5344 ((void) cookie);
nexpaq 0:6c56fb4bc5f0 5345 ((void) cookie_len);
nexpaq 0:6c56fb4bc5f0 5346 ((void) cli_id);
nexpaq 0:6c56fb4bc5f0 5347 ((void) cli_id_len);
nexpaq 0:6c56fb4bc5f0 5348
nexpaq 0:6c56fb4bc5f0 5349 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
nexpaq 0:6c56fb4bc5f0 5350 }
nexpaq 0:6c56fb4bc5f0 5351 #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
nexpaq 0:6c56fb4bc5f0 5352
nexpaq 0:6c56fb4bc5f0 5353 /*
nexpaq 0:6c56fb4bc5f0 5354 * Initialize an SSL context
nexpaq 0:6c56fb4bc5f0 5355 */
nexpaq 0:6c56fb4bc5f0 5356 void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 5357 {
nexpaq 0:6c56fb4bc5f0 5358 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
nexpaq 0:6c56fb4bc5f0 5359 }
nexpaq 0:6c56fb4bc5f0 5360
nexpaq 0:6c56fb4bc5f0 5361 /*
nexpaq 0:6c56fb4bc5f0 5362 * Setup an SSL context
nexpaq 0:6c56fb4bc5f0 5363 */
nexpaq 0:6c56fb4bc5f0 5364 int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
nexpaq 0:6c56fb4bc5f0 5365 const mbedtls_ssl_config *conf )
nexpaq 0:6c56fb4bc5f0 5366 {
nexpaq 0:6c56fb4bc5f0 5367 int ret;
nexpaq 0:6c56fb4bc5f0 5368 const size_t len = MBEDTLS_SSL_BUFFER_LEN;
nexpaq 0:6c56fb4bc5f0 5369
nexpaq 0:6c56fb4bc5f0 5370 ssl->conf = conf;
nexpaq 0:6c56fb4bc5f0 5371
nexpaq 0:6c56fb4bc5f0 5372 /*
nexpaq 0:6c56fb4bc5f0 5373 * Prepare base structures
nexpaq 0:6c56fb4bc5f0 5374 */
nexpaq 0:6c56fb4bc5f0 5375 if( ( ssl-> in_buf = mbedtls_calloc( 1, len ) ) == NULL ||
nexpaq 0:6c56fb4bc5f0 5376 ( ssl->out_buf = mbedtls_calloc( 1, len ) ) == NULL )
nexpaq 0:6c56fb4bc5f0 5377 {
nexpaq 0:6c56fb4bc5f0 5378 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", len ) );
nexpaq 0:6c56fb4bc5f0 5379 mbedtls_free( ssl->in_buf );
nexpaq 0:6c56fb4bc5f0 5380 ssl->in_buf = NULL;
nexpaq 0:6c56fb4bc5f0 5381 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
nexpaq 0:6c56fb4bc5f0 5382 }
nexpaq 0:6c56fb4bc5f0 5383
nexpaq 0:6c56fb4bc5f0 5384 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 5385 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
nexpaq 0:6c56fb4bc5f0 5386 {
nexpaq 0:6c56fb4bc5f0 5387 ssl->out_hdr = ssl->out_buf;
nexpaq 0:6c56fb4bc5f0 5388 ssl->out_ctr = ssl->out_buf + 3;
nexpaq 0:6c56fb4bc5f0 5389 ssl->out_len = ssl->out_buf + 11;
nexpaq 0:6c56fb4bc5f0 5390 ssl->out_iv = ssl->out_buf + 13;
nexpaq 0:6c56fb4bc5f0 5391 ssl->out_msg = ssl->out_buf + 13;
nexpaq 0:6c56fb4bc5f0 5392
nexpaq 0:6c56fb4bc5f0 5393 ssl->in_hdr = ssl->in_buf;
nexpaq 0:6c56fb4bc5f0 5394 ssl->in_ctr = ssl->in_buf + 3;
nexpaq 0:6c56fb4bc5f0 5395 ssl->in_len = ssl->in_buf + 11;
nexpaq 0:6c56fb4bc5f0 5396 ssl->in_iv = ssl->in_buf + 13;
nexpaq 0:6c56fb4bc5f0 5397 ssl->in_msg = ssl->in_buf + 13;
nexpaq 0:6c56fb4bc5f0 5398 }
nexpaq 0:6c56fb4bc5f0 5399 else
nexpaq 0:6c56fb4bc5f0 5400 #endif
nexpaq 0:6c56fb4bc5f0 5401 {
nexpaq 0:6c56fb4bc5f0 5402 ssl->out_ctr = ssl->out_buf;
nexpaq 0:6c56fb4bc5f0 5403 ssl->out_hdr = ssl->out_buf + 8;
nexpaq 0:6c56fb4bc5f0 5404 ssl->out_len = ssl->out_buf + 11;
nexpaq 0:6c56fb4bc5f0 5405 ssl->out_iv = ssl->out_buf + 13;
nexpaq 0:6c56fb4bc5f0 5406 ssl->out_msg = ssl->out_buf + 13;
nexpaq 0:6c56fb4bc5f0 5407
nexpaq 0:6c56fb4bc5f0 5408 ssl->in_ctr = ssl->in_buf;
nexpaq 0:6c56fb4bc5f0 5409 ssl->in_hdr = ssl->in_buf + 8;
nexpaq 0:6c56fb4bc5f0 5410 ssl->in_len = ssl->in_buf + 11;
nexpaq 0:6c56fb4bc5f0 5411 ssl->in_iv = ssl->in_buf + 13;
nexpaq 0:6c56fb4bc5f0 5412 ssl->in_msg = ssl->in_buf + 13;
nexpaq 0:6c56fb4bc5f0 5413 }
nexpaq 0:6c56fb4bc5f0 5414
nexpaq 0:6c56fb4bc5f0 5415 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 5416 return( ret );
nexpaq 0:6c56fb4bc5f0 5417
nexpaq 0:6c56fb4bc5f0 5418 return( 0 );
nexpaq 0:6c56fb4bc5f0 5419 }
nexpaq 0:6c56fb4bc5f0 5420
nexpaq 0:6c56fb4bc5f0 5421 /*
nexpaq 0:6c56fb4bc5f0 5422 * Reset an initialized and used SSL context for re-use while retaining
nexpaq 0:6c56fb4bc5f0 5423 * all application-set variables, function pointers and data.
nexpaq 0:6c56fb4bc5f0 5424 *
nexpaq 0:6c56fb4bc5f0 5425 * If partial is non-zero, keep data in the input buffer and client ID.
nexpaq 0:6c56fb4bc5f0 5426 * (Use when a DTLS client reconnects from the same port.)
nexpaq 0:6c56fb4bc5f0 5427 */
nexpaq 0:6c56fb4bc5f0 5428 static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
nexpaq 0:6c56fb4bc5f0 5429 {
nexpaq 0:6c56fb4bc5f0 5430 int ret;
nexpaq 0:6c56fb4bc5f0 5431
nexpaq 0:6c56fb4bc5f0 5432 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
nexpaq 0:6c56fb4bc5f0 5433
nexpaq 0:6c56fb4bc5f0 5434 /* Cancel any possibly running timer */
nexpaq 0:6c56fb4bc5f0 5435 ssl_set_timer( ssl, 0 );
nexpaq 0:6c56fb4bc5f0 5436
nexpaq 0:6c56fb4bc5f0 5437 #if defined(MBEDTLS_SSL_RENEGOTIATION)
nexpaq 0:6c56fb4bc5f0 5438 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
nexpaq 0:6c56fb4bc5f0 5439 ssl->renego_records_seen = 0;
nexpaq 0:6c56fb4bc5f0 5440
nexpaq 0:6c56fb4bc5f0 5441 ssl->verify_data_len = 0;
nexpaq 0:6c56fb4bc5f0 5442 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
nexpaq 0:6c56fb4bc5f0 5443 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
nexpaq 0:6c56fb4bc5f0 5444 #endif
nexpaq 0:6c56fb4bc5f0 5445 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
nexpaq 0:6c56fb4bc5f0 5446
nexpaq 0:6c56fb4bc5f0 5447 ssl->in_offt = NULL;
nexpaq 0:6c56fb4bc5f0 5448
nexpaq 0:6c56fb4bc5f0 5449 ssl->in_msg = ssl->in_buf + 13;
nexpaq 0:6c56fb4bc5f0 5450 ssl->in_msgtype = 0;
nexpaq 0:6c56fb4bc5f0 5451 ssl->in_msglen = 0;
nexpaq 0:6c56fb4bc5f0 5452 if( partial == 0 )
nexpaq 0:6c56fb4bc5f0 5453 ssl->in_left = 0;
nexpaq 0:6c56fb4bc5f0 5454 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 5455 ssl->next_record_offset = 0;
nexpaq 0:6c56fb4bc5f0 5456 ssl->in_epoch = 0;
nexpaq 0:6c56fb4bc5f0 5457 #endif
nexpaq 0:6c56fb4bc5f0 5458 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
nexpaq 0:6c56fb4bc5f0 5459 ssl_dtls_replay_reset( ssl );
nexpaq 0:6c56fb4bc5f0 5460 #endif
nexpaq 0:6c56fb4bc5f0 5461
nexpaq 0:6c56fb4bc5f0 5462 ssl->in_hslen = 0;
nexpaq 0:6c56fb4bc5f0 5463 ssl->nb_zero = 0;
nexpaq 0:6c56fb4bc5f0 5464 ssl->record_read = 0;
nexpaq 0:6c56fb4bc5f0 5465
nexpaq 0:6c56fb4bc5f0 5466 ssl->out_msg = ssl->out_buf + 13;
nexpaq 0:6c56fb4bc5f0 5467 ssl->out_msgtype = 0;
nexpaq 0:6c56fb4bc5f0 5468 ssl->out_msglen = 0;
nexpaq 0:6c56fb4bc5f0 5469 ssl->out_left = 0;
nexpaq 0:6c56fb4bc5f0 5470 #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
nexpaq 0:6c56fb4bc5f0 5471 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
nexpaq 0:6c56fb4bc5f0 5472 ssl->split_done = 0;
nexpaq 0:6c56fb4bc5f0 5473 #endif
nexpaq 0:6c56fb4bc5f0 5474
nexpaq 0:6c56fb4bc5f0 5475 ssl->transform_in = NULL;
nexpaq 0:6c56fb4bc5f0 5476 ssl->transform_out = NULL;
nexpaq 0:6c56fb4bc5f0 5477
nexpaq 0:6c56fb4bc5f0 5478 memset( ssl->out_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
nexpaq 0:6c56fb4bc5f0 5479 if( partial == 0 )
nexpaq 0:6c56fb4bc5f0 5480 memset( ssl->in_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
nexpaq 0:6c56fb4bc5f0 5481
nexpaq 0:6c56fb4bc5f0 5482 #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
nexpaq 0:6c56fb4bc5f0 5483 if( mbedtls_ssl_hw_record_reset != NULL )
nexpaq 0:6c56fb4bc5f0 5484 {
nexpaq 0:6c56fb4bc5f0 5485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
nexpaq 0:6c56fb4bc5f0 5486 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 5487 {
nexpaq 0:6c56fb4bc5f0 5488 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
nexpaq 0:6c56fb4bc5f0 5489 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
nexpaq 0:6c56fb4bc5f0 5490 }
nexpaq 0:6c56fb4bc5f0 5491 }
nexpaq 0:6c56fb4bc5f0 5492 #endif
nexpaq 0:6c56fb4bc5f0 5493
nexpaq 0:6c56fb4bc5f0 5494 if( ssl->transform )
nexpaq 0:6c56fb4bc5f0 5495 {
nexpaq 0:6c56fb4bc5f0 5496 mbedtls_ssl_transform_free( ssl->transform );
nexpaq 0:6c56fb4bc5f0 5497 mbedtls_free( ssl->transform );
nexpaq 0:6c56fb4bc5f0 5498 ssl->transform = NULL;
nexpaq 0:6c56fb4bc5f0 5499 }
nexpaq 0:6c56fb4bc5f0 5500
nexpaq 0:6c56fb4bc5f0 5501 if( ssl->session )
nexpaq 0:6c56fb4bc5f0 5502 {
nexpaq 0:6c56fb4bc5f0 5503 mbedtls_ssl_session_free( ssl->session );
nexpaq 0:6c56fb4bc5f0 5504 mbedtls_free( ssl->session );
nexpaq 0:6c56fb4bc5f0 5505 ssl->session = NULL;
nexpaq 0:6c56fb4bc5f0 5506 }
nexpaq 0:6c56fb4bc5f0 5507
nexpaq 0:6c56fb4bc5f0 5508 #if defined(MBEDTLS_SSL_ALPN)
nexpaq 0:6c56fb4bc5f0 5509 ssl->alpn_chosen = NULL;
nexpaq 0:6c56fb4bc5f0 5510 #endif
nexpaq 0:6c56fb4bc5f0 5511
nexpaq 0:6c56fb4bc5f0 5512 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
nexpaq 0:6c56fb4bc5f0 5513 if( partial == 0 )
nexpaq 0:6c56fb4bc5f0 5514 {
nexpaq 0:6c56fb4bc5f0 5515 mbedtls_free( ssl->cli_id );
nexpaq 0:6c56fb4bc5f0 5516 ssl->cli_id = NULL;
nexpaq 0:6c56fb4bc5f0 5517 ssl->cli_id_len = 0;
nexpaq 0:6c56fb4bc5f0 5518 }
nexpaq 0:6c56fb4bc5f0 5519 #endif
nexpaq 0:6c56fb4bc5f0 5520
nexpaq 0:6c56fb4bc5f0 5521 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 5522 return( ret );
nexpaq 0:6c56fb4bc5f0 5523
nexpaq 0:6c56fb4bc5f0 5524 return( 0 );
nexpaq 0:6c56fb4bc5f0 5525 }
nexpaq 0:6c56fb4bc5f0 5526
nexpaq 0:6c56fb4bc5f0 5527 /*
nexpaq 0:6c56fb4bc5f0 5528 * Reset an initialized and used SSL context for re-use while retaining
nexpaq 0:6c56fb4bc5f0 5529 * all application-set variables, function pointers and data.
nexpaq 0:6c56fb4bc5f0 5530 */
nexpaq 0:6c56fb4bc5f0 5531 int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 5532 {
nexpaq 0:6c56fb4bc5f0 5533 return( ssl_session_reset_int( ssl, 0 ) );
nexpaq 0:6c56fb4bc5f0 5534 }
nexpaq 0:6c56fb4bc5f0 5535
nexpaq 0:6c56fb4bc5f0 5536 /*
nexpaq 0:6c56fb4bc5f0 5537 * SSL set accessors
nexpaq 0:6c56fb4bc5f0 5538 */
nexpaq 0:6c56fb4bc5f0 5539 void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
nexpaq 0:6c56fb4bc5f0 5540 {
nexpaq 0:6c56fb4bc5f0 5541 conf->endpoint = endpoint;
nexpaq 0:6c56fb4bc5f0 5542 }
nexpaq 0:6c56fb4bc5f0 5543
nexpaq 0:6c56fb4bc5f0 5544 void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
nexpaq 0:6c56fb4bc5f0 5545 {
nexpaq 0:6c56fb4bc5f0 5546 conf->transport = transport;
nexpaq 0:6c56fb4bc5f0 5547 }
nexpaq 0:6c56fb4bc5f0 5548
nexpaq 0:6c56fb4bc5f0 5549 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
nexpaq 0:6c56fb4bc5f0 5550 void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
nexpaq 0:6c56fb4bc5f0 5551 {
nexpaq 0:6c56fb4bc5f0 5552 conf->anti_replay = mode;
nexpaq 0:6c56fb4bc5f0 5553 }
nexpaq 0:6c56fb4bc5f0 5554 #endif
nexpaq 0:6c56fb4bc5f0 5555
nexpaq 0:6c56fb4bc5f0 5556 #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
nexpaq 0:6c56fb4bc5f0 5557 void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
nexpaq 0:6c56fb4bc5f0 5558 {
nexpaq 0:6c56fb4bc5f0 5559 conf->badmac_limit = limit;
nexpaq 0:6c56fb4bc5f0 5560 }
nexpaq 0:6c56fb4bc5f0 5561 #endif
nexpaq 0:6c56fb4bc5f0 5562
nexpaq 0:6c56fb4bc5f0 5563 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 5564 void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max )
nexpaq 0:6c56fb4bc5f0 5565 {
nexpaq 0:6c56fb4bc5f0 5566 conf->hs_timeout_min = min;
nexpaq 0:6c56fb4bc5f0 5567 conf->hs_timeout_max = max;
nexpaq 0:6c56fb4bc5f0 5568 }
nexpaq 0:6c56fb4bc5f0 5569 #endif
nexpaq 0:6c56fb4bc5f0 5570
nexpaq 0:6c56fb4bc5f0 5571 void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
nexpaq 0:6c56fb4bc5f0 5572 {
nexpaq 0:6c56fb4bc5f0 5573 conf->authmode = authmode;
nexpaq 0:6c56fb4bc5f0 5574 }
nexpaq 0:6c56fb4bc5f0 5575
nexpaq 0:6c56fb4bc5f0 5576 #if defined(MBEDTLS_X509_CRT_PARSE_C)
nexpaq 0:6c56fb4bc5f0 5577 void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
nexpaq 0:6c56fb4bc5f0 5578 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
nexpaq 0:6c56fb4bc5f0 5579 void *p_vrfy )
nexpaq 0:6c56fb4bc5f0 5580 {
nexpaq 0:6c56fb4bc5f0 5581 conf->f_vrfy = f_vrfy;
nexpaq 0:6c56fb4bc5f0 5582 conf->p_vrfy = p_vrfy;
nexpaq 0:6c56fb4bc5f0 5583 }
nexpaq 0:6c56fb4bc5f0 5584 #endif /* MBEDTLS_X509_CRT_PARSE_C */
nexpaq 0:6c56fb4bc5f0 5585
nexpaq 0:6c56fb4bc5f0 5586 void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
nexpaq 0:6c56fb4bc5f0 5587 int (*f_rng)(void *, unsigned char *, size_t),
nexpaq 0:6c56fb4bc5f0 5588 void *p_rng )
nexpaq 0:6c56fb4bc5f0 5589 {
nexpaq 0:6c56fb4bc5f0 5590 conf->f_rng = f_rng;
nexpaq 0:6c56fb4bc5f0 5591 conf->p_rng = p_rng;
nexpaq 0:6c56fb4bc5f0 5592 }
nexpaq 0:6c56fb4bc5f0 5593
nexpaq 0:6c56fb4bc5f0 5594 void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
nexpaq 0:6c56fb4bc5f0 5595 void (*f_dbg)(void *, int, const char *, int, const char *),
nexpaq 0:6c56fb4bc5f0 5596 void *p_dbg )
nexpaq 0:6c56fb4bc5f0 5597 {
nexpaq 0:6c56fb4bc5f0 5598 conf->f_dbg = f_dbg;
nexpaq 0:6c56fb4bc5f0 5599 conf->p_dbg = p_dbg;
nexpaq 0:6c56fb4bc5f0 5600 }
nexpaq 0:6c56fb4bc5f0 5601
nexpaq 0:6c56fb4bc5f0 5602 void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
nexpaq 0:6c56fb4bc5f0 5603 void *p_bio,
nexpaq 0:6c56fb4bc5f0 5604 mbedtls_ssl_send_t *f_send,
nexpaq 0:6c56fb4bc5f0 5605 mbedtls_ssl_recv_t *f_recv,
nexpaq 0:6c56fb4bc5f0 5606 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
nexpaq 0:6c56fb4bc5f0 5607 {
nexpaq 0:6c56fb4bc5f0 5608 ssl->p_bio = p_bio;
nexpaq 0:6c56fb4bc5f0 5609 ssl->f_send = f_send;
nexpaq 0:6c56fb4bc5f0 5610 ssl->f_recv = f_recv;
nexpaq 0:6c56fb4bc5f0 5611 ssl->f_recv_timeout = f_recv_timeout;
nexpaq 0:6c56fb4bc5f0 5612 }
nexpaq 0:6c56fb4bc5f0 5613
nexpaq 0:6c56fb4bc5f0 5614 void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
nexpaq 0:6c56fb4bc5f0 5615 {
nexpaq 0:6c56fb4bc5f0 5616 conf->read_timeout = timeout;
nexpaq 0:6c56fb4bc5f0 5617 }
nexpaq 0:6c56fb4bc5f0 5618
nexpaq 0:6c56fb4bc5f0 5619 void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
nexpaq 0:6c56fb4bc5f0 5620 void *p_timer,
nexpaq 0:6c56fb4bc5f0 5621 mbedtls_ssl_set_timer_t *f_set_timer,
nexpaq 0:6c56fb4bc5f0 5622 mbedtls_ssl_get_timer_t *f_get_timer )
nexpaq 0:6c56fb4bc5f0 5623 {
nexpaq 0:6c56fb4bc5f0 5624 ssl->p_timer = p_timer;
nexpaq 0:6c56fb4bc5f0 5625 ssl->f_set_timer = f_set_timer;
nexpaq 0:6c56fb4bc5f0 5626 ssl->f_get_timer = f_get_timer;
nexpaq 0:6c56fb4bc5f0 5627
nexpaq 0:6c56fb4bc5f0 5628 /* Make sure we start with no timer running */
nexpaq 0:6c56fb4bc5f0 5629 ssl_set_timer( ssl, 0 );
nexpaq 0:6c56fb4bc5f0 5630 }
nexpaq 0:6c56fb4bc5f0 5631
nexpaq 0:6c56fb4bc5f0 5632 #if defined(MBEDTLS_SSL_SRV_C)
nexpaq 0:6c56fb4bc5f0 5633 void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
nexpaq 0:6c56fb4bc5f0 5634 void *p_cache,
nexpaq 0:6c56fb4bc5f0 5635 int (*f_get_cache)(void *, mbedtls_ssl_session *),
nexpaq 0:6c56fb4bc5f0 5636 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
nexpaq 0:6c56fb4bc5f0 5637 {
nexpaq 0:6c56fb4bc5f0 5638 conf->p_cache = p_cache;
nexpaq 0:6c56fb4bc5f0 5639 conf->f_get_cache = f_get_cache;
nexpaq 0:6c56fb4bc5f0 5640 conf->f_set_cache = f_set_cache;
nexpaq 0:6c56fb4bc5f0 5641 }
nexpaq 0:6c56fb4bc5f0 5642 #endif /* MBEDTLS_SSL_SRV_C */
nexpaq 0:6c56fb4bc5f0 5643
nexpaq 0:6c56fb4bc5f0 5644 #if defined(MBEDTLS_SSL_CLI_C)
nexpaq 0:6c56fb4bc5f0 5645 int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
nexpaq 0:6c56fb4bc5f0 5646 {
nexpaq 0:6c56fb4bc5f0 5647 int ret;
nexpaq 0:6c56fb4bc5f0 5648
nexpaq 0:6c56fb4bc5f0 5649 if( ssl == NULL ||
nexpaq 0:6c56fb4bc5f0 5650 session == NULL ||
nexpaq 0:6c56fb4bc5f0 5651 ssl->session_negotiate == NULL ||
nexpaq 0:6c56fb4bc5f0 5652 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
nexpaq 0:6c56fb4bc5f0 5653 {
nexpaq 0:6c56fb4bc5f0 5654 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 5655 }
nexpaq 0:6c56fb4bc5f0 5656
nexpaq 0:6c56fb4bc5f0 5657 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 5658 return( ret );
nexpaq 0:6c56fb4bc5f0 5659
nexpaq 0:6c56fb4bc5f0 5660 ssl->handshake->resume = 1;
nexpaq 0:6c56fb4bc5f0 5661
nexpaq 0:6c56fb4bc5f0 5662 return( 0 );
nexpaq 0:6c56fb4bc5f0 5663 }
nexpaq 0:6c56fb4bc5f0 5664 #endif /* MBEDTLS_SSL_CLI_C */
nexpaq 0:6c56fb4bc5f0 5665
nexpaq 0:6c56fb4bc5f0 5666 void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
nexpaq 0:6c56fb4bc5f0 5667 const int *ciphersuites )
nexpaq 0:6c56fb4bc5f0 5668 {
nexpaq 0:6c56fb4bc5f0 5669 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
nexpaq 0:6c56fb4bc5f0 5670 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
nexpaq 0:6c56fb4bc5f0 5671 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
nexpaq 0:6c56fb4bc5f0 5672 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
nexpaq 0:6c56fb4bc5f0 5673 }
nexpaq 0:6c56fb4bc5f0 5674
nexpaq 0:6c56fb4bc5f0 5675 void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
nexpaq 0:6c56fb4bc5f0 5676 const int *ciphersuites,
nexpaq 0:6c56fb4bc5f0 5677 int major, int minor )
nexpaq 0:6c56fb4bc5f0 5678 {
nexpaq 0:6c56fb4bc5f0 5679 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
nexpaq 0:6c56fb4bc5f0 5680 return;
nexpaq 0:6c56fb4bc5f0 5681
nexpaq 0:6c56fb4bc5f0 5682 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
nexpaq 0:6c56fb4bc5f0 5683 return;
nexpaq 0:6c56fb4bc5f0 5684
nexpaq 0:6c56fb4bc5f0 5685 conf->ciphersuite_list[minor] = ciphersuites;
nexpaq 0:6c56fb4bc5f0 5686 }
nexpaq 0:6c56fb4bc5f0 5687
nexpaq 0:6c56fb4bc5f0 5688 #if defined(MBEDTLS_X509_CRT_PARSE_C)
nexpaq 0:6c56fb4bc5f0 5689 void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
nexpaq 0:6c56fb4bc5f0 5690 const mbedtls_x509_crt_profile *profile )
nexpaq 0:6c56fb4bc5f0 5691 {
nexpaq 0:6c56fb4bc5f0 5692 conf->cert_profile = profile;
nexpaq 0:6c56fb4bc5f0 5693 }
nexpaq 0:6c56fb4bc5f0 5694
nexpaq 0:6c56fb4bc5f0 5695 /* Append a new keycert entry to a (possibly empty) list */
nexpaq 0:6c56fb4bc5f0 5696 static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
nexpaq 0:6c56fb4bc5f0 5697 mbedtls_x509_crt *cert,
nexpaq 0:6c56fb4bc5f0 5698 mbedtls_pk_context *key )
nexpaq 0:6c56fb4bc5f0 5699 {
nexpaq 0:6c56fb4bc5f0 5700 mbedtls_ssl_key_cert *new;
nexpaq 0:6c56fb4bc5f0 5701
nexpaq 0:6c56fb4bc5f0 5702 new = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
nexpaq 0:6c56fb4bc5f0 5703 if( new == NULL )
nexpaq 0:6c56fb4bc5f0 5704 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
nexpaq 0:6c56fb4bc5f0 5705
nexpaq 0:6c56fb4bc5f0 5706 new->cert = cert;
nexpaq 0:6c56fb4bc5f0 5707 new->key = key;
nexpaq 0:6c56fb4bc5f0 5708 new->next = NULL;
nexpaq 0:6c56fb4bc5f0 5709
nexpaq 0:6c56fb4bc5f0 5710 /* Update head is the list was null, else add to the end */
nexpaq 0:6c56fb4bc5f0 5711 if( *head == NULL )
nexpaq 0:6c56fb4bc5f0 5712 {
nexpaq 0:6c56fb4bc5f0 5713 *head = new;
nexpaq 0:6c56fb4bc5f0 5714 }
nexpaq 0:6c56fb4bc5f0 5715 else
nexpaq 0:6c56fb4bc5f0 5716 {
nexpaq 0:6c56fb4bc5f0 5717 mbedtls_ssl_key_cert *cur = *head;
nexpaq 0:6c56fb4bc5f0 5718 while( cur->next != NULL )
nexpaq 0:6c56fb4bc5f0 5719 cur = cur->next;
nexpaq 0:6c56fb4bc5f0 5720 cur->next = new;
nexpaq 0:6c56fb4bc5f0 5721 }
nexpaq 0:6c56fb4bc5f0 5722
nexpaq 0:6c56fb4bc5f0 5723 return( 0 );
nexpaq 0:6c56fb4bc5f0 5724 }
nexpaq 0:6c56fb4bc5f0 5725
nexpaq 0:6c56fb4bc5f0 5726 int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
nexpaq 0:6c56fb4bc5f0 5727 mbedtls_x509_crt *own_cert,
nexpaq 0:6c56fb4bc5f0 5728 mbedtls_pk_context *pk_key )
nexpaq 0:6c56fb4bc5f0 5729 {
nexpaq 0:6c56fb4bc5f0 5730 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
nexpaq 0:6c56fb4bc5f0 5731 }
nexpaq 0:6c56fb4bc5f0 5732
nexpaq 0:6c56fb4bc5f0 5733 void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
nexpaq 0:6c56fb4bc5f0 5734 mbedtls_x509_crt *ca_chain,
nexpaq 0:6c56fb4bc5f0 5735 mbedtls_x509_crl *ca_crl )
nexpaq 0:6c56fb4bc5f0 5736 {
nexpaq 0:6c56fb4bc5f0 5737 conf->ca_chain = ca_chain;
nexpaq 0:6c56fb4bc5f0 5738 conf->ca_crl = ca_crl;
nexpaq 0:6c56fb4bc5f0 5739 }
nexpaq 0:6c56fb4bc5f0 5740 #endif /* MBEDTLS_X509_CRT_PARSE_C */
nexpaq 0:6c56fb4bc5f0 5741
nexpaq 0:6c56fb4bc5f0 5742 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
nexpaq 0:6c56fb4bc5f0 5743 int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
nexpaq 0:6c56fb4bc5f0 5744 mbedtls_x509_crt *own_cert,
nexpaq 0:6c56fb4bc5f0 5745 mbedtls_pk_context *pk_key )
nexpaq 0:6c56fb4bc5f0 5746 {
nexpaq 0:6c56fb4bc5f0 5747 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
nexpaq 0:6c56fb4bc5f0 5748 own_cert, pk_key ) );
nexpaq 0:6c56fb4bc5f0 5749 }
nexpaq 0:6c56fb4bc5f0 5750
nexpaq 0:6c56fb4bc5f0 5751 void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
nexpaq 0:6c56fb4bc5f0 5752 mbedtls_x509_crt *ca_chain,
nexpaq 0:6c56fb4bc5f0 5753 mbedtls_x509_crl *ca_crl )
nexpaq 0:6c56fb4bc5f0 5754 {
nexpaq 0:6c56fb4bc5f0 5755 ssl->handshake->sni_ca_chain = ca_chain;
nexpaq 0:6c56fb4bc5f0 5756 ssl->handshake->sni_ca_crl = ca_crl;
nexpaq 0:6c56fb4bc5f0 5757 }
nexpaq 0:6c56fb4bc5f0 5758
nexpaq 0:6c56fb4bc5f0 5759 void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
nexpaq 0:6c56fb4bc5f0 5760 int authmode )
nexpaq 0:6c56fb4bc5f0 5761 {
nexpaq 0:6c56fb4bc5f0 5762 ssl->handshake->sni_authmode = authmode;
nexpaq 0:6c56fb4bc5f0 5763 }
nexpaq 0:6c56fb4bc5f0 5764 #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
nexpaq 0:6c56fb4bc5f0 5765
nexpaq 0:6c56fb4bc5f0 5766 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
nexpaq 0:6c56fb4bc5f0 5767 /*
nexpaq 0:6c56fb4bc5f0 5768 * Set EC J-PAKE password for current handshake
nexpaq 0:6c56fb4bc5f0 5769 */
nexpaq 0:6c56fb4bc5f0 5770 int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
nexpaq 0:6c56fb4bc5f0 5771 const unsigned char *pw,
nexpaq 0:6c56fb4bc5f0 5772 size_t pw_len )
nexpaq 0:6c56fb4bc5f0 5773 {
nexpaq 0:6c56fb4bc5f0 5774 mbedtls_ecjpake_role role;
nexpaq 0:6c56fb4bc5f0 5775
nexpaq 0:6c56fb4bc5f0 5776 if( ssl->handshake == NULL || ssl->conf == NULL )
nexpaq 0:6c56fb4bc5f0 5777 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 5778
nexpaq 0:6c56fb4bc5f0 5779 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
nexpaq 0:6c56fb4bc5f0 5780 role = MBEDTLS_ECJPAKE_SERVER;
nexpaq 0:6c56fb4bc5f0 5781 else
nexpaq 0:6c56fb4bc5f0 5782 role = MBEDTLS_ECJPAKE_CLIENT;
nexpaq 0:6c56fb4bc5f0 5783
nexpaq 0:6c56fb4bc5f0 5784 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
nexpaq 0:6c56fb4bc5f0 5785 role,
nexpaq 0:6c56fb4bc5f0 5786 MBEDTLS_MD_SHA256,
nexpaq 0:6c56fb4bc5f0 5787 MBEDTLS_ECP_DP_SECP256R1,
nexpaq 0:6c56fb4bc5f0 5788 pw, pw_len ) );
nexpaq 0:6c56fb4bc5f0 5789 }
nexpaq 0:6c56fb4bc5f0 5790 #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
nexpaq 0:6c56fb4bc5f0 5791
nexpaq 0:6c56fb4bc5f0 5792 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
nexpaq 0:6c56fb4bc5f0 5793 int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
nexpaq 0:6c56fb4bc5f0 5794 const unsigned char *psk, size_t psk_len,
nexpaq 0:6c56fb4bc5f0 5795 const unsigned char *psk_identity, size_t psk_identity_len )
nexpaq 0:6c56fb4bc5f0 5796 {
nexpaq 0:6c56fb4bc5f0 5797 if( psk == NULL || psk_identity == NULL )
nexpaq 0:6c56fb4bc5f0 5798 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 5799
nexpaq 0:6c56fb4bc5f0 5800 if( psk_len > MBEDTLS_PSK_MAX_LEN )
nexpaq 0:6c56fb4bc5f0 5801 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 5802
nexpaq 0:6c56fb4bc5f0 5803 /* Identity len will be encoded on two bytes */
nexpaq 0:6c56fb4bc5f0 5804 if( ( psk_identity_len >> 16 ) != 0 ||
nexpaq 0:6c56fb4bc5f0 5805 psk_identity_len > MBEDTLS_SSL_MAX_CONTENT_LEN )
nexpaq 0:6c56fb4bc5f0 5806 {
nexpaq 0:6c56fb4bc5f0 5807 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 5808 }
nexpaq 0:6c56fb4bc5f0 5809
nexpaq 0:6c56fb4bc5f0 5810 if( conf->psk != NULL || conf->psk_identity != NULL )
nexpaq 0:6c56fb4bc5f0 5811 {
nexpaq 0:6c56fb4bc5f0 5812 mbedtls_free( conf->psk );
nexpaq 0:6c56fb4bc5f0 5813 mbedtls_free( conf->psk_identity );
nexpaq 0:6c56fb4bc5f0 5814 conf->psk = NULL;
nexpaq 0:6c56fb4bc5f0 5815 conf->psk_identity = NULL;
nexpaq 0:6c56fb4bc5f0 5816 }
nexpaq 0:6c56fb4bc5f0 5817
nexpaq 0:6c56fb4bc5f0 5818 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
nexpaq 0:6c56fb4bc5f0 5819 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
nexpaq 0:6c56fb4bc5f0 5820 {
nexpaq 0:6c56fb4bc5f0 5821 mbedtls_free( conf->psk );
nexpaq 0:6c56fb4bc5f0 5822 mbedtls_free( conf->psk_identity );
nexpaq 0:6c56fb4bc5f0 5823 conf->psk = NULL;
nexpaq 0:6c56fb4bc5f0 5824 conf->psk_identity = NULL;
nexpaq 0:6c56fb4bc5f0 5825 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
nexpaq 0:6c56fb4bc5f0 5826 }
nexpaq 0:6c56fb4bc5f0 5827
nexpaq 0:6c56fb4bc5f0 5828 conf->psk_len = psk_len;
nexpaq 0:6c56fb4bc5f0 5829 conf->psk_identity_len = psk_identity_len;
nexpaq 0:6c56fb4bc5f0 5830
nexpaq 0:6c56fb4bc5f0 5831 memcpy( conf->psk, psk, conf->psk_len );
nexpaq 0:6c56fb4bc5f0 5832 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
nexpaq 0:6c56fb4bc5f0 5833
nexpaq 0:6c56fb4bc5f0 5834 return( 0 );
nexpaq 0:6c56fb4bc5f0 5835 }
nexpaq 0:6c56fb4bc5f0 5836
nexpaq 0:6c56fb4bc5f0 5837 int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
nexpaq 0:6c56fb4bc5f0 5838 const unsigned char *psk, size_t psk_len )
nexpaq 0:6c56fb4bc5f0 5839 {
nexpaq 0:6c56fb4bc5f0 5840 if( psk == NULL || ssl->handshake == NULL )
nexpaq 0:6c56fb4bc5f0 5841 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 5842
nexpaq 0:6c56fb4bc5f0 5843 if( psk_len > MBEDTLS_PSK_MAX_LEN )
nexpaq 0:6c56fb4bc5f0 5844 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 5845
nexpaq 0:6c56fb4bc5f0 5846 if( ssl->handshake->psk != NULL )
nexpaq 0:6c56fb4bc5f0 5847 mbedtls_free( ssl->handshake->psk );
nexpaq 0:6c56fb4bc5f0 5848
nexpaq 0:6c56fb4bc5f0 5849 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
nexpaq 0:6c56fb4bc5f0 5850 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
nexpaq 0:6c56fb4bc5f0 5851
nexpaq 0:6c56fb4bc5f0 5852 ssl->handshake->psk_len = psk_len;
nexpaq 0:6c56fb4bc5f0 5853 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
nexpaq 0:6c56fb4bc5f0 5854
nexpaq 0:6c56fb4bc5f0 5855 return( 0 );
nexpaq 0:6c56fb4bc5f0 5856 }
nexpaq 0:6c56fb4bc5f0 5857
nexpaq 0:6c56fb4bc5f0 5858 void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
nexpaq 0:6c56fb4bc5f0 5859 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
nexpaq 0:6c56fb4bc5f0 5860 size_t),
nexpaq 0:6c56fb4bc5f0 5861 void *p_psk )
nexpaq 0:6c56fb4bc5f0 5862 {
nexpaq 0:6c56fb4bc5f0 5863 conf->f_psk = f_psk;
nexpaq 0:6c56fb4bc5f0 5864 conf->p_psk = p_psk;
nexpaq 0:6c56fb4bc5f0 5865 }
nexpaq 0:6c56fb4bc5f0 5866 #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
nexpaq 0:6c56fb4bc5f0 5867
nexpaq 0:6c56fb4bc5f0 5868 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
nexpaq 0:6c56fb4bc5f0 5869 int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
nexpaq 0:6c56fb4bc5f0 5870 {
nexpaq 0:6c56fb4bc5f0 5871 int ret;
nexpaq 0:6c56fb4bc5f0 5872
nexpaq 0:6c56fb4bc5f0 5873 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
nexpaq 0:6c56fb4bc5f0 5874 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 5875 {
nexpaq 0:6c56fb4bc5f0 5876 mbedtls_mpi_free( &conf->dhm_P );
nexpaq 0:6c56fb4bc5f0 5877 mbedtls_mpi_free( &conf->dhm_G );
nexpaq 0:6c56fb4bc5f0 5878 return( ret );
nexpaq 0:6c56fb4bc5f0 5879 }
nexpaq 0:6c56fb4bc5f0 5880
nexpaq 0:6c56fb4bc5f0 5881 return( 0 );
nexpaq 0:6c56fb4bc5f0 5882 }
nexpaq 0:6c56fb4bc5f0 5883
nexpaq 0:6c56fb4bc5f0 5884 int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
nexpaq 0:6c56fb4bc5f0 5885 {
nexpaq 0:6c56fb4bc5f0 5886 int ret;
nexpaq 0:6c56fb4bc5f0 5887
nexpaq 0:6c56fb4bc5f0 5888 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
nexpaq 0:6c56fb4bc5f0 5889 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 5890 {
nexpaq 0:6c56fb4bc5f0 5891 mbedtls_mpi_free( &conf->dhm_P );
nexpaq 0:6c56fb4bc5f0 5892 mbedtls_mpi_free( &conf->dhm_G );
nexpaq 0:6c56fb4bc5f0 5893 return( ret );
nexpaq 0:6c56fb4bc5f0 5894 }
nexpaq 0:6c56fb4bc5f0 5895
nexpaq 0:6c56fb4bc5f0 5896 return( 0 );
nexpaq 0:6c56fb4bc5f0 5897 }
nexpaq 0:6c56fb4bc5f0 5898 #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
nexpaq 0:6c56fb4bc5f0 5899
nexpaq 0:6c56fb4bc5f0 5900 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
nexpaq 0:6c56fb4bc5f0 5901 /*
nexpaq 0:6c56fb4bc5f0 5902 * Set the minimum length for Diffie-Hellman parameters
nexpaq 0:6c56fb4bc5f0 5903 */
nexpaq 0:6c56fb4bc5f0 5904 void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
nexpaq 0:6c56fb4bc5f0 5905 unsigned int bitlen )
nexpaq 0:6c56fb4bc5f0 5906 {
nexpaq 0:6c56fb4bc5f0 5907 conf->dhm_min_bitlen = bitlen;
nexpaq 0:6c56fb4bc5f0 5908 }
nexpaq 0:6c56fb4bc5f0 5909 #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
nexpaq 0:6c56fb4bc5f0 5910
nexpaq 0:6c56fb4bc5f0 5911 #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
nexpaq 0:6c56fb4bc5f0 5912 /*
nexpaq 0:6c56fb4bc5f0 5913 * Set allowed/preferred hashes for handshake signatures
nexpaq 0:6c56fb4bc5f0 5914 */
nexpaq 0:6c56fb4bc5f0 5915 void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
nexpaq 0:6c56fb4bc5f0 5916 const int *hashes )
nexpaq 0:6c56fb4bc5f0 5917 {
nexpaq 0:6c56fb4bc5f0 5918 conf->sig_hashes = hashes;
nexpaq 0:6c56fb4bc5f0 5919 }
nexpaq 0:6c56fb4bc5f0 5920 #endif
nexpaq 0:6c56fb4bc5f0 5921
nexpaq 0:6c56fb4bc5f0 5922 #if defined(MBEDTLS_ECP_C)
nexpaq 0:6c56fb4bc5f0 5923 /*
nexpaq 0:6c56fb4bc5f0 5924 * Set the allowed elliptic curves
nexpaq 0:6c56fb4bc5f0 5925 */
nexpaq 0:6c56fb4bc5f0 5926 void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
nexpaq 0:6c56fb4bc5f0 5927 const mbedtls_ecp_group_id *curve_list )
nexpaq 0:6c56fb4bc5f0 5928 {
nexpaq 0:6c56fb4bc5f0 5929 conf->curve_list = curve_list;
nexpaq 0:6c56fb4bc5f0 5930 }
nexpaq 0:6c56fb4bc5f0 5931 #endif
nexpaq 0:6c56fb4bc5f0 5932
nexpaq 0:6c56fb4bc5f0 5933 #if defined(MBEDTLS_X509_CRT_PARSE_C)
nexpaq 0:6c56fb4bc5f0 5934 int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
nexpaq 0:6c56fb4bc5f0 5935 {
nexpaq 0:6c56fb4bc5f0 5936 size_t hostname_len;
nexpaq 0:6c56fb4bc5f0 5937
nexpaq 0:6c56fb4bc5f0 5938 if( hostname == NULL )
nexpaq 0:6c56fb4bc5f0 5939 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 5940
nexpaq 0:6c56fb4bc5f0 5941 hostname_len = strlen( hostname );
nexpaq 0:6c56fb4bc5f0 5942
nexpaq 0:6c56fb4bc5f0 5943 if( hostname_len + 1 == 0 )
nexpaq 0:6c56fb4bc5f0 5944 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 5945
nexpaq 0:6c56fb4bc5f0 5946 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
nexpaq 0:6c56fb4bc5f0 5947 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 5948
nexpaq 0:6c56fb4bc5f0 5949 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
nexpaq 0:6c56fb4bc5f0 5950
nexpaq 0:6c56fb4bc5f0 5951 if( ssl->hostname == NULL )
nexpaq 0:6c56fb4bc5f0 5952 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
nexpaq 0:6c56fb4bc5f0 5953
nexpaq 0:6c56fb4bc5f0 5954 memcpy( ssl->hostname, hostname, hostname_len );
nexpaq 0:6c56fb4bc5f0 5955
nexpaq 0:6c56fb4bc5f0 5956 ssl->hostname[hostname_len] = '\0';
nexpaq 0:6c56fb4bc5f0 5957
nexpaq 0:6c56fb4bc5f0 5958 return( 0 );
nexpaq 0:6c56fb4bc5f0 5959 }
nexpaq 0:6c56fb4bc5f0 5960 #endif
nexpaq 0:6c56fb4bc5f0 5961
nexpaq 0:6c56fb4bc5f0 5962 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
nexpaq 0:6c56fb4bc5f0 5963 void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
nexpaq 0:6c56fb4bc5f0 5964 int (*f_sni)(void *, mbedtls_ssl_context *,
nexpaq 0:6c56fb4bc5f0 5965 const unsigned char *, size_t),
nexpaq 0:6c56fb4bc5f0 5966 void *p_sni )
nexpaq 0:6c56fb4bc5f0 5967 {
nexpaq 0:6c56fb4bc5f0 5968 conf->f_sni = f_sni;
nexpaq 0:6c56fb4bc5f0 5969 conf->p_sni = p_sni;
nexpaq 0:6c56fb4bc5f0 5970 }
nexpaq 0:6c56fb4bc5f0 5971 #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
nexpaq 0:6c56fb4bc5f0 5972
nexpaq 0:6c56fb4bc5f0 5973 #if defined(MBEDTLS_SSL_ALPN)
nexpaq 0:6c56fb4bc5f0 5974 int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
nexpaq 0:6c56fb4bc5f0 5975 {
nexpaq 0:6c56fb4bc5f0 5976 size_t cur_len, tot_len;
nexpaq 0:6c56fb4bc5f0 5977 const char **p;
nexpaq 0:6c56fb4bc5f0 5978
nexpaq 0:6c56fb4bc5f0 5979 /*
nexpaq 0:6c56fb4bc5f0 5980 * "Empty strings MUST NOT be included and byte strings MUST NOT be
nexpaq 0:6c56fb4bc5f0 5981 * truncated". Check lengths now rather than later.
nexpaq 0:6c56fb4bc5f0 5982 */
nexpaq 0:6c56fb4bc5f0 5983 tot_len = 0;
nexpaq 0:6c56fb4bc5f0 5984 for( p = protos; *p != NULL; p++ )
nexpaq 0:6c56fb4bc5f0 5985 {
nexpaq 0:6c56fb4bc5f0 5986 cur_len = strlen( *p );
nexpaq 0:6c56fb4bc5f0 5987 tot_len += cur_len;
nexpaq 0:6c56fb4bc5f0 5988
nexpaq 0:6c56fb4bc5f0 5989 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
nexpaq 0:6c56fb4bc5f0 5990 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 5991 }
nexpaq 0:6c56fb4bc5f0 5992
nexpaq 0:6c56fb4bc5f0 5993 conf->alpn_list = protos;
nexpaq 0:6c56fb4bc5f0 5994
nexpaq 0:6c56fb4bc5f0 5995 return( 0 );
nexpaq 0:6c56fb4bc5f0 5996 }
nexpaq 0:6c56fb4bc5f0 5997
nexpaq 0:6c56fb4bc5f0 5998 const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 5999 {
nexpaq 0:6c56fb4bc5f0 6000 return( ssl->alpn_chosen );
nexpaq 0:6c56fb4bc5f0 6001 }
nexpaq 0:6c56fb4bc5f0 6002 #endif /* MBEDTLS_SSL_ALPN */
nexpaq 0:6c56fb4bc5f0 6003
nexpaq 0:6c56fb4bc5f0 6004 void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
nexpaq 0:6c56fb4bc5f0 6005 {
nexpaq 0:6c56fb4bc5f0 6006 conf->max_major_ver = major;
nexpaq 0:6c56fb4bc5f0 6007 conf->max_minor_ver = minor;
nexpaq 0:6c56fb4bc5f0 6008 }
nexpaq 0:6c56fb4bc5f0 6009
nexpaq 0:6c56fb4bc5f0 6010 void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
nexpaq 0:6c56fb4bc5f0 6011 {
nexpaq 0:6c56fb4bc5f0 6012 conf->min_major_ver = major;
nexpaq 0:6c56fb4bc5f0 6013 conf->min_minor_ver = minor;
nexpaq 0:6c56fb4bc5f0 6014 }
nexpaq 0:6c56fb4bc5f0 6015
nexpaq 0:6c56fb4bc5f0 6016 #if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
nexpaq 0:6c56fb4bc5f0 6017 void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
nexpaq 0:6c56fb4bc5f0 6018 {
nexpaq 0:6c56fb4bc5f0 6019 conf->fallback = fallback;
nexpaq 0:6c56fb4bc5f0 6020 }
nexpaq 0:6c56fb4bc5f0 6021 #endif
nexpaq 0:6c56fb4bc5f0 6022
nexpaq 0:6c56fb4bc5f0 6023 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
nexpaq 0:6c56fb4bc5f0 6024 void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
nexpaq 0:6c56fb4bc5f0 6025 {
nexpaq 0:6c56fb4bc5f0 6026 conf->encrypt_then_mac = etm;
nexpaq 0:6c56fb4bc5f0 6027 }
nexpaq 0:6c56fb4bc5f0 6028 #endif
nexpaq 0:6c56fb4bc5f0 6029
nexpaq 0:6c56fb4bc5f0 6030 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
nexpaq 0:6c56fb4bc5f0 6031 void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
nexpaq 0:6c56fb4bc5f0 6032 {
nexpaq 0:6c56fb4bc5f0 6033 conf->extended_ms = ems;
nexpaq 0:6c56fb4bc5f0 6034 }
nexpaq 0:6c56fb4bc5f0 6035 #endif
nexpaq 0:6c56fb4bc5f0 6036
nexpaq 0:6c56fb4bc5f0 6037 #if defined(MBEDTLS_ARC4_C)
nexpaq 0:6c56fb4bc5f0 6038 void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
nexpaq 0:6c56fb4bc5f0 6039 {
nexpaq 0:6c56fb4bc5f0 6040 conf->arc4_disabled = arc4;
nexpaq 0:6c56fb4bc5f0 6041 }
nexpaq 0:6c56fb4bc5f0 6042 #endif
nexpaq 0:6c56fb4bc5f0 6043
nexpaq 0:6c56fb4bc5f0 6044 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
nexpaq 0:6c56fb4bc5f0 6045 int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
nexpaq 0:6c56fb4bc5f0 6046 {
nexpaq 0:6c56fb4bc5f0 6047 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
nexpaq 0:6c56fb4bc5f0 6048 mfl_code_to_length[mfl_code] > MBEDTLS_SSL_MAX_CONTENT_LEN )
nexpaq 0:6c56fb4bc5f0 6049 {
nexpaq 0:6c56fb4bc5f0 6050 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 6051 }
nexpaq 0:6c56fb4bc5f0 6052
nexpaq 0:6c56fb4bc5f0 6053 conf->mfl_code = mfl_code;
nexpaq 0:6c56fb4bc5f0 6054
nexpaq 0:6c56fb4bc5f0 6055 return( 0 );
nexpaq 0:6c56fb4bc5f0 6056 }
nexpaq 0:6c56fb4bc5f0 6057 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
nexpaq 0:6c56fb4bc5f0 6058
nexpaq 0:6c56fb4bc5f0 6059 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
nexpaq 0:6c56fb4bc5f0 6060 void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
nexpaq 0:6c56fb4bc5f0 6061 {
nexpaq 0:6c56fb4bc5f0 6062 conf->trunc_hmac = truncate;
nexpaq 0:6c56fb4bc5f0 6063 }
nexpaq 0:6c56fb4bc5f0 6064 #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
nexpaq 0:6c56fb4bc5f0 6065
nexpaq 0:6c56fb4bc5f0 6066 #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
nexpaq 0:6c56fb4bc5f0 6067 void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
nexpaq 0:6c56fb4bc5f0 6068 {
nexpaq 0:6c56fb4bc5f0 6069 conf->cbc_record_splitting = split;
nexpaq 0:6c56fb4bc5f0 6070 }
nexpaq 0:6c56fb4bc5f0 6071 #endif
nexpaq 0:6c56fb4bc5f0 6072
nexpaq 0:6c56fb4bc5f0 6073 void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
nexpaq 0:6c56fb4bc5f0 6074 {
nexpaq 0:6c56fb4bc5f0 6075 conf->allow_legacy_renegotiation = allow_legacy;
nexpaq 0:6c56fb4bc5f0 6076 }
nexpaq 0:6c56fb4bc5f0 6077
nexpaq 0:6c56fb4bc5f0 6078 #if defined(MBEDTLS_SSL_RENEGOTIATION)
nexpaq 0:6c56fb4bc5f0 6079 void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
nexpaq 0:6c56fb4bc5f0 6080 {
nexpaq 0:6c56fb4bc5f0 6081 conf->disable_renegotiation = renegotiation;
nexpaq 0:6c56fb4bc5f0 6082 }
nexpaq 0:6c56fb4bc5f0 6083
nexpaq 0:6c56fb4bc5f0 6084 void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
nexpaq 0:6c56fb4bc5f0 6085 {
nexpaq 0:6c56fb4bc5f0 6086 conf->renego_max_records = max_records;
nexpaq 0:6c56fb4bc5f0 6087 }
nexpaq 0:6c56fb4bc5f0 6088
nexpaq 0:6c56fb4bc5f0 6089 void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
nexpaq 0:6c56fb4bc5f0 6090 const unsigned char period[8] )
nexpaq 0:6c56fb4bc5f0 6091 {
nexpaq 0:6c56fb4bc5f0 6092 memcpy( conf->renego_period, period, 8 );
nexpaq 0:6c56fb4bc5f0 6093 }
nexpaq 0:6c56fb4bc5f0 6094 #endif /* MBEDTLS_SSL_RENEGOTIATION */
nexpaq 0:6c56fb4bc5f0 6095
nexpaq 0:6c56fb4bc5f0 6096 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
nexpaq 0:6c56fb4bc5f0 6097 #if defined(MBEDTLS_SSL_CLI_C)
nexpaq 0:6c56fb4bc5f0 6098 void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
nexpaq 0:6c56fb4bc5f0 6099 {
nexpaq 0:6c56fb4bc5f0 6100 conf->session_tickets = use_tickets;
nexpaq 0:6c56fb4bc5f0 6101 }
nexpaq 0:6c56fb4bc5f0 6102 #endif
nexpaq 0:6c56fb4bc5f0 6103
nexpaq 0:6c56fb4bc5f0 6104 #if defined(MBEDTLS_SSL_SRV_C)
nexpaq 0:6c56fb4bc5f0 6105 void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
nexpaq 0:6c56fb4bc5f0 6106 mbedtls_ssl_ticket_write_t *f_ticket_write,
nexpaq 0:6c56fb4bc5f0 6107 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
nexpaq 0:6c56fb4bc5f0 6108 void *p_ticket )
nexpaq 0:6c56fb4bc5f0 6109 {
nexpaq 0:6c56fb4bc5f0 6110 conf->f_ticket_write = f_ticket_write;
nexpaq 0:6c56fb4bc5f0 6111 conf->f_ticket_parse = f_ticket_parse;
nexpaq 0:6c56fb4bc5f0 6112 conf->p_ticket = p_ticket;
nexpaq 0:6c56fb4bc5f0 6113 }
nexpaq 0:6c56fb4bc5f0 6114 #endif
nexpaq 0:6c56fb4bc5f0 6115 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
nexpaq 0:6c56fb4bc5f0 6116
nexpaq 0:6c56fb4bc5f0 6117 #if defined(MBEDTLS_SSL_EXPORT_KEYS)
nexpaq 0:6c56fb4bc5f0 6118 void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
nexpaq 0:6c56fb4bc5f0 6119 mbedtls_ssl_export_keys_t *f_export_keys,
nexpaq 0:6c56fb4bc5f0 6120 void *p_export_keys )
nexpaq 0:6c56fb4bc5f0 6121 {
nexpaq 0:6c56fb4bc5f0 6122 conf->f_export_keys = f_export_keys;
nexpaq 0:6c56fb4bc5f0 6123 conf->p_export_keys = p_export_keys;
nexpaq 0:6c56fb4bc5f0 6124 }
nexpaq 0:6c56fb4bc5f0 6125 #endif
nexpaq 0:6c56fb4bc5f0 6126
nexpaq 0:6c56fb4bc5f0 6127 /*
nexpaq 0:6c56fb4bc5f0 6128 * SSL get accessors
nexpaq 0:6c56fb4bc5f0 6129 */
nexpaq 0:6c56fb4bc5f0 6130 size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 6131 {
nexpaq 0:6c56fb4bc5f0 6132 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
nexpaq 0:6c56fb4bc5f0 6133 }
nexpaq 0:6c56fb4bc5f0 6134
nexpaq 0:6c56fb4bc5f0 6135 uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 6136 {
nexpaq 0:6c56fb4bc5f0 6137 if( ssl->session != NULL )
nexpaq 0:6c56fb4bc5f0 6138 return( ssl->session->verify_result );
nexpaq 0:6c56fb4bc5f0 6139
nexpaq 0:6c56fb4bc5f0 6140 if( ssl->session_negotiate != NULL )
nexpaq 0:6c56fb4bc5f0 6141 return( ssl->session_negotiate->verify_result );
nexpaq 0:6c56fb4bc5f0 6142
nexpaq 0:6c56fb4bc5f0 6143 return( 0xFFFFFFFF );
nexpaq 0:6c56fb4bc5f0 6144 }
nexpaq 0:6c56fb4bc5f0 6145
nexpaq 0:6c56fb4bc5f0 6146 const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 6147 {
nexpaq 0:6c56fb4bc5f0 6148 if( ssl == NULL || ssl->session == NULL )
nexpaq 0:6c56fb4bc5f0 6149 return( NULL );
nexpaq 0:6c56fb4bc5f0 6150
nexpaq 0:6c56fb4bc5f0 6151 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
nexpaq 0:6c56fb4bc5f0 6152 }
nexpaq 0:6c56fb4bc5f0 6153
nexpaq 0:6c56fb4bc5f0 6154 const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 6155 {
nexpaq 0:6c56fb4bc5f0 6156 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 6157 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
nexpaq 0:6c56fb4bc5f0 6158 {
nexpaq 0:6c56fb4bc5f0 6159 switch( ssl->minor_ver )
nexpaq 0:6c56fb4bc5f0 6160 {
nexpaq 0:6c56fb4bc5f0 6161 case MBEDTLS_SSL_MINOR_VERSION_2:
nexpaq 0:6c56fb4bc5f0 6162 return( "DTLSv1.0" );
nexpaq 0:6c56fb4bc5f0 6163
nexpaq 0:6c56fb4bc5f0 6164 case MBEDTLS_SSL_MINOR_VERSION_3:
nexpaq 0:6c56fb4bc5f0 6165 return( "DTLSv1.2" );
nexpaq 0:6c56fb4bc5f0 6166
nexpaq 0:6c56fb4bc5f0 6167 default:
nexpaq 0:6c56fb4bc5f0 6168 return( "unknown (DTLS)" );
nexpaq 0:6c56fb4bc5f0 6169 }
nexpaq 0:6c56fb4bc5f0 6170 }
nexpaq 0:6c56fb4bc5f0 6171 #endif
nexpaq 0:6c56fb4bc5f0 6172
nexpaq 0:6c56fb4bc5f0 6173 switch( ssl->minor_ver )
nexpaq 0:6c56fb4bc5f0 6174 {
nexpaq 0:6c56fb4bc5f0 6175 case MBEDTLS_SSL_MINOR_VERSION_0:
nexpaq 0:6c56fb4bc5f0 6176 return( "SSLv3.0" );
nexpaq 0:6c56fb4bc5f0 6177
nexpaq 0:6c56fb4bc5f0 6178 case MBEDTLS_SSL_MINOR_VERSION_1:
nexpaq 0:6c56fb4bc5f0 6179 return( "TLSv1.0" );
nexpaq 0:6c56fb4bc5f0 6180
nexpaq 0:6c56fb4bc5f0 6181 case MBEDTLS_SSL_MINOR_VERSION_2:
nexpaq 0:6c56fb4bc5f0 6182 return( "TLSv1.1" );
nexpaq 0:6c56fb4bc5f0 6183
nexpaq 0:6c56fb4bc5f0 6184 case MBEDTLS_SSL_MINOR_VERSION_3:
nexpaq 0:6c56fb4bc5f0 6185 return( "TLSv1.2" );
nexpaq 0:6c56fb4bc5f0 6186
nexpaq 0:6c56fb4bc5f0 6187 default:
nexpaq 0:6c56fb4bc5f0 6188 return( "unknown" );
nexpaq 0:6c56fb4bc5f0 6189 }
nexpaq 0:6c56fb4bc5f0 6190 }
nexpaq 0:6c56fb4bc5f0 6191
nexpaq 0:6c56fb4bc5f0 6192 int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 6193 {
nexpaq 0:6c56fb4bc5f0 6194 size_t transform_expansion;
nexpaq 0:6c56fb4bc5f0 6195 const mbedtls_ssl_transform *transform = ssl->transform_out;
nexpaq 0:6c56fb4bc5f0 6196
nexpaq 0:6c56fb4bc5f0 6197 #if defined(MBEDTLS_ZLIB_SUPPORT)
nexpaq 0:6c56fb4bc5f0 6198 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
nexpaq 0:6c56fb4bc5f0 6199 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
nexpaq 0:6c56fb4bc5f0 6200 #endif
nexpaq 0:6c56fb4bc5f0 6201
nexpaq 0:6c56fb4bc5f0 6202 if( transform == NULL )
nexpaq 0:6c56fb4bc5f0 6203 return( (int) mbedtls_ssl_hdr_len( ssl ) );
nexpaq 0:6c56fb4bc5f0 6204
nexpaq 0:6c56fb4bc5f0 6205 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
nexpaq 0:6c56fb4bc5f0 6206 {
nexpaq 0:6c56fb4bc5f0 6207 case MBEDTLS_MODE_GCM:
nexpaq 0:6c56fb4bc5f0 6208 case MBEDTLS_MODE_CCM:
nexpaq 0:6c56fb4bc5f0 6209 case MBEDTLS_MODE_STREAM:
nexpaq 0:6c56fb4bc5f0 6210 transform_expansion = transform->minlen;
nexpaq 0:6c56fb4bc5f0 6211 break;
nexpaq 0:6c56fb4bc5f0 6212
nexpaq 0:6c56fb4bc5f0 6213 case MBEDTLS_MODE_CBC:
nexpaq 0:6c56fb4bc5f0 6214 transform_expansion = transform->maclen
nexpaq 0:6c56fb4bc5f0 6215 + mbedtls_cipher_get_block_size( &transform->cipher_ctx_enc );
nexpaq 0:6c56fb4bc5f0 6216 break;
nexpaq 0:6c56fb4bc5f0 6217
nexpaq 0:6c56fb4bc5f0 6218 default:
nexpaq 0:6c56fb4bc5f0 6219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 6220 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 6221 }
nexpaq 0:6c56fb4bc5f0 6222
nexpaq 0:6c56fb4bc5f0 6223 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
nexpaq 0:6c56fb4bc5f0 6224 }
nexpaq 0:6c56fb4bc5f0 6225
nexpaq 0:6c56fb4bc5f0 6226 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
nexpaq 0:6c56fb4bc5f0 6227 size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 6228 {
nexpaq 0:6c56fb4bc5f0 6229 size_t max_len;
nexpaq 0:6c56fb4bc5f0 6230
nexpaq 0:6c56fb4bc5f0 6231 /*
nexpaq 0:6c56fb4bc5f0 6232 * Assume mfl_code is correct since it was checked when set
nexpaq 0:6c56fb4bc5f0 6233 */
nexpaq 0:6c56fb4bc5f0 6234 max_len = mfl_code_to_length[ssl->conf->mfl_code];
nexpaq 0:6c56fb4bc5f0 6235
nexpaq 0:6c56fb4bc5f0 6236 /*
nexpaq 0:6c56fb4bc5f0 6237 * Check if a smaller max length was negotiated
nexpaq 0:6c56fb4bc5f0 6238 */
nexpaq 0:6c56fb4bc5f0 6239 if( ssl->session_out != NULL &&
nexpaq 0:6c56fb4bc5f0 6240 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
nexpaq 0:6c56fb4bc5f0 6241 {
nexpaq 0:6c56fb4bc5f0 6242 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
nexpaq 0:6c56fb4bc5f0 6243 }
nexpaq 0:6c56fb4bc5f0 6244
nexpaq 0:6c56fb4bc5f0 6245 return max_len;
nexpaq 0:6c56fb4bc5f0 6246 }
nexpaq 0:6c56fb4bc5f0 6247 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
nexpaq 0:6c56fb4bc5f0 6248
nexpaq 0:6c56fb4bc5f0 6249 #if defined(MBEDTLS_X509_CRT_PARSE_C)
nexpaq 0:6c56fb4bc5f0 6250 const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 6251 {
nexpaq 0:6c56fb4bc5f0 6252 if( ssl == NULL || ssl->session == NULL )
nexpaq 0:6c56fb4bc5f0 6253 return( NULL );
nexpaq 0:6c56fb4bc5f0 6254
nexpaq 0:6c56fb4bc5f0 6255 return( ssl->session->peer_cert );
nexpaq 0:6c56fb4bc5f0 6256 }
nexpaq 0:6c56fb4bc5f0 6257 #endif /* MBEDTLS_X509_CRT_PARSE_C */
nexpaq 0:6c56fb4bc5f0 6258
nexpaq 0:6c56fb4bc5f0 6259 #if defined(MBEDTLS_SSL_CLI_C)
nexpaq 0:6c56fb4bc5f0 6260 int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
nexpaq 0:6c56fb4bc5f0 6261 {
nexpaq 0:6c56fb4bc5f0 6262 if( ssl == NULL ||
nexpaq 0:6c56fb4bc5f0 6263 dst == NULL ||
nexpaq 0:6c56fb4bc5f0 6264 ssl->session == NULL ||
nexpaq 0:6c56fb4bc5f0 6265 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
nexpaq 0:6c56fb4bc5f0 6266 {
nexpaq 0:6c56fb4bc5f0 6267 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 6268 }
nexpaq 0:6c56fb4bc5f0 6269
nexpaq 0:6c56fb4bc5f0 6270 return( ssl_session_copy( dst, ssl->session ) );
nexpaq 0:6c56fb4bc5f0 6271 }
nexpaq 0:6c56fb4bc5f0 6272 #endif /* MBEDTLS_SSL_CLI_C */
nexpaq 0:6c56fb4bc5f0 6273
nexpaq 0:6c56fb4bc5f0 6274 /*
nexpaq 0:6c56fb4bc5f0 6275 * Perform a single step of the SSL handshake
nexpaq 0:6c56fb4bc5f0 6276 */
nexpaq 0:6c56fb4bc5f0 6277 int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 6278 {
nexpaq 0:6c56fb4bc5f0 6279 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
nexpaq 0:6c56fb4bc5f0 6280
nexpaq 0:6c56fb4bc5f0 6281 if( ssl == NULL || ssl->conf == NULL )
nexpaq 0:6c56fb4bc5f0 6282 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 6283
nexpaq 0:6c56fb4bc5f0 6284 #if defined(MBEDTLS_SSL_CLI_C)
nexpaq 0:6c56fb4bc5f0 6285 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
nexpaq 0:6c56fb4bc5f0 6286 ret = mbedtls_ssl_handshake_client_step( ssl );
nexpaq 0:6c56fb4bc5f0 6287 #endif
nexpaq 0:6c56fb4bc5f0 6288 #if defined(MBEDTLS_SSL_SRV_C)
nexpaq 0:6c56fb4bc5f0 6289 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
nexpaq 0:6c56fb4bc5f0 6290 ret = mbedtls_ssl_handshake_server_step( ssl );
nexpaq 0:6c56fb4bc5f0 6291 #endif
nexpaq 0:6c56fb4bc5f0 6292
nexpaq 0:6c56fb4bc5f0 6293 return( ret );
nexpaq 0:6c56fb4bc5f0 6294 }
nexpaq 0:6c56fb4bc5f0 6295
nexpaq 0:6c56fb4bc5f0 6296 /*
nexpaq 0:6c56fb4bc5f0 6297 * Perform the SSL handshake
nexpaq 0:6c56fb4bc5f0 6298 */
nexpaq 0:6c56fb4bc5f0 6299 int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 6300 {
nexpaq 0:6c56fb4bc5f0 6301 int ret = 0;
nexpaq 0:6c56fb4bc5f0 6302
nexpaq 0:6c56fb4bc5f0 6303 if( ssl == NULL || ssl->conf == NULL )
nexpaq 0:6c56fb4bc5f0 6304 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 6305
nexpaq 0:6c56fb4bc5f0 6306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
nexpaq 0:6c56fb4bc5f0 6307
nexpaq 0:6c56fb4bc5f0 6308 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
nexpaq 0:6c56fb4bc5f0 6309 {
nexpaq 0:6c56fb4bc5f0 6310 ret = mbedtls_ssl_handshake_step( ssl );
nexpaq 0:6c56fb4bc5f0 6311
nexpaq 0:6c56fb4bc5f0 6312 if( ret != 0 )
nexpaq 0:6c56fb4bc5f0 6313 break;
nexpaq 0:6c56fb4bc5f0 6314 }
nexpaq 0:6c56fb4bc5f0 6315
nexpaq 0:6c56fb4bc5f0 6316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
nexpaq 0:6c56fb4bc5f0 6317
nexpaq 0:6c56fb4bc5f0 6318 return( ret );
nexpaq 0:6c56fb4bc5f0 6319 }
nexpaq 0:6c56fb4bc5f0 6320
nexpaq 0:6c56fb4bc5f0 6321 #if defined(MBEDTLS_SSL_RENEGOTIATION)
nexpaq 0:6c56fb4bc5f0 6322 #if defined(MBEDTLS_SSL_SRV_C)
nexpaq 0:6c56fb4bc5f0 6323 /*
nexpaq 0:6c56fb4bc5f0 6324 * Write HelloRequest to request renegotiation on server
nexpaq 0:6c56fb4bc5f0 6325 */
nexpaq 0:6c56fb4bc5f0 6326 static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 6327 {
nexpaq 0:6c56fb4bc5f0 6328 int ret;
nexpaq 0:6c56fb4bc5f0 6329
nexpaq 0:6c56fb4bc5f0 6330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
nexpaq 0:6c56fb4bc5f0 6331
nexpaq 0:6c56fb4bc5f0 6332 ssl->out_msglen = 4;
nexpaq 0:6c56fb4bc5f0 6333 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
nexpaq 0:6c56fb4bc5f0 6334 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
nexpaq 0:6c56fb4bc5f0 6335
nexpaq 0:6c56fb4bc5f0 6336 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 6337 {
nexpaq 0:6c56fb4bc5f0 6338 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
nexpaq 0:6c56fb4bc5f0 6339 return( ret );
nexpaq 0:6c56fb4bc5f0 6340 }
nexpaq 0:6c56fb4bc5f0 6341
nexpaq 0:6c56fb4bc5f0 6342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
nexpaq 0:6c56fb4bc5f0 6343
nexpaq 0:6c56fb4bc5f0 6344 return( 0 );
nexpaq 0:6c56fb4bc5f0 6345 }
nexpaq 0:6c56fb4bc5f0 6346 #endif /* MBEDTLS_SSL_SRV_C */
nexpaq 0:6c56fb4bc5f0 6347
nexpaq 0:6c56fb4bc5f0 6348 /*
nexpaq 0:6c56fb4bc5f0 6349 * Actually renegotiate current connection, triggered by either:
nexpaq 0:6c56fb4bc5f0 6350 * - any side: calling mbedtls_ssl_renegotiate(),
nexpaq 0:6c56fb4bc5f0 6351 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
nexpaq 0:6c56fb4bc5f0 6352 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
nexpaq 0:6c56fb4bc5f0 6353 * the initial handshake is completed.
nexpaq 0:6c56fb4bc5f0 6354 * If the handshake doesn't complete due to waiting for I/O, it will continue
nexpaq 0:6c56fb4bc5f0 6355 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
nexpaq 0:6c56fb4bc5f0 6356 */
nexpaq 0:6c56fb4bc5f0 6357 static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 6358 {
nexpaq 0:6c56fb4bc5f0 6359 int ret;
nexpaq 0:6c56fb4bc5f0 6360
nexpaq 0:6c56fb4bc5f0 6361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
nexpaq 0:6c56fb4bc5f0 6362
nexpaq 0:6c56fb4bc5f0 6363 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 6364 return( ret );
nexpaq 0:6c56fb4bc5f0 6365
nexpaq 0:6c56fb4bc5f0 6366 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
nexpaq 0:6c56fb4bc5f0 6367 * the ServerHello will have message_seq = 1" */
nexpaq 0:6c56fb4bc5f0 6368 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 6369 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
nexpaq 0:6c56fb4bc5f0 6370 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
nexpaq 0:6c56fb4bc5f0 6371 {
nexpaq 0:6c56fb4bc5f0 6372 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
nexpaq 0:6c56fb4bc5f0 6373 ssl->handshake->out_msg_seq = 1;
nexpaq 0:6c56fb4bc5f0 6374 else
nexpaq 0:6c56fb4bc5f0 6375 ssl->handshake->in_msg_seq = 1;
nexpaq 0:6c56fb4bc5f0 6376 }
nexpaq 0:6c56fb4bc5f0 6377 #endif
nexpaq 0:6c56fb4bc5f0 6378
nexpaq 0:6c56fb4bc5f0 6379 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
nexpaq 0:6c56fb4bc5f0 6380 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
nexpaq 0:6c56fb4bc5f0 6381
nexpaq 0:6c56fb4bc5f0 6382 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 6383 {
nexpaq 0:6c56fb4bc5f0 6384 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
nexpaq 0:6c56fb4bc5f0 6385 return( ret );
nexpaq 0:6c56fb4bc5f0 6386 }
nexpaq 0:6c56fb4bc5f0 6387
nexpaq 0:6c56fb4bc5f0 6388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
nexpaq 0:6c56fb4bc5f0 6389
nexpaq 0:6c56fb4bc5f0 6390 return( 0 );
nexpaq 0:6c56fb4bc5f0 6391 }
nexpaq 0:6c56fb4bc5f0 6392
nexpaq 0:6c56fb4bc5f0 6393 /*
nexpaq 0:6c56fb4bc5f0 6394 * Renegotiate current connection on client,
nexpaq 0:6c56fb4bc5f0 6395 * or request renegotiation on server
nexpaq 0:6c56fb4bc5f0 6396 */
nexpaq 0:6c56fb4bc5f0 6397 int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 6398 {
nexpaq 0:6c56fb4bc5f0 6399 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
nexpaq 0:6c56fb4bc5f0 6400
nexpaq 0:6c56fb4bc5f0 6401 if( ssl == NULL || ssl->conf == NULL )
nexpaq 0:6c56fb4bc5f0 6402 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 6403
nexpaq 0:6c56fb4bc5f0 6404 #if defined(MBEDTLS_SSL_SRV_C)
nexpaq 0:6c56fb4bc5f0 6405 /* On server, just send the request */
nexpaq 0:6c56fb4bc5f0 6406 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
nexpaq 0:6c56fb4bc5f0 6407 {
nexpaq 0:6c56fb4bc5f0 6408 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
nexpaq 0:6c56fb4bc5f0 6409 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 6410
nexpaq 0:6c56fb4bc5f0 6411 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
nexpaq 0:6c56fb4bc5f0 6412
nexpaq 0:6c56fb4bc5f0 6413 /* Did we already try/start sending HelloRequest? */
nexpaq 0:6c56fb4bc5f0 6414 if( ssl->out_left != 0 )
nexpaq 0:6c56fb4bc5f0 6415 return( mbedtls_ssl_flush_output( ssl ) );
nexpaq 0:6c56fb4bc5f0 6416
nexpaq 0:6c56fb4bc5f0 6417 return( ssl_write_hello_request( ssl ) );
nexpaq 0:6c56fb4bc5f0 6418 }
nexpaq 0:6c56fb4bc5f0 6419 #endif /* MBEDTLS_SSL_SRV_C */
nexpaq 0:6c56fb4bc5f0 6420
nexpaq 0:6c56fb4bc5f0 6421 #if defined(MBEDTLS_SSL_CLI_C)
nexpaq 0:6c56fb4bc5f0 6422 /*
nexpaq 0:6c56fb4bc5f0 6423 * On client, either start the renegotiation process or,
nexpaq 0:6c56fb4bc5f0 6424 * if already in progress, continue the handshake
nexpaq 0:6c56fb4bc5f0 6425 */
nexpaq 0:6c56fb4bc5f0 6426 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
nexpaq 0:6c56fb4bc5f0 6427 {
nexpaq 0:6c56fb4bc5f0 6428 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
nexpaq 0:6c56fb4bc5f0 6429 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 6430
nexpaq 0:6c56fb4bc5f0 6431 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 6432 {
nexpaq 0:6c56fb4bc5f0 6433 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
nexpaq 0:6c56fb4bc5f0 6434 return( ret );
nexpaq 0:6c56fb4bc5f0 6435 }
nexpaq 0:6c56fb4bc5f0 6436 }
nexpaq 0:6c56fb4bc5f0 6437 else
nexpaq 0:6c56fb4bc5f0 6438 {
nexpaq 0:6c56fb4bc5f0 6439 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 6440 {
nexpaq 0:6c56fb4bc5f0 6441 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
nexpaq 0:6c56fb4bc5f0 6442 return( ret );
nexpaq 0:6c56fb4bc5f0 6443 }
nexpaq 0:6c56fb4bc5f0 6444 }
nexpaq 0:6c56fb4bc5f0 6445 #endif /* MBEDTLS_SSL_CLI_C */
nexpaq 0:6c56fb4bc5f0 6446
nexpaq 0:6c56fb4bc5f0 6447 return( ret );
nexpaq 0:6c56fb4bc5f0 6448 }
nexpaq 0:6c56fb4bc5f0 6449
nexpaq 0:6c56fb4bc5f0 6450 /*
nexpaq 0:6c56fb4bc5f0 6451 * Check record counters and renegotiate if they're above the limit.
nexpaq 0:6c56fb4bc5f0 6452 */
nexpaq 0:6c56fb4bc5f0 6453 static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 6454 {
nexpaq 0:6c56fb4bc5f0 6455 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
nexpaq 0:6c56fb4bc5f0 6456 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
nexpaq 0:6c56fb4bc5f0 6457 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
nexpaq 0:6c56fb4bc5f0 6458 {
nexpaq 0:6c56fb4bc5f0 6459 return( 0 );
nexpaq 0:6c56fb4bc5f0 6460 }
nexpaq 0:6c56fb4bc5f0 6461
nexpaq 0:6c56fb4bc5f0 6462 if( memcmp( ssl->in_ctr, ssl->conf->renego_period, 8 ) <= 0 &&
nexpaq 0:6c56fb4bc5f0 6463 memcmp( ssl->out_ctr, ssl->conf->renego_period, 8 ) <= 0 )
nexpaq 0:6c56fb4bc5f0 6464 {
nexpaq 0:6c56fb4bc5f0 6465 return( 0 );
nexpaq 0:6c56fb4bc5f0 6466 }
nexpaq 0:6c56fb4bc5f0 6467
nexpaq 0:6c56fb4bc5f0 6468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
nexpaq 0:6c56fb4bc5f0 6469 return( mbedtls_ssl_renegotiate( ssl ) );
nexpaq 0:6c56fb4bc5f0 6470 }
nexpaq 0:6c56fb4bc5f0 6471 #endif /* MBEDTLS_SSL_RENEGOTIATION */
nexpaq 0:6c56fb4bc5f0 6472
nexpaq 0:6c56fb4bc5f0 6473 /*
nexpaq 0:6c56fb4bc5f0 6474 * Receive application data decrypted from the SSL layer
nexpaq 0:6c56fb4bc5f0 6475 */
nexpaq 0:6c56fb4bc5f0 6476 int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
nexpaq 0:6c56fb4bc5f0 6477 {
nexpaq 0:6c56fb4bc5f0 6478 int ret, record_read = 0;
nexpaq 0:6c56fb4bc5f0 6479 size_t n;
nexpaq 0:6c56fb4bc5f0 6480
nexpaq 0:6c56fb4bc5f0 6481 if( ssl == NULL || ssl->conf == NULL )
nexpaq 0:6c56fb4bc5f0 6482 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 6483
nexpaq 0:6c56fb4bc5f0 6484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
nexpaq 0:6c56fb4bc5f0 6485
nexpaq 0:6c56fb4bc5f0 6486 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 6487 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
nexpaq 0:6c56fb4bc5f0 6488 {
nexpaq 0:6c56fb4bc5f0 6489 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 6490 return( ret );
nexpaq 0:6c56fb4bc5f0 6491
nexpaq 0:6c56fb4bc5f0 6492 if( ssl->handshake != NULL &&
nexpaq 0:6c56fb4bc5f0 6493 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
nexpaq 0:6c56fb4bc5f0 6494 {
nexpaq 0:6c56fb4bc5f0 6495 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 6496 return( ret );
nexpaq 0:6c56fb4bc5f0 6497 }
nexpaq 0:6c56fb4bc5f0 6498 }
nexpaq 0:6c56fb4bc5f0 6499 #endif
nexpaq 0:6c56fb4bc5f0 6500
nexpaq 0:6c56fb4bc5f0 6501 #if defined(MBEDTLS_SSL_RENEGOTIATION)
nexpaq 0:6c56fb4bc5f0 6502 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 6503 {
nexpaq 0:6c56fb4bc5f0 6504 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
nexpaq 0:6c56fb4bc5f0 6505 return( ret );
nexpaq 0:6c56fb4bc5f0 6506 }
nexpaq 0:6c56fb4bc5f0 6507 #endif
nexpaq 0:6c56fb4bc5f0 6508
nexpaq 0:6c56fb4bc5f0 6509 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
nexpaq 0:6c56fb4bc5f0 6510 {
nexpaq 0:6c56fb4bc5f0 6511 ret = mbedtls_ssl_handshake( ssl );
nexpaq 0:6c56fb4bc5f0 6512 if( ret == MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
nexpaq 0:6c56fb4bc5f0 6513 {
nexpaq 0:6c56fb4bc5f0 6514 record_read = 1;
nexpaq 0:6c56fb4bc5f0 6515 }
nexpaq 0:6c56fb4bc5f0 6516 else if( ret != 0 )
nexpaq 0:6c56fb4bc5f0 6517 {
nexpaq 0:6c56fb4bc5f0 6518 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
nexpaq 0:6c56fb4bc5f0 6519 return( ret );
nexpaq 0:6c56fb4bc5f0 6520 }
nexpaq 0:6c56fb4bc5f0 6521 }
nexpaq 0:6c56fb4bc5f0 6522
nexpaq 0:6c56fb4bc5f0 6523 if( ssl->in_offt == NULL )
nexpaq 0:6c56fb4bc5f0 6524 {
nexpaq 0:6c56fb4bc5f0 6525 /* Start timer if not already running */
nexpaq 0:6c56fb4bc5f0 6526 if( ssl->f_get_timer != NULL &&
nexpaq 0:6c56fb4bc5f0 6527 ssl->f_get_timer( ssl->p_timer ) == -1 )
nexpaq 0:6c56fb4bc5f0 6528 {
nexpaq 0:6c56fb4bc5f0 6529 ssl_set_timer( ssl, ssl->conf->read_timeout );
nexpaq 0:6c56fb4bc5f0 6530 }
nexpaq 0:6c56fb4bc5f0 6531
nexpaq 0:6c56fb4bc5f0 6532 if( ! record_read )
nexpaq 0:6c56fb4bc5f0 6533 {
nexpaq 0:6c56fb4bc5f0 6534 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 6535 {
nexpaq 0:6c56fb4bc5f0 6536 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
nexpaq 0:6c56fb4bc5f0 6537 return( 0 );
nexpaq 0:6c56fb4bc5f0 6538
nexpaq 0:6c56fb4bc5f0 6539 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
nexpaq 0:6c56fb4bc5f0 6540 return( ret );
nexpaq 0:6c56fb4bc5f0 6541 }
nexpaq 0:6c56fb4bc5f0 6542 }
nexpaq 0:6c56fb4bc5f0 6543
nexpaq 0:6c56fb4bc5f0 6544 if( ssl->in_msglen == 0 &&
nexpaq 0:6c56fb4bc5f0 6545 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
nexpaq 0:6c56fb4bc5f0 6546 {
nexpaq 0:6c56fb4bc5f0 6547 /*
nexpaq 0:6c56fb4bc5f0 6548 * OpenSSL sends empty messages to randomize the IV
nexpaq 0:6c56fb4bc5f0 6549 */
nexpaq 0:6c56fb4bc5f0 6550 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 6551 {
nexpaq 0:6c56fb4bc5f0 6552 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
nexpaq 0:6c56fb4bc5f0 6553 return( 0 );
nexpaq 0:6c56fb4bc5f0 6554
nexpaq 0:6c56fb4bc5f0 6555 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
nexpaq 0:6c56fb4bc5f0 6556 return( ret );
nexpaq 0:6c56fb4bc5f0 6557 }
nexpaq 0:6c56fb4bc5f0 6558 }
nexpaq 0:6c56fb4bc5f0 6559
nexpaq 0:6c56fb4bc5f0 6560 #if defined(MBEDTLS_SSL_RENEGOTIATION)
nexpaq 0:6c56fb4bc5f0 6561 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
nexpaq 0:6c56fb4bc5f0 6562 {
nexpaq 0:6c56fb4bc5f0 6563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
nexpaq 0:6c56fb4bc5f0 6564
nexpaq 0:6c56fb4bc5f0 6565 #if defined(MBEDTLS_SSL_CLI_C)
nexpaq 0:6c56fb4bc5f0 6566 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
nexpaq 0:6c56fb4bc5f0 6567 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
nexpaq 0:6c56fb4bc5f0 6568 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
nexpaq 0:6c56fb4bc5f0 6569 {
nexpaq 0:6c56fb4bc5f0 6570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
nexpaq 0:6c56fb4bc5f0 6571
nexpaq 0:6c56fb4bc5f0 6572 /* With DTLS, drop the packet (probably from last handshake) */
nexpaq 0:6c56fb4bc5f0 6573 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 6574 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
nexpaq 0:6c56fb4bc5f0 6575 return( MBEDTLS_ERR_SSL_WANT_READ );
nexpaq 0:6c56fb4bc5f0 6576 #endif
nexpaq 0:6c56fb4bc5f0 6577 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
nexpaq 0:6c56fb4bc5f0 6578 }
nexpaq 0:6c56fb4bc5f0 6579
nexpaq 0:6c56fb4bc5f0 6580 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
nexpaq 0:6c56fb4bc5f0 6581 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
nexpaq 0:6c56fb4bc5f0 6582 {
nexpaq 0:6c56fb4bc5f0 6583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
nexpaq 0:6c56fb4bc5f0 6584
nexpaq 0:6c56fb4bc5f0 6585 /* With DTLS, drop the packet (probably from last handshake) */
nexpaq 0:6c56fb4bc5f0 6586 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 6587 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
nexpaq 0:6c56fb4bc5f0 6588 return( MBEDTLS_ERR_SSL_WANT_READ );
nexpaq 0:6c56fb4bc5f0 6589 #endif
nexpaq 0:6c56fb4bc5f0 6590 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
nexpaq 0:6c56fb4bc5f0 6591 }
nexpaq 0:6c56fb4bc5f0 6592 #endif
nexpaq 0:6c56fb4bc5f0 6593
nexpaq 0:6c56fb4bc5f0 6594 if( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
nexpaq 0:6c56fb4bc5f0 6595 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
nexpaq 0:6c56fb4bc5f0 6596 ssl->conf->allow_legacy_renegotiation ==
nexpaq 0:6c56fb4bc5f0 6597 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) )
nexpaq 0:6c56fb4bc5f0 6598 {
nexpaq 0:6c56fb4bc5f0 6599 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
nexpaq 0:6c56fb4bc5f0 6600
nexpaq 0:6c56fb4bc5f0 6601 #if defined(MBEDTLS_SSL_PROTO_SSL3)
nexpaq 0:6c56fb4bc5f0 6602 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
nexpaq 0:6c56fb4bc5f0 6603 {
nexpaq 0:6c56fb4bc5f0 6604 /*
nexpaq 0:6c56fb4bc5f0 6605 * SSLv3 does not have a "no_renegotiation" alert
nexpaq 0:6c56fb4bc5f0 6606 */
nexpaq 0:6c56fb4bc5f0 6607 if( ( ret = mbedtls_ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 6608 return( ret );
nexpaq 0:6c56fb4bc5f0 6609 }
nexpaq 0:6c56fb4bc5f0 6610 else
nexpaq 0:6c56fb4bc5f0 6611 #endif /* MBEDTLS_SSL_PROTO_SSL3 */
nexpaq 0:6c56fb4bc5f0 6612 #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
nexpaq 0:6c56fb4bc5f0 6613 defined(MBEDTLS_SSL_PROTO_TLS1_2)
nexpaq 0:6c56fb4bc5f0 6614 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
nexpaq 0:6c56fb4bc5f0 6615 {
nexpaq 0:6c56fb4bc5f0 6616 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
nexpaq 0:6c56fb4bc5f0 6617 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
nexpaq 0:6c56fb4bc5f0 6618 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 6619 {
nexpaq 0:6c56fb4bc5f0 6620 return( ret );
nexpaq 0:6c56fb4bc5f0 6621 }
nexpaq 0:6c56fb4bc5f0 6622 }
nexpaq 0:6c56fb4bc5f0 6623 else
nexpaq 0:6c56fb4bc5f0 6624 #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
nexpaq 0:6c56fb4bc5f0 6625 MBEDTLS_SSL_PROTO_TLS1_2 */
nexpaq 0:6c56fb4bc5f0 6626 {
nexpaq 0:6c56fb4bc5f0 6627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
nexpaq 0:6c56fb4bc5f0 6628 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
nexpaq 0:6c56fb4bc5f0 6629 }
nexpaq 0:6c56fb4bc5f0 6630 }
nexpaq 0:6c56fb4bc5f0 6631 else
nexpaq 0:6c56fb4bc5f0 6632 {
nexpaq 0:6c56fb4bc5f0 6633 /* DTLS clients need to know renego is server-initiated */
nexpaq 0:6c56fb4bc5f0 6634 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 6635 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
nexpaq 0:6c56fb4bc5f0 6636 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
nexpaq 0:6c56fb4bc5f0 6637 {
nexpaq 0:6c56fb4bc5f0 6638 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
nexpaq 0:6c56fb4bc5f0 6639 }
nexpaq 0:6c56fb4bc5f0 6640 #endif
nexpaq 0:6c56fb4bc5f0 6641 ret = ssl_start_renegotiation( ssl );
nexpaq 0:6c56fb4bc5f0 6642 if( ret == MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
nexpaq 0:6c56fb4bc5f0 6643 {
nexpaq 0:6c56fb4bc5f0 6644 record_read = 1;
nexpaq 0:6c56fb4bc5f0 6645 }
nexpaq 0:6c56fb4bc5f0 6646 else if( ret != 0 )
nexpaq 0:6c56fb4bc5f0 6647 {
nexpaq 0:6c56fb4bc5f0 6648 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
nexpaq 0:6c56fb4bc5f0 6649 return( ret );
nexpaq 0:6c56fb4bc5f0 6650 }
nexpaq 0:6c56fb4bc5f0 6651 }
nexpaq 0:6c56fb4bc5f0 6652
nexpaq 0:6c56fb4bc5f0 6653 /* If a non-handshake record was read during renego, fallthrough,
nexpaq 0:6c56fb4bc5f0 6654 * else tell the user they should call mbedtls_ssl_read() again */
nexpaq 0:6c56fb4bc5f0 6655 if( ! record_read )
nexpaq 0:6c56fb4bc5f0 6656 return( MBEDTLS_ERR_SSL_WANT_READ );
nexpaq 0:6c56fb4bc5f0 6657 }
nexpaq 0:6c56fb4bc5f0 6658 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
nexpaq 0:6c56fb4bc5f0 6659 {
nexpaq 0:6c56fb4bc5f0 6660
nexpaq 0:6c56fb4bc5f0 6661 if( ssl->conf->renego_max_records >= 0 )
nexpaq 0:6c56fb4bc5f0 6662 {
nexpaq 0:6c56fb4bc5f0 6663 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
nexpaq 0:6c56fb4bc5f0 6664 {
nexpaq 0:6c56fb4bc5f0 6665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
nexpaq 0:6c56fb4bc5f0 6666 "but not honored by client" ) );
nexpaq 0:6c56fb4bc5f0 6667 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
nexpaq 0:6c56fb4bc5f0 6668 }
nexpaq 0:6c56fb4bc5f0 6669 }
nexpaq 0:6c56fb4bc5f0 6670 }
nexpaq 0:6c56fb4bc5f0 6671 #endif /* MBEDTLS_SSL_RENEGOTIATION */
nexpaq 0:6c56fb4bc5f0 6672
nexpaq 0:6c56fb4bc5f0 6673 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
nexpaq 0:6c56fb4bc5f0 6674 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
nexpaq 0:6c56fb4bc5f0 6675 {
nexpaq 0:6c56fb4bc5f0 6676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
nexpaq 0:6c56fb4bc5f0 6677 return( MBEDTLS_ERR_SSL_WANT_READ );
nexpaq 0:6c56fb4bc5f0 6678 }
nexpaq 0:6c56fb4bc5f0 6679
nexpaq 0:6c56fb4bc5f0 6680 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
nexpaq 0:6c56fb4bc5f0 6681 {
nexpaq 0:6c56fb4bc5f0 6682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
nexpaq 0:6c56fb4bc5f0 6683 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
nexpaq 0:6c56fb4bc5f0 6684 }
nexpaq 0:6c56fb4bc5f0 6685
nexpaq 0:6c56fb4bc5f0 6686 ssl->in_offt = ssl->in_msg;
nexpaq 0:6c56fb4bc5f0 6687
nexpaq 0:6c56fb4bc5f0 6688 /* We're going to return something now, cancel timer,
nexpaq 0:6c56fb4bc5f0 6689 * except if handshake (renegotiation) is in progress */
nexpaq 0:6c56fb4bc5f0 6690 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
nexpaq 0:6c56fb4bc5f0 6691 ssl_set_timer( ssl, 0 );
nexpaq 0:6c56fb4bc5f0 6692
nexpaq 0:6c56fb4bc5f0 6693 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 6694 /* If we requested renego but received AppData, resend HelloRequest.
nexpaq 0:6c56fb4bc5f0 6695 * Do it now, after setting in_offt, to avoid taking this branch
nexpaq 0:6c56fb4bc5f0 6696 * again if ssl_write_hello_request() returns WANT_WRITE */
nexpaq 0:6c56fb4bc5f0 6697 #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
nexpaq 0:6c56fb4bc5f0 6698 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
nexpaq 0:6c56fb4bc5f0 6699 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
nexpaq 0:6c56fb4bc5f0 6700 {
nexpaq 0:6c56fb4bc5f0 6701 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 6702 {
nexpaq 0:6c56fb4bc5f0 6703 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
nexpaq 0:6c56fb4bc5f0 6704 return( ret );
nexpaq 0:6c56fb4bc5f0 6705 }
nexpaq 0:6c56fb4bc5f0 6706 }
nexpaq 0:6c56fb4bc5f0 6707 #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
nexpaq 0:6c56fb4bc5f0 6708 #endif
nexpaq 0:6c56fb4bc5f0 6709 }
nexpaq 0:6c56fb4bc5f0 6710
nexpaq 0:6c56fb4bc5f0 6711 n = ( len < ssl->in_msglen )
nexpaq 0:6c56fb4bc5f0 6712 ? len : ssl->in_msglen;
nexpaq 0:6c56fb4bc5f0 6713
nexpaq 0:6c56fb4bc5f0 6714 memcpy( buf, ssl->in_offt, n );
nexpaq 0:6c56fb4bc5f0 6715 ssl->in_msglen -= n;
nexpaq 0:6c56fb4bc5f0 6716
nexpaq 0:6c56fb4bc5f0 6717 if( ssl->in_msglen == 0 )
nexpaq 0:6c56fb4bc5f0 6718 /* all bytes consumed */
nexpaq 0:6c56fb4bc5f0 6719 ssl->in_offt = NULL;
nexpaq 0:6c56fb4bc5f0 6720 else
nexpaq 0:6c56fb4bc5f0 6721 /* more data available */
nexpaq 0:6c56fb4bc5f0 6722 ssl->in_offt += n;
nexpaq 0:6c56fb4bc5f0 6723
nexpaq 0:6c56fb4bc5f0 6724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
nexpaq 0:6c56fb4bc5f0 6725
nexpaq 0:6c56fb4bc5f0 6726 return( (int) n );
nexpaq 0:6c56fb4bc5f0 6727 }
nexpaq 0:6c56fb4bc5f0 6728
nexpaq 0:6c56fb4bc5f0 6729 /*
nexpaq 0:6c56fb4bc5f0 6730 * Send application data to be encrypted by the SSL layer,
nexpaq 0:6c56fb4bc5f0 6731 * taking care of max fragment length and buffer size
nexpaq 0:6c56fb4bc5f0 6732 */
nexpaq 0:6c56fb4bc5f0 6733 static int ssl_write_real( mbedtls_ssl_context *ssl,
nexpaq 0:6c56fb4bc5f0 6734 const unsigned char *buf, size_t len )
nexpaq 0:6c56fb4bc5f0 6735 {
nexpaq 0:6c56fb4bc5f0 6736 int ret;
nexpaq 0:6c56fb4bc5f0 6737 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
nexpaq 0:6c56fb4bc5f0 6738 size_t max_len = mbedtls_ssl_get_max_frag_len( ssl );
nexpaq 0:6c56fb4bc5f0 6739
nexpaq 0:6c56fb4bc5f0 6740 if( len > max_len )
nexpaq 0:6c56fb4bc5f0 6741 {
nexpaq 0:6c56fb4bc5f0 6742 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 6743 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
nexpaq 0:6c56fb4bc5f0 6744 {
nexpaq 0:6c56fb4bc5f0 6745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
nexpaq 0:6c56fb4bc5f0 6746 "maximum fragment length: %d > %d",
nexpaq 0:6c56fb4bc5f0 6747 len, max_len ) );
nexpaq 0:6c56fb4bc5f0 6748 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 6749 }
nexpaq 0:6c56fb4bc5f0 6750 else
nexpaq 0:6c56fb4bc5f0 6751 #endif
nexpaq 0:6c56fb4bc5f0 6752 len = max_len;
nexpaq 0:6c56fb4bc5f0 6753 }
nexpaq 0:6c56fb4bc5f0 6754 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
nexpaq 0:6c56fb4bc5f0 6755
nexpaq 0:6c56fb4bc5f0 6756 if( ssl->out_left != 0 )
nexpaq 0:6c56fb4bc5f0 6757 {
nexpaq 0:6c56fb4bc5f0 6758 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 6759 {
nexpaq 0:6c56fb4bc5f0 6760 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
nexpaq 0:6c56fb4bc5f0 6761 return( ret );
nexpaq 0:6c56fb4bc5f0 6762 }
nexpaq 0:6c56fb4bc5f0 6763 }
nexpaq 0:6c56fb4bc5f0 6764 else
nexpaq 0:6c56fb4bc5f0 6765 {
nexpaq 0:6c56fb4bc5f0 6766 ssl->out_msglen = len;
nexpaq 0:6c56fb4bc5f0 6767 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
nexpaq 0:6c56fb4bc5f0 6768 memcpy( ssl->out_msg, buf, len );
nexpaq 0:6c56fb4bc5f0 6769
nexpaq 0:6c56fb4bc5f0 6770 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 6771 {
nexpaq 0:6c56fb4bc5f0 6772 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
nexpaq 0:6c56fb4bc5f0 6773 return( ret );
nexpaq 0:6c56fb4bc5f0 6774 }
nexpaq 0:6c56fb4bc5f0 6775 }
nexpaq 0:6c56fb4bc5f0 6776
nexpaq 0:6c56fb4bc5f0 6777 return( (int) len );
nexpaq 0:6c56fb4bc5f0 6778 }
nexpaq 0:6c56fb4bc5f0 6779
nexpaq 0:6c56fb4bc5f0 6780 /*
nexpaq 0:6c56fb4bc5f0 6781 * Write application data, doing 1/n-1 splitting if necessary.
nexpaq 0:6c56fb4bc5f0 6782 *
nexpaq 0:6c56fb4bc5f0 6783 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
nexpaq 0:6c56fb4bc5f0 6784 * then the caller will call us again with the same arguments, so
nexpaq 0:6c56fb4bc5f0 6785 * remember wether we already did the split or not.
nexpaq 0:6c56fb4bc5f0 6786 */
nexpaq 0:6c56fb4bc5f0 6787 #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
nexpaq 0:6c56fb4bc5f0 6788 static int ssl_write_split( mbedtls_ssl_context *ssl,
nexpaq 0:6c56fb4bc5f0 6789 const unsigned char *buf, size_t len )
nexpaq 0:6c56fb4bc5f0 6790 {
nexpaq 0:6c56fb4bc5f0 6791 int ret;
nexpaq 0:6c56fb4bc5f0 6792
nexpaq 0:6c56fb4bc5f0 6793 if( ssl->conf->cbc_record_splitting ==
nexpaq 0:6c56fb4bc5f0 6794 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
nexpaq 0:6c56fb4bc5f0 6795 len <= 1 ||
nexpaq 0:6c56fb4bc5f0 6796 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
nexpaq 0:6c56fb4bc5f0 6797 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
nexpaq 0:6c56fb4bc5f0 6798 != MBEDTLS_MODE_CBC )
nexpaq 0:6c56fb4bc5f0 6799 {
nexpaq 0:6c56fb4bc5f0 6800 return( ssl_write_real( ssl, buf, len ) );
nexpaq 0:6c56fb4bc5f0 6801 }
nexpaq 0:6c56fb4bc5f0 6802
nexpaq 0:6c56fb4bc5f0 6803 if( ssl->split_done == 0 )
nexpaq 0:6c56fb4bc5f0 6804 {
nexpaq 0:6c56fb4bc5f0 6805 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
nexpaq 0:6c56fb4bc5f0 6806 return( ret );
nexpaq 0:6c56fb4bc5f0 6807 ssl->split_done = 1;
nexpaq 0:6c56fb4bc5f0 6808 }
nexpaq 0:6c56fb4bc5f0 6809
nexpaq 0:6c56fb4bc5f0 6810 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
nexpaq 0:6c56fb4bc5f0 6811 return( ret );
nexpaq 0:6c56fb4bc5f0 6812 ssl->split_done = 0;
nexpaq 0:6c56fb4bc5f0 6813
nexpaq 0:6c56fb4bc5f0 6814 return( ret + 1 );
nexpaq 0:6c56fb4bc5f0 6815 }
nexpaq 0:6c56fb4bc5f0 6816 #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
nexpaq 0:6c56fb4bc5f0 6817
nexpaq 0:6c56fb4bc5f0 6818 /*
nexpaq 0:6c56fb4bc5f0 6819 * Write application data (public-facing wrapper)
nexpaq 0:6c56fb4bc5f0 6820 */
nexpaq 0:6c56fb4bc5f0 6821 int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
nexpaq 0:6c56fb4bc5f0 6822 {
nexpaq 0:6c56fb4bc5f0 6823 int ret;
nexpaq 0:6c56fb4bc5f0 6824
nexpaq 0:6c56fb4bc5f0 6825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
nexpaq 0:6c56fb4bc5f0 6826
nexpaq 0:6c56fb4bc5f0 6827 if( ssl == NULL || ssl->conf == NULL )
nexpaq 0:6c56fb4bc5f0 6828 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 6829
nexpaq 0:6c56fb4bc5f0 6830 #if defined(MBEDTLS_SSL_RENEGOTIATION)
nexpaq 0:6c56fb4bc5f0 6831 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 6832 {
nexpaq 0:6c56fb4bc5f0 6833 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
nexpaq 0:6c56fb4bc5f0 6834 return( ret );
nexpaq 0:6c56fb4bc5f0 6835 }
nexpaq 0:6c56fb4bc5f0 6836 #endif
nexpaq 0:6c56fb4bc5f0 6837
nexpaq 0:6c56fb4bc5f0 6838 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
nexpaq 0:6c56fb4bc5f0 6839 {
nexpaq 0:6c56fb4bc5f0 6840 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 6841 {
nexpaq 0:6c56fb4bc5f0 6842 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
nexpaq 0:6c56fb4bc5f0 6843 return( ret );
nexpaq 0:6c56fb4bc5f0 6844 }
nexpaq 0:6c56fb4bc5f0 6845 }
nexpaq 0:6c56fb4bc5f0 6846
nexpaq 0:6c56fb4bc5f0 6847 #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
nexpaq 0:6c56fb4bc5f0 6848 ret = ssl_write_split( ssl, buf, len );
nexpaq 0:6c56fb4bc5f0 6849 #else
nexpaq 0:6c56fb4bc5f0 6850 ret = ssl_write_real( ssl, buf, len );
nexpaq 0:6c56fb4bc5f0 6851 #endif
nexpaq 0:6c56fb4bc5f0 6852
nexpaq 0:6c56fb4bc5f0 6853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
nexpaq 0:6c56fb4bc5f0 6854
nexpaq 0:6c56fb4bc5f0 6855 return( ret );
nexpaq 0:6c56fb4bc5f0 6856 }
nexpaq 0:6c56fb4bc5f0 6857
nexpaq 0:6c56fb4bc5f0 6858 /*
nexpaq 0:6c56fb4bc5f0 6859 * Notify the peer that the connection is being closed
nexpaq 0:6c56fb4bc5f0 6860 */
nexpaq 0:6c56fb4bc5f0 6861 int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 6862 {
nexpaq 0:6c56fb4bc5f0 6863 int ret;
nexpaq 0:6c56fb4bc5f0 6864
nexpaq 0:6c56fb4bc5f0 6865 if( ssl == NULL || ssl->conf == NULL )
nexpaq 0:6c56fb4bc5f0 6866 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 6867
nexpaq 0:6c56fb4bc5f0 6868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
nexpaq 0:6c56fb4bc5f0 6869
nexpaq 0:6c56fb4bc5f0 6870 if( ssl->out_left != 0 )
nexpaq 0:6c56fb4bc5f0 6871 return( mbedtls_ssl_flush_output( ssl ) );
nexpaq 0:6c56fb4bc5f0 6872
nexpaq 0:6c56fb4bc5f0 6873 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
nexpaq 0:6c56fb4bc5f0 6874 {
nexpaq 0:6c56fb4bc5f0 6875 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
nexpaq 0:6c56fb4bc5f0 6876 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
nexpaq 0:6c56fb4bc5f0 6877 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 6878 {
nexpaq 0:6c56fb4bc5f0 6879 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
nexpaq 0:6c56fb4bc5f0 6880 return( ret );
nexpaq 0:6c56fb4bc5f0 6881 }
nexpaq 0:6c56fb4bc5f0 6882 }
nexpaq 0:6c56fb4bc5f0 6883
nexpaq 0:6c56fb4bc5f0 6884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
nexpaq 0:6c56fb4bc5f0 6885
nexpaq 0:6c56fb4bc5f0 6886 return( 0 );
nexpaq 0:6c56fb4bc5f0 6887 }
nexpaq 0:6c56fb4bc5f0 6888
nexpaq 0:6c56fb4bc5f0 6889 void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
nexpaq 0:6c56fb4bc5f0 6890 {
nexpaq 0:6c56fb4bc5f0 6891 if( transform == NULL )
nexpaq 0:6c56fb4bc5f0 6892 return;
nexpaq 0:6c56fb4bc5f0 6893
nexpaq 0:6c56fb4bc5f0 6894 #if defined(MBEDTLS_ZLIB_SUPPORT)
nexpaq 0:6c56fb4bc5f0 6895 deflateEnd( &transform->ctx_deflate );
nexpaq 0:6c56fb4bc5f0 6896 inflateEnd( &transform->ctx_inflate );
nexpaq 0:6c56fb4bc5f0 6897 #endif
nexpaq 0:6c56fb4bc5f0 6898
nexpaq 0:6c56fb4bc5f0 6899 mbedtls_cipher_free( &transform->cipher_ctx_enc );
nexpaq 0:6c56fb4bc5f0 6900 mbedtls_cipher_free( &transform->cipher_ctx_dec );
nexpaq 0:6c56fb4bc5f0 6901
nexpaq 0:6c56fb4bc5f0 6902 mbedtls_md_free( &transform->md_ctx_enc );
nexpaq 0:6c56fb4bc5f0 6903 mbedtls_md_free( &transform->md_ctx_dec );
nexpaq 0:6c56fb4bc5f0 6904
nexpaq 0:6c56fb4bc5f0 6905 mbedtls_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
nexpaq 0:6c56fb4bc5f0 6906 }
nexpaq 0:6c56fb4bc5f0 6907
nexpaq 0:6c56fb4bc5f0 6908 #if defined(MBEDTLS_X509_CRT_PARSE_C)
nexpaq 0:6c56fb4bc5f0 6909 static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
nexpaq 0:6c56fb4bc5f0 6910 {
nexpaq 0:6c56fb4bc5f0 6911 mbedtls_ssl_key_cert *cur = key_cert, *next;
nexpaq 0:6c56fb4bc5f0 6912
nexpaq 0:6c56fb4bc5f0 6913 while( cur != NULL )
nexpaq 0:6c56fb4bc5f0 6914 {
nexpaq 0:6c56fb4bc5f0 6915 next = cur->next;
nexpaq 0:6c56fb4bc5f0 6916 mbedtls_free( cur );
nexpaq 0:6c56fb4bc5f0 6917 cur = next;
nexpaq 0:6c56fb4bc5f0 6918 }
nexpaq 0:6c56fb4bc5f0 6919 }
nexpaq 0:6c56fb4bc5f0 6920 #endif /* MBEDTLS_X509_CRT_PARSE_C */
nexpaq 0:6c56fb4bc5f0 6921
nexpaq 0:6c56fb4bc5f0 6922 void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake )
nexpaq 0:6c56fb4bc5f0 6923 {
nexpaq 0:6c56fb4bc5f0 6924 if( handshake == NULL )
nexpaq 0:6c56fb4bc5f0 6925 return;
nexpaq 0:6c56fb4bc5f0 6926
nexpaq 0:6c56fb4bc5f0 6927 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
nexpaq 0:6c56fb4bc5f0 6928 defined(MBEDTLS_SSL_PROTO_TLS1_1)
nexpaq 0:6c56fb4bc5f0 6929 mbedtls_md5_free( &handshake->fin_md5 );
nexpaq 0:6c56fb4bc5f0 6930 mbedtls_sha1_free( &handshake->fin_sha1 );
nexpaq 0:6c56fb4bc5f0 6931 #endif
nexpaq 0:6c56fb4bc5f0 6932 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
nexpaq 0:6c56fb4bc5f0 6933 #if defined(MBEDTLS_SHA256_C)
nexpaq 0:6c56fb4bc5f0 6934 mbedtls_sha256_free( &handshake->fin_sha256 );
nexpaq 0:6c56fb4bc5f0 6935 #endif
nexpaq 0:6c56fb4bc5f0 6936 #if defined(MBEDTLS_SHA512_C)
nexpaq 0:6c56fb4bc5f0 6937 mbedtls_sha512_free( &handshake->fin_sha512 );
nexpaq 0:6c56fb4bc5f0 6938 #endif
nexpaq 0:6c56fb4bc5f0 6939 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
nexpaq 0:6c56fb4bc5f0 6940
nexpaq 0:6c56fb4bc5f0 6941 #if defined(MBEDTLS_DHM_C)
nexpaq 0:6c56fb4bc5f0 6942 mbedtls_dhm_free( &handshake->dhm_ctx );
nexpaq 0:6c56fb4bc5f0 6943 #endif
nexpaq 0:6c56fb4bc5f0 6944 #if defined(MBEDTLS_ECDH_C)
nexpaq 0:6c56fb4bc5f0 6945 mbedtls_ecdh_free( &handshake->ecdh_ctx );
nexpaq 0:6c56fb4bc5f0 6946 #endif
nexpaq 0:6c56fb4bc5f0 6947 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
nexpaq 0:6c56fb4bc5f0 6948 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
nexpaq 0:6c56fb4bc5f0 6949 #if defined(MBEDTLS_SSL_CLI_C)
nexpaq 0:6c56fb4bc5f0 6950 mbedtls_free( handshake->ecjpake_cache );
nexpaq 0:6c56fb4bc5f0 6951 handshake->ecjpake_cache = NULL;
nexpaq 0:6c56fb4bc5f0 6952 handshake->ecjpake_cache_len = 0;
nexpaq 0:6c56fb4bc5f0 6953 #endif
nexpaq 0:6c56fb4bc5f0 6954 #endif
nexpaq 0:6c56fb4bc5f0 6955
nexpaq 0:6c56fb4bc5f0 6956 #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
nexpaq 0:6c56fb4bc5f0 6957 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
nexpaq 0:6c56fb4bc5f0 6958 /* explicit void pointer cast for buggy MS compiler */
nexpaq 0:6c56fb4bc5f0 6959 mbedtls_free( (void *) handshake->curves );
nexpaq 0:6c56fb4bc5f0 6960 #endif
nexpaq 0:6c56fb4bc5f0 6961
nexpaq 0:6c56fb4bc5f0 6962 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
nexpaq 0:6c56fb4bc5f0 6963 if( handshake->psk != NULL )
nexpaq 0:6c56fb4bc5f0 6964 {
nexpaq 0:6c56fb4bc5f0 6965 mbedtls_zeroize( handshake->psk, handshake->psk_len );
nexpaq 0:6c56fb4bc5f0 6966 mbedtls_free( handshake->psk );
nexpaq 0:6c56fb4bc5f0 6967 }
nexpaq 0:6c56fb4bc5f0 6968 #endif
nexpaq 0:6c56fb4bc5f0 6969
nexpaq 0:6c56fb4bc5f0 6970 #if defined(MBEDTLS_X509_CRT_PARSE_C) && \
nexpaq 0:6c56fb4bc5f0 6971 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
nexpaq 0:6c56fb4bc5f0 6972 /*
nexpaq 0:6c56fb4bc5f0 6973 * Free only the linked list wrapper, not the keys themselves
nexpaq 0:6c56fb4bc5f0 6974 * since the belong to the SNI callback
nexpaq 0:6c56fb4bc5f0 6975 */
nexpaq 0:6c56fb4bc5f0 6976 if( handshake->sni_key_cert != NULL )
nexpaq 0:6c56fb4bc5f0 6977 {
nexpaq 0:6c56fb4bc5f0 6978 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
nexpaq 0:6c56fb4bc5f0 6979
nexpaq 0:6c56fb4bc5f0 6980 while( cur != NULL )
nexpaq 0:6c56fb4bc5f0 6981 {
nexpaq 0:6c56fb4bc5f0 6982 next = cur->next;
nexpaq 0:6c56fb4bc5f0 6983 mbedtls_free( cur );
nexpaq 0:6c56fb4bc5f0 6984 cur = next;
nexpaq 0:6c56fb4bc5f0 6985 }
nexpaq 0:6c56fb4bc5f0 6986 }
nexpaq 0:6c56fb4bc5f0 6987 #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
nexpaq 0:6c56fb4bc5f0 6988
nexpaq 0:6c56fb4bc5f0 6989 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 6990 mbedtls_free( handshake->verify_cookie );
nexpaq 0:6c56fb4bc5f0 6991 mbedtls_free( handshake->hs_msg );
nexpaq 0:6c56fb4bc5f0 6992 ssl_flight_free( handshake->flight );
nexpaq 0:6c56fb4bc5f0 6993 #endif
nexpaq 0:6c56fb4bc5f0 6994
nexpaq 0:6c56fb4bc5f0 6995 mbedtls_zeroize( handshake, sizeof( mbedtls_ssl_handshake_params ) );
nexpaq 0:6c56fb4bc5f0 6996 }
nexpaq 0:6c56fb4bc5f0 6997
nexpaq 0:6c56fb4bc5f0 6998 void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
nexpaq 0:6c56fb4bc5f0 6999 {
nexpaq 0:6c56fb4bc5f0 7000 if( session == NULL )
nexpaq 0:6c56fb4bc5f0 7001 return;
nexpaq 0:6c56fb4bc5f0 7002
nexpaq 0:6c56fb4bc5f0 7003 #if defined(MBEDTLS_X509_CRT_PARSE_C)
nexpaq 0:6c56fb4bc5f0 7004 if( session->peer_cert != NULL )
nexpaq 0:6c56fb4bc5f0 7005 {
nexpaq 0:6c56fb4bc5f0 7006 mbedtls_x509_crt_free( session->peer_cert );
nexpaq 0:6c56fb4bc5f0 7007 mbedtls_free( session->peer_cert );
nexpaq 0:6c56fb4bc5f0 7008 }
nexpaq 0:6c56fb4bc5f0 7009 #endif
nexpaq 0:6c56fb4bc5f0 7010
nexpaq 0:6c56fb4bc5f0 7011 #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
nexpaq 0:6c56fb4bc5f0 7012 mbedtls_free( session->ticket );
nexpaq 0:6c56fb4bc5f0 7013 #endif
nexpaq 0:6c56fb4bc5f0 7014
nexpaq 0:6c56fb4bc5f0 7015 mbedtls_zeroize( session, sizeof( mbedtls_ssl_session ) );
nexpaq 0:6c56fb4bc5f0 7016 }
nexpaq 0:6c56fb4bc5f0 7017
nexpaq 0:6c56fb4bc5f0 7018 /*
nexpaq 0:6c56fb4bc5f0 7019 * Free an SSL context
nexpaq 0:6c56fb4bc5f0 7020 */
nexpaq 0:6c56fb4bc5f0 7021 void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
nexpaq 0:6c56fb4bc5f0 7022 {
nexpaq 0:6c56fb4bc5f0 7023 if( ssl == NULL )
nexpaq 0:6c56fb4bc5f0 7024 return;
nexpaq 0:6c56fb4bc5f0 7025
nexpaq 0:6c56fb4bc5f0 7026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
nexpaq 0:6c56fb4bc5f0 7027
nexpaq 0:6c56fb4bc5f0 7028 if( ssl->out_buf != NULL )
nexpaq 0:6c56fb4bc5f0 7029 {
nexpaq 0:6c56fb4bc5f0 7030 mbedtls_zeroize( ssl->out_buf, MBEDTLS_SSL_BUFFER_LEN );
nexpaq 0:6c56fb4bc5f0 7031 mbedtls_free( ssl->out_buf );
nexpaq 0:6c56fb4bc5f0 7032 }
nexpaq 0:6c56fb4bc5f0 7033
nexpaq 0:6c56fb4bc5f0 7034 if( ssl->in_buf != NULL )
nexpaq 0:6c56fb4bc5f0 7035 {
nexpaq 0:6c56fb4bc5f0 7036 mbedtls_zeroize( ssl->in_buf, MBEDTLS_SSL_BUFFER_LEN );
nexpaq 0:6c56fb4bc5f0 7037 mbedtls_free( ssl->in_buf );
nexpaq 0:6c56fb4bc5f0 7038 }
nexpaq 0:6c56fb4bc5f0 7039
nexpaq 0:6c56fb4bc5f0 7040 #if defined(MBEDTLS_ZLIB_SUPPORT)
nexpaq 0:6c56fb4bc5f0 7041 if( ssl->compress_buf != NULL )
nexpaq 0:6c56fb4bc5f0 7042 {
nexpaq 0:6c56fb4bc5f0 7043 mbedtls_zeroize( ssl->compress_buf, MBEDTLS_SSL_BUFFER_LEN );
nexpaq 0:6c56fb4bc5f0 7044 mbedtls_free( ssl->compress_buf );
nexpaq 0:6c56fb4bc5f0 7045 }
nexpaq 0:6c56fb4bc5f0 7046 #endif
nexpaq 0:6c56fb4bc5f0 7047
nexpaq 0:6c56fb4bc5f0 7048 if( ssl->transform )
nexpaq 0:6c56fb4bc5f0 7049 {
nexpaq 0:6c56fb4bc5f0 7050 mbedtls_ssl_transform_free( ssl->transform );
nexpaq 0:6c56fb4bc5f0 7051 mbedtls_free( ssl->transform );
nexpaq 0:6c56fb4bc5f0 7052 }
nexpaq 0:6c56fb4bc5f0 7053
nexpaq 0:6c56fb4bc5f0 7054 if( ssl->handshake )
nexpaq 0:6c56fb4bc5f0 7055 {
nexpaq 0:6c56fb4bc5f0 7056 mbedtls_ssl_handshake_free( ssl->handshake );
nexpaq 0:6c56fb4bc5f0 7057 mbedtls_ssl_transform_free( ssl->transform_negotiate );
nexpaq 0:6c56fb4bc5f0 7058 mbedtls_ssl_session_free( ssl->session_negotiate );
nexpaq 0:6c56fb4bc5f0 7059
nexpaq 0:6c56fb4bc5f0 7060 mbedtls_free( ssl->handshake );
nexpaq 0:6c56fb4bc5f0 7061 mbedtls_free( ssl->transform_negotiate );
nexpaq 0:6c56fb4bc5f0 7062 mbedtls_free( ssl->session_negotiate );
nexpaq 0:6c56fb4bc5f0 7063 }
nexpaq 0:6c56fb4bc5f0 7064
nexpaq 0:6c56fb4bc5f0 7065 if( ssl->session )
nexpaq 0:6c56fb4bc5f0 7066 {
nexpaq 0:6c56fb4bc5f0 7067 mbedtls_ssl_session_free( ssl->session );
nexpaq 0:6c56fb4bc5f0 7068 mbedtls_free( ssl->session );
nexpaq 0:6c56fb4bc5f0 7069 }
nexpaq 0:6c56fb4bc5f0 7070
nexpaq 0:6c56fb4bc5f0 7071 #if defined(MBEDTLS_X509_CRT_PARSE_C)
nexpaq 0:6c56fb4bc5f0 7072 if( ssl->hostname != NULL )
nexpaq 0:6c56fb4bc5f0 7073 {
nexpaq 0:6c56fb4bc5f0 7074 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
nexpaq 0:6c56fb4bc5f0 7075 mbedtls_free( ssl->hostname );
nexpaq 0:6c56fb4bc5f0 7076 }
nexpaq 0:6c56fb4bc5f0 7077 #endif
nexpaq 0:6c56fb4bc5f0 7078
nexpaq 0:6c56fb4bc5f0 7079 #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
nexpaq 0:6c56fb4bc5f0 7080 if( mbedtls_ssl_hw_record_finish != NULL )
nexpaq 0:6c56fb4bc5f0 7081 {
nexpaq 0:6c56fb4bc5f0 7082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
nexpaq 0:6c56fb4bc5f0 7083 mbedtls_ssl_hw_record_finish( ssl );
nexpaq 0:6c56fb4bc5f0 7084 }
nexpaq 0:6c56fb4bc5f0 7085 #endif
nexpaq 0:6c56fb4bc5f0 7086
nexpaq 0:6c56fb4bc5f0 7087 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
nexpaq 0:6c56fb4bc5f0 7088 mbedtls_free( ssl->cli_id );
nexpaq 0:6c56fb4bc5f0 7089 #endif
nexpaq 0:6c56fb4bc5f0 7090
nexpaq 0:6c56fb4bc5f0 7091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
nexpaq 0:6c56fb4bc5f0 7092
nexpaq 0:6c56fb4bc5f0 7093 /* Actually clear after last debug message */
nexpaq 0:6c56fb4bc5f0 7094 mbedtls_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
nexpaq 0:6c56fb4bc5f0 7095 }
nexpaq 0:6c56fb4bc5f0 7096
nexpaq 0:6c56fb4bc5f0 7097 /*
nexpaq 0:6c56fb4bc5f0 7098 * Initialze mbedtls_ssl_config
nexpaq 0:6c56fb4bc5f0 7099 */
nexpaq 0:6c56fb4bc5f0 7100 void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
nexpaq 0:6c56fb4bc5f0 7101 {
nexpaq 0:6c56fb4bc5f0 7102 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
nexpaq 0:6c56fb4bc5f0 7103 }
nexpaq 0:6c56fb4bc5f0 7104
nexpaq 0:6c56fb4bc5f0 7105 #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
nexpaq 0:6c56fb4bc5f0 7106 static int ssl_preset_default_hashes[] = {
nexpaq 0:6c56fb4bc5f0 7107 #if defined(MBEDTLS_SHA512_C)
nexpaq 0:6c56fb4bc5f0 7108 MBEDTLS_MD_SHA512,
nexpaq 0:6c56fb4bc5f0 7109 MBEDTLS_MD_SHA384,
nexpaq 0:6c56fb4bc5f0 7110 #endif
nexpaq 0:6c56fb4bc5f0 7111 #if defined(MBEDTLS_SHA256_C)
nexpaq 0:6c56fb4bc5f0 7112 MBEDTLS_MD_SHA256,
nexpaq 0:6c56fb4bc5f0 7113 MBEDTLS_MD_SHA224,
nexpaq 0:6c56fb4bc5f0 7114 #endif
nexpaq 0:6c56fb4bc5f0 7115 #if defined(MBEDTLS_SHA1_C)
nexpaq 0:6c56fb4bc5f0 7116 MBEDTLS_MD_SHA1,
nexpaq 0:6c56fb4bc5f0 7117 #endif
nexpaq 0:6c56fb4bc5f0 7118 MBEDTLS_MD_NONE
nexpaq 0:6c56fb4bc5f0 7119 };
nexpaq 0:6c56fb4bc5f0 7120 #endif
nexpaq 0:6c56fb4bc5f0 7121
nexpaq 0:6c56fb4bc5f0 7122 static int ssl_preset_suiteb_ciphersuites[] = {
nexpaq 0:6c56fb4bc5f0 7123 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
nexpaq 0:6c56fb4bc5f0 7124 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
nexpaq 0:6c56fb4bc5f0 7125 0
nexpaq 0:6c56fb4bc5f0 7126 };
nexpaq 0:6c56fb4bc5f0 7127
nexpaq 0:6c56fb4bc5f0 7128 #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
nexpaq 0:6c56fb4bc5f0 7129 static int ssl_preset_suiteb_hashes[] = {
nexpaq 0:6c56fb4bc5f0 7130 MBEDTLS_MD_SHA256,
nexpaq 0:6c56fb4bc5f0 7131 MBEDTLS_MD_SHA384,
nexpaq 0:6c56fb4bc5f0 7132 MBEDTLS_MD_NONE
nexpaq 0:6c56fb4bc5f0 7133 };
nexpaq 0:6c56fb4bc5f0 7134 #endif
nexpaq 0:6c56fb4bc5f0 7135
nexpaq 0:6c56fb4bc5f0 7136 #if defined(MBEDTLS_ECP_C)
nexpaq 0:6c56fb4bc5f0 7137 static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
nexpaq 0:6c56fb4bc5f0 7138 MBEDTLS_ECP_DP_SECP256R1,
nexpaq 0:6c56fb4bc5f0 7139 MBEDTLS_ECP_DP_SECP384R1,
nexpaq 0:6c56fb4bc5f0 7140 MBEDTLS_ECP_DP_NONE
nexpaq 0:6c56fb4bc5f0 7141 };
nexpaq 0:6c56fb4bc5f0 7142 #endif
nexpaq 0:6c56fb4bc5f0 7143
nexpaq 0:6c56fb4bc5f0 7144 /*
nexpaq 0:6c56fb4bc5f0 7145 * Load default in mbedtls_ssl_config
nexpaq 0:6c56fb4bc5f0 7146 */
nexpaq 0:6c56fb4bc5f0 7147 int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
nexpaq 0:6c56fb4bc5f0 7148 int endpoint, int transport, int preset )
nexpaq 0:6c56fb4bc5f0 7149 {
nexpaq 0:6c56fb4bc5f0 7150 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
nexpaq 0:6c56fb4bc5f0 7151 int ret;
nexpaq 0:6c56fb4bc5f0 7152 #endif
nexpaq 0:6c56fb4bc5f0 7153
nexpaq 0:6c56fb4bc5f0 7154 /* Use the functions here so that they are covered in tests,
nexpaq 0:6c56fb4bc5f0 7155 * but otherwise access member directly for efficiency */
nexpaq 0:6c56fb4bc5f0 7156 mbedtls_ssl_conf_endpoint( conf, endpoint );
nexpaq 0:6c56fb4bc5f0 7157 mbedtls_ssl_conf_transport( conf, transport );
nexpaq 0:6c56fb4bc5f0 7158
nexpaq 0:6c56fb4bc5f0 7159 /*
nexpaq 0:6c56fb4bc5f0 7160 * Things that are common to all presets
nexpaq 0:6c56fb4bc5f0 7161 */
nexpaq 0:6c56fb4bc5f0 7162 #if defined(MBEDTLS_SSL_CLI_C)
nexpaq 0:6c56fb4bc5f0 7163 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
nexpaq 0:6c56fb4bc5f0 7164 {
nexpaq 0:6c56fb4bc5f0 7165 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
nexpaq 0:6c56fb4bc5f0 7166 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
nexpaq 0:6c56fb4bc5f0 7167 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
nexpaq 0:6c56fb4bc5f0 7168 #endif
nexpaq 0:6c56fb4bc5f0 7169 }
nexpaq 0:6c56fb4bc5f0 7170 #endif
nexpaq 0:6c56fb4bc5f0 7171
nexpaq 0:6c56fb4bc5f0 7172 #if defined(MBEDTLS_ARC4_C)
nexpaq 0:6c56fb4bc5f0 7173 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
nexpaq 0:6c56fb4bc5f0 7174 #endif
nexpaq 0:6c56fb4bc5f0 7175
nexpaq 0:6c56fb4bc5f0 7176 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
nexpaq 0:6c56fb4bc5f0 7177 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
nexpaq 0:6c56fb4bc5f0 7178 #endif
nexpaq 0:6c56fb4bc5f0 7179
nexpaq 0:6c56fb4bc5f0 7180 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
nexpaq 0:6c56fb4bc5f0 7181 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
nexpaq 0:6c56fb4bc5f0 7182 #endif
nexpaq 0:6c56fb4bc5f0 7183
nexpaq 0:6c56fb4bc5f0 7184 #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
nexpaq 0:6c56fb4bc5f0 7185 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
nexpaq 0:6c56fb4bc5f0 7186 #endif
nexpaq 0:6c56fb4bc5f0 7187
nexpaq 0:6c56fb4bc5f0 7188 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
nexpaq 0:6c56fb4bc5f0 7189 conf->f_cookie_write = ssl_cookie_write_dummy;
nexpaq 0:6c56fb4bc5f0 7190 conf->f_cookie_check = ssl_cookie_check_dummy;
nexpaq 0:6c56fb4bc5f0 7191 #endif
nexpaq 0:6c56fb4bc5f0 7192
nexpaq 0:6c56fb4bc5f0 7193 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
nexpaq 0:6c56fb4bc5f0 7194 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
nexpaq 0:6c56fb4bc5f0 7195 #endif
nexpaq 0:6c56fb4bc5f0 7196
nexpaq 0:6c56fb4bc5f0 7197 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 7198 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
nexpaq 0:6c56fb4bc5f0 7199 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
nexpaq 0:6c56fb4bc5f0 7200 #endif
nexpaq 0:6c56fb4bc5f0 7201
nexpaq 0:6c56fb4bc5f0 7202 #if defined(MBEDTLS_SSL_RENEGOTIATION)
nexpaq 0:6c56fb4bc5f0 7203 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
nexpaq 0:6c56fb4bc5f0 7204 memset( conf->renego_period, 0xFF, 7 );
nexpaq 0:6c56fb4bc5f0 7205 conf->renego_period[7] = 0x00;
nexpaq 0:6c56fb4bc5f0 7206 #endif
nexpaq 0:6c56fb4bc5f0 7207
nexpaq 0:6c56fb4bc5f0 7208 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
nexpaq 0:6c56fb4bc5f0 7209 if( endpoint == MBEDTLS_SSL_IS_SERVER )
nexpaq 0:6c56fb4bc5f0 7210 {
nexpaq 0:6c56fb4bc5f0 7211 if( ( ret = mbedtls_ssl_conf_dh_param( conf,
nexpaq 0:6c56fb4bc5f0 7212 MBEDTLS_DHM_RFC5114_MODP_2048_P,
nexpaq 0:6c56fb4bc5f0 7213 MBEDTLS_DHM_RFC5114_MODP_2048_G ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 7214 {
nexpaq 0:6c56fb4bc5f0 7215 return( ret );
nexpaq 0:6c56fb4bc5f0 7216 }
nexpaq 0:6c56fb4bc5f0 7217 }
nexpaq 0:6c56fb4bc5f0 7218 #endif
nexpaq 0:6c56fb4bc5f0 7219
nexpaq 0:6c56fb4bc5f0 7220 /*
nexpaq 0:6c56fb4bc5f0 7221 * Preset-specific defaults
nexpaq 0:6c56fb4bc5f0 7222 */
nexpaq 0:6c56fb4bc5f0 7223 switch( preset )
nexpaq 0:6c56fb4bc5f0 7224 {
nexpaq 0:6c56fb4bc5f0 7225 /*
nexpaq 0:6c56fb4bc5f0 7226 * NSA Suite B
nexpaq 0:6c56fb4bc5f0 7227 */
nexpaq 0:6c56fb4bc5f0 7228 case MBEDTLS_SSL_PRESET_SUITEB:
nexpaq 0:6c56fb4bc5f0 7229 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
nexpaq 0:6c56fb4bc5f0 7230 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
nexpaq 0:6c56fb4bc5f0 7231 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
nexpaq 0:6c56fb4bc5f0 7232 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
nexpaq 0:6c56fb4bc5f0 7233
nexpaq 0:6c56fb4bc5f0 7234 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
nexpaq 0:6c56fb4bc5f0 7235 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
nexpaq 0:6c56fb4bc5f0 7236 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
nexpaq 0:6c56fb4bc5f0 7237 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
nexpaq 0:6c56fb4bc5f0 7238 ssl_preset_suiteb_ciphersuites;
nexpaq 0:6c56fb4bc5f0 7239
nexpaq 0:6c56fb4bc5f0 7240 #if defined(MBEDTLS_X509_CRT_PARSE_C)
nexpaq 0:6c56fb4bc5f0 7241 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
nexpaq 0:6c56fb4bc5f0 7242 #endif
nexpaq 0:6c56fb4bc5f0 7243
nexpaq 0:6c56fb4bc5f0 7244 #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
nexpaq 0:6c56fb4bc5f0 7245 conf->sig_hashes = ssl_preset_suiteb_hashes;
nexpaq 0:6c56fb4bc5f0 7246 #endif
nexpaq 0:6c56fb4bc5f0 7247
nexpaq 0:6c56fb4bc5f0 7248 #if defined(MBEDTLS_ECP_C)
nexpaq 0:6c56fb4bc5f0 7249 conf->curve_list = ssl_preset_suiteb_curves;
nexpaq 0:6c56fb4bc5f0 7250 #endif
nexpaq 0:6c56fb4bc5f0 7251 break;
nexpaq 0:6c56fb4bc5f0 7252
nexpaq 0:6c56fb4bc5f0 7253 /*
nexpaq 0:6c56fb4bc5f0 7254 * Default
nexpaq 0:6c56fb4bc5f0 7255 */
nexpaq 0:6c56fb4bc5f0 7256 default:
nexpaq 0:6c56fb4bc5f0 7257 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
nexpaq 0:6c56fb4bc5f0 7258 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_1; /* TLS 1.0 */
nexpaq 0:6c56fb4bc5f0 7259 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
nexpaq 0:6c56fb4bc5f0 7260 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
nexpaq 0:6c56fb4bc5f0 7261
nexpaq 0:6c56fb4bc5f0 7262 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 7263 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
nexpaq 0:6c56fb4bc5f0 7264 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
nexpaq 0:6c56fb4bc5f0 7265 #endif
nexpaq 0:6c56fb4bc5f0 7266
nexpaq 0:6c56fb4bc5f0 7267 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
nexpaq 0:6c56fb4bc5f0 7268 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
nexpaq 0:6c56fb4bc5f0 7269 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
nexpaq 0:6c56fb4bc5f0 7270 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
nexpaq 0:6c56fb4bc5f0 7271 mbedtls_ssl_list_ciphersuites();
nexpaq 0:6c56fb4bc5f0 7272
nexpaq 0:6c56fb4bc5f0 7273 #if defined(MBEDTLS_X509_CRT_PARSE_C)
nexpaq 0:6c56fb4bc5f0 7274 conf->cert_profile = &mbedtls_x509_crt_profile_default;
nexpaq 0:6c56fb4bc5f0 7275 #endif
nexpaq 0:6c56fb4bc5f0 7276
nexpaq 0:6c56fb4bc5f0 7277 #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
nexpaq 0:6c56fb4bc5f0 7278 conf->sig_hashes = ssl_preset_default_hashes;
nexpaq 0:6c56fb4bc5f0 7279 #endif
nexpaq 0:6c56fb4bc5f0 7280
nexpaq 0:6c56fb4bc5f0 7281 #if defined(MBEDTLS_ECP_C)
nexpaq 0:6c56fb4bc5f0 7282 conf->curve_list = mbedtls_ecp_grp_id_list();
nexpaq 0:6c56fb4bc5f0 7283 #endif
nexpaq 0:6c56fb4bc5f0 7284
nexpaq 0:6c56fb4bc5f0 7285 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
nexpaq 0:6c56fb4bc5f0 7286 conf->dhm_min_bitlen = 1024;
nexpaq 0:6c56fb4bc5f0 7287 #endif
nexpaq 0:6c56fb4bc5f0 7288 }
nexpaq 0:6c56fb4bc5f0 7289
nexpaq 0:6c56fb4bc5f0 7290 return( 0 );
nexpaq 0:6c56fb4bc5f0 7291 }
nexpaq 0:6c56fb4bc5f0 7292
nexpaq 0:6c56fb4bc5f0 7293 /*
nexpaq 0:6c56fb4bc5f0 7294 * Free mbedtls_ssl_config
nexpaq 0:6c56fb4bc5f0 7295 */
nexpaq 0:6c56fb4bc5f0 7296 void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
nexpaq 0:6c56fb4bc5f0 7297 {
nexpaq 0:6c56fb4bc5f0 7298 #if defined(MBEDTLS_DHM_C)
nexpaq 0:6c56fb4bc5f0 7299 mbedtls_mpi_free( &conf->dhm_P );
nexpaq 0:6c56fb4bc5f0 7300 mbedtls_mpi_free( &conf->dhm_G );
nexpaq 0:6c56fb4bc5f0 7301 #endif
nexpaq 0:6c56fb4bc5f0 7302
nexpaq 0:6c56fb4bc5f0 7303 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
nexpaq 0:6c56fb4bc5f0 7304 if( conf->psk != NULL )
nexpaq 0:6c56fb4bc5f0 7305 {
nexpaq 0:6c56fb4bc5f0 7306 mbedtls_zeroize( conf->psk, conf->psk_len );
nexpaq 0:6c56fb4bc5f0 7307 mbedtls_zeroize( conf->psk_identity, conf->psk_identity_len );
nexpaq 0:6c56fb4bc5f0 7308 mbedtls_free( conf->psk );
nexpaq 0:6c56fb4bc5f0 7309 mbedtls_free( conf->psk_identity );
nexpaq 0:6c56fb4bc5f0 7310 conf->psk_len = 0;
nexpaq 0:6c56fb4bc5f0 7311 conf->psk_identity_len = 0;
nexpaq 0:6c56fb4bc5f0 7312 }
nexpaq 0:6c56fb4bc5f0 7313 #endif
nexpaq 0:6c56fb4bc5f0 7314
nexpaq 0:6c56fb4bc5f0 7315 #if defined(MBEDTLS_X509_CRT_PARSE_C)
nexpaq 0:6c56fb4bc5f0 7316 ssl_key_cert_free( conf->key_cert );
nexpaq 0:6c56fb4bc5f0 7317 #endif
nexpaq 0:6c56fb4bc5f0 7318
nexpaq 0:6c56fb4bc5f0 7319 mbedtls_zeroize( conf, sizeof( mbedtls_ssl_config ) );
nexpaq 0:6c56fb4bc5f0 7320 }
nexpaq 0:6c56fb4bc5f0 7321
nexpaq 0:6c56fb4bc5f0 7322 #if defined(MBEDTLS_PK_C) && \
nexpaq 0:6c56fb4bc5f0 7323 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
nexpaq 0:6c56fb4bc5f0 7324 /*
nexpaq 0:6c56fb4bc5f0 7325 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
nexpaq 0:6c56fb4bc5f0 7326 */
nexpaq 0:6c56fb4bc5f0 7327 unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
nexpaq 0:6c56fb4bc5f0 7328 {
nexpaq 0:6c56fb4bc5f0 7329 #if defined(MBEDTLS_RSA_C)
nexpaq 0:6c56fb4bc5f0 7330 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
nexpaq 0:6c56fb4bc5f0 7331 return( MBEDTLS_SSL_SIG_RSA );
nexpaq 0:6c56fb4bc5f0 7332 #endif
nexpaq 0:6c56fb4bc5f0 7333 #if defined(MBEDTLS_ECDSA_C)
nexpaq 0:6c56fb4bc5f0 7334 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
nexpaq 0:6c56fb4bc5f0 7335 return( MBEDTLS_SSL_SIG_ECDSA );
nexpaq 0:6c56fb4bc5f0 7336 #endif
nexpaq 0:6c56fb4bc5f0 7337 return( MBEDTLS_SSL_SIG_ANON );
nexpaq 0:6c56fb4bc5f0 7338 }
nexpaq 0:6c56fb4bc5f0 7339
nexpaq 0:6c56fb4bc5f0 7340 mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
nexpaq 0:6c56fb4bc5f0 7341 {
nexpaq 0:6c56fb4bc5f0 7342 switch( sig )
nexpaq 0:6c56fb4bc5f0 7343 {
nexpaq 0:6c56fb4bc5f0 7344 #if defined(MBEDTLS_RSA_C)
nexpaq 0:6c56fb4bc5f0 7345 case MBEDTLS_SSL_SIG_RSA:
nexpaq 0:6c56fb4bc5f0 7346 return( MBEDTLS_PK_RSA );
nexpaq 0:6c56fb4bc5f0 7347 #endif
nexpaq 0:6c56fb4bc5f0 7348 #if defined(MBEDTLS_ECDSA_C)
nexpaq 0:6c56fb4bc5f0 7349 case MBEDTLS_SSL_SIG_ECDSA:
nexpaq 0:6c56fb4bc5f0 7350 return( MBEDTLS_PK_ECDSA );
nexpaq 0:6c56fb4bc5f0 7351 #endif
nexpaq 0:6c56fb4bc5f0 7352 default:
nexpaq 0:6c56fb4bc5f0 7353 return( MBEDTLS_PK_NONE );
nexpaq 0:6c56fb4bc5f0 7354 }
nexpaq 0:6c56fb4bc5f0 7355 }
nexpaq 0:6c56fb4bc5f0 7356 #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
nexpaq 0:6c56fb4bc5f0 7357
nexpaq 0:6c56fb4bc5f0 7358 /*
nexpaq 0:6c56fb4bc5f0 7359 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
nexpaq 0:6c56fb4bc5f0 7360 */
nexpaq 0:6c56fb4bc5f0 7361 mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
nexpaq 0:6c56fb4bc5f0 7362 {
nexpaq 0:6c56fb4bc5f0 7363 switch( hash )
nexpaq 0:6c56fb4bc5f0 7364 {
nexpaq 0:6c56fb4bc5f0 7365 #if defined(MBEDTLS_MD5_C)
nexpaq 0:6c56fb4bc5f0 7366 case MBEDTLS_SSL_HASH_MD5:
nexpaq 0:6c56fb4bc5f0 7367 return( MBEDTLS_MD_MD5 );
nexpaq 0:6c56fb4bc5f0 7368 #endif
nexpaq 0:6c56fb4bc5f0 7369 #if defined(MBEDTLS_SHA1_C)
nexpaq 0:6c56fb4bc5f0 7370 case MBEDTLS_SSL_HASH_SHA1:
nexpaq 0:6c56fb4bc5f0 7371 return( MBEDTLS_MD_SHA1 );
nexpaq 0:6c56fb4bc5f0 7372 #endif
nexpaq 0:6c56fb4bc5f0 7373 #if defined(MBEDTLS_SHA256_C)
nexpaq 0:6c56fb4bc5f0 7374 case MBEDTLS_SSL_HASH_SHA224:
nexpaq 0:6c56fb4bc5f0 7375 return( MBEDTLS_MD_SHA224 );
nexpaq 0:6c56fb4bc5f0 7376 case MBEDTLS_SSL_HASH_SHA256:
nexpaq 0:6c56fb4bc5f0 7377 return( MBEDTLS_MD_SHA256 );
nexpaq 0:6c56fb4bc5f0 7378 #endif
nexpaq 0:6c56fb4bc5f0 7379 #if defined(MBEDTLS_SHA512_C)
nexpaq 0:6c56fb4bc5f0 7380 case MBEDTLS_SSL_HASH_SHA384:
nexpaq 0:6c56fb4bc5f0 7381 return( MBEDTLS_MD_SHA384 );
nexpaq 0:6c56fb4bc5f0 7382 case MBEDTLS_SSL_HASH_SHA512:
nexpaq 0:6c56fb4bc5f0 7383 return( MBEDTLS_MD_SHA512 );
nexpaq 0:6c56fb4bc5f0 7384 #endif
nexpaq 0:6c56fb4bc5f0 7385 default:
nexpaq 0:6c56fb4bc5f0 7386 return( MBEDTLS_MD_NONE );
nexpaq 0:6c56fb4bc5f0 7387 }
nexpaq 0:6c56fb4bc5f0 7388 }
nexpaq 0:6c56fb4bc5f0 7389
nexpaq 0:6c56fb4bc5f0 7390 /*
nexpaq 0:6c56fb4bc5f0 7391 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
nexpaq 0:6c56fb4bc5f0 7392 */
nexpaq 0:6c56fb4bc5f0 7393 unsigned char mbedtls_ssl_hash_from_md_alg( int md )
nexpaq 0:6c56fb4bc5f0 7394 {
nexpaq 0:6c56fb4bc5f0 7395 switch( md )
nexpaq 0:6c56fb4bc5f0 7396 {
nexpaq 0:6c56fb4bc5f0 7397 #if defined(MBEDTLS_MD5_C)
nexpaq 0:6c56fb4bc5f0 7398 case MBEDTLS_MD_MD5:
nexpaq 0:6c56fb4bc5f0 7399 return( MBEDTLS_SSL_HASH_MD5 );
nexpaq 0:6c56fb4bc5f0 7400 #endif
nexpaq 0:6c56fb4bc5f0 7401 #if defined(MBEDTLS_SHA1_C)
nexpaq 0:6c56fb4bc5f0 7402 case MBEDTLS_MD_SHA1:
nexpaq 0:6c56fb4bc5f0 7403 return( MBEDTLS_SSL_HASH_SHA1 );
nexpaq 0:6c56fb4bc5f0 7404 #endif
nexpaq 0:6c56fb4bc5f0 7405 #if defined(MBEDTLS_SHA256_C)
nexpaq 0:6c56fb4bc5f0 7406 case MBEDTLS_MD_SHA224:
nexpaq 0:6c56fb4bc5f0 7407 return( MBEDTLS_SSL_HASH_SHA224 );
nexpaq 0:6c56fb4bc5f0 7408 case MBEDTLS_MD_SHA256:
nexpaq 0:6c56fb4bc5f0 7409 return( MBEDTLS_SSL_HASH_SHA256 );
nexpaq 0:6c56fb4bc5f0 7410 #endif
nexpaq 0:6c56fb4bc5f0 7411 #if defined(MBEDTLS_SHA512_C)
nexpaq 0:6c56fb4bc5f0 7412 case MBEDTLS_MD_SHA384:
nexpaq 0:6c56fb4bc5f0 7413 return( MBEDTLS_SSL_HASH_SHA384 );
nexpaq 0:6c56fb4bc5f0 7414 case MBEDTLS_MD_SHA512:
nexpaq 0:6c56fb4bc5f0 7415 return( MBEDTLS_SSL_HASH_SHA512 );
nexpaq 0:6c56fb4bc5f0 7416 #endif
nexpaq 0:6c56fb4bc5f0 7417 default:
nexpaq 0:6c56fb4bc5f0 7418 return( MBEDTLS_SSL_HASH_NONE );
nexpaq 0:6c56fb4bc5f0 7419 }
nexpaq 0:6c56fb4bc5f0 7420 }
nexpaq 0:6c56fb4bc5f0 7421
nexpaq 0:6c56fb4bc5f0 7422 #if defined(MBEDTLS_ECP_C)
nexpaq 0:6c56fb4bc5f0 7423 /*
nexpaq 0:6c56fb4bc5f0 7424 * Check if a curve proposed by the peer is in our list.
nexpaq 0:6c56fb4bc5f0 7425 * Return 0 if we're willing to use it, -1 otherwise.
nexpaq 0:6c56fb4bc5f0 7426 */
nexpaq 0:6c56fb4bc5f0 7427 int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
nexpaq 0:6c56fb4bc5f0 7428 {
nexpaq 0:6c56fb4bc5f0 7429 const mbedtls_ecp_group_id *gid;
nexpaq 0:6c56fb4bc5f0 7430
nexpaq 0:6c56fb4bc5f0 7431 if( ssl->conf->curve_list == NULL )
nexpaq 0:6c56fb4bc5f0 7432 return( -1 );
nexpaq 0:6c56fb4bc5f0 7433
nexpaq 0:6c56fb4bc5f0 7434 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
nexpaq 0:6c56fb4bc5f0 7435 if( *gid == grp_id )
nexpaq 0:6c56fb4bc5f0 7436 return( 0 );
nexpaq 0:6c56fb4bc5f0 7437
nexpaq 0:6c56fb4bc5f0 7438 return( -1 );
nexpaq 0:6c56fb4bc5f0 7439 }
nexpaq 0:6c56fb4bc5f0 7440 #endif /* MBEDTLS_ECP_C */
nexpaq 0:6c56fb4bc5f0 7441
nexpaq 0:6c56fb4bc5f0 7442 #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
nexpaq 0:6c56fb4bc5f0 7443 /*
nexpaq 0:6c56fb4bc5f0 7444 * Check if a hash proposed by the peer is in our list.
nexpaq 0:6c56fb4bc5f0 7445 * Return 0 if we're willing to use it, -1 otherwise.
nexpaq 0:6c56fb4bc5f0 7446 */
nexpaq 0:6c56fb4bc5f0 7447 int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
nexpaq 0:6c56fb4bc5f0 7448 mbedtls_md_type_t md )
nexpaq 0:6c56fb4bc5f0 7449 {
nexpaq 0:6c56fb4bc5f0 7450 const int *cur;
nexpaq 0:6c56fb4bc5f0 7451
nexpaq 0:6c56fb4bc5f0 7452 if( ssl->conf->sig_hashes == NULL )
nexpaq 0:6c56fb4bc5f0 7453 return( -1 );
nexpaq 0:6c56fb4bc5f0 7454
nexpaq 0:6c56fb4bc5f0 7455 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
nexpaq 0:6c56fb4bc5f0 7456 if( *cur == (int) md )
nexpaq 0:6c56fb4bc5f0 7457 return( 0 );
nexpaq 0:6c56fb4bc5f0 7458
nexpaq 0:6c56fb4bc5f0 7459 return( -1 );
nexpaq 0:6c56fb4bc5f0 7460 }
nexpaq 0:6c56fb4bc5f0 7461 #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
nexpaq 0:6c56fb4bc5f0 7462
nexpaq 0:6c56fb4bc5f0 7463 #if defined(MBEDTLS_X509_CRT_PARSE_C)
nexpaq 0:6c56fb4bc5f0 7464 int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
nexpaq 0:6c56fb4bc5f0 7465 const mbedtls_ssl_ciphersuite_t *ciphersuite,
nexpaq 0:6c56fb4bc5f0 7466 int cert_endpoint,
nexpaq 0:6c56fb4bc5f0 7467 uint32_t *flags )
nexpaq 0:6c56fb4bc5f0 7468 {
nexpaq 0:6c56fb4bc5f0 7469 int ret = 0;
nexpaq 0:6c56fb4bc5f0 7470 #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
nexpaq 0:6c56fb4bc5f0 7471 int usage = 0;
nexpaq 0:6c56fb4bc5f0 7472 #endif
nexpaq 0:6c56fb4bc5f0 7473 #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
nexpaq 0:6c56fb4bc5f0 7474 const char *ext_oid;
nexpaq 0:6c56fb4bc5f0 7475 size_t ext_len;
nexpaq 0:6c56fb4bc5f0 7476 #endif
nexpaq 0:6c56fb4bc5f0 7477
nexpaq 0:6c56fb4bc5f0 7478 #if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
nexpaq 0:6c56fb4bc5f0 7479 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
nexpaq 0:6c56fb4bc5f0 7480 ((void) cert);
nexpaq 0:6c56fb4bc5f0 7481 ((void) cert_endpoint);
nexpaq 0:6c56fb4bc5f0 7482 ((void) flags);
nexpaq 0:6c56fb4bc5f0 7483 #endif
nexpaq 0:6c56fb4bc5f0 7484
nexpaq 0:6c56fb4bc5f0 7485 #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
nexpaq 0:6c56fb4bc5f0 7486 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
nexpaq 0:6c56fb4bc5f0 7487 {
nexpaq 0:6c56fb4bc5f0 7488 /* Server part of the key exchange */
nexpaq 0:6c56fb4bc5f0 7489 switch( ciphersuite->key_exchange )
nexpaq 0:6c56fb4bc5f0 7490 {
nexpaq 0:6c56fb4bc5f0 7491 case MBEDTLS_KEY_EXCHANGE_RSA:
nexpaq 0:6c56fb4bc5f0 7492 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
nexpaq 0:6c56fb4bc5f0 7493 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
nexpaq 0:6c56fb4bc5f0 7494 break;
nexpaq 0:6c56fb4bc5f0 7495
nexpaq 0:6c56fb4bc5f0 7496 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
nexpaq 0:6c56fb4bc5f0 7497 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
nexpaq 0:6c56fb4bc5f0 7498 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
nexpaq 0:6c56fb4bc5f0 7499 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
nexpaq 0:6c56fb4bc5f0 7500 break;
nexpaq 0:6c56fb4bc5f0 7501
nexpaq 0:6c56fb4bc5f0 7502 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
nexpaq 0:6c56fb4bc5f0 7503 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
nexpaq 0:6c56fb4bc5f0 7504 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
nexpaq 0:6c56fb4bc5f0 7505 break;
nexpaq 0:6c56fb4bc5f0 7506
nexpaq 0:6c56fb4bc5f0 7507 /* Don't use default: we want warnings when adding new values */
nexpaq 0:6c56fb4bc5f0 7508 case MBEDTLS_KEY_EXCHANGE_NONE:
nexpaq 0:6c56fb4bc5f0 7509 case MBEDTLS_KEY_EXCHANGE_PSK:
nexpaq 0:6c56fb4bc5f0 7510 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
nexpaq 0:6c56fb4bc5f0 7511 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
nexpaq 0:6c56fb4bc5f0 7512 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
nexpaq 0:6c56fb4bc5f0 7513 usage = 0;
nexpaq 0:6c56fb4bc5f0 7514 }
nexpaq 0:6c56fb4bc5f0 7515 }
nexpaq 0:6c56fb4bc5f0 7516 else
nexpaq 0:6c56fb4bc5f0 7517 {
nexpaq 0:6c56fb4bc5f0 7518 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
nexpaq 0:6c56fb4bc5f0 7519 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
nexpaq 0:6c56fb4bc5f0 7520 }
nexpaq 0:6c56fb4bc5f0 7521
nexpaq 0:6c56fb4bc5f0 7522 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
nexpaq 0:6c56fb4bc5f0 7523 {
nexpaq 0:6c56fb4bc5f0 7524 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
nexpaq 0:6c56fb4bc5f0 7525 ret = -1;
nexpaq 0:6c56fb4bc5f0 7526 }
nexpaq 0:6c56fb4bc5f0 7527 #else
nexpaq 0:6c56fb4bc5f0 7528 ((void) ciphersuite);
nexpaq 0:6c56fb4bc5f0 7529 #endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
nexpaq 0:6c56fb4bc5f0 7530
nexpaq 0:6c56fb4bc5f0 7531 #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
nexpaq 0:6c56fb4bc5f0 7532 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
nexpaq 0:6c56fb4bc5f0 7533 {
nexpaq 0:6c56fb4bc5f0 7534 ext_oid = MBEDTLS_OID_SERVER_AUTH;
nexpaq 0:6c56fb4bc5f0 7535 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
nexpaq 0:6c56fb4bc5f0 7536 }
nexpaq 0:6c56fb4bc5f0 7537 else
nexpaq 0:6c56fb4bc5f0 7538 {
nexpaq 0:6c56fb4bc5f0 7539 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
nexpaq 0:6c56fb4bc5f0 7540 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
nexpaq 0:6c56fb4bc5f0 7541 }
nexpaq 0:6c56fb4bc5f0 7542
nexpaq 0:6c56fb4bc5f0 7543 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
nexpaq 0:6c56fb4bc5f0 7544 {
nexpaq 0:6c56fb4bc5f0 7545 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
nexpaq 0:6c56fb4bc5f0 7546 ret = -1;
nexpaq 0:6c56fb4bc5f0 7547 }
nexpaq 0:6c56fb4bc5f0 7548 #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
nexpaq 0:6c56fb4bc5f0 7549
nexpaq 0:6c56fb4bc5f0 7550 return( ret );
nexpaq 0:6c56fb4bc5f0 7551 }
nexpaq 0:6c56fb4bc5f0 7552 #endif /* MBEDTLS_X509_CRT_PARSE_C */
nexpaq 0:6c56fb4bc5f0 7553
nexpaq 0:6c56fb4bc5f0 7554 /*
nexpaq 0:6c56fb4bc5f0 7555 * Convert version numbers to/from wire format
nexpaq 0:6c56fb4bc5f0 7556 * and, for DTLS, to/from TLS equivalent.
nexpaq 0:6c56fb4bc5f0 7557 *
nexpaq 0:6c56fb4bc5f0 7558 * For TLS this is the identity.
nexpaq 0:6c56fb4bc5f0 7559 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
nexpaq 0:6c56fb4bc5f0 7560 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
nexpaq 0:6c56fb4bc5f0 7561 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
nexpaq 0:6c56fb4bc5f0 7562 */
nexpaq 0:6c56fb4bc5f0 7563 void mbedtls_ssl_write_version( int major, int minor, int transport,
nexpaq 0:6c56fb4bc5f0 7564 unsigned char ver[2] )
nexpaq 0:6c56fb4bc5f0 7565 {
nexpaq 0:6c56fb4bc5f0 7566 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 7567 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
nexpaq 0:6c56fb4bc5f0 7568 {
nexpaq 0:6c56fb4bc5f0 7569 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
nexpaq 0:6c56fb4bc5f0 7570 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
nexpaq 0:6c56fb4bc5f0 7571
nexpaq 0:6c56fb4bc5f0 7572 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
nexpaq 0:6c56fb4bc5f0 7573 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
nexpaq 0:6c56fb4bc5f0 7574 }
nexpaq 0:6c56fb4bc5f0 7575 else
nexpaq 0:6c56fb4bc5f0 7576 #else
nexpaq 0:6c56fb4bc5f0 7577 ((void) transport);
nexpaq 0:6c56fb4bc5f0 7578 #endif
nexpaq 0:6c56fb4bc5f0 7579 {
nexpaq 0:6c56fb4bc5f0 7580 ver[0] = (unsigned char) major;
nexpaq 0:6c56fb4bc5f0 7581 ver[1] = (unsigned char) minor;
nexpaq 0:6c56fb4bc5f0 7582 }
nexpaq 0:6c56fb4bc5f0 7583 }
nexpaq 0:6c56fb4bc5f0 7584
nexpaq 0:6c56fb4bc5f0 7585 void mbedtls_ssl_read_version( int *major, int *minor, int transport,
nexpaq 0:6c56fb4bc5f0 7586 const unsigned char ver[2] )
nexpaq 0:6c56fb4bc5f0 7587 {
nexpaq 0:6c56fb4bc5f0 7588 #if defined(MBEDTLS_SSL_PROTO_DTLS)
nexpaq 0:6c56fb4bc5f0 7589 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
nexpaq 0:6c56fb4bc5f0 7590 {
nexpaq 0:6c56fb4bc5f0 7591 *major = 255 - ver[0] + 2;
nexpaq 0:6c56fb4bc5f0 7592 *minor = 255 - ver[1] + 1;
nexpaq 0:6c56fb4bc5f0 7593
nexpaq 0:6c56fb4bc5f0 7594 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
nexpaq 0:6c56fb4bc5f0 7595 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
nexpaq 0:6c56fb4bc5f0 7596 }
nexpaq 0:6c56fb4bc5f0 7597 else
nexpaq 0:6c56fb4bc5f0 7598 #else
nexpaq 0:6c56fb4bc5f0 7599 ((void) transport);
nexpaq 0:6c56fb4bc5f0 7600 #endif
nexpaq 0:6c56fb4bc5f0 7601 {
nexpaq 0:6c56fb4bc5f0 7602 *major = ver[0];
nexpaq 0:6c56fb4bc5f0 7603 *minor = ver[1];
nexpaq 0:6c56fb4bc5f0 7604 }
nexpaq 0:6c56fb4bc5f0 7605 }
nexpaq 0:6c56fb4bc5f0 7606
nexpaq 0:6c56fb4bc5f0 7607 #endif /* MBEDTLS_SSL_TLS_C */