mbed client on ethernet with LWIP

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

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