Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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