Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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