Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSLv3/TLSv1 shared functions |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 18 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | */ |
| 21 | /* |
| 22 | * The SSL 3.0 specification was drafted by Netscape in 1996, |
| 23 | * and became an IETF standard in 1999. |
| 24 | * |
| 25 | * http://wp.netscape.com/eng/ssl3/ |
| 26 | * http://www.ietf.org/rfc/rfc2246.txt |
| 27 | * http://www.ietf.org/rfc/rfc4346.txt |
| 28 | */ |
| 29 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 34 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 35 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 36 | #if defined(MBEDTLS_SSL_TLS_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 37 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 38 | #if defined(MBEDTLS_PLATFORM_C) |
| 39 | #include "mbedtls/platform.h" |
| 40 | #else |
| 41 | #include <stdlib.h> |
| 42 | #define mbedtls_calloc calloc |
| 43 | #define mbedtls_free free |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 44 | #endif |
| 45 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 46 | #include "mbedtls/debug.h" |
| 47 | #include "mbedtls/ssl.h" |
Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 48 | #include "mbedtls/ssl_internal.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 49 | #include "mbedtls/platform_util.h" |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 50 | #include "mbedtls/version.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 51 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 52 | #include <string.h> |
| 53 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 54 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 55 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 56 | #endif |
| 57 | |
Hanno Becker | ef982d5 | 2019-07-23 15:56:18 +0100 | [diff] [blame] | 58 | #if defined(MBEDTLS_USE_TINYCRYPT) |
| 59 | static int uecc_rng_wrapper( uint8_t *dest, unsigned int size ) |
| 60 | { |
Hanno Becker | d089fad | 2019-07-24 09:05:05 +0100 | [diff] [blame] | 61 | int ret; |
| 62 | ret = mbedtls_ssl_conf_rng_func( NULL, dest, size ); |
| 63 | if( ret == 0 ) |
| 64 | return( (int) size ); |
| 65 | |
| 66 | return( 0 ); |
Hanno Becker | ef982d5 | 2019-07-23 15:56:18 +0100 | [diff] [blame] | 67 | } |
Hanno Becker | 75f12d1 | 2019-07-23 16:16:15 +0100 | [diff] [blame] | 68 | |
| 69 | int mbedtls_ssl_ecdh_read_peerkey( mbedtls_ssl_context *ssl, |
| 70 | unsigned char **p, unsigned char *end ) |
| 71 | { |
| 72 | size_t const secp256r1_uncompressed_point_length = |
| 73 | 1 /* length */ + 1 /* length */ + 2 * NUM_ECC_BYTES /* data */; |
| 74 | |
| 75 | if( (size_t)( end - *p ) < secp256r1_uncompressed_point_length ) |
| 76 | { |
| 77 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Bad ECDH peer pubkey (too short)" ) ); |
| 78 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
| 79 | } |
| 80 | |
| 81 | if( (*p)[0] != 2 * NUM_ECC_BYTES + 1 || |
| 82 | (*p)[1] != 0x04 ) |
| 83 | { |
| 84 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Unexpected ECDH peer pubkey header - expected { %#02x, %#02x }, got { %#02x, %#02x }", |
| 85 | 2 * NUM_ECC_BYTES + 1, |
| 86 | 0x04, |
| 87 | (unsigned) (*p)[0], |
| 88 | (unsigned) (*p)[1] ) ); |
| 89 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
| 90 | } |
| 91 | |
| 92 | memcpy( ssl->handshake->ecdh_peerkey, *p + 2, 2 * NUM_ECC_BYTES ); |
| 93 | |
| 94 | *p += secp256r1_uncompressed_point_length; |
| 95 | return( 0 ); |
| 96 | } |
Hanno Becker | ef982d5 | 2019-07-23 15:56:18 +0100 | [diff] [blame] | 97 | #endif /* MBEDTLS_USE_TINYCRYPT */ |
| 98 | |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 99 | static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl ); |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 100 | static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl ); |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 101 | |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 102 | /* Length of the "epoch" field in the record header */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 103 | static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 104 | { |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 105 | #if !defined(MBEDTLS_SSL_TRANSPORT__BOTH) |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 106 | ((void) ssl); |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 107 | #endif |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 108 | |
| 109 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 110 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
| 111 | return( 2 ); |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 112 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 113 | #endif |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 114 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 115 | return( 0 ); |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 116 | #endif |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Hanno Becker | b82350b | 2019-07-26 07:24:05 +0100 | [diff] [blame] | 119 | static void ssl_send_pending_fatal_alert( mbedtls_ssl_context *ssl ) |
| 120 | { |
| 121 | if( ssl->pending_fatal_alert_msg == MBEDTLS_SSL_ALERT_MSG_NONE ) |
| 122 | return; |
| 123 | |
| 124 | mbedtls_ssl_send_alert_message( ssl, |
| 125 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 126 | ssl->pending_fatal_alert_msg ); |
| 127 | ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE; |
| 128 | } |
| 129 | |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 130 | /* |
| 131 | * Start a timer. |
| 132 | * Passing millisecs = 0 cancels a running timer. |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 133 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 134 | static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs ) |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 135 | { |
Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 136 | if( mbedtls_ssl_get_set_timer( ssl ) == NULL ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 137 | return; |
| 138 | |
| 139 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) ); |
Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 140 | mbedtls_ssl_get_set_timer( ssl )( ssl->p_timer, |
| 141 | millisecs / 4, |
| 142 | millisecs ); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Return -1 is timer is expired, 0 if it isn't. |
| 147 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 148 | static int ssl_check_timer( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 149 | { |
Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 150 | if( mbedtls_ssl_get_get_timer( ssl ) == NULL ) |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 151 | return( 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 152 | |
Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 153 | if( mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == 2 ) |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 154 | { |
| 155 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) ); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 156 | return( -1 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 157 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 158 | |
| 159 | return( 0 ); |
| 160 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 161 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 162 | static void ssl_update_out_pointers( mbedtls_ssl_context *ssl, |
| 163 | mbedtls_ssl_transform *transform ); |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 164 | static void ssl_update_in_pointers( mbedtls_ssl_context *ssl ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 165 | |
Hanno Becker | 02f2609 | 2019-07-03 16:13:00 +0100 | [diff] [blame] | 166 | #if defined(MBEDTLS_SSL_RECORD_CHECKING) |
Hanno Becker | 03e2db6 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 167 | static int ssl_parse_record_header( mbedtls_ssl_context const *ssl, |
| 168 | unsigned char *buf, |
| 169 | size_t len, |
| 170 | mbedtls_record *rec ); |
| 171 | |
Hanno Becker | 02f2609 | 2019-07-03 16:13:00 +0100 | [diff] [blame] | 172 | int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl, |
| 173 | unsigned char *buf, |
| 174 | size_t buflen ) |
| 175 | { |
Hanno Becker | 03e2db6 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 176 | int ret = 0; |
| 177 | mbedtls_record rec; |
| 178 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) ); |
| 179 | MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen ); |
| 180 | |
| 181 | /* We don't support record checking in TLS because |
| 182 | * (a) there doesn't seem to be a usecase for it, and |
| 183 | * (b) In SSLv3 and TLS 1.0, CBC record decryption has state |
| 184 | * and we'd need to backup the transform here. |
| 185 | */ |
| 186 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 187 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) |
| 188 | { |
| 189 | ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 190 | goto exit; |
| 191 | } |
| 192 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 193 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
| 194 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 195 | { |
| 196 | ret = ssl_parse_record_header( ssl, buf, buflen, &rec ); |
| 197 | if( ret != 0 ) |
| 198 | { |
| 199 | MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret ); |
| 200 | goto exit; |
| 201 | } |
| 202 | |
| 203 | if( ssl->transform_in != NULL ) |
| 204 | { |
| 205 | ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec ); |
| 206 | if( ret != 0 ) |
| 207 | { |
| 208 | MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret ); |
| 209 | goto exit; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 214 | |
| 215 | exit: |
| 216 | /* On success, we have decrypted the buffer in-place, so make |
| 217 | * sure we don't leak any plaintext data. */ |
| 218 | mbedtls_platform_zeroize( buf, buflen ); |
| 219 | |
| 220 | /* For the purpose of this API, treat messages with unexpected CID |
| 221 | * as well as such from future epochs as unexpected. */ |
| 222 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID || |
| 223 | ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 224 | { |
| 225 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
| 226 | } |
| 227 | |
| 228 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) ); |
| 229 | return( ret ); |
Hanno Becker | 02f2609 | 2019-07-03 16:13:00 +0100 | [diff] [blame] | 230 | } |
| 231 | #endif /* MBEDTLS_SSL_RECORD_CHECKING */ |
| 232 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 233 | #define SSL_DONT_FORCE_FLUSH 0 |
| 234 | #define SSL_FORCE_FLUSH 1 |
| 235 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 236 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 237 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 238 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 239 | /* Top-level Connection ID API */ |
| 240 | |
Hanno Becker | e0200da | 2019-06-13 09:23:43 +0100 | [diff] [blame] | 241 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \ |
| 242 | !defined(MBEDTLS_SSL_CONF_CID_LEN) && \ |
| 243 | !defined(MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID) |
Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 244 | int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf, |
| 245 | size_t len, |
| 246 | int ignore_other_cid ) |
Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 247 | { |
| 248 | if( len > MBEDTLS_SSL_CID_IN_LEN_MAX ) |
| 249 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 250 | |
Hanno Becker | 791ec6b | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 251 | if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && |
| 252 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 253 | { |
| 254 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 255 | } |
| 256 | |
| 257 | conf->ignore_unexpected_cid = ignore_other_cid; |
Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 258 | conf->cid_len = len; |
| 259 | return( 0 ); |
| 260 | } |
Hanno Becker | e0200da | 2019-06-13 09:23:43 +0100 | [diff] [blame] | 261 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID && |
| 262 | !MBEDTLS_SSL_CONF_CID_LEN && |
| 263 | !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */ |
| 264 | |
| 265 | #if MBEDTLS_SSL_CONF_CID_LEN > MBEDTLS_SSL_CID_IN_LEN_MAX |
| 266 | #error "Invalid hardcoded value for MBEDTLS_SSL_CONF_CID_LEN" |
| 267 | #endif |
| 268 | #if MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE && \ |
| 269 | MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_FAIL |
| 270 | #error "Invalid hardcoded value for MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID" |
| 271 | #endif |
| 272 | |
| 273 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID && |
| 274 | !MBEDTLS_SSL_CONF_CID_LEN && |
| 275 | !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */ |
Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 276 | |
Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 277 | int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl, |
| 278 | int enable, |
| 279 | unsigned char const *own_cid, |
| 280 | size_t own_cid_len ) |
| 281 | { |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 282 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) |
Hanno Becker | 78c4302 | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 283 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 284 | |
Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 285 | ssl->negotiate_cid = enable; |
| 286 | if( enable == MBEDTLS_SSL_CID_DISABLED ) |
| 287 | { |
| 288 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) ); |
| 289 | return( 0 ); |
| 290 | } |
| 291 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) ); |
Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 292 | MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len ); |
Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 293 | |
Hanno Becker | e0200da | 2019-06-13 09:23:43 +0100 | [diff] [blame] | 294 | if( own_cid_len != mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) |
Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 295 | { |
Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 296 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config", |
| 297 | (unsigned) own_cid_len, |
Hanno Becker | e0200da | 2019-06-13 09:23:43 +0100 | [diff] [blame] | 298 | (unsigned) mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) ); |
Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 299 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 300 | } |
| 301 | |
| 302 | memcpy( ssl->own_cid, own_cid, own_cid_len ); |
Hanno Becker | b4a5606 | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 303 | /* Truncation is not an issue here because |
| 304 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ |
| 305 | ssl->own_cid_len = (uint8_t) own_cid_len; |
Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 306 | |
Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 307 | return( 0 ); |
| 308 | } |
| 309 | |
| 310 | int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl, |
| 311 | int *enabled, |
| 312 | unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ], |
| 313 | size_t *peer_cid_len ) |
| 314 | { |
Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 315 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 316 | |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 317 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) || |
Hanno Becker | 78c4302 | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 318 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 319 | { |
Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 320 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 78c4302 | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 321 | } |
Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 322 | |
Hanno Becker | cb063f5 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 323 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions |
| 324 | * were used, but client and server requested the empty CID. |
| 325 | * This is indistinguishable from not using the CID extension |
| 326 | * in the first place. */ |
Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 327 | if( ssl->transform_in->in_cid_len == 0 && |
| 328 | ssl->transform_in->out_cid_len == 0 ) |
| 329 | { |
| 330 | return( 0 ); |
| 331 | } |
| 332 | |
Hanno Becker | 633d604 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 333 | if( peer_cid_len != NULL ) |
| 334 | { |
| 335 | *peer_cid_len = ssl->transform_in->out_cid_len; |
| 336 | if( peer_cid != NULL ) |
| 337 | { |
| 338 | memcpy( peer_cid, ssl->transform_in->out_cid, |
| 339 | ssl->transform_in->out_cid_len ); |
| 340 | } |
| 341 | } |
Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 342 | |
| 343 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 344 | |
Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 345 | return( 0 ); |
| 346 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 347 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 348 | |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 349 | /* Forward declarations for functions related to message buffering. */ |
| 350 | static void ssl_buffering_free( mbedtls_ssl_context *ssl ); |
| 351 | static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl, |
| 352 | uint8_t slot ); |
| 353 | static void ssl_free_buffered_record( mbedtls_ssl_context *ssl ); |
| 354 | static int ssl_load_buffered_message( mbedtls_ssl_context *ssl ); |
| 355 | static int ssl_load_buffered_record( mbedtls_ssl_context *ssl ); |
| 356 | static int ssl_buffer_message( mbedtls_ssl_context *ssl ); |
Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 357 | static int ssl_buffer_future_record( mbedtls_ssl_context *ssl, |
| 358 | mbedtls_record const *rec ); |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 359 | static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl ); |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 360 | |
Hanno Becker | a67dee2 | 2018-08-22 10:05:20 +0100 | [diff] [blame] | 361 | static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl ); |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 362 | static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl ) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 363 | { |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 364 | size_t mtu = ssl_get_current_mtu( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 365 | |
| 366 | if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN ) |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 367 | return( mtu ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 368 | |
| 369 | return( MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 370 | } |
| 371 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 372 | static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl ) |
| 373 | { |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 374 | size_t const bytes_written = ssl->out_left; |
| 375 | size_t const mtu = ssl_get_maximum_datagram_size( ssl ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 376 | |
| 377 | /* Double-check that the write-index hasn't gone |
| 378 | * past what we can transmit in a single datagram. */ |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 379 | if( bytes_written > mtu ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 380 | { |
| 381 | /* Should never happen... */ |
| 382 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 383 | } |
| 384 | |
| 385 | return( (int) ( mtu - bytes_written ) ); |
| 386 | } |
| 387 | |
| 388 | static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl ) |
| 389 | { |
| 390 | int ret; |
| 391 | size_t remaining, expansion; |
Andrzej Kurek | 748face | 2018-10-11 07:20:19 -0400 | [diff] [blame] | 392 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 393 | |
| 394 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 395 | const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl ); |
| 396 | |
| 397 | if( max_len > mfl ) |
| 398 | max_len = mfl; |
Hanno Becker | f4b010e | 2018-08-24 10:47:29 +0100 | [diff] [blame] | 399 | |
| 400 | /* By the standard (RFC 6066 Sect. 4), the MFL extension |
| 401 | * only limits the maximum record payload size, so in theory |
| 402 | * we would be allowed to pack multiple records of payload size |
| 403 | * MFL into a single datagram. However, this would mean that there's |
| 404 | * no way to explicitly communicate MTU restrictions to the peer. |
| 405 | * |
| 406 | * The following reduction of max_len makes sure that we never |
| 407 | * write datagrams larger than MFL + Record Expansion Overhead. |
| 408 | */ |
| 409 | if( max_len <= ssl->out_left ) |
| 410 | return( 0 ); |
| 411 | |
| 412 | max_len -= ssl->out_left; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 413 | #endif |
| 414 | |
| 415 | ret = ssl_get_remaining_space_in_datagram( ssl ); |
| 416 | if( ret < 0 ) |
| 417 | return( ret ); |
| 418 | remaining = (size_t) ret; |
| 419 | |
| 420 | ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 421 | if( ret < 0 ) |
| 422 | return( ret ); |
| 423 | expansion = (size_t) ret; |
| 424 | |
| 425 | if( remaining <= expansion ) |
| 426 | return( 0 ); |
| 427 | |
| 428 | remaining -= expansion; |
| 429 | if( remaining >= max_len ) |
| 430 | remaining = max_len; |
| 431 | |
| 432 | return( (int) remaining ); |
| 433 | } |
| 434 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 435 | /* |
| 436 | * Double the retransmit timeout value, within the allowed range, |
| 437 | * returning -1 if the maximum value has already been reached. |
| 438 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 439 | static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 440 | { |
| 441 | uint32_t new_timeout; |
| 442 | |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 443 | if( ssl->handshake->retransmit_timeout >= |
| 444 | mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) ) |
| 445 | { |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 446 | return( -1 ); |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 447 | } |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 448 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 449 | /* Implement the final paragraph of RFC 6347 section 4.1.1.1 |
| 450 | * in the following way: after the initial transmission and a first |
| 451 | * retransmission, back off to a temporary estimated MTU of 508 bytes. |
| 452 | * This value is guaranteed to be deliverable (if not guaranteed to be |
| 453 | * delivered) of any compliant IPv4 (and IPv6) network, and should work |
| 454 | * on most non-IP stacks too. */ |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 455 | if( ssl->handshake->retransmit_timeout != |
| 456 | mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) ) |
Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 457 | { |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 458 | ssl->handshake->mtu = 508; |
Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 459 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) ); |
| 460 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 461 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 462 | new_timeout = 2 * ssl->handshake->retransmit_timeout; |
| 463 | |
| 464 | /* Avoid arithmetic overflow and range overflow */ |
| 465 | if( new_timeout < ssl->handshake->retransmit_timeout || |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 466 | new_timeout > mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 467 | { |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 468 | new_timeout = mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | ssl->handshake->retransmit_timeout = new_timeout; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 472 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs", |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 473 | ssl->handshake->retransmit_timeout ) ); |
| 474 | |
| 475 | return( 0 ); |
| 476 | } |
| 477 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 478 | static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 479 | { |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 480 | ssl->handshake->retransmit_timeout = mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 481 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs", |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 482 | ssl->handshake->retransmit_timeout ) ); |
| 483 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 484 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 485 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 486 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 487 | /* |
| 488 | * Convert max_fragment_length codes to length. |
| 489 | * RFC 6066 says: |
| 490 | * enum{ |
| 491 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 492 | * } MaxFragmentLength; |
| 493 | * and we add 0 -> extension unused |
| 494 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 495 | static unsigned int ssl_mfl_code_to_length( int mfl ) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 496 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 497 | switch( mfl ) |
| 498 | { |
| 499 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: |
| 500 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 501 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: |
| 502 | return 512; |
| 503 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: |
| 504 | return 1024; |
| 505 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: |
| 506 | return 2048; |
| 507 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: |
| 508 | return 4096; |
| 509 | default: |
| 510 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 511 | } |
| 512 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 513 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 514 | |
Hanno Becker | 58fccf2 | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 515 | int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst, |
| 516 | const mbedtls_ssl_session *src ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 517 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 518 | mbedtls_ssl_session_free( dst ); |
| 519 | memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 520 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 521 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | d5258fa | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 522 | |
| 523 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 524 | if( src->peer_cert != NULL ) |
| 525 | { |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 526 | int ret; |
| 527 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 528 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 529 | if( dst->peer_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 530 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 531 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 532 | mbedtls_x509_crt_init( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 533 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 534 | if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p, |
Manuel Pégourié-Gonnard | 4d2a8eb | 2014-06-13 20:33:27 +0200 | [diff] [blame] | 535 | src->peer_cert->raw.len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 536 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 537 | mbedtls_free( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 538 | dst->peer_cert = NULL; |
| 539 | return( ret ); |
| 540 | } |
| 541 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 542 | #elif defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 9fb6e2e | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 543 | if( src->peer_cert_digest != NULL ) |
| 544 | { |
Hanno Becker | 9fb6e2e | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 545 | dst->peer_cert_digest = |
Hanno Becker | 9d64b78 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 546 | mbedtls_calloc( 1, src->peer_cert_digest_len ); |
Hanno Becker | 9fb6e2e | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 547 | if( dst->peer_cert_digest == NULL ) |
| 548 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 549 | |
| 550 | memcpy( dst->peer_cert_digest, src->peer_cert_digest, |
| 551 | src->peer_cert_digest_len ); |
| 552 | dst->peer_cert_digest_type = src->peer_cert_digest_type; |
Hanno Becker | 9d64b78 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 553 | dst->peer_cert_digest_len = src->peer_cert_digest_len; |
Hanno Becker | 9fb6e2e | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 554 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 555 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 9fb6e2e | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 556 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 557 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 558 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 559 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 560 | if( src->ticket != NULL ) |
| 561 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 562 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 563 | if( dst->ticket == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 564 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 565 | |
| 566 | memcpy( dst->ticket, src->ticket, src->ticket_len ); |
| 567 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 568 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 569 | |
| 570 | return( 0 ); |
| 571 | } |
| 572 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 573 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 574 | int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl, |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 575 | const unsigned char *key_enc, const unsigned char *key_dec, |
| 576 | size_t keylen, |
| 577 | const unsigned char *iv_enc, const unsigned char *iv_dec, |
| 578 | size_t ivlen, |
| 579 | const unsigned char *mac_enc, const unsigned char *mac_dec, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 580 | size_t maclen ) = NULL; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 581 | int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL; |
| 582 | int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL; |
| 583 | int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL; |
| 584 | int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL; |
| 585 | int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL; |
| 586 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 587 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 588 | /* |
| 589 | * Key material generation |
| 590 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 591 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 592 | static int ssl3_prf( const unsigned char *secret, size_t slen, |
| 593 | const char *label, |
| 594 | const unsigned char *random, size_t rlen, |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 595 | unsigned char *dstbuf, size_t dlen ) |
| 596 | { |
Andres Amaya Garcia | 3395250 | 2017-07-20 16:29:16 +0100 | [diff] [blame] | 597 | int ret = 0; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 598 | size_t i; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 599 | mbedtls_md5_context md5; |
| 600 | mbedtls_sha1_context sha1; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 601 | unsigned char padding[16]; |
| 602 | unsigned char sha1sum[20]; |
| 603 | ((void)label); |
| 604 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 605 | mbedtls_md5_init( &md5 ); |
| 606 | mbedtls_sha1_init( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 607 | |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 608 | /* |
| 609 | * SSLv3: |
| 610 | * block = |
| 611 | * MD5( secret + SHA1( 'A' + secret + random ) ) + |
| 612 | * MD5( secret + SHA1( 'BB' + secret + random ) ) + |
| 613 | * MD5( secret + SHA1( 'CCC' + secret + random ) ) + |
| 614 | * ... |
| 615 | */ |
| 616 | for( i = 0; i < dlen / 16; i++ ) |
| 617 | { |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 618 | memset( padding, (unsigned char) ('A' + i), 1 + i ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 619 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 620 | if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 621 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 622 | if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 623 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 624 | if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 625 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 626 | if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 627 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 628 | if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 629 | goto exit; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 630 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 631 | if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 632 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 633 | if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 634 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 635 | if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 636 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 637 | if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 638 | goto exit; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 641 | exit: |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 642 | mbedtls_md5_free( &md5 ); |
| 643 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 644 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 645 | mbedtls_platform_zeroize( padding, sizeof( padding ) ); |
| 646 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 647 | |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 648 | return( ret ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 649 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 650 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 651 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 652 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 653 | static int tls1_prf( const unsigned char *secret, size_t slen, |
| 654 | const char *label, |
| 655 | const unsigned char *random, size_t rlen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 656 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 657 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 658 | size_t nb, hs; |
| 659 | size_t i, j, k; |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 660 | const unsigned char *S1, *S2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 661 | unsigned char tmp[128]; |
| 662 | unsigned char h_i[20]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 663 | const mbedtls_md_info_t *md_info; |
| 664 | mbedtls_md_context_t md_ctx; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 665 | int ret; |
| 666 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 667 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 668 | |
| 669 | if( sizeof( tmp ) < 20 + strlen( label ) + rlen ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 670 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 671 | |
| 672 | hs = ( slen + 1 ) / 2; |
| 673 | S1 = secret; |
| 674 | S2 = secret + slen - hs; |
| 675 | |
| 676 | nb = strlen( label ); |
| 677 | memcpy( tmp + 20, label, nb ); |
| 678 | memcpy( tmp + 20 + nb, random, rlen ); |
| 679 | nb += rlen; |
| 680 | |
| 681 | /* |
| 682 | * First compute P_md5(secret,label+random)[0..dlen] |
| 683 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 684 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL ) |
| 685 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 686 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 687 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 688 | return( ret ); |
| 689 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 690 | mbedtls_md_hmac_starts( &md_ctx, S1, hs ); |
| 691 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); |
| 692 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 693 | |
| 694 | for( i = 0; i < dlen; i += 16 ) |
| 695 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 696 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 697 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb ); |
| 698 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 699 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 700 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 701 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 ); |
| 702 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 703 | |
| 704 | k = ( i + 16 > dlen ) ? dlen % 16 : 16; |
| 705 | |
| 706 | for( j = 0; j < k; j++ ) |
| 707 | dstbuf[i + j] = h_i[j]; |
| 708 | } |
| 709 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 710 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 711 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 712 | /* |
| 713 | * XOR out with P_sha1(secret,label+random)[0..dlen] |
| 714 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 715 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL ) |
| 716 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 717 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 718 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 719 | return( ret ); |
| 720 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 721 | mbedtls_md_hmac_starts( &md_ctx, S2, hs ); |
| 722 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); |
| 723 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 724 | |
| 725 | for( i = 0; i < dlen; i += 20 ) |
| 726 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 727 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 728 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb ); |
| 729 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 730 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 731 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 732 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 ); |
| 733 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 734 | |
| 735 | k = ( i + 20 > dlen ) ? dlen % 20 : 20; |
| 736 | |
| 737 | for( j = 0; j < k; j++ ) |
| 738 | dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] ); |
| 739 | } |
| 740 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 741 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 742 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 743 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 744 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 745 | |
| 746 | return( 0 ); |
| 747 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 748 | #endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 749 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 750 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 751 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 752 | const unsigned char *secret, size_t slen, |
| 753 | const char *label, |
| 754 | const unsigned char *random, size_t rlen, |
| 755 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 756 | { |
| 757 | size_t nb; |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 758 | size_t i, j, k, md_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 759 | unsigned char tmp[128]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 760 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 761 | const mbedtls_md_info_t *md_info; |
| 762 | mbedtls_md_context_t md_ctx; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 763 | int ret; |
| 764 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 765 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 766 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 767 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 768 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 769 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 770 | md_len = mbedtls_md_get_size( md_info ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 771 | |
| 772 | if( sizeof( tmp ) < md_len + strlen( label ) + rlen ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 773 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 774 | |
| 775 | nb = strlen( label ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 776 | memcpy( tmp + md_len, label, nb ); |
| 777 | memcpy( tmp + md_len + nb, random, rlen ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 778 | nb += rlen; |
| 779 | |
| 780 | /* |
| 781 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 782 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 783 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 784 | return( ret ); |
| 785 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 786 | mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 787 | mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 788 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 789 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 790 | for( i = 0; i < dlen; i += md_len ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 791 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 792 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 793 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 794 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 795 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 796 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 797 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 798 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 799 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 800 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 801 | |
| 802 | for( j = 0; j < k; j++ ) |
| 803 | dstbuf[i + j] = h_i[j]; |
| 804 | } |
| 805 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 806 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 807 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 808 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 809 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 810 | |
| 811 | return( 0 ); |
| 812 | } |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 813 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 814 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 815 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 816 | const char *label, |
| 817 | const unsigned char *random, size_t rlen, |
| 818 | unsigned char *dstbuf, size_t dlen ) |
| 819 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 820 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 821 | label, random, rlen, dstbuf, dlen ) ); |
| 822 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 823 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 824 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 825 | #if defined(MBEDTLS_SHA512_C) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 826 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 827 | const char *label, |
| 828 | const unsigned char *random, size_t rlen, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 829 | unsigned char *dstbuf, size_t dlen ) |
| 830 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 831 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 832 | label, random, rlen, dstbuf, dlen ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 833 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 834 | #endif /* MBEDTLS_SHA512_C */ |
| 835 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 836 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 837 | static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 838 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 839 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 840 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 841 | static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 842 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 843 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 844 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 845 | static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 846 | static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 847 | #endif |
| 848 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 849 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 850 | static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 851 | static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 852 | #endif |
| 853 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 854 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 855 | #if defined(MBEDTLS_SHA256_C) |
| 856 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 857 | static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 858 | static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 859 | #endif |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 860 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 861 | #if defined(MBEDTLS_SHA512_C) |
| 862 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 863 | static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 864 | static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 865 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 866 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 867 | |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 868 | /* Type for the TLS PRF */ |
| 869 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, |
| 870 | const unsigned char *, size_t, |
| 871 | unsigned char *, size_t); |
| 872 | |
Manuel Pégourié-Gonnard | 5ed5e90 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 873 | /* |
Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 874 | * Populate a transform structure with session keys and all the other |
| 875 | * necessary information. |
Manuel Pégourié-Gonnard | 5ed5e90 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 876 | * |
Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 877 | * Parameters: |
| 878 | * - [in/out]: transform: structure to populate |
| 879 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 880 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 881 | * - [in] ciphersuite |
| 882 | * - [in] master |
| 883 | * - [in] encrypt_then_mac |
| 884 | * - [in] trunc_hmac |
| 885 | * - [in] compression |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 886 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 887 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 888 | * - [in] minor_ver: SSL/TLS minor version |
| 889 | * - [in] endpoint: client or server |
| 890 | * - [in] ssl: optionally used for: |
| 891 | * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context |
| 892 | * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys |
| 893 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
Manuel Pégourié-Gonnard | 5ed5e90 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 894 | */ |
Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 895 | static int ssl_populate_transform( mbedtls_ssl_transform *transform, |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 896 | int ciphersuite, |
| 897 | const unsigned char master[48], |
Jaeden Amero | 2eaf2c7 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 898 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 899 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 900 | int encrypt_then_mac, |
| 901 | #endif |
| 902 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 903 | int trunc_hmac, |
| 904 | #endif |
Jaeden Amero | 2eaf2c7 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 905 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 906 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 907 | int compression, |
| 908 | #endif |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 909 | ssl_tls_prf_t tls_prf, |
| 910 | const unsigned char randbytes[64], |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 911 | int minor_ver, |
| 912 | unsigned endpoint, |
Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 913 | const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 914 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 915 | int ret = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 916 | unsigned char keyblk[256]; |
| 917 | unsigned char *key1; |
| 918 | unsigned char *key2; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 919 | unsigned char *mac_enc; |
| 920 | unsigned char *mac_dec; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 921 | size_t mac_key_len; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 922 | size_t iv_copy_len; |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 923 | unsigned keylen; |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 924 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 925 | const mbedtls_cipher_info_t *cipher_info; |
| 926 | const mbedtls_md_info_t *md_info; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 927 | |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 928 | #if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \ |
| 929 | !defined(MBEDTLS_SSL_EXPORT_KEYS) && \ |
| 930 | !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | 86e48c2 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 931 | ssl = NULL; /* make sure we don't use it except for those cases */ |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 932 | (void) ssl; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 933 | #endif |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 934 | |
Manuel Pégourié-Gonnard | a3024ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 935 | /* |
| 936 | * Some data just needs copying into the structure |
| 937 | */ |
Jaeden Amero | 2eaf2c7 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 938 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
| 939 | defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 940 | transform->encrypt_then_mac = encrypt_then_mac; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 941 | #endif |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 942 | |
| 943 | #if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER) |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 944 | transform->minor_ver = minor_ver; |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 945 | #else |
| 946 | ((void) minor_ver); |
| 947 | #endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 948 | |
Manuel Pégourié-Gonnard | a3024ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 949 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 950 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); |
| 951 | #endif |
| 952 | |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 953 | /* |
| 954 | * Get various info structures |
| 955 | */ |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 956 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 957 | if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ) |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 958 | { |
| 959 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 960 | ciphersuite ) ); |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 961 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 962 | } |
| 963 | |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 964 | cipher_info = mbedtls_cipher_info_from_type( |
| 965 | mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 966 | if( cipher_info == NULL ) |
| 967 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 968 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found", |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 969 | mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 970 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 971 | } |
| 972 | |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 973 | md_info = mbedtls_md_info_from_type( |
| 974 | mbedtls_ssl_suite_get_mac( ciphersuite_info ) ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 975 | if( md_info == NULL ) |
| 976 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 977 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found", |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 978 | mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 979 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 980 | } |
| 981 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 982 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | dd0afca | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 983 | /* Copy own and peer's CID if the use of the CID |
| 984 | * extension has been negotiated. */ |
| 985 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 986 | { |
| 987 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
Hanno Becker | d91dc37 | 2019-04-30 13:52:29 +0100 | [diff] [blame] | 988 | |
Hanno Becker | 4932f9f | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 989 | transform->in_cid_len = ssl->own_cid_len; |
Hanno Becker | 4932f9f | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 990 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
Hanno Becker | 8013b27 | 2019-05-03 12:55:51 +0100 | [diff] [blame] | 991 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
Hanno Becker | dd0afca | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 992 | transform->in_cid_len ); |
Hanno Becker | e582d12 | 2019-05-15 10:21:55 +0100 | [diff] [blame] | 993 | |
| 994 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 995 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 996 | ssl->handshake->peer_cid_len ); |
| 997 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 998 | transform->out_cid_len ); |
Hanno Becker | dd0afca | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 999 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1000 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | dd0afca | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 1001 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1002 | /* |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1003 | * Compute key block using the PRF |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 1004 | */ |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1005 | ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 1006 | if( ret != 0 ) |
| 1007 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1008 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 1009 | return( ret ); |
| 1010 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1011 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1012 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
Manuel Pégourié-Gonnard | 762d011 | 2019-05-20 10:27:20 +0200 | [diff] [blame] | 1013 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1014 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1015 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1016 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1017 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1018 | /* |
| 1019 | * Determine the appropriate key, IV and MAC length. |
| 1020 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1021 | |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1022 | keylen = cipher_info->key_bitlen / 8; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1023 | |
Hanno Becker | f122944 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1024 | #if defined(MBEDTLS_GCM_C) || \ |
| 1025 | defined(MBEDTLS_CCM_C) || \ |
| 1026 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1027 | if( cipher_info->mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1028 | cipher_info->mode == MBEDTLS_MODE_CCM || |
| 1029 | cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1030 | { |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1031 | size_t explicit_ivlen; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1032 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1033 | transform->maclen = 0; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1034 | mac_key_len = 0; |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 1035 | transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) & |
| 1036 | MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1037 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1038 | /* All modes haves 96-bit IVs; |
| 1039 | * GCM and CCM has 4 implicit and 8 explicit bytes |
| 1040 | * ChachaPoly has all 12 bytes implicit |
| 1041 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1042 | transform->ivlen = 12; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1043 | if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) |
| 1044 | transform->fixed_ivlen = 12; |
| 1045 | else |
| 1046 | transform->fixed_ivlen = 4; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1047 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1048 | /* Minimum length of encrypted record */ |
| 1049 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1050 | transform->minlen = explicit_ivlen + transform->taglen; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1051 | } |
| 1052 | else |
Hanno Becker | f122944 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1053 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 1054 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
| 1055 | if( cipher_info->mode == MBEDTLS_MODE_STREAM || |
| 1056 | cipher_info->mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1057 | { |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1058 | /* Initialize HMAC contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1059 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 1060 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1061 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1062 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1063 | return( ret ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1064 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1065 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1066 | /* Get MAC length */ |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1067 | mac_key_len = mbedtls_md_get_size( md_info ); |
| 1068 | transform->maclen = mac_key_len; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1069 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1070 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1071 | /* |
| 1072 | * If HMAC is to be truncated, we shall keep the leftmost bytes, |
| 1073 | * (rfc 6066 page 13 or rfc 2104 section 4), |
| 1074 | * so we only need to adjust the length here. |
| 1075 | */ |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1076 | if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED ) |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1077 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1078 | transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN; |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1079 | |
| 1080 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT) |
| 1081 | /* Fall back to old, non-compliant version of the truncated |
Hanno Becker | 563423f | 2017-11-21 17:20:17 +0000 | [diff] [blame] | 1082 | * HMAC implementation which also truncates the key |
| 1083 | * (Mbed TLS versions from 1.3 to 2.6.0) */ |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1084 | mac_key_len = transform->maclen; |
| 1085 | #endif |
| 1086 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1087 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1088 | |
| 1089 | /* IV length */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1090 | transform->ivlen = cipher_info->iv_size; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1091 | |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1092 | /* Minimum length */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1093 | if( cipher_info->mode == MBEDTLS_MODE_STREAM ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1094 | transform->minlen = transform->maclen; |
| 1095 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1096 | { |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1097 | /* |
| 1098 | * GenericBlockCipher: |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 1099 | * 1. if EtM is in use: one block plus MAC |
| 1100 | * otherwise: * first multiple of blocklen greater than maclen |
| 1101 | * 2. IV except for SSL3 and TLS 1.0 |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1102 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1103 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1104 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 1105 | { |
| 1106 | transform->minlen = transform->maclen |
| 1107 | + cipher_info->block_size; |
| 1108 | } |
| 1109 | else |
| 1110 | #endif |
| 1111 | { |
| 1112 | transform->minlen = transform->maclen |
| 1113 | + cipher_info->block_size |
| 1114 | - transform->maclen % cipher_info->block_size; |
| 1115 | } |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1116 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1117 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1118 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 || |
| 1119 | minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1120 | ; /* No need to adjust minlen */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1121 | else |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1122 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1123 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1124 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 || |
| 1125 | minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1126 | { |
| 1127 | transform->minlen += transform->ivlen; |
| 1128 | } |
| 1129 | else |
| 1130 | #endif |
| 1131 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1132 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1133 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1134 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1135 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1136 | } |
Hanno Becker | f122944 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1137 | else |
| 1138 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
| 1139 | { |
| 1140 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1141 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1142 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1143 | |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1144 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 1145 | (unsigned) keylen, |
| 1146 | (unsigned) transform->minlen, |
| 1147 | (unsigned) transform->ivlen, |
| 1148 | (unsigned) transform->maclen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1149 | |
| 1150 | /* |
| 1151 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 1152 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1153 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1154 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1155 | { |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1156 | key1 = keyblk + mac_key_len * 2; |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1157 | key2 = keyblk + mac_key_len * 2 + keylen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1158 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1159 | mac_enc = keyblk; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1160 | mac_dec = keyblk + mac_key_len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1161 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1162 | /* |
| 1163 | * This is not used in TLS v1.1. |
| 1164 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1165 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 1166 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1167 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 1168 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1169 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1170 | } |
| 1171 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1172 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 1173 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1174 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1175 | { |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1176 | key1 = keyblk + mac_key_len * 2 + keylen; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1177 | key2 = keyblk + mac_key_len * 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1178 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1179 | mac_enc = keyblk + mac_key_len; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1180 | mac_dec = keyblk; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1181 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1182 | /* |
| 1183 | * This is not used in TLS v1.1. |
| 1184 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1185 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 1186 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1187 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 1188 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1189 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1190 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1191 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1192 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1193 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1194 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1195 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1196 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1197 | |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1198 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1199 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1200 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1201 | { |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1202 | if( mac_key_len > sizeof( transform->mac_enc ) ) |
Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1203 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1204 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1205 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1206 | } |
| 1207 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1208 | memcpy( transform->mac_enc, mac_enc, mac_key_len ); |
| 1209 | memcpy( transform->mac_dec, mac_dec, mac_key_len ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1210 | } |
| 1211 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1212 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 1213 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 1214 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1215 | if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1216 | { |
Gilles Peskine | 039fd12 | 2018-03-19 19:06:08 +0100 | [diff] [blame] | 1217 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 1218 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 1219 | if( mac_key_len != 0 ) |
| 1220 | { |
| 1221 | mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 1222 | mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 1223 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1224 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1225 | else |
| 1226 | #endif |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1227 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1228 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1229 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1230 | } |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1231 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1232 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1233 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 1234 | if( mbedtls_ssl_hw_record_init != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1235 | { |
| 1236 | int ret = 0; |
| 1237 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1238 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1239 | |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1240 | if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen, |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 1241 | transform->iv_enc, transform->iv_dec, |
| 1242 | iv_copy_len, |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1243 | mac_enc, mac_dec, |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1244 | mac_key_len ) ) != 0 ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1245 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1246 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret ); |
| 1247 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1248 | } |
| 1249 | } |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1250 | #else |
| 1251 | ((void) mac_dec); |
| 1252 | ((void) mac_enc); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1253 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1254 | |
Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1255 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 1256 | if( ssl->conf->f_export_keys != NULL ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1257 | { |
Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1258 | ssl->conf->f_export_keys( ssl->conf->p_export_keys, |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1259 | master, keyblk, |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1260 | mac_key_len, keylen, |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1261 | iv_copy_len ); |
| 1262 | } |
| 1263 | #endif |
| 1264 | |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1265 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1266 | cipher_info ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1267 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1268 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1269 | return( ret ); |
| 1270 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1271 | |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1272 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1273 | cipher_info ) ) != 0 ) |
| 1274 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1275 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1276 | return( ret ); |
| 1277 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1278 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1279 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1280 | cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1281 | MBEDTLS_ENCRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1282 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1283 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1284 | return( ret ); |
| 1285 | } |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1286 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1287 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1288 | cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1289 | MBEDTLS_DECRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1290 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1291 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1292 | return( ret ); |
| 1293 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1294 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1295 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 1296 | if( cipher_info->mode == MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1297 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1298 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 1299 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1300 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1301 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1302 | return( ret ); |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1303 | } |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1304 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1305 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 1306 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1307 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1308 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1309 | return( ret ); |
| 1310 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1311 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1312 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1313 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 1314 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1315 | |
Manuel Pégourié-Gonnard | a1abb26 | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1316 | /* Initialize Zlib contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1317 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1318 | if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1319 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1320 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1321 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1322 | memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) ); |
| 1323 | memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1324 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1325 | if( deflateInit( &transform->ctx_deflate, |
| 1326 | Z_DEFAULT_COMPRESSION ) != Z_OK || |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1327 | inflateInit( &transform->ctx_inflate ) != Z_OK ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1328 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1329 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) ); |
| 1330 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1331 | } |
| 1332 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1333 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1334 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1335 | return( 0 ); |
| 1336 | } |
| 1337 | |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1338 | /* |
Manuel Pégourié-Gonnard | 42c814f | 2019-05-20 10:10:17 +0200 | [diff] [blame] | 1339 | * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1340 | * |
| 1341 | * Inputs: |
| 1342 | * - SSL/TLS minor version |
| 1343 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 1344 | * |
Manuel Pégourié-Gonnard | cf31216 | 2019-05-10 10:25:00 +0200 | [diff] [blame] | 1345 | * Outputs: |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1346 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 1347 | */ |
| 1348 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
| 1349 | int minor_ver, |
| 1350 | mbedtls_md_type_t hash ) |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1351 | { |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1352 | #if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C) |
| 1353 | (void) hash; |
| 1354 | #endif |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1355 | |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1356 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1357 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1358 | { |
| 1359 | handshake->tls_prf = ssl3_prf; |
| 1360 | handshake->calc_verify = ssl_calc_verify_ssl; |
| 1361 | handshake->calc_finished = ssl_calc_finished_ssl; |
| 1362 | } |
| 1363 | else |
| 1364 | #endif |
| 1365 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1366 | if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1367 | { |
| 1368 | handshake->tls_prf = tls1_prf; |
| 1369 | handshake->calc_verify = ssl_calc_verify_tls; |
| 1370 | handshake->calc_finished = ssl_calc_finished_tls; |
| 1371 | } |
| 1372 | else |
| 1373 | #endif |
| 1374 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1375 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1376 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
| 1377 | hash == MBEDTLS_MD_SHA384 ) |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1378 | { |
| 1379 | handshake->tls_prf = tls_prf_sha384; |
| 1380 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 1381 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 1382 | } |
| 1383 | else |
| 1384 | #endif |
| 1385 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1386 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1387 | { |
| 1388 | handshake->tls_prf = tls_prf_sha256; |
| 1389 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 1390 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 1391 | } |
| 1392 | else |
| 1393 | #endif |
| 1394 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1395 | { |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1396 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1397 | } |
| 1398 | |
| 1399 | return( 0 ); |
| 1400 | } |
| 1401 | |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1402 | /* |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1403 | * Compute master secret if needed |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1404 | * |
| 1405 | * Parameters: |
| 1406 | * [in/out] handshake |
| 1407 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
| 1408 | * [out] premaster (cleared) |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1409 | * [out] master |
| 1410 | * [in] ssl: optionally used for debugging and calc_verify |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1411 | */ |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1412 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1413 | unsigned char *master, |
Manuel Pégourié-Gonnard | ed3b7a9 | 2019-05-03 09:58:33 +0200 | [diff] [blame] | 1414 | const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1415 | { |
| 1416 | int ret; |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1417 | |
| 1418 | #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 86e48c2 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 1419 | ssl = NULL; /* make sure we don't use it except for debug and EMS */ |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1420 | (void) ssl; |
| 1421 | #endif |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1422 | |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 1423 | #if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION) |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1424 | if( handshake->resume != 0 ) |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1425 | { |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1426 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
| 1427 | return( 0 ); |
| 1428 | } |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 1429 | #endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */ |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1430 | |
| 1431 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster, |
| 1432 | handshake->pmslen ); |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1433 | |
| 1434 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Hanno Becker | a49ec56 | 2019-06-11 14:47:55 +0100 | [diff] [blame] | 1435 | if( mbedtls_ssl_hs_get_extended_ms( handshake ) |
| 1436 | == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1437 | { |
| 1438 | unsigned char session_hash[48]; |
| 1439 | size_t hash_len; |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1440 | |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1441 | handshake->calc_verify( ssl, session_hash, &hash_len ); |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1442 | |
Manuel Pégourié-Gonnard | 9c5bcc9 | 2019-05-20 12:09:50 +0200 | [diff] [blame] | 1443 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
| 1444 | session_hash, hash_len ); |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1445 | |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1446 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1447 | "extended master secret", |
| 1448 | session_hash, hash_len, |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1449 | master, 48 ); |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1450 | } |
| 1451 | else |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1452 | #endif |
Manuel Pégourié-Gonnard | bcf258e | 2019-05-03 11:46:27 +0200 | [diff] [blame] | 1453 | { |
| 1454 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
| 1455 | "master secret", |
| 1456 | handshake->randbytes, 64, |
| 1457 | master, 48 ); |
| 1458 | } |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1459 | if( ret != 0 ) |
| 1460 | { |
| 1461 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 1462 | return( ret ); |
| 1463 | } |
| 1464 | |
| 1465 | mbedtls_platform_zeroize( handshake->premaster, |
| 1466 | sizeof(handshake->premaster) ); |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1467 | |
| 1468 | return( 0 ); |
| 1469 | } |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1470 | |
Manuel Pégourié-Gonnard | 5ed5e90 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1471 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 1472 | { |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1473 | int ret; |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 1474 | mbedtls_ssl_ciphersuite_handle_t const ciphersuite_info = |
| 1475 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1476 | |
Manuel Pégourié-Gonnard | 707728d | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1477 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 1478 | |
| 1479 | /* Set PRF, calc_verify and calc_finished function pointers */ |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1480 | ret = ssl_set_handshake_prfs( ssl->handshake, |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 1481 | mbedtls_ssl_get_minor_ver( ssl ), |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 1482 | mbedtls_ssl_suite_get_mac( ciphersuite_info ) ); |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1483 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1484 | { |
| 1485 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1486 | return( ret ); |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1487 | } |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1488 | |
Manuel Pégourié-Gonnard | 707728d | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1489 | /* Compute master secret if needed */ |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1490 | ret = ssl_compute_master( ssl->handshake, |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1491 | ssl->session_negotiate->master, |
| 1492 | ssl ); |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1493 | if( ret != 0 ) |
| 1494 | { |
| 1495 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 1496 | return( ret ); |
| 1497 | } |
| 1498 | |
Manuel Pégourié-Gonnard | 707728d | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1499 | /* Swap the client and server random values: |
| 1500 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 1501 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 1502 | { |
| 1503 | unsigned char tmp[64]; |
| 1504 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 1505 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 1506 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 1507 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 1508 | } |
| 1509 | |
| 1510 | /* Populate transform structure */ |
Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1511 | ret = ssl_populate_transform( ssl->transform_negotiate, |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1512 | mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ), |
| 1513 | ssl->session_negotiate->master, |
Jaeden Amero | 2eaf2c7 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 1514 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1515 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1516 | ssl->session_negotiate->encrypt_then_mac, |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1517 | #endif |
| 1518 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1519 | ssl->session_negotiate->trunc_hmac, |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1520 | #endif |
Jaeden Amero | 2eaf2c7 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 1521 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1522 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1523 | ssl->session_negotiate->compression, |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1524 | #endif |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1525 | ssl->handshake->tls_prf, |
| 1526 | ssl->handshake->randbytes, |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 1527 | mbedtls_ssl_get_minor_ver( ssl ), |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1528 | mbedtls_ssl_conf_get_endpoint( ssl->conf ), |
| 1529 | ssl ); |
Manuel Pégourié-Gonnard | 707728d | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1530 | if( ret != 0 ) |
| 1531 | { |
| 1532 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret ); |
| 1533 | return( ret ); |
| 1534 | } |
| 1535 | |
| 1536 | /* We no longer need Server/ClientHello.random values */ |
| 1537 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 1538 | sizeof( ssl->handshake->randbytes ) ); |
| 1539 | |
Manuel Pégourié-Gonnard | a1abb26 | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1540 | /* Allocate compression buffer */ |
| 1541 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 1542 | if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE && |
| 1543 | ssl->compress_buf == NULL ) |
| 1544 | { |
| 1545 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) ); |
| 1546 | ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN ); |
| 1547 | if( ssl->compress_buf == NULL ) |
| 1548 | { |
| 1549 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
Manuel Pégourié-Gonnard | 762d011 | 2019-05-20 10:27:20 +0200 | [diff] [blame] | 1550 | MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) ); |
Manuel Pégourié-Gonnard | a1abb26 | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1551 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1552 | } |
| 1553 | } |
| 1554 | #endif |
| 1555 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1556 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 1557 | |
| 1558 | return( 0 ); |
| 1559 | } |
| 1560 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1561 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1562 | void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl, |
| 1563 | unsigned char hash[36], |
| 1564 | size_t *hlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1565 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1566 | mbedtls_md5_context md5; |
| 1567 | mbedtls_sha1_context sha1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1568 | unsigned char pad_1[48]; |
| 1569 | unsigned char pad_2[48]; |
| 1570 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1571 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1572 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1573 | mbedtls_md5_init( &md5 ); |
| 1574 | mbedtls_sha1_init( &sha1 ); |
| 1575 | |
| 1576 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 1577 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1578 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1579 | memset( pad_1, 0x36, 48 ); |
| 1580 | memset( pad_2, 0x5C, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1581 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1582 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); |
| 1583 | mbedtls_md5_update_ret( &md5, pad_1, 48 ); |
| 1584 | mbedtls_md5_finish_ret( &md5, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1585 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1586 | mbedtls_md5_starts_ret( &md5 ); |
| 1587 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); |
| 1588 | mbedtls_md5_update_ret( &md5, pad_2, 48 ); |
| 1589 | mbedtls_md5_update_ret( &md5, hash, 16 ); |
| 1590 | mbedtls_md5_finish_ret( &md5, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1591 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1592 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); |
| 1593 | mbedtls_sha1_update_ret( &sha1, pad_1, 40 ); |
| 1594 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1595 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1596 | mbedtls_sha1_starts_ret( &sha1 ); |
| 1597 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); |
| 1598 | mbedtls_sha1_update_ret( &sha1, pad_2, 40 ); |
| 1599 | mbedtls_sha1_update_ret( &sha1, hash + 16, 20 ); |
| 1600 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1601 | |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1602 | *hlen = 36; |
| 1603 | |
| 1604 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1605 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1606 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1607 | mbedtls_md5_free( &md5 ); |
| 1608 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1609 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1610 | return; |
| 1611 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1612 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1613 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1614 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1615 | void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl, |
| 1616 | unsigned char hash[36], |
| 1617 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1618 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1619 | mbedtls_md5_context md5; |
| 1620 | mbedtls_sha1_context sha1; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1621 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1622 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1623 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1624 | mbedtls_md5_init( &md5 ); |
| 1625 | mbedtls_sha1_init( &sha1 ); |
| 1626 | |
| 1627 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 1628 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1629 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1630 | mbedtls_md5_finish_ret( &md5, hash ); |
| 1631 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1632 | |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1633 | *hlen = 36; |
| 1634 | |
| 1635 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1636 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1637 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1638 | mbedtls_md5_free( &md5 ); |
| 1639 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1640 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1641 | return; |
| 1642 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1643 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1644 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1645 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1646 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1647 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
| 1648 | unsigned char hash[32], |
| 1649 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1650 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1651 | mbedtls_sha256_context sha256; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1652 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1653 | mbedtls_sha256_init( &sha256 ); |
| 1654 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1655 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1656 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1657 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1658 | mbedtls_sha256_finish_ret( &sha256, hash ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1659 | |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1660 | *hlen = 32; |
| 1661 | |
| 1662 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1663 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1664 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1665 | mbedtls_sha256_free( &sha256 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1666 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1667 | return; |
| 1668 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1669 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1670 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1671 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1672 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
| 1673 | unsigned char hash[48], |
| 1674 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1675 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1676 | mbedtls_sha512_context sha512; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1677 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1678 | mbedtls_sha512_init( &sha512 ); |
| 1679 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1680 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1681 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1682 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1683 | mbedtls_sha512_finish_ret( &sha512, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1684 | |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1685 | *hlen = 48; |
| 1686 | |
| 1687 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1688 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1689 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1690 | mbedtls_sha512_free( &sha512 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1691 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1692 | return; |
| 1693 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1694 | #endif /* MBEDTLS_SHA512_C */ |
| 1695 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1696 | |
Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 1697 | int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl ) |
| 1698 | { |
| 1699 | int ret; |
| 1700 | |
| 1701 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
| 1702 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
| 1703 | |
Hanno Becker | a3c2c17 | 2019-07-23 16:51:57 +0100 | [diff] [blame^] | 1704 | #if defined(MBEDTLS_USE_TINYCRYPT) |
| 1705 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 1706 | == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || |
| 1707 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 1708 | == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ) |
| 1709 | { |
| 1710 | const struct uECC_Curve_t * uecc_curve = uECC_secp256r1(); |
| 1711 | |
| 1712 | if( !uECC_shared_secret( ssl->handshake->ecdh_peerkey, |
| 1713 | ssl->handshake->ecdh_privkey, |
| 1714 | ssl->handshake->premaster, |
| 1715 | uecc_curve ) ) |
| 1716 | { |
| 1717 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1718 | } |
| 1719 | |
| 1720 | ssl->handshake->pmslen = NUM_ECC_BYTES; |
| 1721 | } |
| 1722 | else |
| 1723 | #endif |
Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 1724 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) |
| 1725 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 1726 | == MBEDTLS_KEY_EXCHANGE_DHE_RSA ) |
| 1727 | { |
| 1728 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
| 1729 | ssl->handshake->premaster, |
| 1730 | MBEDTLS_PREMASTER_SIZE, |
| 1731 | &ssl->handshake->pmslen, |
| 1732 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 1733 | mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 ) |
Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 1734 | { |
| 1735 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
| 1736 | return( ret ); |
| 1737 | } |
| 1738 | |
| 1739 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
| 1740 | } |
| 1741 | else |
| 1742 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */ |
| 1743 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 1744 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ |
| 1745 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 1746 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
| 1747 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 1748 | == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || |
| 1749 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 1750 | == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || |
| 1751 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 1752 | == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || |
| 1753 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 1754 | == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) |
| 1755 | { |
| 1756 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, |
| 1757 | &ssl->handshake->pmslen, |
| 1758 | ssl->handshake->premaster, |
| 1759 | MBEDTLS_MPI_MAX_SIZE, |
| 1760 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 1761 | mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 ) |
Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 1762 | { |
| 1763 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
| 1764 | return( ret ); |
| 1765 | } |
| 1766 | |
| 1767 | MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z ); |
| 1768 | } |
| 1769 | else |
| 1770 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
| 1771 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || |
| 1772 | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || |
| 1773 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
| 1774 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 1775 | if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) ) |
| 1776 | { |
| 1777 | if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, |
Manuel Pégourié-Gonnard | fb02e96 | 2019-08-01 10:48:49 +0200 | [diff] [blame] | 1778 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ) ) != 0 ) |
Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 1779 | { |
| 1780 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret ); |
| 1781 | return( ret ); |
| 1782 | } |
| 1783 | } |
| 1784 | else |
| 1785 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
| 1786 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 1787 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == |
| 1788 | MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
| 1789 | { |
| 1790 | ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx, |
| 1791 | ssl->handshake->premaster, 32, &ssl->handshake->pmslen, |
| 1792 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 1793 | mbedtls_ssl_conf_get_prng( ssl->conf ) ); |
Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 1794 | if( ret != 0 ) |
| 1795 | { |
| 1796 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret ); |
| 1797 | return( ret ); |
| 1798 | } |
| 1799 | } |
| 1800 | else |
| 1801 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
| 1802 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) |
| 1803 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 1804 | == MBEDTLS_KEY_EXCHANGE_RSA ) |
| 1805 | { |
| 1806 | ((void) ret); |
Manuel Pégourié-Gonnard | 8793fab | 2019-08-01 10:44:07 +0200 | [diff] [blame] | 1807 | /* The premaster secret has already been set by |
Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 1808 | * ssl_rsa_generate_partial_pms(). Only the |
| 1809 | * PMS length needs to be set. */ |
| 1810 | ssl->handshake->pmslen = 48; |
| 1811 | } |
| 1812 | else |
| 1813 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
| 1814 | { |
| 1815 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1816 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1817 | } |
| 1818 | |
| 1819 | return( 0 ); |
| 1820 | } |
| 1821 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1822 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 1823 | int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1824 | { |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1825 | unsigned char *p = ssl->handshake->premaster; |
| 1826 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1827 | const unsigned char *psk = ssl->conf->psk; |
| 1828 | size_t psk_len = ssl->conf->psk_len; |
| 1829 | |
| 1830 | /* If the psk callback was called, use its result */ |
| 1831 | if( ssl->handshake->psk != NULL ) |
| 1832 | { |
| 1833 | psk = ssl->handshake->psk; |
| 1834 | psk_len = ssl->handshake->psk_len; |
| 1835 | } |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1836 | |
| 1837 | /* |
| 1838 | * PMS = struct { |
| 1839 | * opaque other_secret<0..2^16-1>; |
| 1840 | * opaque psk<0..2^16-1>; |
| 1841 | * }; |
| 1842 | * with "other_secret" depending on the particular key exchange |
| 1843 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1844 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1845 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1846 | { |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1847 | if( end - p < 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1848 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1849 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1850 | *(p++) = (unsigned char)( psk_len >> 8 ); |
| 1851 | *(p++) = (unsigned char)( psk_len ); |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1852 | |
| 1853 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 1854 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1855 | |
| 1856 | memset( p, 0, psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1857 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1858 | } |
| 1859 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1860 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 1861 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 1862 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1863 | { |
| 1864 | /* |
| 1865 | * other_secret already set by the ClientKeyExchange message, |
| 1866 | * and is 48 bytes long |
| 1867 | */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1868 | if( end - p < 2 ) |
| 1869 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1870 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1871 | *p++ = 0; |
| 1872 | *p++ = 48; |
| 1873 | p += 48; |
| 1874 | } |
| 1875 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1876 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 1877 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 1878 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1879 | { |
Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 1880 | int ret; |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1881 | size_t len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1882 | |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1883 | /* Write length only when we know the actual value */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1884 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1885 | p + 2, end - ( p + 2 ), &len, |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 1886 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 1887 | mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1888 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1889 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1890 | return( ret ); |
| 1891 | } |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1892 | *(p++) = (unsigned char)( len >> 8 ); |
| 1893 | *(p++) = (unsigned char)( len ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1894 | p += len; |
| 1895 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1896 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1897 | } |
| 1898 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1899 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 1900 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 1901 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1902 | { |
Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 1903 | int ret; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1904 | size_t zlen; |
| 1905 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1906 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1907 | p + 2, end - ( p + 2 ), |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 1908 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 1909 | mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1910 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1911 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1912 | return( ret ); |
| 1913 | } |
| 1914 | |
| 1915 | *(p++) = (unsigned char)( zlen >> 8 ); |
| 1916 | *(p++) = (unsigned char)( zlen ); |
| 1917 | p += zlen; |
| 1918 | |
Janos Follath | 3fbdada | 2018-08-15 10:26:53 +0100 | [diff] [blame] | 1919 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 1920 | MBEDTLS_DEBUG_ECDH_Z ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1921 | } |
| 1922 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1923 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1924 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1925 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1926 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | /* opaque psk<0..2^16-1>; */ |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1930 | if( end - p < 2 ) |
| 1931 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 1932 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1933 | *(p++) = (unsigned char)( psk_len >> 8 ); |
| 1934 | *(p++) = (unsigned char)( psk_len ); |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1935 | |
| 1936 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 1937 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1938 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1939 | memcpy( p, psk, psk_len ); |
| 1940 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1941 | |
| 1942 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 1943 | |
| 1944 | return( 0 ); |
| 1945 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1946 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1947 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1948 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1949 | /* |
| 1950 | * SSLv3.0 MAC functions |
| 1951 | */ |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 1952 | #define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */ |
Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 1953 | static void ssl_mac( mbedtls_md_context_t *md_ctx, |
| 1954 | const unsigned char *secret, |
| 1955 | const unsigned char *buf, size_t len, |
| 1956 | const unsigned char *ctr, int type, |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 1957 | unsigned char out[SSL_MAC_MAX_BYTES] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1958 | { |
| 1959 | unsigned char header[11]; |
| 1960 | unsigned char padding[48]; |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 1961 | int padlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1962 | int md_size = mbedtls_md_get_size( md_ctx->md_info ); |
| 1963 | int md_type = mbedtls_md_get_type( md_ctx->md_info ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1964 | |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 1965 | /* Only MD5 and SHA-1 supported */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1966 | if( md_type == MBEDTLS_MD_MD5 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1967 | padlen = 48; |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 1968 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1969 | padlen = 40; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1970 | |
| 1971 | memcpy( header, ctr, 8 ); |
| 1972 | header[ 8] = (unsigned char) type; |
| 1973 | header[ 9] = (unsigned char)( len >> 8 ); |
| 1974 | header[10] = (unsigned char)( len ); |
| 1975 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1976 | memset( padding, 0x36, padlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1977 | mbedtls_md_starts( md_ctx ); |
| 1978 | mbedtls_md_update( md_ctx, secret, md_size ); |
| 1979 | mbedtls_md_update( md_ctx, padding, padlen ); |
| 1980 | mbedtls_md_update( md_ctx, header, 11 ); |
| 1981 | mbedtls_md_update( md_ctx, buf, len ); |
Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 1982 | mbedtls_md_finish( md_ctx, out ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1983 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1984 | memset( padding, 0x5C, padlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1985 | mbedtls_md_starts( md_ctx ); |
| 1986 | mbedtls_md_update( md_ctx, secret, md_size ); |
| 1987 | mbedtls_md_update( md_ctx, padding, padlen ); |
Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 1988 | mbedtls_md_update( md_ctx, out, md_size ); |
| 1989 | mbedtls_md_finish( md_ctx, out ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 1990 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1991 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 1992 | |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1993 | /* The function below is only used in the Lucky 13 counter-measure in |
Hanno Becker | 30d02cd | 2018-10-18 15:43:13 +0100 | [diff] [blame] | 1994 | * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */ |
Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 1995 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \ |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1996 | ( defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 1997 | defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 1998 | defined(MBEDTLS_SSL_PROTO_TLS1_2) ) |
| 1999 | /* This function makes sure every byte in the memory region is accessed |
| 2000 | * (in ascending addresses order) */ |
| 2001 | static void ssl_read_memory( unsigned char *p, size_t len ) |
| 2002 | { |
| 2003 | unsigned char acc = 0; |
| 2004 | volatile unsigned char force; |
| 2005 | |
| 2006 | for( ; len != 0; p++, len-- ) |
| 2007 | acc ^= *p; |
| 2008 | |
| 2009 | force = acc; |
| 2010 | (void) force; |
| 2011 | } |
| 2012 | #endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */ |
| 2013 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 2014 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2015 | * Encryption/decryption functions |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 2016 | */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2017 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2018 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 8969369 | 2019-05-20 15:06:12 +0100 | [diff] [blame] | 2019 | /* This functions transforms a DTLS plaintext fragment and a record content |
| 2020 | * type into an instance of the DTLSInnerPlaintext structure: |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2021 | * |
| 2022 | * struct { |
| 2023 | * opaque content[DTLSPlaintext.length]; |
| 2024 | * ContentType real_type; |
| 2025 | * uint8 zeros[length_of_padding]; |
| 2026 | * } DTLSInnerPlaintext; |
| 2027 | * |
| 2028 | * Input: |
| 2029 | * - `content`: The beginning of the buffer holding the |
| 2030 | * plaintext to be wrapped. |
| 2031 | * - `*content_size`: The length of the plaintext in Bytes. |
| 2032 | * - `max_len`: The number of Bytes available starting from |
| 2033 | * `content`. This must be `>= *content_size`. |
| 2034 | * - `rec_type`: The desired record content type. |
| 2035 | * |
| 2036 | * Output: |
| 2037 | * - `content`: The beginning of the resulting DTLSInnerPlaintext structure. |
| 2038 | * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure. |
| 2039 | * |
| 2040 | * Returns: |
| 2041 | * - `0` on success. |
| 2042 | * - A negative error code if `max_len` didn't offer enough space |
| 2043 | * for the expansion. |
| 2044 | */ |
| 2045 | static int ssl_cid_build_inner_plaintext( unsigned char *content, |
| 2046 | size_t *content_size, |
| 2047 | size_t remaining, |
| 2048 | uint8_t rec_type ) |
| 2049 | { |
| 2050 | size_t len = *content_size; |
Hanno Becker | 7842609 | 2019-05-13 15:31:17 +0100 | [diff] [blame] | 2051 | size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY - |
| 2052 | ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) % |
| 2053 | MBEDTLS_SSL_CID_PADDING_GRANULARITY; |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2054 | |
| 2055 | /* Write real content type */ |
| 2056 | if( remaining == 0 ) |
| 2057 | return( -1 ); |
| 2058 | content[ len ] = rec_type; |
| 2059 | len++; |
| 2060 | remaining--; |
| 2061 | |
| 2062 | if( remaining < pad ) |
| 2063 | return( -1 ); |
| 2064 | memset( content + len, 0, pad ); |
| 2065 | len += pad; |
| 2066 | remaining -= pad; |
| 2067 | |
| 2068 | *content_size = len; |
| 2069 | return( 0 ); |
| 2070 | } |
| 2071 | |
Hanno Becker | 7dc2577 | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 2072 | /* This function parses a DTLSInnerPlaintext structure. |
| 2073 | * See ssl_cid_build_inner_plaintext() for details. */ |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2074 | static int ssl_cid_parse_inner_plaintext( unsigned char const *content, |
| 2075 | size_t *content_size, |
| 2076 | uint8_t *rec_type ) |
| 2077 | { |
| 2078 | size_t remaining = *content_size; |
| 2079 | |
| 2080 | /* Determine length of padding by skipping zeroes from the back. */ |
| 2081 | do |
| 2082 | { |
| 2083 | if( remaining == 0 ) |
| 2084 | return( -1 ); |
| 2085 | remaining--; |
| 2086 | } while( content[ remaining ] == 0 ); |
| 2087 | |
| 2088 | *content_size = remaining; |
| 2089 | *rec_type = content[ remaining ]; |
| 2090 | |
| 2091 | return( 0 ); |
| 2092 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2093 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2094 | |
Hanno Becker | 99abf51 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2095 | /* `add_data` must have size 13 Bytes if the CID extension is disabled, |
Hanno Becker | acadb0a | 2019-05-08 18:15:21 +0100 | [diff] [blame] | 2096 | * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2097 | static void ssl_extract_add_data_from_record( unsigned char* add_data, |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2098 | size_t *add_data_len, |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2099 | mbedtls_record *rec ) |
| 2100 | { |
Hanno Becker | 99abf51 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2101 | /* Quoting RFC 5246 (TLS 1.2): |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2102 | * |
| 2103 | * additional_data = seq_num + TLSCompressed.type + |
| 2104 | * TLSCompressed.version + TLSCompressed.length; |
| 2105 | * |
Hanno Becker | 99abf51 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2106 | * For the CID extension, this is extended as follows |
| 2107 | * (quoting draft-ietf-tls-dtls-connection-id-05, |
| 2108 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05): |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2109 | * |
| 2110 | * additional_data = seq_num + DTLSPlaintext.type + |
| 2111 | * DTLSPlaintext.version + |
Hanno Becker | 99abf51 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2112 | * cid + |
| 2113 | * cid_length + |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2114 | * length_of_DTLSInnerPlaintext; |
| 2115 | */ |
| 2116 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2117 | memcpy( add_data, rec->ctr, sizeof( rec->ctr ) ); |
| 2118 | add_data[8] = rec->type; |
Hanno Becker | 24ce1eb | 2019-05-20 15:01:46 +0100 | [diff] [blame] | 2119 | memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) ); |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2120 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2121 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 1f02f05 | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 2122 | if( rec->cid_len != 0 ) |
| 2123 | { |
| 2124 | memcpy( add_data + 11, rec->cid, rec->cid_len ); |
| 2125 | add_data[11 + rec->cid_len + 0] = rec->cid_len; |
| 2126 | add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF; |
| 2127 | add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF; |
| 2128 | *add_data_len = 13 + 1 + rec->cid_len; |
| 2129 | } |
| 2130 | else |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2131 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 1f02f05 | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 2132 | { |
| 2133 | add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF; |
| 2134 | add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF; |
| 2135 | *add_data_len = 13; |
| 2136 | } |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2139 | int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl, |
| 2140 | mbedtls_ssl_transform *transform, |
| 2141 | mbedtls_record *rec, |
| 2142 | int (*f_rng)(void *, unsigned char *, size_t), |
| 2143 | void *p_rng ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2144 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2145 | mbedtls_cipher_mode_t mode; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2146 | int auth_done = 0; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2147 | unsigned char * data; |
Hanno Becker | 28a0c4e | 2019-05-20 14:54:26 +0100 | [diff] [blame] | 2148 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ]; |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2149 | size_t add_data_len; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2150 | size_t post_avail; |
| 2151 | |
| 2152 | /* The SSL context is only used for debugging purposes! */ |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2153 | #if !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | 86e48c2 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 2154 | ssl = NULL; /* make sure we don't use it except for debug */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2155 | ((void) ssl); |
| 2156 | #endif |
| 2157 | |
| 2158 | /* The PRNG is used for dynamic IV generation that's used |
| 2159 | * for CBC transformations in TLS 1.1 and TLS 1.2. */ |
| 2160 | #if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
| 2161 | ( defined(MBEDTLS_AES_C) || \ |
| 2162 | defined(MBEDTLS_ARIA_C) || \ |
| 2163 | defined(MBEDTLS_CAMELLIA_C) ) && \ |
| 2164 | ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) ) |
| 2165 | ((void) f_rng); |
| 2166 | ((void) p_rng); |
| 2167 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2168 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2169 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2170 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2171 | if( transform == NULL ) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2172 | { |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2173 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) ); |
| 2174 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2175 | } |
Hanno Becker | 505089d | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 2176 | if( rec == NULL |
| 2177 | || rec->buf == NULL |
| 2178 | || rec->buf_len < rec->data_offset |
| 2179 | || rec->buf_len - rec->data_offset < rec->data_len |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2180 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 505089d | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 2181 | || rec->cid_len != 0 |
| 2182 | #endif |
| 2183 | ) |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2184 | { |
| 2185 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2186 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2187 | } |
| 2188 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2189 | data = rec->buf + rec->data_offset; |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2190 | post_avail = rec->buf_len - ( rec->data_len + rec->data_offset ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2191 | MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload", |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2192 | data, rec->data_len ); |
| 2193 | |
| 2194 | mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ); |
| 2195 | |
| 2196 | if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
| 2197 | { |
| 2198 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d", |
| 2199 | (unsigned) rec->data_len, |
| 2200 | MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 2201 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2202 | } |
Manuel Pégourié-Gonnard | 60346be | 2014-11-21 11:38:37 +0100 | [diff] [blame] | 2203 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2204 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2205 | /* |
| 2206 | * Add CID information |
| 2207 | */ |
| 2208 | rec->cid_len = transform->out_cid_len; |
| 2209 | memcpy( rec->cid, transform->out_cid, transform->out_cid_len ); |
| 2210 | MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len ); |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2211 | |
| 2212 | if( rec->cid_len != 0 ) |
| 2213 | { |
| 2214 | /* |
Hanno Becker | 7dc2577 | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 2215 | * Wrap plaintext into DTLSInnerPlaintext structure. |
| 2216 | * See ssl_cid_build_inner_plaintext() for more information. |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2217 | * |
Hanno Becker | 7dc2577 | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 2218 | * Note that this changes `rec->data_len`, and hence |
| 2219 | * `post_avail` needs to be recalculated afterwards. |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2220 | */ |
| 2221 | if( ssl_cid_build_inner_plaintext( data, |
| 2222 | &rec->data_len, |
| 2223 | post_avail, |
| 2224 | rec->type ) != 0 ) |
| 2225 | { |
| 2226 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2227 | } |
| 2228 | |
| 2229 | rec->type = MBEDTLS_SSL_MSG_CID; |
| 2230 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2231 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2232 | |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2233 | post_avail = rec->buf_len - ( rec->data_len + rec->data_offset ); |
| 2234 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2235 | /* |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 2236 | * Add MAC before if needed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2237 | */ |
Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2238 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2239 | if( mode == MBEDTLS_MODE_STREAM || |
| 2240 | ( mode == MBEDTLS_MODE_CBC |
| 2241 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2242 | && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2243 | #endif |
| 2244 | ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2245 | { |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2246 | if( post_avail < transform->maclen ) |
| 2247 | { |
| 2248 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2249 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2250 | } |
| 2251 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2252 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 2253 | if( mbedtls_ssl_transform_get_minor_ver( transform ) == |
| 2254 | MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2255 | { |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 2256 | unsigned char mac[SSL_MAC_MAX_BYTES]; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2257 | ssl_mac( &transform->md_ctx_enc, transform->mac_enc, |
| 2258 | data, rec->data_len, rec->ctr, rec->type, mac ); |
| 2259 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2260 | } |
| 2261 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2262 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2263 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2264 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 2265 | if( mbedtls_ssl_transform_get_minor_ver( transform ) >= |
| 2266 | MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2267 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2268 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
| 2269 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2270 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2271 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2272 | mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data, |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2273 | add_data_len ); |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2274 | mbedtls_md_hmac_update( &transform->md_ctx_enc, |
| 2275 | data, rec->data_len ); |
| 2276 | mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac ); |
| 2277 | mbedtls_md_hmac_reset( &transform->md_ctx_enc ); |
| 2278 | |
| 2279 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2280 | } |
| 2281 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2282 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2283 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2284 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2285 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2286 | } |
| 2287 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2288 | MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len, |
| 2289 | transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2290 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2291 | rec->data_len += transform->maclen; |
| 2292 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2293 | auth_done++; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2294 | } |
Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2295 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2296 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2297 | /* |
| 2298 | * Encrypt |
| 2299 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2300 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
| 2301 | if( mode == MBEDTLS_MODE_STREAM ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2302 | { |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2303 | int ret; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2304 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2305 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2306 | "including %d bytes of padding", |
| 2307 | rec->data_len, 0 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2308 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2309 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc, |
| 2310 | transform->iv_enc, transform->ivlen, |
| 2311 | data, rec->data_len, |
| 2312 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2313 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2314 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2315 | return( ret ); |
| 2316 | } |
| 2317 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2318 | if( rec->data_len != olen ) |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2319 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2320 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2321 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2322 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2323 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2324 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2325 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2326 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2327 | #if defined(MBEDTLS_GCM_C) || \ |
| 2328 | defined(MBEDTLS_CCM_C) || \ |
| 2329 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2330 | if( mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2331 | mode == MBEDTLS_MODE_CCM || |
| 2332 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2333 | { |
Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 2334 | int ret; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2335 | unsigned char iv[12]; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2336 | size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2337 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2338 | /* Check that there's space for both the authentication tag |
| 2339 | * and the explicit IV before and after the record content. */ |
| 2340 | if( post_avail < transform->taglen || |
| 2341 | rec->data_offset < explicit_iv_len ) |
| 2342 | { |
| 2343 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2344 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2345 | } |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2346 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2347 | /* |
| 2348 | * Generate IV |
| 2349 | */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2350 | if( transform->ivlen == 12 && transform->fixed_ivlen == 4 ) |
| 2351 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2352 | /* GCM and CCM: fixed || explicit (=seqnum) */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2353 | memcpy( iv, transform->iv_enc, transform->fixed_ivlen ); |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2354 | memcpy( iv + transform->fixed_ivlen, rec->ctr, |
| 2355 | explicit_iv_len ); |
| 2356 | /* Prefix record content with explicit IV. */ |
| 2357 | memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len ); |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2358 | } |
| 2359 | else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 ) |
| 2360 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2361 | /* ChachaPoly: fixed XOR sequence number */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2362 | unsigned char i; |
| 2363 | |
| 2364 | memcpy( iv, transform->iv_enc, transform->fixed_ivlen ); |
| 2365 | |
| 2366 | for( i = 0; i < 8; i++ ) |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2367 | iv[i+4] ^= rec->ctr[i]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2368 | } |
| 2369 | else |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 2370 | { |
| 2371 | /* Reminder if we ever add an AEAD mode with a different size */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2372 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2373 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 2374 | } |
| 2375 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2376 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 0888581 | 2019-04-26 13:34:37 +0100 | [diff] [blame] | 2377 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2378 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)", |
| 2379 | iv, transform->ivlen ); |
| 2380 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)", |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2381 | data - explicit_iv_len, explicit_iv_len ); |
| 2382 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD", |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2383 | add_data, add_data_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2384 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2385 | "including 0 bytes of padding", |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2386 | rec->data_len ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2387 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2388 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2389 | * Encrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2390 | */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2391 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2392 | if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc, |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2393 | iv, transform->ivlen, |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2394 | add_data, add_data_len, /* add data */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2395 | data, rec->data_len, /* source */ |
| 2396 | data, &rec->data_len, /* destination */ |
| 2397 | data + rec->data_len, transform->taglen ) ) != 0 ) |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2398 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2399 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2400 | return( ret ); |
| 2401 | } |
| 2402 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2403 | MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", |
| 2404 | data + rec->data_len, transform->taglen ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2405 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2406 | rec->data_len += transform->taglen + explicit_iv_len; |
| 2407 | rec->data_offset -= explicit_iv_len; |
| 2408 | post_avail -= transform->taglen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2409 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2410 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2411 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2412 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
| 2413 | #if defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2414 | ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2415 | if( mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2416 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2417 | int ret; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2418 | size_t padlen, i; |
| 2419 | size_t olen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2420 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2421 | /* Currently we're always using minimal padding |
| 2422 | * (up to 255 bytes would be allowed). */ |
| 2423 | padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen; |
| 2424 | if( padlen == transform->ivlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2425 | padlen = 0; |
| 2426 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2427 | /* Check there's enough space in the buffer for the padding. */ |
| 2428 | if( post_avail < padlen + 1 ) |
| 2429 | { |
| 2430 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2431 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2432 | } |
| 2433 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2434 | for( i = 0; i <= padlen; i++ ) |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2435 | data[rec->data_len + i] = (unsigned char) padlen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2436 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2437 | rec->data_len += padlen + 1; |
| 2438 | post_avail -= padlen + 1; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2439 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2440 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2441 | /* |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2442 | * Prepend per-record IV for block cipher in TLS v1.1 and up as per |
| 2443 | * Method 1 (6.2.3.2. in RFC4346 and RFC5246) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2444 | */ |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 2445 | if( mbedtls_ssl_transform_get_minor_ver( transform ) >= |
| 2446 | MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2447 | { |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2448 | if( f_rng == NULL ) |
| 2449 | { |
| 2450 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) ); |
| 2451 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2452 | } |
| 2453 | |
| 2454 | if( rec->data_offset < transform->ivlen ) |
| 2455 | { |
| 2456 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2457 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2458 | } |
| 2459 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2460 | /* |
| 2461 | * Generate IV |
| 2462 | */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2463 | ret = f_rng( p_rng, transform->iv_enc, transform->ivlen ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2464 | if( ret != 0 ) |
| 2465 | return( ret ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2466 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2467 | memcpy( data - transform->ivlen, transform->iv_enc, |
| 2468 | transform->ivlen ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2469 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2470 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2471 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2472 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2473 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2474 | "including %d bytes of IV and %d bytes of padding", |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2475 | rec->data_len, transform->ivlen, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 2476 | padlen + 1 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2477 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2478 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc, |
| 2479 | transform->iv_enc, |
| 2480 | transform->ivlen, |
| 2481 | data, rec->data_len, |
| 2482 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2483 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2484 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2485 | return( ret ); |
| 2486 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2487 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2488 | if( rec->data_len != olen ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2489 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2490 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2491 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2492 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2493 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2494 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 2495 | if( mbedtls_ssl_transform_get_minor_ver( transform ) < |
| 2496 | MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2497 | { |
| 2498 | /* |
| 2499 | * Save IV in SSL3 and TLS1 |
| 2500 | */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2501 | memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv, |
| 2502 | transform->ivlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2503 | } |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2504 | else |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2505 | #endif |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2506 | { |
| 2507 | data -= transform->ivlen; |
| 2508 | rec->data_offset -= transform->ivlen; |
| 2509 | rec->data_len += transform->ivlen; |
| 2510 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2511 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2512 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2513 | if( auth_done == 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2514 | { |
Hanno Becker | 3d8c907 | 2018-01-05 16:24:22 +0000 | [diff] [blame] | 2515 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
| 2516 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2517 | /* |
| 2518 | * MAC(MAC_write_key, seq_num + |
| 2519 | * TLSCipherText.type + |
| 2520 | * TLSCipherText.version + |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2521 | * length_of( (IV +) ENC(...) ) + |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2522 | * IV + // except for TLS 1.0 |
| 2523 | * ENC(content + padding + padding_length)); |
| 2524 | */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2525 | |
| 2526 | if( post_avail < transform->maclen) |
| 2527 | { |
| 2528 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2529 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2530 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2531 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2532 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2533 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2534 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2535 | MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2536 | add_data_len ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2537 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2538 | mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data, |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2539 | add_data_len ); |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2540 | mbedtls_md_hmac_update( &transform->md_ctx_enc, |
| 2541 | data, rec->data_len ); |
| 2542 | mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac ); |
| 2543 | mbedtls_md_hmac_reset( &transform->md_ctx_enc ); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2544 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2545 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2546 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2547 | rec->data_len += transform->maclen; |
| 2548 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2549 | auth_done++; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2550 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2551 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2552 | } |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2553 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2554 | #endif /* MBEDTLS_CIPHER_MODE_CBC && |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2555 | ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2556 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2557 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2558 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2559 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2560 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2561 | /* Make extra sure authentication was performed, exactly once */ |
| 2562 | if( auth_done != 1 ) |
| 2563 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2564 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2565 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2566 | } |
| 2567 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2568 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2569 | |
| 2570 | return( 0 ); |
| 2571 | } |
| 2572 | |
Hanno Becker | 40478be | 2019-07-12 08:23:59 +0100 | [diff] [blame] | 2573 | int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl, |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2574 | mbedtls_ssl_transform *transform, |
| 2575 | mbedtls_record *rec ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2576 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2577 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2578 | mbedtls_cipher_mode_t mode; |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2579 | int ret, auth_done = 0; |
Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2580 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 2581 | size_t padlen = 0, correct = 1; |
| 2582 | #endif |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2583 | unsigned char* data; |
Hanno Becker | 28a0c4e | 2019-05-20 14:54:26 +0100 | [diff] [blame] | 2584 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ]; |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2585 | size_t add_data_len; |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2586 | |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2587 | #if !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | 86e48c2 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 2588 | ssl = NULL; /* make sure we don't use it except for debug */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2589 | ((void) ssl); |
| 2590 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2591 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2592 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2593 | if( rec == NULL || |
| 2594 | rec->buf == NULL || |
| 2595 | rec->buf_len < rec->data_offset || |
| 2596 | rec->buf_len - rec->data_offset < rec->data_len ) |
| 2597 | { |
| 2598 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2599 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2600 | } |
| 2601 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2602 | data = rec->buf + rec->data_offset; |
| 2603 | mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2604 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2605 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2606 | /* |
| 2607 | * Match record's CID with incoming CID. |
| 2608 | */ |
Hanno Becker | abd7c89 | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 2609 | if( rec->cid_len != transform->in_cid_len || |
| 2610 | memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) |
| 2611 | { |
Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 2612 | return( MBEDTLS_ERR_SSL_UNEXPECTED_CID ); |
Hanno Becker | abd7c89 | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 2613 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2614 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2615 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2616 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
| 2617 | if( mode == MBEDTLS_MODE_STREAM ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2618 | { |
| 2619 | padlen = 0; |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2620 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec, |
| 2621 | transform->iv_dec, |
| 2622 | transform->ivlen, |
| 2623 | data, rec->data_len, |
| 2624 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2625 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2626 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2627 | return( ret ); |
| 2628 | } |
| 2629 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2630 | if( rec->data_len != olen ) |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2631 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2632 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2633 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2634 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2635 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2636 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2637 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2638 | #if defined(MBEDTLS_GCM_C) || \ |
| 2639 | defined(MBEDTLS_CCM_C) || \ |
| 2640 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2641 | if( mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2642 | mode == MBEDTLS_MODE_CCM || |
| 2643 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2644 | { |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2645 | unsigned char iv[12]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2646 | size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2647 | |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2648 | /* |
| 2649 | * Prepare IV from explicit and implicit data. |
| 2650 | */ |
| 2651 | |
| 2652 | /* Check that there's enough space for the explicit IV |
| 2653 | * (at the beginning of the record) and the MAC (at the |
| 2654 | * end of the record). */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2655 | if( rec->data_len < explicit_iv_len + transform->taglen ) |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 2656 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2657 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) " |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2658 | "+ taglen (%d)", rec->data_len, |
| 2659 | explicit_iv_len, transform->taglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2660 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 2661 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2662 | |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2663 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2664 | if( transform->ivlen == 12 && transform->fixed_ivlen == 4 ) |
| 2665 | { |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2666 | /* GCM and CCM: fixed || explicit */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2667 | |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2668 | /* Fixed */ |
| 2669 | memcpy( iv, transform->iv_dec, transform->fixed_ivlen ); |
| 2670 | /* Explicit */ |
| 2671 | memcpy( iv + transform->fixed_ivlen, data, 8 ); |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2672 | } |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2673 | else |
| 2674 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
| 2675 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 2676 | if( transform->ivlen == 12 && transform->fixed_ivlen == 12 ) |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2677 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2678 | /* ChachaPoly: fixed XOR sequence number */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2679 | unsigned char i; |
| 2680 | |
| 2681 | memcpy( iv, transform->iv_dec, transform->fixed_ivlen ); |
| 2682 | |
| 2683 | for( i = 0; i < 8; i++ ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2684 | iv[i+4] ^= rec->ctr[i]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2685 | } |
| 2686 | else |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2687 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2688 | { |
| 2689 | /* Reminder if we ever add an AEAD mode with a different size */ |
| 2690 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2691 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2692 | } |
| 2693 | |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2694 | /* Group changes to data, data_len, and add_data, because |
| 2695 | * add_data depends on data_len. */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2696 | data += explicit_iv_len; |
| 2697 | rec->data_offset += explicit_iv_len; |
| 2698 | rec->data_len -= explicit_iv_len + transform->taglen; |
| 2699 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2700 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2701 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD", |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2702 | add_data, add_data_len ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2703 | |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2704 | /* Because of the check above, we know that there are |
| 2705 | * explicit_iv_len Bytes preceeding data, and taglen |
| 2706 | * bytes following data + data_len. This justifies |
Hanno Becker | 07d420d | 2019-07-10 11:44:13 +0100 | [diff] [blame] | 2707 | * the debug message and the invocation of |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2708 | * mbedtls_cipher_auth_decrypt() below. */ |
| 2709 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2710 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2711 | MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len, |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2712 | transform->taglen ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2713 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2714 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2715 | * Decrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2716 | */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2717 | if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec, |
| 2718 | iv, transform->ivlen, |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2719 | add_data, add_data_len, |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2720 | data, rec->data_len, |
| 2721 | data, &olen, |
| 2722 | data + rec->data_len, |
| 2723 | transform->taglen ) ) != 0 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2724 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2725 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret ); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2726 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2727 | if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED ) |
| 2728 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2729 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2730 | return( ret ); |
| 2731 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2732 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2733 | |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2734 | /* Double-check that AEAD decryption doesn't change content length. */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2735 | if( olen != rec->data_len ) |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2736 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2737 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2738 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2739 | } |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2740 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2741 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2742 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
| 2743 | #if defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2744 | ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2745 | if( mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2746 | { |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2747 | size_t minlen = 0; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2748 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2749 | /* |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2750 | * Check immediate ciphertext sanity |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2751 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2752 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 2753 | if( mbedtls_ssl_transform_get_minor_ver( transform ) >= |
| 2754 | MBEDTLS_SSL_MINOR_VERSION_2 ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2755 | { |
| 2756 | /* The ciphertext is prefixed with the CBC IV. */ |
| 2757 | minlen += transform->ivlen; |
| 2758 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2759 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2760 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2761 | /* Size considerations: |
| 2762 | * |
| 2763 | * - The CBC cipher text must not be empty and hence |
| 2764 | * at least of size transform->ivlen. |
| 2765 | * |
| 2766 | * Together with the potential IV-prefix, this explains |
| 2767 | * the first of the two checks below. |
| 2768 | * |
| 2769 | * - The record must contain a MAC, either in plain or |
| 2770 | * encrypted, depending on whether Encrypt-then-MAC |
| 2771 | * is used or not. |
| 2772 | * - If it is, the message contains the IV-prefix, |
| 2773 | * the CBC ciphertext, and the MAC. |
| 2774 | * - If it is not, the padded plaintext, and hence |
| 2775 | * the CBC ciphertext, has at least length maclen + 1 |
| 2776 | * because there is at least the padding length byte. |
| 2777 | * |
| 2778 | * As the CBC ciphertext is not empty, both cases give the |
| 2779 | * lower bound minlen + maclen + 1 on the record size, which |
| 2780 | * we test for in the second check below. |
| 2781 | */ |
| 2782 | if( rec->data_len < minlen + transform->ivlen || |
| 2783 | rec->data_len < minlen + transform->maclen + 1 ) |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2784 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2785 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) " |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2786 | "+ 1 ) ( + expl IV )", rec->data_len, |
| 2787 | transform->ivlen, |
| 2788 | transform->maclen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2789 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2790 | } |
| 2791 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2792 | /* |
| 2793 | * Authenticate before decrypt if enabled |
| 2794 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2795 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2796 | if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2797 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2798 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2799 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2800 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2801 | |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2802 | /* Update data_len in tandem with add_data. |
| 2803 | * |
| 2804 | * The subtraction is safe because of the previous check |
| 2805 | * data_len >= minlen + maclen + 1. |
| 2806 | * |
| 2807 | * Afterwards, we know that data + data_len is followed by at |
| 2808 | * least maclen Bytes, which justifies the call to |
| 2809 | * mbedtls_ssl_safer_memcmp() below. |
| 2810 | * |
| 2811 | * Further, we still know that data_len > minlen */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2812 | rec->data_len -= transform->maclen; |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2813 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2814 | |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2815 | /* Calculate expected MAC. */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2816 | MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, |
| 2817 | add_data_len ); |
| 2818 | mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, |
| 2819 | add_data_len ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2820 | mbedtls_md_hmac_update( &transform->md_ctx_dec, |
| 2821 | data, rec->data_len ); |
| 2822 | mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect ); |
| 2823 | mbedtls_md_hmac_reset( &transform->md_ctx_dec ); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2824 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2825 | MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, |
| 2826 | transform->maclen ); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2827 | MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2828 | transform->maclen ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2829 | |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2830 | /* Compare expected MAC with MAC at the end of the record. */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2831 | if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect, |
| 2832 | transform->maclen ) != 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2833 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2834 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2835 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2836 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2837 | auth_done++; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2838 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2839 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2840 | |
| 2841 | /* |
| 2842 | * Check length sanity |
| 2843 | */ |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2844 | |
| 2845 | /* We know from above that data_len > minlen >= 0, |
| 2846 | * so the following check in particular implies that |
| 2847 | * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2848 | if( rec->data_len % transform->ivlen != 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2849 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2850 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0", |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2851 | rec->data_len, transform->ivlen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2852 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2853 | } |
| 2854 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2855 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2856 | /* |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2857 | * Initialize for prepended IV for block cipher in TLS v1.1 and up |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2858 | */ |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 2859 | if( mbedtls_ssl_transform_get_minor_ver( transform ) >= |
| 2860 | MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2861 | { |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2862 | /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2863 | memcpy( transform->iv_dec, data, transform->ivlen ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2864 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2865 | data += transform->ivlen; |
| 2866 | rec->data_offset += transform->ivlen; |
| 2867 | rec->data_len -= transform->ivlen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2868 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2869 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2870 | |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2871 | /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */ |
| 2872 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2873 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec, |
| 2874 | transform->iv_dec, transform->ivlen, |
| 2875 | data, rec->data_len, data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2876 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2877 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2878 | return( ret ); |
| 2879 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2880 | |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2881 | /* Double-check that length hasn't changed during decryption. */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2882 | if( rec->data_len != olen ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2883 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2884 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2885 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2886 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2887 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2888 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 2889 | if( mbedtls_ssl_transform_get_minor_ver( transform ) < |
| 2890 | MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2891 | { |
| 2892 | /* |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2893 | * Save IV in SSL3 and TLS1, where CBC decryption of consecutive |
| 2894 | * records is equivalent to CBC decryption of the concatenation |
| 2895 | * of the records; in other words, IVs are maintained across |
| 2896 | * record decryptions. |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2897 | */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2898 | memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv, |
| 2899 | transform->ivlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2900 | } |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2901 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2902 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2903 | /* Safe since data_len >= minlen + maclen + 1, so after having |
| 2904 | * subtracted at most minlen and maclen up to this point, |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2905 | * data_len > 0 (because of data_len % ivlen == 0, it's actually |
| 2906 | * >= ivlen ). */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2907 | padlen = data[rec->data_len - 1]; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2908 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2909 | if( auth_done == 1 ) |
| 2910 | { |
| 2911 | correct *= ( rec->data_len >= padlen + 1 ); |
| 2912 | padlen *= ( rec->data_len >= padlen + 1 ); |
| 2913 | } |
| 2914 | else |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2915 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2916 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2917 | if( rec->data_len < transform->maclen + padlen + 1 ) |
| 2918 | { |
| 2919 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)", |
| 2920 | rec->data_len, |
| 2921 | transform->maclen, |
| 2922 | padlen + 1 ) ); |
| 2923 | } |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 2924 | #endif |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2925 | |
| 2926 | correct *= ( rec->data_len >= transform->maclen + padlen + 1 ); |
| 2927 | padlen *= ( rec->data_len >= transform->maclen + padlen + 1 ); |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2928 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2929 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2930 | padlen++; |
| 2931 | |
| 2932 | /* Regardless of the validity of the padding, |
| 2933 | * we have data_len >= padlen here. */ |
| 2934 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2935 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 2936 | if( mbedtls_ssl_transform_get_minor_ver( transform ) == |
| 2937 | MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2938 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2939 | if( padlen > transform->ivlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2940 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2941 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 2942 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, " |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2943 | "should be no more than %d", |
| 2944 | padlen, transform->ivlen ) ); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 2945 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2946 | correct = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2947 | } |
| 2948 | } |
| 2949 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2950 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 2951 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2952 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 2953 | if( mbedtls_ssl_transform_get_minor_ver( transform ) > |
| 2954 | MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2955 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2956 | /* The padding check involves a series of up to 256 |
| 2957 | * consecutive memory reads at the end of the record |
| 2958 | * plaintext buffer. In order to hide the length and |
| 2959 | * validity of the padding, always perform exactly |
| 2960 | * `min(256,plaintext_len)` reads (but take into account |
| 2961 | * only the last `padlen` bytes for the padding check). */ |
| 2962 | size_t pad_count = 0; |
| 2963 | size_t real_count = 0; |
| 2964 | volatile unsigned char* const check = data; |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2965 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2966 | /* Index of first padding byte; it has been ensured above |
| 2967 | * that the subtraction is safe. */ |
| 2968 | size_t const padding_idx = rec->data_len - padlen; |
| 2969 | size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256; |
| 2970 | size_t const start_idx = rec->data_len - num_checks; |
| 2971 | size_t idx; |
Paul Bakker | 956c9e0 | 2013-12-19 14:42:28 +0100 | [diff] [blame] | 2972 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2973 | for( idx = start_idx; idx < rec->data_len; idx++ ) |
Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 2974 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2975 | real_count |= ( idx >= padding_idx ); |
| 2976 | pad_count += real_count * ( check[idx] == padlen - 1 ); |
Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 2977 | } |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2978 | correct &= ( pad_count == padlen ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2979 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2980 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 2981 | if( padlen > 0 && correct == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2982 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) ); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 2983 | #endif |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2984 | padlen &= correct * 0x1FF; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2985 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2986 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2987 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 2988 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2989 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2990 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2991 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2992 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2993 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2994 | /* If the padding was found to be invalid, padlen == 0 |
| 2995 | * and the subtraction is safe. If the padding was found valid, |
| 2996 | * padlen hasn't been changed and the previous assertion |
| 2997 | * data_len >= padlen still holds. */ |
| 2998 | rec->data_len -= padlen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2999 | } |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 3000 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3001 | #endif /* MBEDTLS_CIPHER_MODE_CBC && |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 3002 | ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 3003 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3004 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3005 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 3006 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3007 | |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 3008 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3009 | MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption", |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3010 | data, rec->data_len ); |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 3011 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3012 | |
| 3013 | /* |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3014 | * Authenticate if not done yet. |
| 3015 | * Compute the MAC regardless of the padding result (RFC4346, CBCTIME). |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3016 | */ |
Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 3017 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3018 | if( auth_done == 0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3019 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 3020 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 3021 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3022 | /* If the initial value of padlen was such that |
| 3023 | * data_len < maclen + padlen + 1, then padlen |
| 3024 | * got reset to 1, and the initial check |
| 3025 | * data_len >= minlen + maclen + 1 |
| 3026 | * guarantees that at this point we still |
| 3027 | * have at least data_len >= maclen. |
| 3028 | * |
| 3029 | * If the initial value of padlen was such that |
| 3030 | * data_len >= maclen + padlen + 1, then we have |
| 3031 | * subtracted either padlen + 1 (if the padding was correct) |
| 3032 | * or 0 (if the padding was incorrect) since then, |
| 3033 | * hence data_len >= maclen in any case. |
| 3034 | */ |
| 3035 | rec->data_len -= transform->maclen; |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3036 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3037 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3038 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 3039 | if( mbedtls_ssl_transform_get_minor_ver( transform ) == |
| 3040 | MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3041 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3042 | ssl_mac( &transform->md_ctx_dec, |
| 3043 | transform->mac_dec, |
| 3044 | data, rec->data_len, |
| 3045 | rec->ctr, rec->type, |
| 3046 | mac_expect ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3047 | } |
| 3048 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3049 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 3050 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 3051 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 3052 | if( mbedtls_ssl_transform_get_minor_ver( transform ) > |
| 3053 | MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3054 | { |
| 3055 | /* |
| 3056 | * Process MAC and always update for padlen afterwards to make |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3057 | * total time independent of padlen. |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3058 | * |
| 3059 | * Known timing attacks: |
| 3060 | * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf) |
| 3061 | * |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3062 | * To compensate for different timings for the MAC calculation |
| 3063 | * depending on how much padding was removed (which is determined |
| 3064 | * by padlen), process extra_run more blocks through the hash |
| 3065 | * function. |
| 3066 | * |
| 3067 | * The formula in the paper is |
| 3068 | * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 ) |
| 3069 | * where L1 is the size of the header plus the decrypted message |
| 3070 | * plus CBC padding and L2 is the size of the header plus the |
| 3071 | * decrypted message. This is for an underlying hash function |
| 3072 | * with 64-byte blocks. |
| 3073 | * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values |
| 3074 | * correctly. We round down instead of up, so -56 is the correct |
| 3075 | * value for our calculations instead of -55. |
| 3076 | * |
Gilles Peskine | 1bd9d58 | 2018-06-04 11:58:44 +0200 | [diff] [blame] | 3077 | * Repeat the formula rather than defining a block_size variable. |
| 3078 | * This avoids requiring division by a variable at runtime |
| 3079 | * (which would be marginally less efficient and would require |
| 3080 | * linking an extra division function in some builds). |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3081 | */ |
| 3082 | size_t j, extra_run = 0; |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3083 | unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE]; |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3084 | |
| 3085 | /* |
| 3086 | * The next two sizes are the minimum and maximum values of |
| 3087 | * in_msglen over all padlen values. |
| 3088 | * |
| 3089 | * They're independent of padlen, since we previously did |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3090 | * data_len -= padlen. |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3091 | * |
| 3092 | * Note that max_len + maclen is never more than the buffer |
| 3093 | * length, as we previously did in_msglen -= maclen too. |
| 3094 | */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3095 | const size_t max_len = rec->data_len + padlen; |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3096 | const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0; |
| 3097 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3098 | memset( tmp, 0, sizeof( tmp ) ); |
| 3099 | |
| 3100 | switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) ) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3101 | { |
Gilles Peskine | d0e55a4 | 2018-06-04 12:03:30 +0200 | [diff] [blame] | 3102 | #if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \ |
| 3103 | defined(MBEDTLS_SHA256_C) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3104 | case MBEDTLS_MD_MD5: |
| 3105 | case MBEDTLS_MD_SHA1: |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3106 | case MBEDTLS_MD_SHA256: |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3107 | /* 8 bytes of message size, 64-byte compression blocks */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3108 | extra_run = |
| 3109 | ( add_data_len + rec->data_len + padlen + 8 ) / 64 - |
| 3110 | ( add_data_len + rec->data_len + 8 ) / 64; |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3111 | break; |
| 3112 | #endif |
Gilles Peskine | a7fe25d | 2018-06-04 12:01:18 +0200 | [diff] [blame] | 3113 | #if defined(MBEDTLS_SHA512_C) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3114 | case MBEDTLS_MD_SHA384: |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3115 | /* 16 bytes of message size, 128-byte compression blocks */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3116 | extra_run = |
| 3117 | ( add_data_len + rec->data_len + padlen + 16 ) / 128 - |
| 3118 | ( add_data_len + rec->data_len + 16 ) / 128; |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3119 | break; |
| 3120 | #endif |
| 3121 | default: |
Gilles Peskine | 5c38984 | 2018-06-04 12:02:43 +0200 | [diff] [blame] | 3122 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3123 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3124 | } |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3125 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3126 | extra_run &= correct * 0xFF; |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3127 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3128 | mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, |
| 3129 | add_data_len ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3130 | mbedtls_md_hmac_update( &transform->md_ctx_dec, data, |
| 3131 | rec->data_len ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3132 | /* Make sure we access everything even when padlen > 0. This |
| 3133 | * makes the synchronisation requirements for just-in-time |
| 3134 | * Prime+Probe attacks much tighter and hopefully impractical. */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3135 | ssl_read_memory( data + rec->data_len, padlen ); |
| 3136 | mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3137 | |
| 3138 | /* Call mbedtls_md_process at least once due to cache attacks |
| 3139 | * that observe whether md_process() was called of not */ |
Manuel Pégourié-Gonnard | 47fede0 | 2015-04-29 01:35:48 +0200 | [diff] [blame] | 3140 | for( j = 0; j < extra_run + 1; j++ ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3141 | mbedtls_md_process( &transform->md_ctx_dec, tmp ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3142 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3143 | mbedtls_md_hmac_reset( &transform->md_ctx_dec ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3144 | |
| 3145 | /* Make sure we access all the memory that could contain the MAC, |
| 3146 | * before we check it in the next code block. This makes the |
| 3147 | * synchronisation requirements for just-in-time Prime+Probe |
| 3148 | * attacks much tighter and hopefully impractical. */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3149 | ssl_read_memory( data + min_len, |
| 3150 | max_len - min_len + transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3151 | } |
| 3152 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3153 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 3154 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3155 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3156 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3157 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3158 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3159 | |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3160 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3161 | MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen ); |
| 3162 | MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3163 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3164 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3165 | if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect, |
| 3166 | transform->maclen ) != 0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3167 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3168 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 3169 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3170 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3171 | correct = 0; |
| 3172 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3173 | auth_done++; |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3174 | } |
Hanno Becker | dd3ab13 | 2018-10-17 14:43:14 +0100 | [diff] [blame] | 3175 | |
| 3176 | /* |
| 3177 | * Finally check the correct flag |
| 3178 | */ |
| 3179 | if( correct == 0 ) |
| 3180 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 3181 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3182 | |
| 3183 | /* Make extra sure authentication was performed, exactly once */ |
| 3184 | if( auth_done != 1 ) |
| 3185 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3186 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3187 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3188 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3189 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3190 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 3191 | if( rec->cid_len != 0 ) |
| 3192 | { |
| 3193 | ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len, |
| 3194 | &rec->type ); |
| 3195 | if( ret != 0 ) |
| 3196 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 3197 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3198 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 3199 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3200 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3201 | |
| 3202 | return( 0 ); |
| 3203 | } |
| 3204 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 3205 | #undef MAC_NONE |
| 3206 | #undef MAC_PLAINTEXT |
| 3207 | #undef MAC_CIPHERTEXT |
| 3208 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3209 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3210 | /* |
| 3211 | * Compression/decompression functions |
| 3212 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3213 | static int ssl_compress_buf( mbedtls_ssl_context *ssl ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3214 | { |
| 3215 | int ret; |
| 3216 | unsigned char *msg_post = ssl->out_msg; |
Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 3217 | ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3218 | size_t len_pre = ssl->out_msglen; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 3219 | unsigned char *msg_pre = ssl->compress_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3220 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3221 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3222 | |
Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 3223 | if( len_pre == 0 ) |
| 3224 | return( 0 ); |
| 3225 | |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3226 | memcpy( msg_pre, ssl->out_msg, len_pre ); |
| 3227 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3228 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3229 | ssl->out_msglen ) ); |
| 3230 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3231 | MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3232 | ssl->out_msg, ssl->out_msglen ); |
| 3233 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3234 | ssl->transform_out->ctx_deflate.next_in = msg_pre; |
| 3235 | ssl->transform_out->ctx_deflate.avail_in = len_pre; |
| 3236 | ssl->transform_out->ctx_deflate.next_out = msg_post; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3237 | ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3238 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3239 | ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3240 | if( ret != Z_OK ) |
| 3241 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3242 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) ); |
| 3243 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3244 | } |
| 3245 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3246 | ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN - |
Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 3247 | ssl->transform_out->ctx_deflate.avail_out - bytes_written; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3248 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3249 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3250 | ssl->out_msglen ) ); |
| 3251 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3252 | MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3253 | ssl->out_msg, ssl->out_msglen ); |
| 3254 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3255 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3256 | |
| 3257 | return( 0 ); |
| 3258 | } |
| 3259 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3260 | static int ssl_decompress_buf( mbedtls_ssl_context *ssl ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3261 | { |
| 3262 | int ret; |
| 3263 | unsigned char *msg_post = ssl->in_msg; |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 3264 | ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3265 | size_t len_pre = ssl->in_msglen; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 3266 | unsigned char *msg_pre = ssl->compress_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3267 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3268 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3269 | |
Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 3270 | if( len_pre == 0 ) |
| 3271 | return( 0 ); |
| 3272 | |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3273 | memcpy( msg_pre, ssl->in_msg, len_pre ); |
| 3274 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3275 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3276 | ssl->in_msglen ) ); |
| 3277 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3278 | MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3279 | ssl->in_msg, ssl->in_msglen ); |
| 3280 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3281 | ssl->transform_in->ctx_inflate.next_in = msg_pre; |
| 3282 | ssl->transform_in->ctx_inflate.avail_in = len_pre; |
| 3283 | ssl->transform_in->ctx_inflate.next_out = msg_post; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3284 | ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN - |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 3285 | header_bytes; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3286 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3287 | ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3288 | if( ret != Z_OK ) |
| 3289 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3290 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) ); |
| 3291 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3292 | } |
| 3293 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3294 | ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN - |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 3295 | ssl->transform_in->ctx_inflate.avail_out - header_bytes; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3296 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3297 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3298 | ssl->in_msglen ) ); |
| 3299 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3300 | MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3301 | ssl->in_msg, ssl->in_msglen ); |
| 3302 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3303 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3304 | |
| 3305 | return( 0 ); |
| 3306 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3307 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3308 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3309 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3310 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3311 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3312 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3313 | static int ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3314 | { |
| 3315 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 3316 | * timeout if we were using the usual handshake doubling scheme */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3317 | if( ssl->conf->renego_max_records < 0 ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3318 | { |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 3319 | uint32_t ratio = |
| 3320 | mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) / |
| 3321 | mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1; |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3322 | unsigned char doublings = 1; |
| 3323 | |
| 3324 | while( ratio != 0 ) |
| 3325 | { |
| 3326 | ++doublings; |
| 3327 | ratio >>= 1; |
| 3328 | } |
| 3329 | |
| 3330 | if( ++ssl->renego_records_seen > doublings ) |
| 3331 | { |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 3332 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3333 | return( 0 ); |
| 3334 | } |
| 3335 | } |
| 3336 | |
| 3337 | return( ssl_write_hello_request( ssl ) ); |
| 3338 | } |
| 3339 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3340 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3341 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3342 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3343 | * Fill the input message buffer by appending data to it. |
| 3344 | * The amount of data already fetched is in ssl->in_left. |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3345 | * |
| 3346 | * If we return 0, is it guaranteed that (at least) nb_want bytes are |
| 3347 | * available (from this read and/or a previous one). Otherwise, an error code |
| 3348 | * is returned (possibly EOF or WANT_READ). |
| 3349 | * |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3350 | * With stream transport (TLS) on success ssl->in_left == nb_want, but |
| 3351 | * with datagram transport (DTLS) on success ssl->in_left >= nb_want, |
| 3352 | * since we always read a whole datagram at once. |
| 3353 | * |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 3354 | * For DTLS, it is up to the caller to set ssl->next_record_offset when |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3355 | * they're done reading a record. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3356 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3357 | int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3358 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 3359 | int ret; |
| 3360 | size_t len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3361 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3362 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3363 | |
Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 3364 | if( mbedtls_ssl_get_recv( ssl ) == NULL && |
| 3365 | mbedtls_ssl_get_recv_timeout( ssl ) == NULL ) |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3366 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3367 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() " |
Manuel Pégourié-Gonnard | 1b511f9 | 2015-05-06 15:54:23 +0100 | [diff] [blame] | 3368 | "or mbedtls_ssl_set_bio()" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3369 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3370 | } |
| 3371 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3372 | if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) ) |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 3373 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3374 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) ); |
| 3375 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 3376 | } |
| 3377 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3378 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 25838b7 | 2019-03-19 10:23:56 +0100 | [diff] [blame] | 3379 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3380 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3381 | uint32_t timeout; |
| 3382 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3383 | /* Just to be sure */ |
Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 3384 | if( mbedtls_ssl_get_set_timer( ssl ) == NULL || |
| 3385 | mbedtls_ssl_get_get_timer( ssl ) == NULL ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3386 | { |
| 3387 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 3388 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 3389 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3390 | } |
| 3391 | |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3392 | /* |
| 3393 | * The point is, we need to always read a full datagram at once, so we |
| 3394 | * sometimes read more then requested, and handle the additional data. |
| 3395 | * It could be the rest of the current record (while fetching the |
| 3396 | * header) and/or some other records in the same datagram. |
| 3397 | */ |
| 3398 | |
| 3399 | /* |
| 3400 | * Move to the next record in the already read datagram if applicable |
| 3401 | */ |
| 3402 | if( ssl->next_record_offset != 0 ) |
| 3403 | { |
| 3404 | if( ssl->in_left < ssl->next_record_offset ) |
| 3405 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3406 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3407 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3408 | } |
| 3409 | |
| 3410 | ssl->in_left -= ssl->next_record_offset; |
| 3411 | |
| 3412 | if( ssl->in_left != 0 ) |
| 3413 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3414 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d", |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3415 | ssl->next_record_offset ) ); |
| 3416 | memmove( ssl->in_hdr, |
| 3417 | ssl->in_hdr + ssl->next_record_offset, |
| 3418 | ssl->in_left ); |
| 3419 | } |
| 3420 | |
| 3421 | ssl->next_record_offset = 0; |
| 3422 | } |
| 3423 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3424 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3425 | ssl->in_left, nb_want ) ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3426 | |
| 3427 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3428 | * Done if we already have enough data. |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3429 | */ |
| 3430 | if( nb_want <= ssl->in_left) |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3431 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3432 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3433 | return( 0 ); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3434 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3435 | |
| 3436 | /* |
Antonin Décimo | d5f4759 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 3437 | * A record can't be split across datagrams. If we need to read but |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3438 | * are not at the beginning of a new record, the caller did something |
| 3439 | * wrong. |
| 3440 | */ |
| 3441 | if( ssl->in_left != 0 ) |
| 3442 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3443 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3444 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3445 | } |
| 3446 | |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3447 | /* |
| 3448 | * Don't even try to read if time's out already. |
| 3449 | * This avoids by-passing the timer when repeatedly receiving messages |
| 3450 | * that will end up being dropped. |
| 3451 | */ |
| 3452 | if( ssl_check_timer( ssl ) != 0 ) |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 3453 | { |
| 3454 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3455 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 3456 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3457 | else |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3458 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3459 | len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3460 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3461 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3462 | timeout = ssl->handshake->retransmit_timeout; |
| 3463 | else |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 3464 | timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3465 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3466 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3467 | |
Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 3468 | if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL ) |
| 3469 | { |
| 3470 | ret = mbedtls_ssl_get_recv_timeout( ssl ) |
| 3471 | ( ssl->p_bio, ssl->in_hdr, len, timeout ); |
| 3472 | } |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3473 | else |
Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 3474 | { |
| 3475 | ret = mbedtls_ssl_get_recv( ssl ) |
| 3476 | ( ssl->p_bio, ssl->in_hdr, len ); |
| 3477 | } |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3478 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3479 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3480 | |
| 3481 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3482 | return( MBEDTLS_ERR_SSL_CONN_EOF ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3483 | } |
| 3484 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3485 | if( ret == MBEDTLS_ERR_SSL_TIMEOUT ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3486 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3487 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3488 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3489 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3490 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3491 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3492 | if( ssl_double_retransmit_timeout( ssl ) != 0 ) |
| 3493 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3494 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3495 | return( MBEDTLS_ERR_SSL_TIMEOUT ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3496 | } |
| 3497 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3498 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3499 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3500 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3501 | return( ret ); |
| 3502 | } |
| 3503 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3504 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3505 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3506 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 3507 | else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 3508 | MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3509 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3510 | { |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3511 | if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3512 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3513 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3514 | return( ret ); |
| 3515 | } |
| 3516 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3517 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3518 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3519 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3520 | } |
| 3521 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3522 | if( ret < 0 ) |
| 3523 | return( ret ); |
| 3524 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3525 | ssl->in_left = ret; |
| 3526 | } |
Manuel Pégourié-Gonnard | 25838b7 | 2019-03-19 10:23:56 +0100 | [diff] [blame] | 3527 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 3528 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3529 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3530 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3531 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d", |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3532 | ssl->in_left, nb_want ) ); |
| 3533 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3534 | while( ssl->in_left < nb_want ) |
| 3535 | { |
| 3536 | len = nb_want - ssl->in_left; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3537 | |
| 3538 | if( ssl_check_timer( ssl ) != 0 ) |
| 3539 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
| 3540 | else |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3541 | { |
Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 3542 | if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL ) |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3543 | { |
Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 3544 | ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio, |
| 3545 | ssl->in_hdr + ssl->in_left, len, |
| 3546 | mbedtls_ssl_conf_get_read_timeout( ssl->conf ) ); |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3547 | } |
| 3548 | else |
| 3549 | { |
Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 3550 | ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio, |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3551 | ssl->in_hdr + ssl->in_left, len ); |
| 3552 | } |
| 3553 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3554 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3555 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d", |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3556 | ssl->in_left, nb_want ) ); |
| 3557 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3558 | |
| 3559 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3560 | return( MBEDTLS_ERR_SSL_CONN_EOF ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3561 | |
| 3562 | if( ret < 0 ) |
| 3563 | return( ret ); |
| 3564 | |
mohammad1603 | 52aecb9 | 2018-03-28 23:41:40 -0700 | [diff] [blame] | 3565 | if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) ) |
mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 3566 | { |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3567 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3568 | ( "f_recv returned %d bytes but only %lu were requested", |
mohammad1603 | 19d392b | 2018-04-02 07:25:26 -0700 | [diff] [blame] | 3569 | ret, (unsigned long)len ) ); |
mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 3570 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3571 | } |
| 3572 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3573 | ssl->in_left += ret; |
| 3574 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3575 | } |
Manuel Pégourié-Gonnard | 25838b7 | 2019-03-19 10:23:56 +0100 | [diff] [blame] | 3576 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3577 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3578 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3579 | |
| 3580 | return( 0 ); |
| 3581 | } |
| 3582 | |
| 3583 | /* |
| 3584 | * Flush any data not yet written |
| 3585 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3586 | int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3587 | { |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 3588 | int ret; |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 3589 | unsigned char *buf; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3590 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3591 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3592 | |
Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 3593 | if( mbedtls_ssl_get_send( ssl ) == NULL ) |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3594 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3595 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() " |
Manuel Pégourié-Gonnard | 1b511f9 | 2015-05-06 15:54:23 +0100 | [diff] [blame] | 3596 | "or mbedtls_ssl_set_bio()" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3597 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3598 | } |
| 3599 | |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3600 | /* Avoid incrementing counter if data is flushed */ |
| 3601 | if( ssl->out_left == 0 ) |
| 3602 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3603 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3604 | return( 0 ); |
| 3605 | } |
| 3606 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3607 | while( ssl->out_left > 0 ) |
| 3608 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3609 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d", |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 3610 | mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3611 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3612 | buf = ssl->out_hdr - ssl->out_left; |
Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 3613 | ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left ); |
Paul Bakker | 186751d | 2012-05-08 13:16:14 +0000 | [diff] [blame] | 3614 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3615 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3616 | |
| 3617 | if( ret <= 0 ) |
| 3618 | return( ret ); |
| 3619 | |
mohammad1603 | 52aecb9 | 2018-03-28 23:41:40 -0700 | [diff] [blame] | 3620 | if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) ) |
mohammad1603 | 4bbaeb4 | 2018-02-22 04:29:04 -0800 | [diff] [blame] | 3621 | { |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3622 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3623 | ( "f_send returned %d bytes but only %lu bytes were sent", |
mohammad1603 | 19d392b | 2018-04-02 07:25:26 -0700 | [diff] [blame] | 3624 | ret, (unsigned long)ssl->out_left ) ); |
mohammad1603 | 4bbaeb4 | 2018-02-22 04:29:04 -0800 | [diff] [blame] | 3625 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3626 | } |
| 3627 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3628 | ssl->out_left -= ret; |
| 3629 | } |
| 3630 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3631 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 3632 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3633 | { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3634 | ssl->out_hdr = ssl->out_buf; |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3635 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 3636 | MBEDTLS_SSL_TRANSPORT_ELSE |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3637 | #endif |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 3638 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3639 | { |
| 3640 | ssl->out_hdr = ssl->out_buf + 8; |
| 3641 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 3642 | #endif |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3643 | ssl_update_out_pointers( ssl, ssl->transform_out ); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3644 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3645 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3646 | |
| 3647 | return( 0 ); |
| 3648 | } |
| 3649 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3650 | /* |
| 3651 | * Functions to handle the DTLS retransmission state machine |
| 3652 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3653 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3654 | /* |
| 3655 | * Append current handshake message to current outgoing flight |
| 3656 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3657 | static int ssl_flight_append( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3658 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3659 | mbedtls_ssl_flight_item *msg; |
Hanno Becker | 3b23590 | 2018-08-06 09:54:53 +0100 | [diff] [blame] | 3660 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) ); |
| 3661 | MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight", |
| 3662 | ssl->out_msg, ssl->out_msglen ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3663 | |
| 3664 | /* Allocate space for current message */ |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3665 | if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3666 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 3667 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3668 | sizeof( mbedtls_ssl_flight_item ) ) ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3669 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3670 | } |
| 3671 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3672 | if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3673 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 3674 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3675 | mbedtls_free( msg ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3676 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3677 | } |
| 3678 | |
| 3679 | /* Copy current handshake message with headers */ |
| 3680 | memcpy( msg->p, ssl->out_msg, ssl->out_msglen ); |
| 3681 | msg->len = ssl->out_msglen; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3682 | msg->type = ssl->out_msgtype; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3683 | msg->next = NULL; |
| 3684 | |
| 3685 | /* Append to the current flight */ |
| 3686 | if( ssl->handshake->flight == NULL ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3687 | ssl->handshake->flight = msg; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3688 | else |
| 3689 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3690 | mbedtls_ssl_flight_item *cur = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3691 | while( cur->next != NULL ) |
| 3692 | cur = cur->next; |
| 3693 | cur->next = msg; |
| 3694 | } |
| 3695 | |
Hanno Becker | 3b23590 | 2018-08-06 09:54:53 +0100 | [diff] [blame] | 3696 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3697 | return( 0 ); |
| 3698 | } |
| 3699 | |
| 3700 | /* |
| 3701 | * Free the current flight of handshake messages |
| 3702 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3703 | static void ssl_flight_free( mbedtls_ssl_flight_item *flight ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3704 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3705 | mbedtls_ssl_flight_item *cur = flight; |
| 3706 | mbedtls_ssl_flight_item *next; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3707 | |
| 3708 | while( cur != NULL ) |
| 3709 | { |
| 3710 | next = cur->next; |
| 3711 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3712 | mbedtls_free( cur->p ); |
| 3713 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3714 | |
| 3715 | cur = next; |
| 3716 | } |
| 3717 | } |
| 3718 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3719 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3720 | static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 3721 | #endif |
| 3722 | |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3723 | /* |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3724 | * Swap transform_out and out_ctr with the alternative ones |
| 3725 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3726 | static void ssl_swap_epochs( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3727 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3728 | mbedtls_ssl_transform *tmp_transform; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3729 | unsigned char tmp_out_ctr[8]; |
| 3730 | |
| 3731 | if( ssl->transform_out == ssl->handshake->alt_transform_out ) |
| 3732 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3733 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3734 | return; |
| 3735 | } |
| 3736 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3737 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3738 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3739 | /* Swap transforms */ |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3740 | tmp_transform = ssl->transform_out; |
| 3741 | ssl->transform_out = ssl->handshake->alt_transform_out; |
| 3742 | ssl->handshake->alt_transform_out = tmp_transform; |
| 3743 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3744 | /* Swap epoch + sequence_number */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3745 | memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 ); |
| 3746 | memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3747 | memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3748 | |
| 3749 | /* Adjust to the newly activated transform */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 3750 | ssl_update_out_pointers( ssl, ssl->transform_out ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3751 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3752 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 3753 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3754 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3755 | if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 ) |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3756 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3757 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
| 3758 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3759 | } |
| 3760 | } |
| 3761 | #endif |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3762 | } |
| 3763 | |
| 3764 | /* |
| 3765 | * Retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3766 | */ |
| 3767 | int mbedtls_ssl_resend( mbedtls_ssl_context *ssl ) |
| 3768 | { |
| 3769 | int ret = 0; |
| 3770 | |
| 3771 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) ); |
| 3772 | |
| 3773 | ret = mbedtls_ssl_flight_transmit( ssl ); |
| 3774 | |
| 3775 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) ); |
| 3776 | |
| 3777 | return( ret ); |
| 3778 | } |
| 3779 | |
| 3780 | /* |
| 3781 | * Transmit or retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3782 | * |
| 3783 | * Need to remember the current message in case flush_output returns |
| 3784 | * WANT_WRITE, causing us to exit this function and come back later. |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3785 | * This function must be called until state is no longer SENDING. |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3786 | */ |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3787 | int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3788 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3789 | int ret; |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3790 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3791 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3792 | if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3793 | { |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 3794 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3795 | |
| 3796 | ssl->handshake->cur_msg = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3797 | ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3798 | ssl_swap_epochs( ssl ); |
| 3799 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3800 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3801 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3802 | |
| 3803 | while( ssl->handshake->cur_msg != NULL ) |
| 3804 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3805 | size_t max_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3806 | const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3807 | |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3808 | int const is_finished = |
| 3809 | ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3810 | cur->p[0] == MBEDTLS_SSL_HS_FINISHED ); |
| 3811 | |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3812 | uint8_t const force_flush = ssl->disable_datagram_packing == 1 ? |
| 3813 | SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH; |
| 3814 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3815 | /* Swap epochs before sending Finished: we can't do it after |
| 3816 | * sending ChangeCipherSpec, in case write returns WANT_READ. |
| 3817 | * Must be done before copying, may change out_msg pointer */ |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3818 | if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) ) |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3819 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3820 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3821 | ssl_swap_epochs( ssl ); |
| 3822 | } |
| 3823 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3824 | ret = ssl_get_remaining_payload_in_datagram( ssl ); |
| 3825 | if( ret < 0 ) |
| 3826 | return( ret ); |
| 3827 | max_frag_len = (size_t) ret; |
| 3828 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3829 | /* CCS is copied as is, while HS messages may need fragmentation */ |
| 3830 | if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
| 3831 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3832 | if( max_frag_len == 0 ) |
| 3833 | { |
| 3834 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3835 | return( ret ); |
| 3836 | |
| 3837 | continue; |
| 3838 | } |
| 3839 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3840 | memcpy( ssl->out_msg, cur->p, cur->len ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3841 | ssl->out_msglen = cur->len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3842 | ssl->out_msgtype = cur->type; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3843 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3844 | /* Update position inside current message */ |
| 3845 | ssl->handshake->cur_msg_p += cur->len; |
| 3846 | } |
| 3847 | else |
| 3848 | { |
| 3849 | const unsigned char * const p = ssl->handshake->cur_msg_p; |
| 3850 | const size_t hs_len = cur->len - 12; |
| 3851 | const size_t frag_off = p - ( cur->p + 12 ); |
| 3852 | const size_t rem_len = hs_len - frag_off; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3853 | size_t cur_hs_frag_len, max_hs_frag_len; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3854 | |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3855 | if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) ) |
Manuel Pégourié-Gonnard | a1071a5 | 2018-08-20 11:56:14 +0200 | [diff] [blame] | 3856 | { |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3857 | if( is_finished ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3858 | ssl_swap_epochs( ssl ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3859 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3860 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3861 | return( ret ); |
| 3862 | |
| 3863 | continue; |
| 3864 | } |
| 3865 | max_hs_frag_len = max_frag_len - 12; |
| 3866 | |
| 3867 | cur_hs_frag_len = rem_len > max_hs_frag_len ? |
| 3868 | max_hs_frag_len : rem_len; |
| 3869 | |
| 3870 | if( frag_off == 0 && cur_hs_frag_len != hs_len ) |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 3871 | { |
| 3872 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)", |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3873 | (unsigned) cur_hs_frag_len, |
| 3874 | (unsigned) max_hs_frag_len ) ); |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 3875 | } |
Manuel Pégourié-Gonnard | b747c6c | 2018-08-12 13:28:53 +0200 | [diff] [blame] | 3876 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3877 | /* Messages are stored with handshake headers as if not fragmented, |
| 3878 | * copy beginning of headers then fill fragmentation fields. |
| 3879 | * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */ |
| 3880 | memcpy( ssl->out_msg, cur->p, 6 ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3881 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3882 | ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff ); |
| 3883 | ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff ); |
| 3884 | ssl->out_msg[8] = ( ( frag_off ) & 0xff ); |
| 3885 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3886 | ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff ); |
| 3887 | ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff ); |
| 3888 | ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff ); |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3889 | |
| 3890 | MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 ); |
| 3891 | |
Hanno Becker | 3f7b973 | 2018-08-28 09:53:25 +0100 | [diff] [blame] | 3892 | /* Copy the handshake message content and set records fields */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3893 | memcpy( ssl->out_msg + 12, p, cur_hs_frag_len ); |
| 3894 | ssl->out_msglen = cur_hs_frag_len + 12; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3895 | ssl->out_msgtype = cur->type; |
| 3896 | |
| 3897 | /* Update position inside current message */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3898 | ssl->handshake->cur_msg_p += cur_hs_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3899 | } |
| 3900 | |
| 3901 | /* If done with the current message move to the next one if any */ |
| 3902 | if( ssl->handshake->cur_msg_p >= cur->p + cur->len ) |
| 3903 | { |
| 3904 | if( cur->next != NULL ) |
| 3905 | { |
| 3906 | ssl->handshake->cur_msg = cur->next; |
| 3907 | ssl->handshake->cur_msg_p = cur->next->p + 12; |
| 3908 | } |
| 3909 | else |
| 3910 | { |
| 3911 | ssl->handshake->cur_msg = NULL; |
| 3912 | ssl->handshake->cur_msg_p = NULL; |
| 3913 | } |
| 3914 | } |
| 3915 | |
| 3916 | /* Actually send the message out */ |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3917 | if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3918 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3919 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3920 | return( ret ); |
| 3921 | } |
| 3922 | } |
| 3923 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3924 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3925 | return( ret ); |
| 3926 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3927 | /* Update state and set timer */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3928 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 3929 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 23b7b70 | 2014-09-25 13:50:12 +0200 | [diff] [blame] | 3930 | else |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3931 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3932 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3933 | ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); |
| 3934 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3935 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3936 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3937 | |
| 3938 | return( 0 ); |
| 3939 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3940 | |
| 3941 | /* |
| 3942 | * To be called when the last message of an incoming flight is received. |
| 3943 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3944 | void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3945 | { |
| 3946 | /* We won't need to resend that one any more */ |
| 3947 | ssl_flight_free( ssl->handshake->flight ); |
| 3948 | ssl->handshake->flight = NULL; |
| 3949 | ssl->handshake->cur_msg = NULL; |
| 3950 | |
| 3951 | /* The next incoming flight will start with this msg_seq */ |
| 3952 | ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq; |
| 3953 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3954 | /* We don't want to remember CCS's across flight boundaries. */ |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 3955 | ssl->handshake->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3956 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3957 | /* Clear future message buffering structure. */ |
| 3958 | ssl_buffering_free( ssl ); |
| 3959 | |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 3960 | /* Cancel timer */ |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3961 | ssl_set_timer( ssl, 0 ); |
| 3962 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3963 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3964 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3965 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3966 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3967 | } |
| 3968 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3969 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3970 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3971 | |
| 3972 | /* |
| 3973 | * To be called when the last message of an outgoing flight is send. |
| 3974 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3975 | void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3976 | { |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 3977 | ssl_reset_retransmit_timeout( ssl ); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3978 | ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3979 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3980 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3981 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3982 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3983 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3984 | } |
| 3985 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3986 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3987 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3988 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3989 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3990 | /* |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3991 | * Handshake layer functions |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3992 | */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3993 | |
| 3994 | /* |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3995 | * Write (DTLS: or queue) current handshake (including CCS) message. |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3996 | * |
| 3997 | * - fill in handshake headers |
| 3998 | * - update handshake checksum |
| 3999 | * - DTLS: save message for resending |
| 4000 | * - then pass to the record layer |
| 4001 | * |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4002 | * DTLS: except for HelloRequest, messages are only queued, and will only be |
| 4003 | * actually sent when calling flight_transmit() or resend(). |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4004 | * |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4005 | * Inputs: |
| 4006 | * - ssl->out_msglen: 4 + actual handshake message len |
| 4007 | * (4 is the size of handshake headers for TLS) |
| 4008 | * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc) |
| 4009 | * - ssl->out_msg + 4: the handshake message body |
| 4010 | * |
Manuel Pégourié-Gonnard | 065a2a3 | 2018-08-20 11:09:26 +0200 | [diff] [blame] | 4011 | * Outputs, ie state before passing to flight_append() or write_record(): |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4012 | * - ssl->out_msglen: the length of the record contents |
| 4013 | * (including handshake headers but excluding record headers) |
| 4014 | * - ssl->out_msg: the record contents (handshake headers + content) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4015 | */ |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4016 | int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4017 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4018 | int ret; |
| 4019 | const size_t hs_len = ssl->out_msglen - 4; |
| 4020 | const unsigned char hs_type = ssl->out_msg[0]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4021 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4022 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) ); |
| 4023 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4024 | /* |
| 4025 | * Sanity checks |
| 4026 | */ |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 4027 | if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE && |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4028 | ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
| 4029 | { |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 4030 | /* In SSLv3, the client might send a NoCertificate alert. */ |
| 4031 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 4032 | if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 && |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 4033 | ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT && |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 4034 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 4035 | MBEDTLS_SSL_IS_CLIENT ) ) |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 4036 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ |
| 4037 | { |
| 4038 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4039 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4040 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4041 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4042 | |
Hanno Becker | f6d6e30 | 2018-11-07 11:57:51 +0000 | [diff] [blame] | 4043 | /* Whenever we send anything different from a |
| 4044 | * HelloRequest we should be in a handshake - double check. */ |
| 4045 | if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4046 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) && |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4047 | ssl->handshake == NULL ) |
| 4048 | { |
| 4049 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4050 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4051 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4052 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4053 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4054 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4055 | ssl->handshake != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4056 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4057 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4058 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4059 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4060 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4061 | #endif |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4062 | |
Hanno Becker | b50a253 | 2018-08-06 11:52:54 +0100 | [diff] [blame] | 4063 | /* Double-check that we did not exceed the bounds |
| 4064 | * of the outgoing record buffer. |
| 4065 | * This should never fail as the various message |
| 4066 | * writing functions must obey the bounds of the |
| 4067 | * outgoing record buffer, but better be safe. |
| 4068 | * |
| 4069 | * Note: We deliberately do not check for the MTU or MFL here. |
| 4070 | */ |
| 4071 | if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
| 4072 | { |
| 4073 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: " |
| 4074 | "size %u, maximum %u", |
| 4075 | (unsigned) ssl->out_msglen, |
| 4076 | (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 4077 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4078 | } |
| 4079 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4080 | /* |
| 4081 | * Fill handshake headers |
| 4082 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4083 | if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4084 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4085 | ssl->out_msg[1] = (unsigned char)( hs_len >> 16 ); |
| 4086 | ssl->out_msg[2] = (unsigned char)( hs_len >> 8 ); |
| 4087 | ssl->out_msg[3] = (unsigned char)( hs_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4088 | |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4089 | /* |
| 4090 | * DTLS has additional fields in the Handshake layer, |
| 4091 | * between the length field and the actual payload: |
| 4092 | * uint16 message_seq; |
| 4093 | * uint24 fragment_offset; |
| 4094 | * uint24 fragment_length; |
| 4095 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4096 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4097 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4098 | { |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 4099 | /* Make room for the additional DTLS fields */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4100 | if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 ) |
Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 4101 | { |
| 4102 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: " |
| 4103 | "size %u, maximum %u", |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4104 | (unsigned) ( hs_len ), |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4105 | (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) ); |
Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 4106 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4107 | } |
| 4108 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4109 | memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len ); |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4110 | ssl->out_msglen += 8; |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4111 | |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 4112 | /* Write message_seq and update it, except for HelloRequest */ |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4113 | if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 4114 | { |
Manuel Pégourié-Gonnard | d9ba0d9 | 2014-09-02 18:30:26 +0200 | [diff] [blame] | 4115 | ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF; |
| 4116 | ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF; |
| 4117 | ++( ssl->handshake->out_msg_seq ); |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 4118 | } |
| 4119 | else |
| 4120 | { |
| 4121 | ssl->out_msg[4] = 0; |
| 4122 | ssl->out_msg[5] = 0; |
| 4123 | } |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 4124 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4125 | /* Handshake hashes are computed without fragmentation, |
| 4126 | * so set frag_offset = 0 and frag_len = hs_len for now */ |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 4127 | memset( ssl->out_msg + 6, 0x00, 3 ); |
| 4128 | memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 ); |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4129 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4130 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4131 | |
Hanno Becker | 0207e53 | 2018-08-28 10:28:28 +0100 | [diff] [blame] | 4132 | /* Update running hashes of handshake messages seen */ |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4133 | if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) |
| 4134 | ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4137 | /* Either send now, or just save to be sent (and resent) later */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4138 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4139 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | f6d6e30 | 2018-11-07 11:57:51 +0000 | [diff] [blame] | 4140 | ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4141 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4142 | { |
| 4143 | if( ( ret = ssl_flight_append( ssl ) ) != 0 ) |
| 4144 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4145 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4146 | return( ret ); |
| 4147 | } |
| 4148 | } |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4149 | else |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4150 | #endif |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4151 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4152 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4153 | { |
| 4154 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret ); |
| 4155 | return( ret ); |
| 4156 | } |
| 4157 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4158 | |
| 4159 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) ); |
| 4160 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4161 | return( 0 ); |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4162 | } |
| 4163 | |
| 4164 | /* |
| 4165 | * Record layer functions |
| 4166 | */ |
| 4167 | |
| 4168 | /* |
| 4169 | * Write current record. |
| 4170 | * |
| 4171 | * Uses: |
| 4172 | * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS) |
| 4173 | * - ssl->out_msglen: length of the record content (excl headers) |
| 4174 | * - ssl->out_msg: record content |
| 4175 | */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4176 | int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush ) |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4177 | { |
| 4178 | int ret, done = 0; |
| 4179 | size_t len = ssl->out_msglen; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4180 | uint8_t flush = force_flush; |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4181 | |
| 4182 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4183 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4184 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4185 | if( ssl->transform_out != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4186 | ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4187 | { |
| 4188 | if( ( ret = ssl_compress_buf( ssl ) ) != 0 ) |
| 4189 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4190 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4191 | return( ret ); |
| 4192 | } |
| 4193 | |
| 4194 | len = ssl->out_msglen; |
| 4195 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4196 | #endif /*MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4197 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4198 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 4199 | if( mbedtls_ssl_hw_record_write != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4200 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4201 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4202 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4203 | ret = mbedtls_ssl_hw_record_write( ssl ); |
| 4204 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4205 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4206 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret ); |
| 4207 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4208 | } |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 4209 | |
| 4210 | if( ret == 0 ) |
| 4211 | done = 1; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4212 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4213 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4214 | if( !done ) |
| 4215 | { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4216 | unsigned i; |
| 4217 | size_t protected_record_size; |
| 4218 | |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4219 | /* Skip writing the record content type to after the encryption, |
| 4220 | * as it may change when using the CID extension. */ |
| 4221 | |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 4222 | mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ), |
| 4223 | mbedtls_ssl_get_minor_ver( ssl ), |
| 4224 | ssl->conf->transport, ssl->out_hdr + 1 ); |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 4225 | |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 4226 | memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 4227 | ssl->out_len[0] = (unsigned char)( len >> 8 ); |
| 4228 | ssl->out_len[1] = (unsigned char)( len ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4229 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4230 | if( ssl->transform_out != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4231 | { |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4232 | mbedtls_record rec; |
| 4233 | |
| 4234 | rec.buf = ssl->out_iv; |
| 4235 | rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - |
| 4236 | ( ssl->out_iv - ssl->out_buf ); |
| 4237 | rec.data_len = ssl->out_msglen; |
| 4238 | rec.data_offset = ssl->out_msg - rec.buf; |
| 4239 | |
| 4240 | memcpy( &rec.ctr[0], ssl->out_ctr, 8 ); |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 4241 | mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ), |
| 4242 | mbedtls_ssl_get_minor_ver( ssl ), |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4243 | ssl->conf->transport, rec.ver ); |
| 4244 | rec.type = ssl->out_msgtype; |
| 4245 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4246 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 505089d | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 4247 | /* The CID is set by mbedtls_ssl_encrypt_buf(). */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 4248 | rec.cid_len = 0; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4249 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 4250 | |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 4251 | if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec, |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 4252 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 4253 | mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4254 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4255 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4256 | return( ret ); |
| 4257 | } |
| 4258 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4259 | if( rec.data_offset != 0 ) |
| 4260 | { |
| 4261 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4262 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4263 | } |
| 4264 | |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4265 | /* Update the record content type and CID. */ |
| 4266 | ssl->out_msgtype = rec.type; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4267 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID ) |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4268 | memcpy( ssl->out_cid, rec.cid, rec.cid_len ); |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4269 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | c5aee96 | 2019-03-14 12:56:23 +0000 | [diff] [blame] | 4270 | ssl->out_msglen = len = rec.data_len; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4271 | ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 ); |
| 4272 | ssl->out_len[1] = (unsigned char)( rec.data_len ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4273 | } |
| 4274 | |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 4275 | protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4276 | |
| 4277 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4278 | /* In case of DTLS, double-check that we don't exceed |
| 4279 | * the remaining space in the datagram. */ |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4280 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4281 | { |
Hanno Becker | 554b0af | 2018-08-22 20:33:41 +0100 | [diff] [blame] | 4282 | ret = ssl_get_remaining_space_in_datagram( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4283 | if( ret < 0 ) |
| 4284 | return( ret ); |
| 4285 | |
| 4286 | if( protected_record_size > (size_t) ret ) |
| 4287 | { |
| 4288 | /* Should never happen */ |
| 4289 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4290 | } |
| 4291 | } |
| 4292 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4293 | |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4294 | /* Now write the potentially updated record content type. */ |
| 4295 | ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype; |
| 4296 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4297 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, " |
Hanno Becker | ecbdf1c | 2018-08-28 09:53:54 +0100 | [diff] [blame] | 4298 | "version = [%d:%d], msglen = %d", |
| 4299 | ssl->out_hdr[0], ssl->out_hdr[1], |
| 4300 | ssl->out_hdr[2], len ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4301 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4302 | MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network", |
Hanno Becker | ecbdf1c | 2018-08-28 09:53:54 +0100 | [diff] [blame] | 4303 | ssl->out_hdr, protected_record_size ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4304 | |
| 4305 | ssl->out_left += protected_record_size; |
| 4306 | ssl->out_hdr += protected_record_size; |
| 4307 | ssl_update_out_pointers( ssl, ssl->transform_out ); |
| 4308 | |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 4309 | for( i = 8; i > ssl_ep_len( ssl ); i-- ) |
| 4310 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
| 4311 | break; |
| 4312 | |
| 4313 | /* The loop goes to its end iff the counter is wrapping */ |
| 4314 | if( i == ssl_ep_len( ssl ) ) |
| 4315 | { |
| 4316 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) ); |
| 4317 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 4318 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4319 | } |
| 4320 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4321 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4322 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | 47db877 | 2018-08-21 13:32:13 +0100 | [diff] [blame] | 4323 | flush == SSL_DONT_FORCE_FLUSH ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4324 | { |
Hanno Becker | 1f5a15d | 2018-08-21 13:31:31 +0100 | [diff] [blame] | 4325 | size_t remaining; |
| 4326 | ret = ssl_get_remaining_payload_in_datagram( ssl ); |
| 4327 | if( ret < 0 ) |
| 4328 | { |
| 4329 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram", |
| 4330 | ret ); |
| 4331 | return( ret ); |
| 4332 | } |
| 4333 | |
| 4334 | remaining = (size_t) ret; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4335 | if( remaining == 0 ) |
Hanno Becker | f0da667 | 2018-08-28 09:55:10 +0100 | [diff] [blame] | 4336 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4337 | flush = SSL_FORCE_FLUSH; |
Hanno Becker | f0da667 | 2018-08-28 09:55:10 +0100 | [diff] [blame] | 4338 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4339 | else |
| 4340 | { |
Hanno Becker | 513815a | 2018-08-20 11:56:09 +0100 | [diff] [blame] | 4341 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4342 | } |
| 4343 | } |
| 4344 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4345 | |
| 4346 | if( ( flush == SSL_FORCE_FLUSH ) && |
| 4347 | ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4348 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4349 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4350 | return( ret ); |
| 4351 | } |
| 4352 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4353 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4354 | |
| 4355 | return( 0 ); |
| 4356 | } |
| 4357 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4358 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4359 | |
| 4360 | static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl ) |
| 4361 | { |
| 4362 | if( ssl->in_msglen < ssl->in_hslen || |
| 4363 | memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 || |
| 4364 | memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ) |
| 4365 | { |
| 4366 | return( 1 ); |
| 4367 | } |
| 4368 | return( 0 ); |
| 4369 | } |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4370 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4371 | static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl ) |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4372 | { |
| 4373 | return( ( ssl->in_msg[9] << 16 ) | |
| 4374 | ( ssl->in_msg[10] << 8 ) | |
| 4375 | ssl->in_msg[11] ); |
| 4376 | } |
| 4377 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4378 | static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl ) |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4379 | { |
| 4380 | return( ( ssl->in_msg[6] << 16 ) | |
| 4381 | ( ssl->in_msg[7] << 8 ) | |
| 4382 | ssl->in_msg[8] ); |
| 4383 | } |
| 4384 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4385 | static int ssl_check_hs_header( mbedtls_ssl_context const *ssl ) |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4386 | { |
| 4387 | uint32_t msg_len, frag_off, frag_len; |
| 4388 | |
| 4389 | msg_len = ssl_get_hs_total_len( ssl ); |
| 4390 | frag_off = ssl_get_hs_frag_off( ssl ); |
| 4391 | frag_len = ssl_get_hs_frag_len( ssl ); |
| 4392 | |
| 4393 | if( frag_off > msg_len ) |
| 4394 | return( -1 ); |
| 4395 | |
| 4396 | if( frag_len > msg_len - frag_off ) |
| 4397 | return( -1 ); |
| 4398 | |
| 4399 | if( frag_len + 12 > ssl->in_msglen ) |
| 4400 | return( -1 ); |
| 4401 | |
| 4402 | return( 0 ); |
| 4403 | } |
| 4404 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4405 | /* |
| 4406 | * Mark bits in bitmask (used for DTLS HS reassembly) |
| 4407 | */ |
| 4408 | static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len ) |
| 4409 | { |
| 4410 | unsigned int start_bits, end_bits; |
| 4411 | |
| 4412 | start_bits = 8 - ( offset % 8 ); |
| 4413 | if( start_bits != 8 ) |
| 4414 | { |
| 4415 | size_t first_byte_idx = offset / 8; |
| 4416 | |
Manuel Pégourié-Gonnard | ac03052 | 2014-09-02 14:23:40 +0200 | [diff] [blame] | 4417 | /* Special case */ |
| 4418 | if( len <= start_bits ) |
| 4419 | { |
| 4420 | for( ; len != 0; len-- ) |
| 4421 | mask[first_byte_idx] |= 1 << ( start_bits - len ); |
| 4422 | |
| 4423 | /* Avoid potential issues with offset or len becoming invalid */ |
| 4424 | return; |
| 4425 | } |
| 4426 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4427 | offset += start_bits; /* Now offset % 8 == 0 */ |
| 4428 | len -= start_bits; |
| 4429 | |
| 4430 | for( ; start_bits != 0; start_bits-- ) |
| 4431 | mask[first_byte_idx] |= 1 << ( start_bits - 1 ); |
| 4432 | } |
| 4433 | |
| 4434 | end_bits = len % 8; |
| 4435 | if( end_bits != 0 ) |
| 4436 | { |
| 4437 | size_t last_byte_idx = ( offset + len ) / 8; |
| 4438 | |
| 4439 | len -= end_bits; /* Now len % 8 == 0 */ |
| 4440 | |
| 4441 | for( ; end_bits != 0; end_bits-- ) |
| 4442 | mask[last_byte_idx] |= 1 << ( 8 - end_bits ); |
| 4443 | } |
| 4444 | |
| 4445 | memset( mask + offset / 8, 0xFF, len / 8 ); |
| 4446 | } |
| 4447 | |
| 4448 | /* |
| 4449 | * Check that bitmask is full |
| 4450 | */ |
| 4451 | static int ssl_bitmask_check( unsigned char *mask, size_t len ) |
| 4452 | { |
| 4453 | size_t i; |
| 4454 | |
| 4455 | for( i = 0; i < len / 8; i++ ) |
| 4456 | if( mask[i] != 0xFF ) |
| 4457 | return( -1 ); |
| 4458 | |
| 4459 | for( i = 0; i < len % 8; i++ ) |
| 4460 | if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 ) |
| 4461 | return( -1 ); |
| 4462 | |
| 4463 | return( 0 ); |
| 4464 | } |
| 4465 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4466 | /* msg_len does not include the handshake header */ |
Hanno Becker | 65dc885 | 2018-08-23 09:40:49 +0100 | [diff] [blame] | 4467 | static size_t ssl_get_reassembly_buffer_size( size_t msg_len, |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4468 | unsigned add_bitmap ) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4469 | { |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4470 | size_t alloc_len; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4471 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4472 | alloc_len = 12; /* Handshake header */ |
| 4473 | alloc_len += msg_len; /* Content buffer */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4474 | |
Hanno Becker | d07df86 | 2018-08-16 09:14:58 +0100 | [diff] [blame] | 4475 | if( add_bitmap ) |
| 4476 | alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */ |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4477 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4478 | return( alloc_len ); |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4479 | } |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4480 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4481 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4482 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4483 | static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl ) |
Hanno Becker | 12555c6 | 2018-08-16 12:47:53 +0100 | [diff] [blame] | 4484 | { |
| 4485 | return( ( ssl->in_msg[1] << 16 ) | |
| 4486 | ( ssl->in_msg[2] << 8 ) | |
| 4487 | ssl->in_msg[3] ); |
| 4488 | } |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4489 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4490 | int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4491 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4492 | if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) ) |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4493 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4494 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d", |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4495 | ssl->in_msglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4496 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4497 | } |
| 4498 | |
Hanno Becker | 12555c6 | 2018-08-16 12:47:53 +0100 | [diff] [blame] | 4499 | ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl ); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4500 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4501 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen =" |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4502 | " %d, type = %d, hslen = %d", |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4503 | ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) ); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4504 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4505 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4506 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4507 | { |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4508 | int ret; |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4509 | unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5]; |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4510 | |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4511 | if( ssl_check_hs_header( ssl ) != 0 ) |
| 4512 | { |
| 4513 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) ); |
| 4514 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4515 | } |
| 4516 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4517 | if( ssl->handshake != NULL && |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 4518 | ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && |
| 4519 | recv_msg_seq != ssl->handshake->in_msg_seq ) || |
| 4520 | ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 4521 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4522 | { |
Hanno Becker | 9e1ec22 | 2018-08-15 15:54:43 +0100 | [diff] [blame] | 4523 | if( recv_msg_seq > ssl->handshake->in_msg_seq ) |
| 4524 | { |
| 4525 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)", |
| 4526 | recv_msg_seq, |
| 4527 | ssl->handshake->in_msg_seq ) ); |
| 4528 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 4529 | } |
| 4530 | |
Manuel Pégourié-Gonnard | fc572dd | 2014-10-09 17:56:57 +0200 | [diff] [blame] | 4531 | /* Retransmit only on last message from previous flight, to avoid |
| 4532 | * too many retransmissions. |
| 4533 | * Besides, No sane server ever retransmits HelloVerifyRequest */ |
| 4534 | if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4535 | ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST ) |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4536 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4537 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, " |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4538 | "message_seq = %d, start_of_flight = %d", |
| 4539 | recv_msg_seq, |
| 4540 | ssl->handshake->in_flight_start_seq ) ); |
| 4541 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4542 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4543 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4544 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4545 | return( ret ); |
| 4546 | } |
| 4547 | } |
| 4548 | else |
| 4549 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4550 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: " |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4551 | "message_seq = %d, expected = %d", |
| 4552 | recv_msg_seq, |
| 4553 | ssl->handshake->in_msg_seq ) ); |
| 4554 | } |
| 4555 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 4556 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4557 | } |
| 4558 | /* Wait until message completion to increment in_msg_seq */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4559 | |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4560 | /* Message reassembly is handled alongside buffering of future |
| 4561 | * messages; the commonality is that both handshake fragments and |
Hanno Becker | 83ab41c | 2018-08-28 17:19:38 +0100 | [diff] [blame] | 4562 | * future messages cannot be forwarded immediately to the |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4563 | * handshake logic layer. */ |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4564 | if( ssl_hs_is_proper_fragment( ssl ) == 1 ) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4565 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4566 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) ); |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4567 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4568 | } |
| 4569 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4570 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4571 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4572 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4573 | { |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4574 | /* With TLS we don't handle fragmentation (for now) */ |
| 4575 | if( ssl->in_msglen < ssl->in_hslen ) |
| 4576 | { |
| 4577 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) ); |
| 4578 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4579 | } |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4580 | } |
Manuel Pégourié-Gonnard | ec1c222 | 2019-06-12 10:18:26 +0200 | [diff] [blame] | 4581 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4582 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4583 | return( 0 ); |
| 4584 | } |
| 4585 | |
| 4586 | void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl ) |
| 4587 | { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4588 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4589 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4590 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL ) |
Manuel Pégourié-Gonnard | 14bf706 | 2015-06-23 14:07:13 +0200 | [diff] [blame] | 4591 | { |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4592 | ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen ); |
Manuel Pégourié-Gonnard | 14bf706 | 2015-06-23 14:07:13 +0200 | [diff] [blame] | 4593 | } |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4594 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4595 | /* Handshake message is complete, increment counter */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4596 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4597 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4598 | ssl->handshake != NULL ) |
| 4599 | { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4600 | unsigned offset; |
| 4601 | mbedtls_ssl_hs_buffer *hs_buf; |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4602 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4603 | /* Increment handshake sequence number */ |
| 4604 | hs->in_msg_seq++; |
| 4605 | |
| 4606 | /* |
| 4607 | * Clear up handshake buffering and reassembly structure. |
| 4608 | */ |
| 4609 | |
| 4610 | /* Free first entry */ |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 4611 | ssl_buffering_free_slot( ssl, 0 ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4612 | |
| 4613 | /* Shift all other entries */ |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 4614 | for( offset = 0, hs_buf = &hs->buffering.hs[0]; |
| 4615 | offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS; |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4616 | offset++, hs_buf++ ) |
| 4617 | { |
| 4618 | *hs_buf = *(hs_buf + 1); |
| 4619 | } |
| 4620 | |
| 4621 | /* Create a fresh last entry */ |
| 4622 | memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4623 | } |
| 4624 | #endif |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4625 | } |
| 4626 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4627 | /* |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4628 | * DTLS anti-replay: RFC 6347 4.1.2.6 |
| 4629 | * |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4630 | * in_window is a field of bits numbered from 0 (lsb) to 63 (msb). |
| 4631 | * Bit n is set iff record number in_window_top - n has been seen. |
| 4632 | * |
| 4633 | * Usually, in_window_top is the last record number seen and the lsb of |
| 4634 | * in_window is set. The only exception is the initial state (record number 0 |
| 4635 | * not seen yet). |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4636 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4637 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4638 | static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4639 | { |
| 4640 | ssl->in_window_top = 0; |
| 4641 | ssl->in_window = 0; |
| 4642 | } |
| 4643 | |
| 4644 | static inline uint64_t ssl_load_six_bytes( unsigned char *buf ) |
| 4645 | { |
| 4646 | return( ( (uint64_t) buf[0] << 40 ) | |
| 4647 | ( (uint64_t) buf[1] << 32 ) | |
| 4648 | ( (uint64_t) buf[2] << 24 ) | |
| 4649 | ( (uint64_t) buf[3] << 16 ) | |
| 4650 | ( (uint64_t) buf[4] << 8 ) | |
| 4651 | ( (uint64_t) buf[5] ) ); |
| 4652 | } |
| 4653 | |
| 4654 | /* |
| 4655 | * Return 0 if sequence number is acceptable, -1 otherwise |
| 4656 | */ |
Hanno Becker | fc55172 | 2019-07-12 08:50:37 +0100 | [diff] [blame] | 4657 | int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4658 | { |
| 4659 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); |
| 4660 | uint64_t bit; |
| 4661 | |
Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 4662 | if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) == |
| 4663 | MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) |
| 4664 | { |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4665 | return( 0 ); |
Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 4666 | } |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4667 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4668 | if( rec_seqnum > ssl->in_window_top ) |
| 4669 | return( 0 ); |
| 4670 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4671 | bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4672 | |
| 4673 | if( bit >= 64 ) |
| 4674 | return( -1 ); |
| 4675 | |
| 4676 | if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 ) |
| 4677 | return( -1 ); |
| 4678 | |
| 4679 | return( 0 ); |
| 4680 | } |
| 4681 | |
| 4682 | /* |
| 4683 | * Update replay window on new validated record |
| 4684 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4685 | void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4686 | { |
| 4687 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); |
| 4688 | |
Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 4689 | if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) == |
| 4690 | MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) |
| 4691 | { |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4692 | return; |
Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 4693 | } |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4694 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4695 | if( rec_seqnum > ssl->in_window_top ) |
| 4696 | { |
| 4697 | /* Update window_top and the contents of the window */ |
| 4698 | uint64_t shift = rec_seqnum - ssl->in_window_top; |
| 4699 | |
| 4700 | if( shift >= 64 ) |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4701 | ssl->in_window = 1; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4702 | else |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4703 | { |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4704 | ssl->in_window <<= shift; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4705 | ssl->in_window |= 1; |
| 4706 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4707 | |
| 4708 | ssl->in_window_top = rec_seqnum; |
| 4709 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4710 | else |
| 4711 | { |
| 4712 | /* Mark that number as seen in the current window */ |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4713 | uint64_t bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4714 | |
| 4715 | if( bit < 64 ) /* Always true, but be extra sure */ |
| 4716 | ssl->in_window |= (uint64_t) 1 << bit; |
| 4717 | } |
| 4718 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4719 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4720 | |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 4721 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4722 | /* Forward declaration */ |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 4723 | static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ); |
| 4724 | |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4725 | /* |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4726 | * Without any SSL context, check if a datagram looks like a ClientHello with |
| 4727 | * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message. |
Simon Butcher | 0789aed | 2015-09-11 17:15:17 +0100 | [diff] [blame] | 4728 | * Both input and output include full DTLS headers. |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4729 | * |
| 4730 | * - if cookie is valid, return 0 |
| 4731 | * - if ClientHello looks superficially valid but cookie is not, |
| 4732 | * fill obuf and set olen, then |
| 4733 | * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED |
| 4734 | * - otherwise return a specific error code |
| 4735 | */ |
| 4736 | static int ssl_check_dtls_clihlo_cookie( |
| 4737 | mbedtls_ssl_cookie_write_t *f_cookie_write, |
| 4738 | mbedtls_ssl_cookie_check_t *f_cookie_check, |
| 4739 | void *p_cookie, |
| 4740 | const unsigned char *cli_id, size_t cli_id_len, |
| 4741 | const unsigned char *in, size_t in_len, |
| 4742 | unsigned char *obuf, size_t buf_len, size_t *olen ) |
| 4743 | { |
| 4744 | size_t sid_len, cookie_len; |
| 4745 | unsigned char *p; |
| 4746 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4747 | /* |
| 4748 | * Structure of ClientHello with record and handshake headers, |
| 4749 | * and expected values. We don't need to check a lot, more checks will be |
| 4750 | * done when actually parsing the ClientHello - skipping those checks |
| 4751 | * avoids code duplication and does not make cookie forging any easier. |
| 4752 | * |
| 4753 | * 0-0 ContentType type; copied, must be handshake |
| 4754 | * 1-2 ProtocolVersion version; copied |
| 4755 | * 3-4 uint16 epoch; copied, must be 0 |
| 4756 | * 5-10 uint48 sequence_number; copied |
| 4757 | * 11-12 uint16 length; (ignored) |
| 4758 | * |
| 4759 | * 13-13 HandshakeType msg_type; (ignored) |
| 4760 | * 14-16 uint24 length; (ignored) |
| 4761 | * 17-18 uint16 message_seq; copied |
| 4762 | * 19-21 uint24 fragment_offset; copied, must be 0 |
| 4763 | * 22-24 uint24 fragment_length; (ignored) |
| 4764 | * |
| 4765 | * 25-26 ProtocolVersion client_version; (ignored) |
| 4766 | * 27-58 Random random; (ignored) |
| 4767 | * 59-xx SessionID session_id; 1 byte len + sid_len content |
| 4768 | * 60+ opaque cookie<0..2^8-1>; 1 byte len + content |
| 4769 | * ... |
| 4770 | * |
| 4771 | * Minimum length is 61 bytes. |
| 4772 | */ |
| 4773 | if( in_len < 61 || |
| 4774 | in[0] != MBEDTLS_SSL_MSG_HANDSHAKE || |
| 4775 | in[3] != 0 || in[4] != 0 || |
| 4776 | in[19] != 0 || in[20] != 0 || in[21] != 0 ) |
| 4777 | { |
| 4778 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 4779 | } |
| 4780 | |
| 4781 | sid_len = in[59]; |
| 4782 | if( sid_len > in_len - 61 ) |
| 4783 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 4784 | |
| 4785 | cookie_len = in[60 + sid_len]; |
| 4786 | if( cookie_len > in_len - 60 ) |
| 4787 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 4788 | |
| 4789 | if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len, |
| 4790 | cli_id, cli_id_len ) == 0 ) |
| 4791 | { |
| 4792 | /* Valid cookie */ |
| 4793 | return( 0 ); |
| 4794 | } |
| 4795 | |
| 4796 | /* |
| 4797 | * If we get here, we've got an invalid cookie, let's prepare HVR. |
| 4798 | * |
| 4799 | * 0-0 ContentType type; copied |
| 4800 | * 1-2 ProtocolVersion version; copied |
| 4801 | * 3-4 uint16 epoch; copied |
| 4802 | * 5-10 uint48 sequence_number; copied |
| 4803 | * 11-12 uint16 length; olen - 13 |
| 4804 | * |
| 4805 | * 13-13 HandshakeType msg_type; hello_verify_request |
| 4806 | * 14-16 uint24 length; olen - 25 |
| 4807 | * 17-18 uint16 message_seq; copied |
| 4808 | * 19-21 uint24 fragment_offset; copied |
| 4809 | * 22-24 uint24 fragment_length; olen - 25 |
| 4810 | * |
| 4811 | * 25-26 ProtocolVersion server_version; 0xfe 0xff |
| 4812 | * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie |
| 4813 | * |
| 4814 | * Minimum length is 28. |
| 4815 | */ |
| 4816 | if( buf_len < 28 ) |
| 4817 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 4818 | |
| 4819 | /* Copy most fields and adapt others */ |
| 4820 | memcpy( obuf, in, 25 ); |
| 4821 | obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST; |
| 4822 | obuf[25] = 0xfe; |
| 4823 | obuf[26] = 0xff; |
| 4824 | |
| 4825 | /* Generate and write actual cookie */ |
| 4826 | p = obuf + 28; |
| 4827 | if( f_cookie_write( p_cookie, |
| 4828 | &p, obuf + buf_len, cli_id, cli_id_len ) != 0 ) |
| 4829 | { |
| 4830 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4831 | } |
| 4832 | |
| 4833 | *olen = p - obuf; |
| 4834 | |
| 4835 | /* Go back and fill length fields */ |
| 4836 | obuf[27] = (unsigned char)( *olen - 28 ); |
| 4837 | |
| 4838 | obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 ); |
| 4839 | obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 ); |
| 4840 | obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) ); |
| 4841 | |
| 4842 | obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 ); |
| 4843 | obuf[12] = (unsigned char)( ( *olen - 13 ) ); |
| 4844 | |
| 4845 | return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); |
| 4846 | } |
| 4847 | |
| 4848 | /* |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4849 | * Handle possible client reconnect with the same UDP quadruplet |
| 4850 | * (RFC 6347 Section 4.2.8). |
| 4851 | * |
| 4852 | * Called by ssl_parse_record_header() in case we receive an epoch 0 record |
| 4853 | * that looks like a ClientHello. |
| 4854 | * |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4855 | * - if the input looks like a ClientHello without cookies, |
| 4856 | * send back HelloVerifyRequest, then |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4857 | * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4858 | * - if the input looks like a ClientHello with a valid cookie, |
| 4859 | * reset the session of the current context, and |
Manuel Pégourié-Gonnard | be619c1 | 2015-09-08 11:21:21 +0200 | [diff] [blame] | 4860 | * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4861 | * - if anything goes wrong, return a specific error code |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4862 | * |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4863 | * mbedtls_ssl_read_record() will ignore the record if anything else than |
Simon Butcher | d0bf6a3 | 2015-09-11 17:34:49 +0100 | [diff] [blame] | 4864 | * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function |
| 4865 | * cannot not return 0. |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4866 | */ |
| 4867 | static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl ) |
| 4868 | { |
| 4869 | int ret; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4870 | size_t len; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4871 | |
Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4872 | if( ssl->conf->f_cookie_write == NULL || |
| 4873 | ssl->conf->f_cookie_check == NULL ) |
| 4874 | { |
| 4875 | /* If we can't use cookies to verify reachability of the peer, |
| 4876 | * drop the record. */ |
| 4877 | return( 0 ); |
| 4878 | } |
| 4879 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4880 | ret = ssl_check_dtls_clihlo_cookie( |
| 4881 | ssl->conf->f_cookie_write, |
| 4882 | ssl->conf->f_cookie_check, |
| 4883 | ssl->conf->p_cookie, |
| 4884 | ssl->cli_id, ssl->cli_id_len, |
| 4885 | ssl->in_buf, ssl->in_left, |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4886 | ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4887 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4888 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret ); |
| 4889 | |
| 4890 | if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4891 | { |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 4892 | /* Don't check write errors as we can't do anything here. |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4893 | * If the error is permanent we'll catch it later, |
| 4894 | * if it's not, then hopefully it'll work next time. */ |
Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 4895 | (void) mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len ); |
Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4896 | ret = 0; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4897 | } |
| 4898 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4899 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4900 | { |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4901 | /* Got a valid cookie, partially reset context */ |
| 4902 | if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 ) |
| 4903 | { |
| 4904 | MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret ); |
| 4905 | return( ret ); |
| 4906 | } |
| 4907 | |
| 4908 | return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4909 | } |
| 4910 | |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4911 | return( ret ); |
| 4912 | } |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 4913 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4914 | |
Hanno Becker | 46483f1 | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 4915 | static int ssl_check_record_type( uint8_t record_type ) |
| 4916 | { |
| 4917 | if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4918 | record_type != MBEDTLS_SSL_MSG_ALERT && |
| 4919 | record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC && |
| 4920 | record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
| 4921 | { |
| 4922 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4923 | } |
| 4924 | |
| 4925 | return( 0 ); |
| 4926 | } |
| 4927 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4928 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4929 | * ContentType type; |
| 4930 | * ProtocolVersion version; |
| 4931 | * uint16 epoch; // DTLS only |
| 4932 | * uint48 sequence_number; // DTLS only |
| 4933 | * uint16 length; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4934 | * |
| 4935 | * Return 0 if header looks sane (and, for DTLS, the record is expected) |
Simon Butcher | 207990d | 2015-12-16 01:51:30 +0000 | [diff] [blame] | 4936 | * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad, |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4937 | * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected. |
| 4938 | * |
| 4939 | * With DTLS, mbedtls_ssl_read_record() will: |
Simon Butcher | 207990d | 2015-12-16 01:51:30 +0000 | [diff] [blame] | 4940 | * 1. proceed with the record if this function returns 0 |
| 4941 | * 2. drop only the current record if this function returns UNEXPECTED_RECORD |
| 4942 | * 3. return CLIENT_RECONNECT if this function return that value |
| 4943 | * 4. drop the whole datagram if this function returns anything else. |
| 4944 | * Point 2 is needed when the peer is resending, and we have already received |
| 4945 | * the first record from a datagram but are still waiting for the others. |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4946 | */ |
Hanno Becker | 21fc61c | 2019-07-12 11:10:16 +0100 | [diff] [blame] | 4947 | static int ssl_parse_record_header( mbedtls_ssl_context const *ssl, |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4948 | unsigned char *buf, |
| 4949 | size_t len, |
| 4950 | mbedtls_record *rec ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4951 | { |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 4952 | int major_ver, minor_ver; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4953 | |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4954 | size_t const rec_hdr_type_offset = 0; |
| 4955 | size_t const rec_hdr_type_len = 1; |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 4956 | |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4957 | size_t const rec_hdr_version_offset = rec_hdr_type_offset + |
| 4958 | rec_hdr_type_len; |
| 4959 | size_t const rec_hdr_version_len = 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4960 | |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4961 | size_t const rec_hdr_ctr_len = 8; |
| 4962 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 6181761 | 2019-07-25 10:13:02 +0100 | [diff] [blame] | 4963 | uint32_t rec_epoch; |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4964 | size_t const rec_hdr_ctr_offset = rec_hdr_version_offset + |
| 4965 | rec_hdr_version_len; |
| 4966 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4967 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4968 | size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset + |
| 4969 | rec_hdr_ctr_len; |
Hanno Becker | 6181761 | 2019-07-25 10:13:02 +0100 | [diff] [blame] | 4970 | size_t rec_hdr_cid_len = 0; |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4971 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4972 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4973 | |
| 4974 | size_t rec_hdr_len_offset; /* To be determined */ |
| 4975 | size_t const rec_hdr_len_len = 2; |
| 4976 | |
| 4977 | /* |
| 4978 | * Check minimum lengths for record header. |
| 4979 | */ |
| 4980 | |
| 4981 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4982 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
| 4983 | { |
| 4984 | rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len; |
| 4985 | } |
| 4986 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 4987 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4988 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 4989 | { |
| 4990 | rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len; |
| 4991 | } |
| 4992 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4993 | |
| 4994 | if( len < rec_hdr_len_offset + rec_hdr_len_len ) |
| 4995 | { |
| 4996 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u", |
| 4997 | (unsigned) len, |
| 4998 | (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) ); |
| 4999 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5000 | } |
| 5001 | |
| 5002 | /* |
| 5003 | * Parse and validate record content type |
| 5004 | */ |
| 5005 | |
| 5006 | rec->type = buf[ rec_hdr_type_offset ]; |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5007 | |
| 5008 | /* Check record content type */ |
| 5009 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 5010 | rec->cid_len = 0; |
| 5011 | |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 5012 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5013 | mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 && |
| 5014 | rec->type == MBEDTLS_SSL_MSG_CID ) |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5015 | { |
| 5016 | /* Shift pointers to account for record header including CID |
| 5017 | * struct { |
| 5018 | * ContentType special_type = tls12_cid; |
| 5019 | * ProtocolVersion version; |
| 5020 | * uint16 epoch; |
| 5021 | * uint48 sequence_number; |
Hanno Becker | 3b2bf5b | 2019-05-23 17:03:19 +0100 | [diff] [blame] | 5022 | * opaque cid[cid_length]; // Additional field compared to |
| 5023 | * // default DTLS record format |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5024 | * uint16 length; |
| 5025 | * opaque enc_content[DTLSCiphertext.length]; |
| 5026 | * } DTLSCiphertext; |
| 5027 | */ |
| 5028 | |
| 5029 | /* So far, we only support static CID lengths |
| 5030 | * fixed in the configuration. */ |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5031 | rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf ); |
| 5032 | rec_hdr_len_offset += rec_hdr_cid_len; |
Hanno Becker | de7d6d3 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 5033 | |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5034 | if( len < rec_hdr_len_offset + rec_hdr_len_len ) |
Hanno Becker | de7d6d3 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 5035 | { |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5036 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u", |
| 5037 | (unsigned) len, |
| 5038 | (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) ); |
Hanno Becker | 2982346 | 2019-07-10 14:53:43 +0100 | [diff] [blame] | 5039 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Hanno Becker | de7d6d3 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 5040 | } |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5041 | |
Manuel Pégourié-Gonnard | f3a15b3 | 2019-08-02 10:17:15 +0200 | [diff] [blame] | 5042 | /* configured CID len is guaranteed at most 255, see |
| 5043 | * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */ |
| 5044 | rec->cid_len = (uint8_t) rec_hdr_cid_len; |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5045 | memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len ); |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5046 | } |
| 5047 | else |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5048 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 5049 | { |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5050 | if( ssl_check_record_type( rec->type ) ) |
| 5051 | { |
Hanno Becker | 03e2db6 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 5052 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u", |
| 5053 | (unsigned) rec->type ) ); |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5054 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5055 | } |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 5056 | } |
| 5057 | |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5058 | /* |
| 5059 | * Parse and validate record version |
| 5060 | */ |
| 5061 | |
Hanno Becker | 8061c6e | 2019-07-26 08:07:03 +0100 | [diff] [blame] | 5062 | rec->ver[0] = buf[ rec_hdr_version_offset + 0 ]; |
| 5063 | rec->ver[1] = buf[ rec_hdr_version_offset + 1 ]; |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5064 | mbedtls_ssl_read_version( &major_ver, &minor_ver, |
| 5065 | ssl->conf->transport, |
Hanno Becker | 8061c6e | 2019-07-26 08:07:03 +0100 | [diff] [blame] | 5066 | &rec->ver[0] ); |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5067 | |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 5068 | if( major_ver != mbedtls_ssl_get_major_ver( ssl ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5069 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5070 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) ); |
| 5071 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5072 | } |
| 5073 | |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 5074 | if( minor_ver > mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5075 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5076 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) ); |
| 5077 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5078 | } |
| 5079 | |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5080 | /* |
| 5081 | * Parse/Copy record sequence number. |
| 5082 | */ |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5083 | |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5084 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5085 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
| 5086 | { |
| 5087 | /* Copy explicit record sequence number from input buffer. */ |
| 5088 | memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset, |
| 5089 | rec_hdr_ctr_len ); |
| 5090 | } |
| 5091 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 5092 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5093 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 5094 | { |
| 5095 | /* Copy implicit record sequence number from SSL context structure. */ |
| 5096 | memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len ); |
| 5097 | } |
| 5098 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5099 | |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5100 | /* |
| 5101 | * Parse record length. |
| 5102 | */ |
| 5103 | |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5104 | rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len; |
Hanno Becker | 7b5ba84 | 2019-07-25 10:16:37 +0100 | [diff] [blame] | 5105 | rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) | |
| 5106 | ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 ); |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5107 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset ); |
| 5108 | |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5109 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, " |
Hanno Becker | d8f7c4a | 2019-05-23 17:03:44 +0100 | [diff] [blame] | 5110 | "version = [%d:%d], msglen = %d", |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5111 | rec->type, |
| 5112 | major_ver, minor_ver, rec->data_len ) ); |
| 5113 | |
| 5114 | rec->buf = buf; |
| 5115 | rec->buf_len = rec->data_offset + rec->data_len; |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5116 | |
Hanno Becker | ec01408 | 2019-07-26 08:20:27 +0100 | [diff] [blame] | 5117 | if( rec->data_len == 0 ) |
| 5118 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5119 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5120 | /* |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 5121 | * DTLS-related tests. |
| 5122 | * Check epoch before checking length constraint because |
| 5123 | * the latter varies with the epoch. E.g., if a ChangeCipherSpec |
| 5124 | * message gets duplicated before the corresponding Finished message, |
| 5125 | * the second ChangeCipherSpec should be discarded because it belongs |
| 5126 | * to an old epoch, but not because its length is shorter than |
| 5127 | * the minimum record length for packets using the new record transform. |
| 5128 | * Note that these two kinds of failures are handled differently, |
| 5129 | * as an unexpected record is silently skipped but an invalid |
| 5130 | * record leads to the entire datagram being dropped. |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5131 | */ |
| 5132 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 5133 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5134 | { |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5135 | rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1]; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5136 | |
Hanno Becker | e045277 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 5137 | /* Check that the datagram is large enough to contain a record |
| 5138 | * of the advertised length. */ |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5139 | if( len < rec->data_offset + rec->data_len ) |
Hanno Becker | e045277 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 5140 | { |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5141 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.", |
| 5142 | (unsigned) len, |
| 5143 | (unsigned)( rec->data_offset + rec->data_len ) ) ); |
Hanno Becker | e045277 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 5144 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5145 | } |
Hanno Becker | 6c0e53c | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 5146 | |
Hanno Becker | 6c0e53c | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 5147 | /* Records from other, non-matching epochs are silently discarded. |
| 5148 | * (The case of same-port Client reconnects must be considered in |
| 5149 | * the caller). */ |
Hanno Becker | c1c173c | 2019-07-19 10:59:12 +0100 | [diff] [blame] | 5150 | if( rec_epoch != ssl->in_epoch ) |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5151 | { |
| 5152 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: " |
| 5153 | "expected %d, received %d", |
| 5154 | ssl->in_epoch, rec_epoch ) ); |
Hanno Becker | c1c173c | 2019-07-19 10:59:12 +0100 | [diff] [blame] | 5155 | |
| 5156 | /* Records from the next epoch are considered for buffering |
| 5157 | * (concretely: early Finished messages). */ |
| 5158 | if( rec_epoch == (unsigned) ssl->in_epoch + 1 ) |
| 5159 | { |
| 5160 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) ); |
| 5161 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 5162 | } |
| 5163 | |
Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5164 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5165 | } |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5166 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Hanno Becker | 6c0e53c | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 5167 | /* For records from the correct epoch, check whether their |
| 5168 | * sequence number has been seen before. */ |
Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5169 | else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5170 | { |
| 5171 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) ); |
| 5172 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
| 5173 | } |
| 5174 | #endif |
| 5175 | } |
| 5176 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5177 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5178 | return( 0 ); |
| 5179 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5180 | |
Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5181 | |
| 5182 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
| 5183 | static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl ) |
| 5184 | { |
| 5185 | unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1]; |
| 5186 | |
| 5187 | /* |
| 5188 | * Check for an epoch 0 ClientHello. We can't use in_msg here to |
| 5189 | * access the first byte of record content (handshake type), as we |
| 5190 | * have an active transform (possibly iv_len != 0), so use the |
| 5191 | * fact that the record header len is 13 instead. |
| 5192 | */ |
| 5193 | if( rec_epoch == 0 && |
| 5194 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 5195 | MBEDTLS_SSL_IS_SERVER && |
| 5196 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 5197 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 5198 | ssl->in_left > 13 && |
| 5199 | ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO ) |
| 5200 | { |
| 5201 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect " |
| 5202 | "from the same port" ) ); |
| 5203 | return( ssl_handle_possible_reconnect( ssl ) ); |
| 5204 | } |
| 5205 | |
| 5206 | return( 0 ); |
| 5207 | } |
| 5208 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
| 5209 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5210 | /* |
| 5211 | * If applicable, decrypt (and decompress) record content |
| 5212 | */ |
Hanno Becker | a89610a | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 5213 | static int ssl_prepare_record_content( mbedtls_ssl_context *ssl, |
| 5214 | mbedtls_record *rec ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5215 | { |
| 5216 | int ret, done = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 5217 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5218 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network", |
Hanno Becker | a89610a | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 5219 | rec->buf, rec->buf_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5220 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5221 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 5222 | if( mbedtls_ssl_hw_record_read != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5223 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5224 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5225 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5226 | ret = mbedtls_ssl_hw_record_read( ssl ); |
| 5227 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5228 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5229 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret ); |
| 5230 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5231 | } |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 5232 | |
| 5233 | if( ret == 0 ) |
| 5234 | done = 1; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5235 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5236 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5237 | if( !done && ssl->transform_in != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5238 | { |
Hanno Becker | 106f3da | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5239 | unsigned char const old_msg_type = rec->type; |
| 5240 | |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 5241 | if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, |
Hanno Becker | a89610a | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 5242 | rec ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5243 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5244 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret ); |
Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 5245 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5246 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 5247 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID && |
Hanno Becker | e0200da | 2019-06-13 09:23:43 +0100 | [diff] [blame] | 5248 | mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf ) |
| 5249 | == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 5250 | { |
Hanno Becker | 675c4d6 | 2019-05-24 10:11:06 +0100 | [diff] [blame] | 5251 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) ); |
Hanno Becker | 687e0fb | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 5252 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 5253 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5254 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 687e0fb | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 5255 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5256 | return( ret ); |
| 5257 | } |
| 5258 | |
Hanno Becker | 106f3da | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5259 | if( old_msg_type != rec->type ) |
Hanno Becker | 93012fe | 2018-08-07 14:30:18 +0100 | [diff] [blame] | 5260 | { |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5261 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d", |
Hanno Becker | 106f3da | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5262 | old_msg_type, rec->type ) ); |
Hanno Becker | 93012fe | 2018-08-07 14:30:18 +0100 | [diff] [blame] | 5263 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5264 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5265 | MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt", |
Hanno Becker | 106f3da | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5266 | rec->buf + rec->data_offset, rec->data_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5267 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5268 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5269 | /* We have already checked the record content type |
| 5270 | * in ssl_parse_record_header(), failing or silently |
| 5271 | * dropping the record in the case of an unknown type. |
| 5272 | * |
| 5273 | * Since with the use of CIDs, the record content type |
| 5274 | * might change during decryption, re-check the record |
| 5275 | * content type, but treat a failure as fatal this time. */ |
Hanno Becker | 106f3da | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5276 | if( ssl_check_record_type( rec->type ) ) |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5277 | { |
| 5278 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) ); |
| 5279 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5280 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5281 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5282 | |
Hanno Becker | 106f3da | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5283 | if( rec->data_len == 0 ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5284 | { |
| 5285 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 5286 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3 |
Hanno Becker | 106f3da | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5287 | && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5288 | { |
| 5289 | /* TLS v1.2 explicitly disallows zero-length messages which are not application data */ |
| 5290 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) ); |
| 5291 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5292 | } |
| 5293 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5294 | |
| 5295 | ssl->nb_zero++; |
| 5296 | |
| 5297 | /* |
| 5298 | * Three or more empty messages may be a DoS attack |
| 5299 | * (excessive CPU consumption). |
| 5300 | */ |
| 5301 | if( ssl->nb_zero > 3 ) |
| 5302 | { |
| 5303 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty " |
Hanno Becker | 70463db | 2019-05-08 10:38:32 +0100 | [diff] [blame] | 5304 | "messages, possible DoS attack" ) ); |
| 5305 | /* Treat the records as if they were not properly authenticated, |
| 5306 | * thereby failing the connection if we see more than allowed |
| 5307 | * by the configured bad MAC threshold. */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5308 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
| 5309 | } |
| 5310 | } |
| 5311 | else |
| 5312 | ssl->nb_zero = 0; |
| 5313 | |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5314 | /* Only needed for TLS, as with DTLS in_ctr is read from the header */ |
| 5315 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 5316 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5317 | { |
| 5318 | unsigned i; |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5319 | for( i = 8; i > 0; i-- ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5320 | if( ++ssl->in_ctr[i - 1] != 0 ) |
| 5321 | break; |
| 5322 | |
Manuel Pégourié-Gonnard | 8794a42 | 2019-06-11 10:04:57 +0200 | [diff] [blame] | 5323 | /* The loop goes to its end only if the counter is wrapping around */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5324 | if( i == 0 ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5325 | { |
| 5326 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) ); |
| 5327 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 5328 | } |
| 5329 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5330 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5331 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5332 | } |
| 5333 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5334 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5335 | if( ssl->transform_in != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5336 | ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5337 | { |
| 5338 | if( ( ret = ssl_decompress_buf( ssl ) ) != 0 ) |
| 5339 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5340 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5341 | return( ret ); |
| 5342 | } |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5343 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5344 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5345 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5346 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 5347 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 5348 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5349 | mbedtls_ssl_dtls_replay_update( ssl ); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 5350 | } |
| 5351 | #endif |
| 5352 | |
Hanno Becker | f024285 | 2019-07-09 17:30:02 +0100 | [diff] [blame] | 5353 | /* Check actual (decrypted) record content length against |
| 5354 | * configured maximum. */ |
| 5355 | if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN ) |
| 5356 | { |
| 5357 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 5358 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5359 | } |
| 5360 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5361 | return( 0 ); |
| 5362 | } |
| 5363 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5364 | static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 5365 | |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5366 | /* |
| 5367 | * Read a record. |
| 5368 | * |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 5369 | * Silently ignore non-fatal alert (and for DTLS, invalid records as well, |
| 5370 | * RFC 6347 4.1.2.7) and continue reading until a valid record is found. |
| 5371 | * |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5372 | */ |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5373 | |
| 5374 | /* Helper functions for mbedtls_ssl_read_record(). */ |
| 5375 | static int ssl_consume_current_message( mbedtls_ssl_context *ssl ); |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5376 | static int ssl_get_next_record( mbedtls_ssl_context *ssl ); |
| 5377 | static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl ); |
Hanno Becker | 4162b11 | 2018-08-15 14:05:04 +0100 | [diff] [blame] | 5378 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 5379 | int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl, |
Hanno Becker | 3a0aad1 | 2018-08-20 09:44:02 +0100 | [diff] [blame] | 5380 | unsigned update_hs_digest ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5381 | { |
| 5382 | int ret; |
| 5383 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5384 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5385 | |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5386 | if( ssl->keep_current_message == 0 ) |
| 5387 | { |
| 5388 | do { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5389 | |
Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 5390 | ret = ssl_consume_current_message( ssl ); |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5391 | if( ret != 0 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5392 | return( ret ); |
Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 5393 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5394 | if( ssl_record_is_in_progress( ssl ) == 0 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5395 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5396 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5397 | int have_buffered = 0; |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5398 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5399 | /* We only check for buffered messages if the |
| 5400 | * current datagram is fully consumed. */ |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 5401 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 5402 | ssl_next_record_is_in_datagram( ssl ) == 0 ) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5403 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5404 | if( ssl_load_buffered_message( ssl ) == 0 ) |
| 5405 | have_buffered = 1; |
| 5406 | } |
| 5407 | |
| 5408 | if( have_buffered == 0 ) |
| 5409 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5410 | { |
| 5411 | ret = ssl_get_next_record( ssl ); |
| 5412 | if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ) |
| 5413 | continue; |
| 5414 | |
| 5415 | if( ret != 0 ) |
| 5416 | { |
Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 5417 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret ); |
Hanno Becker | 42a6b04 | 2019-07-26 07:25:20 +0100 | [diff] [blame] | 5418 | ssl_send_pending_fatal_alert( ssl ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5419 | return( ret ); |
| 5420 | } |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5421 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5422 | } |
| 5423 | |
| 5424 | ret = mbedtls_ssl_handle_message_type( ssl ); |
| 5425 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5426 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5427 | if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 5428 | { |
| 5429 | /* Buffer future message */ |
| 5430 | ret = ssl_buffer_message( ssl ); |
| 5431 | if( ret != 0 ) |
| 5432 | return( ret ); |
| 5433 | |
| 5434 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
| 5435 | } |
| 5436 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5437 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5438 | } while( MBEDTLS_ERR_SSL_NON_FATAL == ret || |
| 5439 | MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5440 | |
| 5441 | if( 0 != ret ) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5442 | { |
Hanno Becker | 05c4fc8 | 2017-11-09 14:34:06 +0000 | [diff] [blame] | 5443 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5444 | return( ret ); |
| 5445 | } |
| 5446 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 5447 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
Hanno Becker | 3a0aad1 | 2018-08-20 09:44:02 +0100 | [diff] [blame] | 5448 | update_hs_digest == 1 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5449 | { |
| 5450 | mbedtls_ssl_update_handshake_status( ssl ); |
| 5451 | } |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5452 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5453 | else |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5454 | { |
Hanno Becker | 02f5907 | 2018-08-15 14:00:24 +0100 | [diff] [blame] | 5455 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5456 | ssl->keep_current_message = 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5457 | } |
| 5458 | |
| 5459 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) ); |
| 5460 | |
| 5461 | return( 0 ); |
| 5462 | } |
| 5463 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5464 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 5465 | static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl ) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5466 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5467 | if( ssl->in_left > ssl->next_record_offset ) |
| 5468 | return( 1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5469 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5470 | return( 0 ); |
| 5471 | } |
| 5472 | |
| 5473 | static int ssl_load_buffered_message( mbedtls_ssl_context *ssl ) |
| 5474 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5475 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5476 | mbedtls_ssl_hs_buffer * hs_buf; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5477 | int ret = 0; |
| 5478 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5479 | if( hs == NULL ) |
| 5480 | return( -1 ); |
| 5481 | |
Hanno Becker | e00ae37 | 2018-08-20 09:39:42 +0100 | [diff] [blame] | 5482 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) ); |
| 5483 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5484 | if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC || |
| 5485 | ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) |
| 5486 | { |
| 5487 | /* Check if we have seen a ChangeCipherSpec before. |
| 5488 | * If yes, synthesize a CCS record. */ |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5489 | if( !hs->buffering.seen_ccs ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5490 | { |
| 5491 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) ); |
| 5492 | ret = -1; |
Hanno Becker | 0d4b376 | 2018-08-20 09:36:59 +0100 | [diff] [blame] | 5493 | goto exit; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5494 | } |
| 5495 | |
Hanno Becker | 39b8bc9 | 2018-08-28 17:17:13 +0100 | [diff] [blame] | 5496 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) ); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5497 | ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
| 5498 | ssl->in_msglen = 1; |
| 5499 | ssl->in_msg[0] = 1; |
| 5500 | |
| 5501 | /* As long as they are equal, the exact value doesn't matter. */ |
| 5502 | ssl->in_left = 0; |
| 5503 | ssl->next_record_offset = 0; |
| 5504 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 5505 | hs->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5506 | goto exit; |
| 5507 | } |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5508 | |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 5509 | #if defined(MBEDTLS_DEBUG_C) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5510 | /* Debug only */ |
| 5511 | { |
| 5512 | unsigned offset; |
| 5513 | for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ ) |
| 5514 | { |
| 5515 | hs_buf = &hs->buffering.hs[offset]; |
| 5516 | if( hs_buf->is_valid == 1 ) |
| 5517 | { |
| 5518 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.", |
| 5519 | hs->in_msg_seq + offset, |
Hanno Becker | a591c48 | 2018-08-28 17:20:00 +0100 | [diff] [blame] | 5520 | hs_buf->is_complete ? "fully" : "partially" ) ); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5521 | } |
| 5522 | } |
| 5523 | } |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 5524 | #endif /* MBEDTLS_DEBUG_C */ |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5525 | |
| 5526 | /* Check if we have buffered and/or fully reassembled the |
| 5527 | * next handshake message. */ |
| 5528 | hs_buf = &hs->buffering.hs[0]; |
| 5529 | if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) ) |
| 5530 | { |
| 5531 | /* Synthesize a record containing the buffered HS message. */ |
| 5532 | size_t msg_len = ( hs_buf->data[1] << 16 ) | |
| 5533 | ( hs_buf->data[2] << 8 ) | |
| 5534 | hs_buf->data[3]; |
| 5535 | |
| 5536 | /* Double-check that we haven't accidentally buffered |
| 5537 | * a message that doesn't fit into the input buffer. */ |
| 5538 | if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN ) |
| 5539 | { |
| 5540 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5541 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5542 | } |
| 5543 | |
| 5544 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) ); |
| 5545 | MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)", |
| 5546 | hs_buf->data, msg_len + 12 ); |
| 5547 | |
| 5548 | ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 5549 | ssl->in_hslen = msg_len + 12; |
| 5550 | ssl->in_msglen = msg_len + 12; |
| 5551 | memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen ); |
| 5552 | |
| 5553 | ret = 0; |
| 5554 | goto exit; |
| 5555 | } |
| 5556 | else |
| 5557 | { |
| 5558 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered", |
| 5559 | hs->in_msg_seq ) ); |
| 5560 | } |
| 5561 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5562 | ret = -1; |
| 5563 | |
| 5564 | exit: |
| 5565 | |
| 5566 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) ); |
| 5567 | return( ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5568 | } |
| 5569 | |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5570 | static int ssl_buffer_make_space( mbedtls_ssl_context *ssl, |
| 5571 | size_t desired ) |
| 5572 | { |
| 5573 | int offset; |
| 5574 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5575 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available", |
| 5576 | (unsigned) desired ) ); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5577 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5578 | /* Get rid of future records epoch first, if such exist. */ |
| 5579 | ssl_free_buffered_record( ssl ); |
| 5580 | |
| 5581 | /* Check if we have enough space available now. */ |
| 5582 | if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5583 | hs->buffering.total_bytes_buffered ) ) |
| 5584 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5585 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) ); |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5586 | return( 0 ); |
| 5587 | } |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5588 | |
Hanno Becker | 4f432ad | 2018-08-28 10:02:32 +0100 | [diff] [blame] | 5589 | /* We don't have enough space to buffer the next expected handshake |
| 5590 | * message. Remove buffers used for future messages to gain space, |
| 5591 | * starting with the most distant one. */ |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5592 | for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1; |
| 5593 | offset >= 0; offset-- ) |
| 5594 | { |
| 5595 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message", |
| 5596 | offset ) ); |
| 5597 | |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 5598 | ssl_buffering_free_slot( ssl, (uint8_t) offset ); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5599 | |
| 5600 | /* Check if we have enough space available now. */ |
| 5601 | if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5602 | hs->buffering.total_bytes_buffered ) ) |
| 5603 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5604 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) ); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5605 | return( 0 ); |
| 5606 | } |
| 5607 | } |
| 5608 | |
| 5609 | return( -1 ); |
| 5610 | } |
| 5611 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5612 | static int ssl_buffer_message( mbedtls_ssl_context *ssl ) |
| 5613 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5614 | int ret = 0; |
| 5615 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5616 | |
| 5617 | if( hs == NULL ) |
| 5618 | return( 0 ); |
| 5619 | |
| 5620 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) ); |
| 5621 | |
| 5622 | switch( ssl->in_msgtype ) |
| 5623 | { |
| 5624 | case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: |
| 5625 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) ); |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5626 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 5627 | hs->buffering.seen_ccs = 1; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5628 | break; |
| 5629 | |
| 5630 | case MBEDTLS_SSL_MSG_HANDSHAKE: |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5631 | { |
| 5632 | unsigned recv_msg_seq_offset; |
| 5633 | unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5]; |
| 5634 | mbedtls_ssl_hs_buffer *hs_buf; |
| 5635 | size_t msg_len = ssl->in_hslen - 12; |
| 5636 | |
| 5637 | /* We should never receive an old handshake |
| 5638 | * message - double-check nonetheless. */ |
| 5639 | if( recv_msg_seq < ssl->handshake->in_msg_seq ) |
| 5640 | { |
| 5641 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5642 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5643 | } |
| 5644 | |
| 5645 | recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq; |
| 5646 | if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS ) |
| 5647 | { |
| 5648 | /* Silently ignore -- message too far in the future */ |
| 5649 | MBEDTLS_SSL_DEBUG_MSG( 2, |
| 5650 | ( "Ignore future HS message with sequence number %u, " |
| 5651 | "buffering window %u - %u", |
| 5652 | recv_msg_seq, ssl->handshake->in_msg_seq, |
| 5653 | ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) ); |
| 5654 | |
| 5655 | goto exit; |
| 5656 | } |
| 5657 | |
| 5658 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ", |
| 5659 | recv_msg_seq, recv_msg_seq_offset ) ); |
| 5660 | |
| 5661 | hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ]; |
| 5662 | |
| 5663 | /* Check if the buffering for this seq nr has already commenced. */ |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5664 | if( !hs_buf->is_valid ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5665 | { |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 5666 | size_t reassembly_buf_sz; |
| 5667 | |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5668 | hs_buf->is_fragmented = |
| 5669 | ( ssl_hs_is_proper_fragment( ssl ) == 1 ); |
| 5670 | |
| 5671 | /* We copy the message back into the input buffer |
| 5672 | * after reassembly, so check that it's not too large. |
| 5673 | * This is an implementation-specific limitation |
| 5674 | * and not one from the standard, hence it is not |
| 5675 | * checked in ssl_check_hs_header(). */ |
Hanno Becker | 96a6c69 | 2018-08-21 15:56:03 +0100 | [diff] [blame] | 5676 | if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5677 | { |
| 5678 | /* Ignore message */ |
| 5679 | goto exit; |
| 5680 | } |
| 5681 | |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5682 | /* Check if we have enough space to buffer the message. */ |
| 5683 | if( hs->buffering.total_bytes_buffered > |
| 5684 | MBEDTLS_SSL_DTLS_MAX_BUFFERING ) |
| 5685 | { |
| 5686 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5687 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5688 | } |
| 5689 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 5690 | reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len, |
| 5691 | hs_buf->is_fragmented ); |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5692 | |
| 5693 | if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5694 | hs->buffering.total_bytes_buffered ) ) |
| 5695 | { |
| 5696 | if( recv_msg_seq_offset > 0 ) |
| 5697 | { |
| 5698 | /* If we can't buffer a future message because |
| 5699 | * of space limitations -- ignore. */ |
| 5700 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n", |
| 5701 | (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 5702 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
| 5703 | goto exit; |
| 5704 | } |
Hanno Becker | e180139 | 2018-08-21 16:51:05 +0100 | [diff] [blame] | 5705 | else |
| 5706 | { |
| 5707 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- attempt to make space by freeing buffered future messages\n", |
| 5708 | (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 5709 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
| 5710 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5711 | |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5712 | if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 ) |
Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 5713 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5714 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %u (%u with bitmap) would exceed the compile-time limit %u (already %u bytes buffered) -- fail\n", |
| 5715 | (unsigned) msg_len, |
| 5716 | (unsigned) reassembly_buf_sz, |
| 5717 | MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5718 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 5719 | ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 5720 | goto exit; |
| 5721 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5722 | } |
| 5723 | |
| 5724 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d", |
| 5725 | msg_len ) ); |
| 5726 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 5727 | hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz ); |
| 5728 | if( hs_buf->data == NULL ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5729 | { |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5730 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5731 | goto exit; |
| 5732 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5733 | hs_buf->data_len = reassembly_buf_sz; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5734 | |
| 5735 | /* Prepare final header: copy msg_type, length and message_seq, |
| 5736 | * then add standardised fragment_offset and fragment_length */ |
| 5737 | memcpy( hs_buf->data, ssl->in_msg, 6 ); |
| 5738 | memset( hs_buf->data + 6, 0, 3 ); |
| 5739 | memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 ); |
| 5740 | |
| 5741 | hs_buf->is_valid = 1; |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5742 | |
| 5743 | hs->buffering.total_bytes_buffered += reassembly_buf_sz; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5744 | } |
| 5745 | else |
| 5746 | { |
| 5747 | /* Make sure msg_type and length are consistent */ |
| 5748 | if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 ) |
| 5749 | { |
| 5750 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) ); |
| 5751 | /* Ignore */ |
| 5752 | goto exit; |
| 5753 | } |
| 5754 | } |
| 5755 | |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5756 | if( !hs_buf->is_complete ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5757 | { |
| 5758 | size_t frag_len, frag_off; |
| 5759 | unsigned char * const msg = hs_buf->data + 12; |
| 5760 | |
| 5761 | /* |
| 5762 | * Check and copy current fragment |
| 5763 | */ |
| 5764 | |
| 5765 | /* Validation of header fields already done in |
| 5766 | * mbedtls_ssl_prepare_handshake_record(). */ |
| 5767 | frag_off = ssl_get_hs_frag_off( ssl ); |
| 5768 | frag_len = ssl_get_hs_frag_len( ssl ); |
| 5769 | |
| 5770 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d", |
| 5771 | frag_off, frag_len ) ); |
| 5772 | memcpy( msg + frag_off, ssl->in_msg + 12, frag_len ); |
| 5773 | |
| 5774 | if( hs_buf->is_fragmented ) |
| 5775 | { |
| 5776 | unsigned char * const bitmask = msg + msg_len; |
| 5777 | ssl_bitmask_set( bitmask, frag_off, frag_len ); |
| 5778 | hs_buf->is_complete = ( ssl_bitmask_check( bitmask, |
| 5779 | msg_len ) == 0 ); |
| 5780 | } |
| 5781 | else |
| 5782 | { |
| 5783 | hs_buf->is_complete = 1; |
| 5784 | } |
| 5785 | |
| 5786 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete", |
| 5787 | hs_buf->is_complete ? "" : "not yet " ) ); |
| 5788 | } |
| 5789 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5790 | break; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5791 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5792 | |
| 5793 | default: |
Hanno Becker | 360bef3 | 2018-08-28 10:04:33 +0100 | [diff] [blame] | 5794 | /* We don't buffer other types of messages. */ |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5795 | break; |
| 5796 | } |
| 5797 | |
| 5798 | exit: |
| 5799 | |
| 5800 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) ); |
| 5801 | return( ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5802 | } |
| 5803 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5804 | |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5805 | static int ssl_consume_current_message( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5806 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5807 | /* |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5808 | * Consume last content-layer message and potentially |
| 5809 | * update in_msglen which keeps track of the contents' |
| 5810 | * consumption state. |
| 5811 | * |
| 5812 | * (1) Handshake messages: |
| 5813 | * Remove last handshake message, move content |
| 5814 | * and adapt in_msglen. |
| 5815 | * |
| 5816 | * (2) Alert messages: |
| 5817 | * Consume whole record content, in_msglen = 0. |
| 5818 | * |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5819 | * (3) Change cipher spec: |
| 5820 | * Consume whole record content, in_msglen = 0. |
| 5821 | * |
| 5822 | * (4) Application data: |
| 5823 | * Don't do anything - the record layer provides |
| 5824 | * the application data as a stream transport |
| 5825 | * and consumes through mbedtls_ssl_read only. |
| 5826 | * |
| 5827 | */ |
| 5828 | |
| 5829 | /* Case (1): Handshake messages */ |
| 5830 | if( ssl->in_hslen != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5831 | { |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 5832 | /* Hard assertion to be sure that no application data |
| 5833 | * is in flight, as corrupting ssl->in_msglen during |
| 5834 | * ssl->in_offt != NULL is fatal. */ |
| 5835 | if( ssl->in_offt != NULL ) |
| 5836 | { |
| 5837 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5838 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5839 | } |
| 5840 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5841 | /* |
| 5842 | * Get next Handshake message in the current record |
| 5843 | */ |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5844 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5845 | /* Notes: |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 5846 | * (1) in_hslen is not necessarily the size of the |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5847 | * current handshake content: If DTLS handshake |
| 5848 | * fragmentation is used, that's the fragment |
| 5849 | * size instead. Using the total handshake message |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 5850 | * size here is faulty and should be changed at |
| 5851 | * some point. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5852 | * (2) While it doesn't seem to cause problems, one |
| 5853 | * has to be very careful not to assume that in_hslen |
| 5854 | * is always <= in_msglen in a sensible communication. |
| 5855 | * Again, it's wrong for DTLS handshake fragmentation. |
| 5856 | * The following check is therefore mandatory, and |
| 5857 | * should not be treated as a silently corrected assertion. |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 5858 | * Additionally, ssl->in_hslen might be arbitrarily out of |
| 5859 | * bounds after handling a DTLS message with an unexpected |
| 5860 | * sequence number, see mbedtls_ssl_prepare_handshake_record. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5861 | */ |
| 5862 | if( ssl->in_hslen < ssl->in_msglen ) |
| 5863 | { |
| 5864 | ssl->in_msglen -= ssl->in_hslen; |
| 5865 | memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen, |
| 5866 | ssl->in_msglen ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5867 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5868 | MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record", |
| 5869 | ssl->in_msg, ssl->in_msglen ); |
| 5870 | } |
| 5871 | else |
| 5872 | { |
| 5873 | ssl->in_msglen = 0; |
| 5874 | } |
Manuel Pégourié-Gonnard | 4a17536 | 2014-09-09 17:45:31 +0200 | [diff] [blame] | 5875 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5876 | ssl->in_hslen = 0; |
| 5877 | } |
| 5878 | /* Case (4): Application data */ |
| 5879 | else if( ssl->in_offt != NULL ) |
| 5880 | { |
| 5881 | return( 0 ); |
| 5882 | } |
| 5883 | /* Everything else (CCS & Alerts) */ |
| 5884 | else |
| 5885 | { |
| 5886 | ssl->in_msglen = 0; |
| 5887 | } |
| 5888 | |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5889 | return( 0 ); |
| 5890 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5891 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5892 | static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl ) |
| 5893 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5894 | if( ssl->in_msglen > 0 ) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5895 | return( 1 ); |
| 5896 | |
| 5897 | return( 0 ); |
| 5898 | } |
| 5899 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5900 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5901 | |
| 5902 | static void ssl_free_buffered_record( mbedtls_ssl_context *ssl ) |
| 5903 | { |
| 5904 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5905 | if( hs == NULL ) |
| 5906 | return; |
| 5907 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5908 | if( hs->buffering.future_record.data != NULL ) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5909 | { |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5910 | hs->buffering.total_bytes_buffered -= |
| 5911 | hs->buffering.future_record.len; |
| 5912 | |
| 5913 | mbedtls_free( hs->buffering.future_record.data ); |
| 5914 | hs->buffering.future_record.data = NULL; |
| 5915 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5916 | } |
| 5917 | |
| 5918 | static int ssl_load_buffered_record( mbedtls_ssl_context *ssl ) |
| 5919 | { |
| 5920 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5921 | unsigned char * rec; |
| 5922 | size_t rec_len; |
| 5923 | unsigned rec_epoch; |
| 5924 | |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 5925 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5926 | return( 0 ); |
| 5927 | |
| 5928 | if( hs == NULL ) |
| 5929 | return( 0 ); |
| 5930 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5931 | rec = hs->buffering.future_record.data; |
| 5932 | rec_len = hs->buffering.future_record.len; |
| 5933 | rec_epoch = hs->buffering.future_record.epoch; |
| 5934 | |
| 5935 | if( rec == NULL ) |
| 5936 | return( 0 ); |
| 5937 | |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 5938 | /* Only consider loading future records if the |
| 5939 | * input buffer is empty. */ |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 5940 | if( ssl_next_record_is_in_datagram( ssl ) == 1 ) |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 5941 | return( 0 ); |
| 5942 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5943 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) ); |
| 5944 | |
| 5945 | if( rec_epoch != ssl->in_epoch ) |
| 5946 | { |
| 5947 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) ); |
| 5948 | goto exit; |
| 5949 | } |
| 5950 | |
| 5951 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) ); |
| 5952 | |
| 5953 | /* Double-check that the record is not too large */ |
| 5954 | if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN - |
| 5955 | (size_t)( ssl->in_hdr - ssl->in_buf ) ) |
| 5956 | { |
| 5957 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5958 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5959 | } |
| 5960 | |
| 5961 | memcpy( ssl->in_hdr, rec, rec_len ); |
| 5962 | ssl->in_left = rec_len; |
| 5963 | ssl->next_record_offset = 0; |
| 5964 | |
| 5965 | ssl_free_buffered_record( ssl ); |
| 5966 | |
| 5967 | exit: |
| 5968 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) ); |
| 5969 | return( 0 ); |
| 5970 | } |
| 5971 | |
Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 5972 | static int ssl_buffer_future_record( mbedtls_ssl_context *ssl, |
| 5973 | mbedtls_record const *rec ) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5974 | { |
| 5975 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5976 | |
| 5977 | /* Don't buffer future records outside handshakes. */ |
| 5978 | if( hs == NULL ) |
| 5979 | return( 0 ); |
| 5980 | |
| 5981 | /* Only buffer handshake records (we are only interested |
| 5982 | * in Finished messages). */ |
Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 5983 | if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5984 | return( 0 ); |
| 5985 | |
| 5986 | /* Don't buffer more than one future epoch record. */ |
| 5987 | if( hs->buffering.future_record.data != NULL ) |
| 5988 | return( 0 ); |
| 5989 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5990 | /* Don't buffer record if there's not enough buffering space remaining. */ |
Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 5991 | if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5992 | hs->buffering.total_bytes_buffered ) ) |
| 5993 | { |
| 5994 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n", |
Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 5995 | (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5996 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5997 | return( 0 ); |
| 5998 | } |
| 5999 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6000 | /* Buffer record */ |
| 6001 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u", |
| 6002 | ssl->in_epoch + 1 ) ); |
Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6003 | MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6004 | |
| 6005 | /* ssl_parse_record_header() only considers records |
| 6006 | * of the next epoch as candidates for buffering. */ |
| 6007 | hs->buffering.future_record.epoch = ssl->in_epoch + 1; |
Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6008 | hs->buffering.future_record.len = rec->buf_len; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6009 | |
| 6010 | hs->buffering.future_record.data = |
| 6011 | mbedtls_calloc( 1, hs->buffering.future_record.len ); |
| 6012 | if( hs->buffering.future_record.data == NULL ) |
| 6013 | { |
| 6014 | /* If we run out of RAM trying to buffer a |
| 6015 | * record from the next epoch, just ignore. */ |
| 6016 | return( 0 ); |
| 6017 | } |
| 6018 | |
Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6019 | memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6020 | |
Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6021 | hs->buffering.total_bytes_buffered += rec->buf_len; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6022 | return( 0 ); |
| 6023 | } |
| 6024 | |
| 6025 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6026 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 6027 | static int ssl_get_next_record( mbedtls_ssl_context *ssl ) |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 6028 | { |
| 6029 | int ret; |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 6030 | mbedtls_record rec; |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 6031 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6032 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6033 | /* We might have buffered a future record; if so, |
| 6034 | * and if the epoch matches now, load it. |
| 6035 | * On success, this call will set ssl->in_left to |
| 6036 | * the length of the buffered record, so that |
| 6037 | * the calls to ssl_fetch_input() below will |
| 6038 | * essentially be no-ops. */ |
| 6039 | ret = ssl_load_buffered_record( ssl ); |
| 6040 | if( ret != 0 ) |
| 6041 | return( ret ); |
| 6042 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6043 | |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 6044 | /* Ensure that we have enough space available for the default form |
| 6045 | * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS, |
| 6046 | * with no space for CIDs counted in). */ |
| 6047 | ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) ); |
| 6048 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6049 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6050 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6051 | return( ret ); |
| 6052 | } |
| 6053 | |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 6054 | ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec ); |
| 6055 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6056 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6057 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6058 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6059 | { |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6060 | if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 6061 | { |
Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6062 | ret = ssl_buffer_future_record( ssl, &rec ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6063 | if( ret != 0 ) |
| 6064 | return( ret ); |
| 6065 | |
| 6066 | /* Fall through to handling of unexpected records */ |
| 6067 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
| 6068 | } |
| 6069 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 6070 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ) |
| 6071 | { |
Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6072 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 6837972 | 2019-07-12 09:23:47 +0100 | [diff] [blame] | 6073 | /* Reset in pointers to default state for TLS/DTLS records, |
| 6074 | * assuming no CID and no offset between record content and |
| 6075 | * record plaintext. */ |
| 6076 | ssl_update_in_pointers( ssl ); |
| 6077 | |
Hanno Becker | 6941245 | 2019-07-12 08:33:49 +0100 | [diff] [blame] | 6078 | /* Setup internal message pointers from record structure. */ |
| 6079 | ssl->in_msgtype = rec.type; |
| 6080 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6081 | ssl->in_len = ssl->in_cid + rec.cid_len; |
| 6082 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | c360dcc | 2019-07-12 10:00:45 +0100 | [diff] [blame] | 6083 | ssl->in_msg = ssl->in_len + 2; |
Hanno Becker | 6941245 | 2019-07-12 08:33:49 +0100 | [diff] [blame] | 6084 | ssl->in_msglen = rec.data_len; |
| 6085 | |
Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6086 | ret = ssl_check_client_reconnect( ssl ); |
| 6087 | if( ret != 0 ) |
| 6088 | return( ret ); |
| 6089 | #endif |
| 6090 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 6091 | /* Skip unexpected record (but not whole datagram) */ |
Hanno Becker | 2528ee0 | 2019-07-11 12:48:53 +0100 | [diff] [blame] | 6092 | ssl->next_record_offset = rec.buf_len; |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6093 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 6094 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record " |
| 6095 | "(header)" ) ); |
| 6096 | } |
| 6097 | else |
| 6098 | { |
| 6099 | /* Skip invalid record and the rest of the datagram */ |
| 6100 | ssl->next_record_offset = 0; |
| 6101 | ssl->in_left = 0; |
| 6102 | |
| 6103 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record " |
| 6104 | "(header)" ) ); |
| 6105 | } |
| 6106 | |
| 6107 | /* Get next record */ |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 6108 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6109 | } |
Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6110 | else |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6111 | #endif |
Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6112 | { |
| 6113 | return( ret ); |
| 6114 | } |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6115 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6116 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6117 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6118 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 6119 | { |
Hanno Becker | dc4d627 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 6120 | /* Remember offset of next record within datagram. */ |
Hanno Becker | 2720f4c | 2019-07-11 12:50:10 +0100 | [diff] [blame] | 6121 | ssl->next_record_offset = rec.buf_len; |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 6122 | if( ssl->next_record_offset < ssl->in_left ) |
| 6123 | { |
| 6124 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) ); |
| 6125 | } |
| 6126 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6127 | MBEDTLS_SSL_TRANSPORT_ELSE |
Hanno Becker | dc4d627 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 6128 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6129 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 6130 | { |
Hanno Becker | e045277 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 6131 | /* |
| 6132 | * Fetch record contents from underlying transport. |
| 6133 | */ |
Hanno Becker | 9babbf7 | 2019-07-11 12:50:29 +0100 | [diff] [blame] | 6134 | ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len ); |
Hanno Becker | dc4d627 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 6135 | if( ret != 0 ) |
| 6136 | { |
| 6137 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
| 6138 | return( ret ); |
| 6139 | } |
| 6140 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6141 | ssl->in_left = 0; |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6142 | } |
Hanno Becker | dc4d627 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 6143 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
| 6144 | |
| 6145 | /* |
| 6146 | * Decrypt record contents. |
| 6147 | */ |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6148 | |
Hanno Becker | a89610a | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 6149 | if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6150 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6151 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6152 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6153 | { |
| 6154 | /* Silently discard invalid records */ |
Hanno Becker | 16e9ae2 | 2019-05-03 16:36:59 +0100 | [diff] [blame] | 6155 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6156 | { |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 6157 | /* Except when waiting for Finished as a bad mac here |
| 6158 | * probably means something went wrong in the handshake |
| 6159 | * (eg wrong psk used, mitm downgrade attempt, etc.) */ |
| 6160 | if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED || |
| 6161 | ssl->state == MBEDTLS_SSL_SERVER_FINISHED ) |
| 6162 | { |
| 6163 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
| 6164 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
| 6165 | { |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 6166 | mbedtls_ssl_pend_fatal_alert( ssl, |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 6167 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); |
| 6168 | } |
| 6169 | #endif |
| 6170 | return( ret ); |
| 6171 | } |
| 6172 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6173 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
Hanno Becker | de67154 | 2019-06-12 16:30:46 +0100 | [diff] [blame] | 6174 | if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 && |
| 6175 | ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) ) |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 6176 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6177 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) ); |
| 6178 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 6179 | } |
| 6180 | #endif |
| 6181 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6182 | /* As above, invalid records cause |
| 6183 | * dismissal of the whole datagram. */ |
| 6184 | |
| 6185 | ssl->next_record_offset = 0; |
| 6186 | ssl->in_left = 0; |
| 6187 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6188 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) ); |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 6189 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6190 | } |
| 6191 | |
| 6192 | return( ret ); |
| 6193 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6194 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 6195 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6196 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6197 | { |
| 6198 | /* Error out (and send alert) on invalid records */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6199 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
| 6200 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6201 | { |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 6202 | mbedtls_ssl_pend_fatal_alert( ssl, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6203 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6204 | } |
| 6205 | #endif |
| 6206 | return( ret ); |
| 6207 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6208 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6209 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6210 | |
Hanno Becker | bd70c8e | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 6211 | |
| 6212 | /* Reset in pointers to default state for TLS/DTLS records, |
| 6213 | * assuming no CID and no offset between record content and |
| 6214 | * record plaintext. */ |
| 6215 | ssl_update_in_pointers( ssl ); |
Hanno Becker | bd70c8e | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 6216 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6217 | ssl->in_len = ssl->in_cid + rec.cid_len; |
| 6218 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | c360dcc | 2019-07-12 10:00:45 +0100 | [diff] [blame] | 6219 | ssl->in_msg = ssl->in_len + 2; |
Hanno Becker | bd70c8e | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 6220 | |
Hanno Becker | bf256cd | 2019-07-12 09:37:30 +0100 | [diff] [blame] | 6221 | /* The record content type may change during decryption, |
| 6222 | * so re-read it. */ |
| 6223 | ssl->in_msgtype = rec.type; |
| 6224 | /* Also update the input buffer, because unfortunately |
| 6225 | * the server-side ssl_parse_client_hello() reparses the |
| 6226 | * record header when receiving a ClientHello initiating |
| 6227 | * a renegotiation. */ |
| 6228 | ssl->in_hdr[0] = rec.type; |
| 6229 | ssl->in_msg = rec.buf + rec.data_offset; |
| 6230 | ssl->in_msglen = rec.data_len; |
| 6231 | ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 ); |
| 6232 | ssl->in_len[1] = (unsigned char)( rec.data_len ); |
| 6233 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6234 | return( 0 ); |
| 6235 | } |
| 6236 | |
| 6237 | int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl ) |
| 6238 | { |
| 6239 | int ret; |
| 6240 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6241 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6242 | * Handle particular types of records |
| 6243 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6244 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6245 | { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6246 | if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 ) |
| 6247 | { |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 6248 | return( ret ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6249 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6250 | } |
| 6251 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6252 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6253 | { |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6254 | if( ssl->in_msglen != 1 ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6255 | { |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6256 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d", |
| 6257 | ssl->in_msglen ) ); |
| 6258 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6259 | } |
| 6260 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6261 | if( ssl->in_msg[0] != 1 ) |
| 6262 | { |
| 6263 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x", |
| 6264 | ssl->in_msg[0] ) ); |
| 6265 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 6266 | } |
| 6267 | |
| 6268 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 6269 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6270 | ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC && |
| 6271 | ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) |
| 6272 | { |
| 6273 | if( ssl->handshake == NULL ) |
| 6274 | { |
| 6275 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) ); |
| 6276 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
| 6277 | } |
| 6278 | |
| 6279 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) ); |
| 6280 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 6281 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6282 | #endif |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6283 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6284 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6285 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6286 | { |
Angus Gratton | 1a7a17e | 2018-06-20 15:43:50 +1000 | [diff] [blame] | 6287 | if( ssl->in_msglen != 2 ) |
| 6288 | { |
| 6289 | /* Note: Standard allows for more than one 2 byte alert |
| 6290 | to be packed in a single message, but Mbed TLS doesn't |
| 6291 | currently support this. */ |
| 6292 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d", |
| 6293 | ssl->in_msglen ) ); |
| 6294 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 6295 | } |
| 6296 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6297 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6298 | ssl->in_msg[0], ssl->in_msg[1] ) ); |
| 6299 | |
| 6300 | /* |
Simon Butcher | 459a950 | 2015-10-27 16:09:03 +0000 | [diff] [blame] | 6301 | * Ignore non-fatal alerts, except close_notify and no_renegotiation |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6302 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6303 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6304 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6305 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 6306 | ssl->in_msg[1] ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6307 | return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6308 | } |
| 6309 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6310 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6311 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6312 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6313 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) ); |
| 6314 | return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6315 | } |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 6316 | |
| 6317 | #if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED) |
| 6318 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6319 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) |
| 6320 | { |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 6321 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) ); |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 6322 | /* Will be handled when trying to parse ServerHello */ |
| 6323 | return( 0 ); |
| 6324 | } |
| 6325 | #endif |
| 6326 | |
| 6327 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 6328 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 && |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 6329 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 6330 | MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 6331 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6332 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) |
| 6333 | { |
| 6334 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) ); |
| 6335 | /* Will be handled in mbedtls_ssl_parse_certificate() */ |
| 6336 | return( 0 ); |
| 6337 | } |
| 6338 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ |
| 6339 | |
| 6340 | /* Silently ignore: fetch new message */ |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6341 | return MBEDTLS_ERR_SSL_NON_FATAL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6342 | } |
| 6343 | |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6344 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 6345 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6346 | { |
Hanno Becker | 74dd3a7 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 6347 | /* Drop unexpected ApplicationData records, |
| 6348 | * except at the beginning of renegotiations */ |
| 6349 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA && |
| 6350 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER |
| 6351 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6352 | && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 6353 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6354 | #endif |
Hanno Becker | 74dd3a7 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 6355 | ) |
| 6356 | { |
| 6357 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) ); |
| 6358 | return( MBEDTLS_ERR_SSL_NON_FATAL ); |
| 6359 | } |
| 6360 | |
| 6361 | if( ssl->handshake != NULL && |
| 6362 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 6363 | { |
| 6364 | ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 6365 | } |
| 6366 | } |
Hanno Becker | f65ad82 | 2019-05-08 16:26:21 +0100 | [diff] [blame] | 6367 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6368 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6369 | return( 0 ); |
| 6370 | } |
| 6371 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6372 | int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 6373 | { |
| 6374 | int ret; |
| 6375 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6376 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 6377 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6378 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 6379 | { |
| 6380 | return( ret ); |
| 6381 | } |
| 6382 | |
| 6383 | return( 0 ); |
| 6384 | } |
| 6385 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6386 | int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl, |
Hanno Becker | 1facd55 | 2019-07-03 13:57:23 +0100 | [diff] [blame] | 6387 | unsigned char level, |
| 6388 | unsigned char message ) |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6389 | { |
| 6390 | int ret; |
| 6391 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 6392 | if( ssl == NULL || ssl->conf == NULL ) |
| 6393 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6394 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6395 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6396 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message )); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6397 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6398 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6399 | ssl->out_msglen = 2; |
| 6400 | ssl->out_msg[0] = level; |
| 6401 | ssl->out_msg[1] = message; |
| 6402 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 6403 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6404 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6405 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6406 | return( ret ); |
| 6407 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6408 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) ); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6409 | |
| 6410 | return( 0 ); |
| 6411 | } |
| 6412 | |
Hanno Becker | 1757247 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 6413 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6414 | static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) |
| 6415 | { |
| 6416 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6417 | if( session->peer_cert != NULL ) |
| 6418 | { |
| 6419 | mbedtls_x509_crt_free( session->peer_cert ); |
| 6420 | mbedtls_free( session->peer_cert ); |
| 6421 | session->peer_cert = NULL; |
| 6422 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 6423 | #elif defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 1757247 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 6424 | if( session->peer_cert_digest != NULL ) |
| 6425 | { |
| 6426 | /* Zeroization is not necessary. */ |
| 6427 | mbedtls_free( session->peer_cert_digest ); |
| 6428 | session->peer_cert_digest = NULL; |
| 6429 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 6430 | session->peer_cert_digest_len = 0; |
| 6431 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 6432 | #else |
| 6433 | ((void) session); |
| 6434 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 1757247 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 6435 | } |
| 6436 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 6437 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6438 | /* |
| 6439 | * Handshake functions |
| 6440 | */ |
Hanno Becker | b71e90a | 2019-02-05 13:20:55 +0000 | [diff] [blame] | 6441 | #if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6442 | /* No certificate support -> dummy functions */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6443 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6444 | { |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 6445 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
| 6446 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6447 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6448 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6449 | |
Hanno Becker | 5097cba | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 6450 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 6451 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6452 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 6453 | ssl->state++; |
| 6454 | return( 0 ); |
| 6455 | } |
| 6456 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6457 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6458 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6459 | } |
| 6460 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6461 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6462 | { |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 6463 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
| 6464 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6465 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6466 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6467 | |
Hanno Becker | 5097cba | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 6468 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6469 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6470 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6471 | ssl->state++; |
| 6472 | return( 0 ); |
| 6473 | } |
| 6474 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6475 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6476 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6477 | } |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6478 | |
Hanno Becker | b71e90a | 2019-02-05 13:20:55 +0000 | [diff] [blame] | 6479 | #else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6480 | /* Some certificate support -> implement write and parse */ |
| 6481 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6482 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6483 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6484 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6485 | size_t i, n; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6486 | const mbedtls_x509_crt *crt; |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 6487 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
| 6488 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6489 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6490 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6491 | |
Hanno Becker | 5097cba | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 6492 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6493 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6494 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6495 | ssl->state++; |
| 6496 | return( 0 ); |
| 6497 | } |
| 6498 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6499 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 6500 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 6501 | MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6502 | { |
| 6503 | if( ssl->client_auth == 0 ) |
| 6504 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6505 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6506 | ssl->state++; |
| 6507 | return( 0 ); |
| 6508 | } |
| 6509 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6510 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6511 | /* |
| 6512 | * If using SSLv3 and got no cert, send an Alert message |
| 6513 | * (otherwise an empty Certificate message will be sent). |
| 6514 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6515 | if( mbedtls_ssl_own_cert( ssl ) == NULL && |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 6516 | mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6517 | { |
| 6518 | ssl->out_msglen = 2; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6519 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
| 6520 | ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING; |
| 6521 | ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6522 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6523 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6524 | goto write_msg; |
| 6525 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6526 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6527 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6528 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 6529 | #if defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 6530 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6531 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6532 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6533 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6534 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) ); |
| 6535 | return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6536 | } |
| 6537 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 6538 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6539 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6540 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6541 | |
| 6542 | /* |
| 6543 | * 0 . 0 handshake type |
| 6544 | * 1 . 3 handshake length |
| 6545 | * 4 . 6 length of all certs |
| 6546 | * 7 . 9 length of cert. 1 |
| 6547 | * 10 . n-1 peer certificate |
| 6548 | * n . n+2 length of cert. 2 |
| 6549 | * n+3 . ... upper level cert, etc. |
| 6550 | */ |
| 6551 | i = 7; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6552 | crt = mbedtls_ssl_own_cert( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6553 | |
Paul Bakker | 2908713 | 2010-03-21 21:03:34 +0000 | [diff] [blame] | 6554 | while( crt != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6555 | { |
| 6556 | n = crt->raw.len; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6557 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6558 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6559 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d", |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6560 | i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6561 | return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6562 | } |
| 6563 | |
| 6564 | ssl->out_msg[i ] = (unsigned char)( n >> 16 ); |
| 6565 | ssl->out_msg[i + 1] = (unsigned char)( n >> 8 ); |
| 6566 | ssl->out_msg[i + 2] = (unsigned char)( n ); |
| 6567 | |
| 6568 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 6569 | i += n; crt = crt->next; |
| 6570 | } |
| 6571 | |
| 6572 | ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 ); |
| 6573 | ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 ); |
| 6574 | ssl->out_msg[6] = (unsigned char)( ( i - 7 ) ); |
| 6575 | |
| 6576 | ssl->out_msglen = i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6577 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6578 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6579 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 6580 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6581 | write_msg: |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6582 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6583 | |
| 6584 | ssl->state++; |
| 6585 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 6586 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6587 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 6588 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6589 | return( ret ); |
| 6590 | } |
| 6591 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6592 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6593 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 6594 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6595 | } |
| 6596 | |
Hanno Becker | 285ff0c | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 6597 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | df75938 | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 6598 | |
| 6599 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6600 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6601 | unsigned char *crt_buf, |
| 6602 | size_t crt_buf_len ) |
| 6603 | { |
| 6604 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 6605 | |
| 6606 | if( peer_crt == NULL ) |
| 6607 | return( -1 ); |
| 6608 | |
| 6609 | if( peer_crt->raw.len != crt_buf_len ) |
| 6610 | return( -1 ); |
| 6611 | |
Hanno Becker | 68b856d | 2019-02-08 14:00:04 +0000 | [diff] [blame] | 6612 | return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) ); |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6613 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 6614 | #elif defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | df75938 | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 6615 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6616 | unsigned char *crt_buf, |
| 6617 | size_t crt_buf_len ) |
| 6618 | { |
| 6619 | int ret; |
| 6620 | unsigned char const * const peer_cert_digest = |
| 6621 | ssl->session->peer_cert_digest; |
| 6622 | mbedtls_md_type_t const peer_cert_digest_type = |
| 6623 | ssl->session->peer_cert_digest_type; |
| 6624 | mbedtls_md_info_t const * const digest_info = |
| 6625 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 6626 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 6627 | size_t digest_len; |
| 6628 | |
| 6629 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 6630 | return( -1 ); |
| 6631 | |
| 6632 | digest_len = mbedtls_md_get_size( digest_info ); |
| 6633 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 6634 | return( -1 ); |
| 6635 | |
| 6636 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 6637 | if( ret != 0 ) |
| 6638 | return( -1 ); |
| 6639 | |
| 6640 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 6641 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 6642 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 285ff0c | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 6643 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6644 | |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6645 | /* |
| 6646 | * Once the certificate message is read, parse it into a cert chain and |
| 6647 | * perform basic checks, but leave actual verification to the caller |
| 6648 | */ |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6649 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 6650 | mbedtls_x509_crt *chain ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6651 | { |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6652 | int ret; |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6653 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6654 | int crt_cnt=0; |
| 6655 | #endif |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 6656 | size_t i, n; |
Gilles Peskine | 064a85c | 2017-05-10 10:46:40 +0200 | [diff] [blame] | 6657 | uint8_t alert; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6658 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6659 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6660 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6661 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 6662 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 6663 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6664 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6665 | } |
| 6666 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6667 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE || |
| 6668 | ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6669 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6670 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 6671 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 6672 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6673 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6674 | } |
| 6675 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6676 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 6677 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6678 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6679 | * Same message structure as in mbedtls_ssl_write_certificate() |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6680 | */ |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 6681 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6682 | |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 6683 | if( ssl->in_msg[i] != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6684 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6685 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6686 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 6687 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 6688 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6689 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6690 | } |
| 6691 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6692 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 6693 | i += 3; |
| 6694 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6695 | /* Iterate through and parse the CRTs in the provided chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6696 | while( i < ssl->in_hslen ) |
| 6697 | { |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6698 | /* Check that there's room for the next CRT's length fields. */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 6699 | if ( i + 3 > ssl->in_hslen ) { |
| 6700 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 6701 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 6702 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 6703 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
| 6704 | } |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6705 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 6706 | * anything beyond 2**16 ~ 64K. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6707 | if( ssl->in_msg[i] != 0 ) |
| 6708 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6709 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 6710 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 6711 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6712 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6713 | } |
| 6714 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6715 | /* Read length of the next CRT in the chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6716 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 6717 | | (unsigned int) ssl->in_msg[i + 2]; |
| 6718 | i += 3; |
| 6719 | |
| 6720 | if( n < 128 || i + n > ssl->in_hslen ) |
| 6721 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6722 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 6723 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 6724 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6725 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6726 | } |
| 6727 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6728 | /* Check if we're handling the first CRT in the chain. */ |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6729 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6730 | if( crt_cnt++ == 0 && |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 6731 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 6732 | MBEDTLS_SSL_IS_CLIENT && |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6733 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6734 | { |
Hanno Becker | 68b856d | 2019-02-08 14:00:04 +0000 | [diff] [blame] | 6735 | /* During client-side renegotiation, check that the server's |
| 6736 | * end-CRTs hasn't changed compared to the initial handshake, |
| 6737 | * mitigating the triple handshake attack. On success, reuse |
| 6738 | * the original end-CRT instead of parsing it again. */ |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6739 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 6740 | if( ssl_check_peer_crt_unchanged( ssl, |
| 6741 | &ssl->in_msg[i], |
| 6742 | n ) != 0 ) |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6743 | { |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6744 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 6745 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 6746 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6747 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6748 | } |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6749 | |
| 6750 | /* Now we can safely free the original chain. */ |
| 6751 | ssl_clear_peer_cert( ssl->session ); |
| 6752 | } |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6753 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6754 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6755 | /* Parse the next certificate in the chain. */ |
Hanno Becker | 0cc7af5 | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 6756 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6757 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
Hanno Becker | 0cc7af5 | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 6758 | #else |
Hanno Becker | 42de8f8 | 2019-02-26 11:51:34 +0000 | [diff] [blame] | 6759 | /* If we don't need to store the CRT chain permanently, parse |
Hanno Becker | 0cc7af5 | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 6760 | * it in-place from the input buffer instead of making a copy. */ |
| 6761 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 6762 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6763 | switch( ret ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6764 | { |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6765 | case 0: /*ok*/ |
| 6766 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 6767 | /* Ignore certificate with an unknown algorithm: maybe a |
| 6768 | prior certificate was already trusted. */ |
| 6769 | break; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6770 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6771 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 6772 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 6773 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6774 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6775 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 6776 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6777 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6778 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6779 | default: |
| 6780 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6781 | crt_parse_der_failed: |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 6782 | mbedtls_ssl_pend_fatal_alert( ssl, alert ); |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6783 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 6784 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6785 | } |
| 6786 | |
| 6787 | i += n; |
| 6788 | } |
| 6789 | |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6790 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6791 | return( 0 ); |
| 6792 | } |
| 6793 | |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6794 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6795 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 6796 | { |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 6797 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT ) |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6798 | return( -1 ); |
| 6799 | |
| 6800 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 6801 | /* |
| 6802 | * Check if the client sent an empty certificate |
| 6803 | */ |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 6804 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6805 | { |
| 6806 | if( ssl->in_msglen == 2 && |
| 6807 | ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT && |
| 6808 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6809 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) |
| 6810 | { |
| 6811 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) ); |
| 6812 | return( 0 ); |
| 6813 | } |
| 6814 | |
| 6815 | return( -1 ); |
| 6816 | } |
| 6817 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 6818 | |
| 6819 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 6820 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6821 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 6822 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 6823 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 6824 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 6825 | { |
| 6826 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) ); |
| 6827 | return( 0 ); |
| 6828 | } |
| 6829 | |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6830 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 6831 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 17daaa5 | 2019-06-18 12:31:45 +0100 | [diff] [blame] | 6832 | |
| 6833 | return( -1 ); |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6834 | } |
| 6835 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6836 | |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6837 | /* Check if a certificate message is expected. |
| 6838 | * Return either |
| 6839 | * - SSL_CERTIFICATE_EXPECTED, or |
| 6840 | * - SSL_CERTIFICATE_SKIP |
| 6841 | * indicating whether a Certificate message is expected or not. |
| 6842 | */ |
| 6843 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 6844 | #define SSL_CERTIFICATE_SKIP 1 |
| 6845 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 6846 | int authmode ) |
| 6847 | { |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 6848 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 6849 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6850 | |
| 6851 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6852 | return( SSL_CERTIFICATE_SKIP ); |
| 6853 | |
| 6854 | #if defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 6855 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER ) |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6856 | { |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 6857 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == |
| 6858 | MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 6859 | { |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6860 | return( SSL_CERTIFICATE_SKIP ); |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 6861 | } |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6862 | |
| 6863 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6864 | { |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6865 | ssl->session_negotiate->verify_result = |
| 6866 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 6867 | return( SSL_CERTIFICATE_SKIP ); |
| 6868 | } |
| 6869 | } |
Hanno Becker | fd5dc8a | 2019-03-01 08:10:46 +0000 | [diff] [blame] | 6870 | #else |
| 6871 | ((void) authmode); |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6872 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6873 | |
| 6874 | return( SSL_CERTIFICATE_EXPECTED ); |
| 6875 | } |
| 6876 | |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6877 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 6878 | int authmode, |
| 6879 | mbedtls_x509_crt *chain, |
| 6880 | void *rs_ctx ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6881 | { |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6882 | int verify_ret; |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 6883 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 6884 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6885 | mbedtls_x509_crt *ca_chain; |
| 6886 | mbedtls_x509_crl *ca_crl; |
| 6887 | |
| 6888 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6889 | return( 0 ); |
| 6890 | |
| 6891 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6892 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 6893 | { |
| 6894 | ca_chain = ssl->handshake->sni_ca_chain; |
| 6895 | ca_crl = ssl->handshake->sni_ca_crl; |
| 6896 | } |
| 6897 | else |
| 6898 | #endif |
| 6899 | { |
| 6900 | ca_chain = ssl->conf->ca_chain; |
| 6901 | ca_crl = ssl->conf->ca_crl; |
| 6902 | } |
| 6903 | |
| 6904 | /* |
| 6905 | * Main check: verify certificate |
| 6906 | */ |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6907 | verify_ret = mbedtls_x509_crt_verify_restartable( |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6908 | chain, |
| 6909 | ca_chain, ca_crl, |
| 6910 | ssl->conf->cert_profile, |
| 6911 | ssl->hostname, |
| 6912 | &ssl->session_negotiate->verify_result, |
| 6913 | ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx ); |
| 6914 | |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6915 | if( verify_ret != 0 ) |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6916 | { |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6917 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret ); |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6918 | } |
| 6919 | |
| 6920 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6921 | if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6922 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 6923 | #endif |
| 6924 | |
| 6925 | /* |
| 6926 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 6927 | */ |
| 6928 | |
| 6929 | #if defined(MBEDTLS_ECP_C) |
| 6930 | { |
Manuel Pégourié-Gonnard | b391766 | 2019-07-03 11:19:30 +0200 | [diff] [blame] | 6931 | int ret; |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6932 | mbedtls_pk_context *pk; |
| 6933 | ret = mbedtls_x509_crt_pk_acquire( chain, &pk ); |
| 6934 | if( ret != 0 ) |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 6935 | { |
| 6936 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret ); |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6937 | return( ret ); |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 6938 | } |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6939 | |
| 6940 | /* If certificate uses an EC key, make sure the curve is OK */ |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6941 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) ) |
| 6942 | ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ); |
| 6943 | |
Hanno Becker | c6d1c3e | 2019-03-05 13:50:56 +0000 | [diff] [blame] | 6944 | mbedtls_x509_crt_pk_release( chain ); |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6945 | |
| 6946 | if( ret != 0 ) |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6947 | { |
| 6948 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; |
| 6949 | |
| 6950 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6951 | if( verify_ret == 0 ) |
| 6952 | verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6953 | } |
| 6954 | } |
| 6955 | #endif /* MBEDTLS_ECP_C */ |
| 6956 | |
| 6957 | if( mbedtls_ssl_check_cert_usage( chain, |
| 6958 | ciphersuite_info, |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 6959 | ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 6960 | MBEDTLS_SSL_IS_CLIENT ), |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6961 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 6962 | { |
| 6963 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6964 | if( verify_ret == 0 ) |
| 6965 | verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6966 | } |
| 6967 | |
| 6968 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 6969 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 6970 | * with details encoded in the verification flags. All other kinds |
| 6971 | * of error codes, including those from the user provided f_vrfy |
| 6972 | * functions, are treated as fatal and lead to a failure of |
| 6973 | * ssl_parse_certificate even if verification was optional. */ |
| 6974 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6975 | ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 6976 | verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) ) |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6977 | { |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6978 | verify_ret = 0; |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6979 | } |
| 6980 | |
| 6981 | if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
| 6982 | { |
| 6983 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6984 | verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6985 | } |
| 6986 | |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6987 | if( verify_ret != 0 ) |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6988 | { |
| 6989 | uint8_t alert; |
| 6990 | |
| 6991 | /* The certificate may have been rejected for several reasons. |
| 6992 | Pick one and send the corresponding alert. Which alert to send |
| 6993 | may be a subject of debate in some cases. */ |
| 6994 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 6995 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 6996 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 6997 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6998 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 6999 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7000 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 7001 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7002 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 7003 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7004 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 7005 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7006 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 7007 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7008 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 7009 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 7010 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 7011 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 7012 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 7013 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 7014 | else |
| 7015 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7016 | mbedtls_ssl_pend_fatal_alert( ssl, alert ); |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7017 | } |
| 7018 | |
| 7019 | #if defined(MBEDTLS_DEBUG_C) |
| 7020 | if( ssl->session_negotiate->verify_result != 0 ) |
| 7021 | { |
| 7022 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x", |
| 7023 | ssl->session_negotiate->verify_result ) ); |
| 7024 | } |
| 7025 | else |
| 7026 | { |
| 7027 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 7028 | } |
| 7029 | #endif /* MBEDTLS_DEBUG_C */ |
| 7030 | |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 7031 | return( verify_ret ); |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7032 | } |
| 7033 | |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7034 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 7035 | |
| 7036 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7037 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 7038 | unsigned char *start, size_t len ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7039 | { |
| 7040 | int ret; |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7041 | /* Remember digest of the peer's end-CRT. */ |
| 7042 | ssl->session_negotiate->peer_cert_digest = |
| 7043 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 7044 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 7045 | { |
| 7046 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 7047 | sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7048 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 7049 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7050 | |
| 7051 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 7052 | } |
| 7053 | |
| 7054 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 7055 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 7056 | start, len, |
| 7057 | ssl->session_negotiate->peer_cert_digest ); |
| 7058 | |
| 7059 | ssl->session_negotiate->peer_cert_digest_type = |
| 7060 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 7061 | ssl->session_negotiate->peer_cert_digest_len = |
| 7062 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 7063 | |
| 7064 | return( ret ); |
| 7065 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 7066 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7067 | |
| 7068 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 7069 | unsigned char *start, size_t len ) |
| 7070 | { |
| 7071 | unsigned char *end = start + len; |
| 7072 | int ret; |
| 7073 | |
| 7074 | /* Make a copy of the peer's raw public key. */ |
| 7075 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 7076 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 7077 | &ssl->handshake->peer_pubkey ); |
| 7078 | if( ret != 0 ) |
| 7079 | { |
| 7080 | /* We should have parsed the public key before. */ |
| 7081 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7082 | } |
| 7083 | |
| 7084 | return( 0 ); |
| 7085 | } |
| 7086 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7087 | |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7088 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 7089 | { |
| 7090 | int ret = 0; |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7091 | int crt_expected; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7092 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7093 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 7094 | ? ssl->handshake->sni_authmode |
Hanno Becker | acd4fc0 | 2019-06-12 16:40:50 +0100 | [diff] [blame] | 7095 | : mbedtls_ssl_conf_get_authmode( ssl->conf ); |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7096 | #else |
Hanno Becker | acd4fc0 | 2019-06-12 16:40:50 +0100 | [diff] [blame] | 7097 | const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf ); |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7098 | #endif |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7099 | void *rs_ctx = NULL; |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7100 | mbedtls_x509_crt *chain = NULL; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7101 | |
| 7102 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 7103 | |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7104 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 7105 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7106 | { |
| 7107 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Hanno Becker | 613d490 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7108 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7109 | } |
| 7110 | |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7111 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 7112 | if( ssl->handshake->ecrs_enabled && |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 7113 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7114 | { |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7115 | chain = ssl->handshake->ecrs_peer_cert; |
| 7116 | ssl->handshake->ecrs_peer_cert = NULL; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7117 | goto crt_verify; |
| 7118 | } |
| 7119 | #endif |
| 7120 | |
Manuel Pégourié-Gonnard | 125af94 | 2018-09-11 11:08:12 +0200 | [diff] [blame] | 7121 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7122 | { |
| 7123 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 7124 | let it decide whether to alert. */ |
| 7125 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7126 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7127 | } |
| 7128 | |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7129 | #if defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7130 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 7131 | { |
| 7132 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7133 | |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7134 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ) |
Hanno Becker | 613d490 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7135 | ret = 0; |
| 7136 | else |
| 7137 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7138 | |
Hanno Becker | 613d490 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7139 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7140 | } |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7141 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7142 | |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7143 | /* Clear existing peer CRT structure in case we tried to |
| 7144 | * reuse a session but it failed, and allocate a new one. */ |
Hanno Becker | a46c287 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 7145 | ssl_clear_peer_cert( ssl->session_negotiate ); |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7146 | |
| 7147 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 7148 | if( chain == NULL ) |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7149 | { |
| 7150 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 7151 | sizeof( mbedtls_x509_crt ) ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7152 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 7153 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Hanno Becker | a46c287 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 7154 | |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7155 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 7156 | goto exit; |
| 7157 | } |
| 7158 | mbedtls_x509_crt_init( chain ); |
| 7159 | |
| 7160 | ret = ssl_parse_certificate_chain( ssl, chain ); |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7161 | if( ret != 0 ) |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7162 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7163 | |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7164 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 7165 | if( ssl->handshake->ecrs_enabled) |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 7166 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7167 | |
| 7168 | crt_verify: |
| 7169 | if( ssl->handshake->ecrs_enabled) |
| 7170 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 7171 | #endif |
| 7172 | |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7173 | ret = ssl_parse_certificate_verify( ssl, authmode, |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7174 | chain, rs_ctx ); |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7175 | if( ret != 0 ) |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7176 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7177 | |
Hanno Becker | 3008d28 | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 7178 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7179 | { |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 7180 | size_t pk_len; |
| 7181 | unsigned char *pk_start; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7182 | |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7183 | /* We parse the CRT chain without copying, so |
| 7184 | * these pointers point into the input buffer, |
| 7185 | * and are hence still valid after freeing the |
| 7186 | * CRT chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7187 | |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 7188 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7189 | unsigned char *crt_start; |
| 7190 | size_t crt_len; |
| 7191 | |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7192 | crt_start = chain->raw.p; |
| 7193 | crt_len = chain->raw.len; |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 7194 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7195 | |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 7196 | pk_start = chain->cache->pk_raw.p; |
| 7197 | pk_len = chain->cache->pk_raw.len; |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7198 | |
| 7199 | /* Free the CRT structures before computing |
| 7200 | * digest and copying the peer's public key. */ |
| 7201 | mbedtls_x509_crt_free( chain ); |
| 7202 | mbedtls_free( chain ); |
| 7203 | chain = NULL; |
| 7204 | |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 7205 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7206 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7207 | if( ret != 0 ) |
Hanno Becker | cf291d6 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 7208 | goto exit; |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 7209 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7210 | |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7211 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7212 | if( ret != 0 ) |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7213 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7214 | } |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7215 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7216 | /* Pass ownership to session structure. */ |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7217 | ssl->session_negotiate->peer_cert = chain; |
| 7218 | chain = NULL; |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7219 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7220 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7221 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7222 | |
Hanno Becker | 613d490 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7223 | exit: |
| 7224 | |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7225 | if( ret == 0 ) |
| 7226 | ssl->state++; |
| 7227 | |
| 7228 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 7229 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 7230 | { |
| 7231 | ssl->handshake->ecrs_peer_cert = chain; |
| 7232 | chain = NULL; |
| 7233 | } |
| 7234 | #endif |
| 7235 | |
| 7236 | if( chain != NULL ) |
| 7237 | { |
| 7238 | mbedtls_x509_crt_free( chain ); |
| 7239 | mbedtls_free( chain ); |
| 7240 | } |
| 7241 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7242 | return( ret ); |
| 7243 | } |
Hanno Becker | b71e90a | 2019-02-05 13:20:55 +0000 | [diff] [blame] | 7244 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7245 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7246 | int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7247 | { |
| 7248 | int ret; |
| 7249 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7250 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7251 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7252 | ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7253 | ssl->out_msglen = 1; |
| 7254 | ssl->out_msg[0] = 1; |
| 7255 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7256 | ssl->state++; |
| 7257 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7258 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7259 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7260 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7261 | return( ret ); |
| 7262 | } |
| 7263 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7264 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7265 | |
| 7266 | return( 0 ); |
| 7267 | } |
| 7268 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7269 | int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7270 | { |
| 7271 | int ret; |
| 7272 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7273 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7274 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 7275 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7276 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7277 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7278 | return( ret ); |
| 7279 | } |
| 7280 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7281 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7282 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7283 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7284 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 7285 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7286 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7287 | } |
| 7288 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 7289 | /* CCS records are only accepted if they have length 1 and content '1', |
| 7290 | * so we don't need to check this here. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7291 | |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7292 | /* |
| 7293 | * Switch to our negotiated transform and session parameters for inbound |
| 7294 | * data. |
| 7295 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7296 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7297 | ssl->transform_in = ssl->transform_negotiate; |
| 7298 | ssl->session_in = ssl->session_negotiate; |
| 7299 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7300 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7301 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7302 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7303 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7304 | ssl_dtls_replay_reset( ssl ); |
| 7305 | #endif |
| 7306 | |
| 7307 | /* Increment epoch */ |
| 7308 | if( ++ssl->in_epoch == 0 ) |
| 7309 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7310 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7311 | /* This is highly unlikely to happen for legitimate reasons, so |
| 7312 | treat it as an attack and don't send an alert. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7313 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7314 | } |
| 7315 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7316 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7317 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7318 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 7319 | { |
| 7320 | memset( ssl->in_ctr, 0, 8 ); |
| 7321 | } |
| 7322 | #endif |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7323 | |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 7324 | ssl_update_in_pointers( ssl ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7325 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7326 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 7327 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7328 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7329 | if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 ) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7330 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7331 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7332 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 7333 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7334 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7335 | } |
| 7336 | } |
| 7337 | #endif |
| 7338 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7339 | ssl->state++; |
| 7340 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7341 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7342 | |
| 7343 | return( 0 ); |
| 7344 | } |
| 7345 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7346 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 7347 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7348 | { |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 7349 | ((void) ciphersuite_info); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 7350 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7351 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7352 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 7353 | if( mbedtls_ssl_get_minor_ver( ssl ) < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7354 | ssl->handshake->update_checksum = ssl_update_checksum_md5sha1; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7355 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7356 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7357 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7358 | #if defined(MBEDTLS_SHA512_C) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 7359 | if( mbedtls_ssl_suite_get_mac( ciphersuite_info ) == MBEDTLS_MD_SHA384 ) |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7360 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 7361 | else |
| 7362 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7363 | #if defined(MBEDTLS_SHA256_C) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 7364 | if( mbedtls_ssl_suite_get_mac( ciphersuite_info ) != MBEDTLS_MD_SHA384 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7365 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7366 | else |
| 7367 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7368 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 7369 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7370 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7371 | return; |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 7372 | } |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7373 | } |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 7374 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7375 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7376 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7377 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7378 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7379 | mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 ); |
| 7380 | mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7381 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7382 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7383 | #if defined(MBEDTLS_SHA256_C) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7384 | mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7385 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7386 | #if defined(MBEDTLS_SHA512_C) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7387 | mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7388 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7389 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7390 | } |
| 7391 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7392 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7393 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7394 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7395 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7396 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7397 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); |
| 7398 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7399 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7400 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7401 | #if defined(MBEDTLS_SHA256_C) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7402 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7403 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7404 | #if defined(MBEDTLS_SHA512_C) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7405 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 7406 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7407 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7408 | } |
| 7409 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7410 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7411 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 7412 | static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7413 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7414 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7415 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); |
| 7416 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7417 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7418 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7419 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7420 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7421 | #if defined(MBEDTLS_SHA256_C) |
| 7422 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7423 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7424 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7425 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7426 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7427 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7428 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7429 | #if defined(MBEDTLS_SHA512_C) |
| 7430 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7431 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7432 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7433 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7434 | } |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 7435 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7436 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7437 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7438 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7439 | static void ssl_calc_finished_ssl( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7440 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7441 | { |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7442 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7443 | mbedtls_md5_context md5; |
| 7444 | mbedtls_sha1_context sha1; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7445 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7446 | unsigned char padbuf[48]; |
| 7447 | unsigned char md5sum[16]; |
| 7448 | unsigned char sha1sum[20]; |
| 7449 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7450 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7451 | if( !session ) |
| 7452 | session = ssl->session; |
| 7453 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7454 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7455 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7456 | mbedtls_md5_init( &md5 ); |
| 7457 | mbedtls_sha1_init( &sha1 ); |
| 7458 | |
| 7459 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 7460 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7461 | |
| 7462 | /* |
| 7463 | * SSLv3: |
| 7464 | * hash = |
| 7465 | * MD5( master + pad2 + |
| 7466 | * MD5( handshake + sender + master + pad1 ) ) |
| 7467 | * + SHA1( master + pad2 + |
| 7468 | * SHA1( handshake + sender + master + pad1 ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7469 | */ |
| 7470 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7471 | #if !defined(MBEDTLS_MD5_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7472 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *) |
| 7473 | md5.state, sizeof( md5.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7474 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7475 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7476 | #if !defined(MBEDTLS_SHA1_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7477 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) |
| 7478 | sha1.state, sizeof( sha1.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7479 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7480 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7481 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT" |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7482 | : "SRVR"; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7483 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7484 | memset( padbuf, 0x36, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7485 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7486 | mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 ); |
| 7487 | mbedtls_md5_update_ret( &md5, session->master, 48 ); |
| 7488 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); |
| 7489 | mbedtls_md5_finish_ret( &md5, md5sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7490 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7491 | mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 ); |
| 7492 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); |
| 7493 | mbedtls_sha1_update_ret( &sha1, padbuf, 40 ); |
| 7494 | mbedtls_sha1_finish_ret( &sha1, sha1sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7495 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7496 | memset( padbuf, 0x5C, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7497 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7498 | mbedtls_md5_starts_ret( &md5 ); |
| 7499 | mbedtls_md5_update_ret( &md5, session->master, 48 ); |
| 7500 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); |
| 7501 | mbedtls_md5_update_ret( &md5, md5sum, 16 ); |
| 7502 | mbedtls_md5_finish_ret( &md5, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7503 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7504 | mbedtls_sha1_starts_ret( &sha1 ); |
| 7505 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); |
| 7506 | mbedtls_sha1_update_ret( &sha1, padbuf , 40 ); |
| 7507 | mbedtls_sha1_update_ret( &sha1, sha1sum, 20 ); |
| 7508 | mbedtls_sha1_finish_ret( &sha1, buf + 16 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7509 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7510 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7511 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7512 | mbedtls_md5_free( &md5 ); |
| 7513 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7514 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7515 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 7516 | mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) ); |
| 7517 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7518 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7519 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7520 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7521 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7522 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7523 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7524 | static void ssl_calc_finished_tls( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7525 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7526 | { |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7527 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7528 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7529 | mbedtls_md5_context md5; |
| 7530 | mbedtls_sha1_context sha1; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7531 | unsigned char padbuf[36]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7532 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7533 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7534 | if( !session ) |
| 7535 | session = ssl->session; |
| 7536 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7537 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7538 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7539 | mbedtls_md5_init( &md5 ); |
| 7540 | mbedtls_sha1_init( &sha1 ); |
| 7541 | |
| 7542 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 7543 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7544 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7545 | /* |
| 7546 | * TLSv1: |
| 7547 | * hash = PRF( master, finished_label, |
| 7548 | * MD5( handshake ) + SHA1( handshake ) )[0..11] |
| 7549 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7550 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7551 | #if !defined(MBEDTLS_MD5_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7552 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *) |
| 7553 | md5.state, sizeof( md5.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7554 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7555 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7556 | #if !defined(MBEDTLS_SHA1_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7557 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) |
| 7558 | sha1.state, sizeof( sha1.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7559 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7560 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7561 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7562 | ? "client finished" |
| 7563 | : "server finished"; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7564 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7565 | mbedtls_md5_finish_ret( &md5, padbuf ); |
| 7566 | mbedtls_sha1_finish_ret( &sha1, padbuf + 16 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7567 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7568 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7569 | padbuf, 36, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7570 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7571 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7572 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7573 | mbedtls_md5_free( &md5 ); |
| 7574 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7575 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7576 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7577 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7578 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7579 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7580 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7581 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7582 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7583 | #if defined(MBEDTLS_SHA256_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7584 | static void ssl_calc_finished_tls_sha256( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7585 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7586 | { |
| 7587 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7588 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7589 | mbedtls_sha256_context sha256; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7590 | unsigned char padbuf[32]; |
| 7591 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7592 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7593 | if( !session ) |
| 7594 | session = ssl->session; |
| 7595 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7596 | mbedtls_sha256_init( &sha256 ); |
| 7597 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7598 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7599 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7600 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7601 | |
| 7602 | /* |
| 7603 | * TLSv1.2: |
| 7604 | * hash = PRF( master, finished_label, |
| 7605 | * Hash( handshake ) )[0.11] |
| 7606 | */ |
| 7607 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7608 | #if !defined(MBEDTLS_SHA256_ALT) |
| 7609 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7610 | sha256.state, sizeof( sha256.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7611 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7612 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7613 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7614 | ? "client finished" |
| 7615 | : "server finished"; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7616 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7617 | mbedtls_sha256_finish_ret( &sha256, padbuf ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7618 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7619 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7620 | padbuf, 32, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7621 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7622 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7623 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7624 | mbedtls_sha256_free( &sha256 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7625 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7626 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7627 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7628 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7629 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7630 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7631 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7632 | #if defined(MBEDTLS_SHA512_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7633 | static void ssl_calc_finished_tls_sha384( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7634 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7635 | { |
| 7636 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7637 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7638 | mbedtls_sha512_context sha512; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7639 | unsigned char padbuf[48]; |
| 7640 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7641 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7642 | if( !session ) |
| 7643 | session = ssl->session; |
| 7644 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7645 | mbedtls_sha512_init( &sha512 ); |
| 7646 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7647 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7648 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7649 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7650 | |
| 7651 | /* |
| 7652 | * TLSv1.2: |
| 7653 | * hash = PRF( master, finished_label, |
| 7654 | * Hash( handshake ) )[0.11] |
| 7655 | */ |
| 7656 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7657 | #if !defined(MBEDTLS_SHA512_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7658 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 7659 | sha512.state, sizeof( sha512.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7660 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7661 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7662 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7663 | ? "client finished" |
| 7664 | : "server finished"; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7665 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7666 | mbedtls_sha512_finish_ret( &sha512, padbuf ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7667 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7668 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7669 | padbuf, 48, buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7670 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7671 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7672 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7673 | mbedtls_sha512_free( &sha512 ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7674 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7675 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7676 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7677 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7678 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7679 | #endif /* MBEDTLS_SHA512_C */ |
| 7680 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7681 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7682 | static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7683 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7684 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7685 | |
| 7686 | /* |
| 7687 | * Free our handshake params |
| 7688 | */ |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 7689 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7690 | mbedtls_free( ssl->handshake ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7691 | ssl->handshake = NULL; |
| 7692 | |
| 7693 | /* |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7694 | * Free the previous transform and swith in the current one |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7695 | */ |
| 7696 | if( ssl->transform ) |
| 7697 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7698 | mbedtls_ssl_transform_free( ssl->transform ); |
| 7699 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7700 | } |
| 7701 | ssl->transform = ssl->transform_negotiate; |
| 7702 | ssl->transform_negotiate = NULL; |
| 7703 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7704 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7705 | } |
| 7706 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7707 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7708 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7709 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7710 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7711 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7712 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7713 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7714 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7715 | ssl->renego_records_seen = 0; |
| 7716 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 7717 | #endif |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7718 | |
| 7719 | /* |
| 7720 | * Free the previous session and switch in the current one |
| 7721 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7722 | if( ssl->session ) |
| 7723 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7724 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 1a03473 | 2014-11-04 17:36:18 +0100 | [diff] [blame] | 7725 | /* RFC 7366 3.1: keep the EtM state */ |
| 7726 | ssl->session_negotiate->encrypt_then_mac = |
| 7727 | ssl->session->encrypt_then_mac; |
| 7728 | #endif |
| 7729 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7730 | mbedtls_ssl_session_free( ssl->session ); |
| 7731 | mbedtls_free( ssl->session ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7732 | } |
| 7733 | ssl->session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7734 | ssl->session_negotiate = NULL; |
| 7735 | |
Manuel Pégourié-Gonnard | 7b80c64 | 2019-07-02 16:21:30 +0200 | [diff] [blame] | 7736 | #if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7737 | /* |
| 7738 | * Add cache entry |
| 7739 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7740 | if( ssl->conf->f_set_cache != NULL && |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 7741 | ssl->session->id_len != 0 && |
Manuel Pégourié-Gonnard | 7b80c64 | 2019-07-02 16:21:30 +0200 | [diff] [blame] | 7742 | ssl->handshake->resume == 0 ) |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 7743 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 7744 | if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7745 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 7746 | } |
Manuel Pégourié-Gonnard | 7b80c64 | 2019-07-02 16:21:30 +0200 | [diff] [blame] | 7747 | #endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7748 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7749 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 7750 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7751 | ssl->handshake->flight != NULL ) |
| 7752 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 7753 | /* Cancel handshake timer */ |
| 7754 | ssl_set_timer( ssl, 0 ); |
| 7755 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7756 | /* Keep last flight around in case we need to resend it: |
| 7757 | * we need the handshake and transform structures for that */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7758 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7759 | } |
| 7760 | else |
| 7761 | #endif |
| 7762 | ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 7763 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7764 | ssl->state++; |
| 7765 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7766 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7767 | } |
| 7768 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7769 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7770 | { |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7771 | int ret, hash_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7772 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7773 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7774 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7775 | ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 7776 | |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7777 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, |
| 7778 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7779 | |
Manuel Pégourié-Gonnard | 214a848 | 2016-02-22 11:27:26 +0100 | [diff] [blame] | 7780 | /* |
| 7781 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 7782 | * may define some other value. Currently (early 2016), no defined |
| 7783 | * ciphersuite does this (and this is unlikely to change as activity has |
| 7784 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 7785 | */ |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 7786 | hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7787 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7788 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7789 | ssl->verify_data_len = hash_len; |
| 7790 | memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 7791 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7792 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7793 | ssl->out_msglen = 4 + hash_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7794 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 7795 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7796 | |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 7797 | #if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7798 | /* |
| 7799 | * In case of session resuming, invert the client and server |
| 7800 | * ChangeCipherSpec messages order. |
| 7801 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7802 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7803 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7804 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7805 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 7806 | MBEDTLS_SSL_IS_CLIENT ) |
| 7807 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7808 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7809 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 7810 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7811 | #if defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7812 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 7813 | MBEDTLS_SSL_IS_SERVER ) |
| 7814 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7815 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7816 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 7817 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7818 | } |
| 7819 | else |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 7820 | #endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7821 | ssl->state++; |
| 7822 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7823 | /* |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 7824 | * Switch to our negotiated transform and session parameters for outbound |
| 7825 | * data. |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7826 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7827 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) ); |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 7828 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7829 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7830 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7831 | { |
| 7832 | unsigned char i; |
| 7833 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 7834 | /* Remember current epoch settings for resending */ |
| 7835 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 7836 | memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 7837 | |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7838 | /* Set sequence_number to zero */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 7839 | memset( ssl->cur_out_ctr + 2, 0, 6 ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7840 | |
| 7841 | /* Increment epoch */ |
| 7842 | for( i = 2; i > 0; i-- ) |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 7843 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7844 | break; |
| 7845 | |
| 7846 | /* The loop goes to its end iff the counter is wrapping */ |
| 7847 | if( i == 0 ) |
| 7848 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7849 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 7850 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7851 | } |
| 7852 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7853 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7854 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7855 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 7856 | { |
| 7857 | memset( ssl->cur_out_ctr, 0, 8 ); |
| 7858 | } |
| 7859 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7860 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 7861 | ssl->transform_out = ssl->transform_negotiate; |
| 7862 | ssl->session_out = ssl->session_negotiate; |
| 7863 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7864 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 7865 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 7866 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7867 | if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 ) |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 7868 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7869 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
| 7870 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 7871 | } |
| 7872 | } |
| 7873 | #endif |
| 7874 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7875 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 7876 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7877 | mbedtls_ssl_send_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 7878 | #endif |
| 7879 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7880 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7881 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7882 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7883 | return( ret ); |
| 7884 | } |
| 7885 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 7886 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 7887 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 7888 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 7889 | { |
| 7890 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 7891 | return( ret ); |
| 7892 | } |
| 7893 | #endif |
| 7894 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7895 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7896 | |
| 7897 | return( 0 ); |
| 7898 | } |
| 7899 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7900 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 7901 | #define SSL_MAX_HASH_LEN 36 |
| 7902 | #else |
| 7903 | #define SSL_MAX_HASH_LEN 12 |
| 7904 | #endif |
| 7905 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7906 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7907 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 7908 | int ret; |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7909 | unsigned int hash_len; |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 7910 | unsigned char buf[SSL_MAX_HASH_LEN]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7911 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7912 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7913 | |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7914 | ssl->handshake->calc_finished( ssl, buf, |
| 7915 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7916 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 7917 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7918 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7919 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7920 | return( ret ); |
| 7921 | } |
| 7922 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7923 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7924 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7925 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7926 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 7927 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7928 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7929 | } |
| 7930 | |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 7931 | /* There is currently no ciphersuite using another length with TLS 1.2 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7932 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 7933 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 7934 | hash_len = 36; |
| 7935 | else |
| 7936 | #endif |
| 7937 | hash_len = 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7938 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7939 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED || |
| 7940 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7941 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7942 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7943 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 7944 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7945 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7946 | } |
| 7947 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7948 | if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), |
Manuel Pégourié-Gonnard | 4abc327 | 2014-09-10 12:02:46 +0000 | [diff] [blame] | 7949 | buf, hash_len ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7950 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7951 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7952 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 7953 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7954 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7955 | } |
| 7956 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7957 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7958 | ssl->verify_data_len = hash_len; |
| 7959 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 7960 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7961 | |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 7962 | #if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7963 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7964 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7965 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7966 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7967 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 7968 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7969 | #if defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7970 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7971 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 7972 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7973 | } |
| 7974 | else |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 7975 | #endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7976 | ssl->state++; |
| 7977 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7978 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 7979 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7980 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 7981 | #endif |
| 7982 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7983 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7984 | |
| 7985 | return( 0 ); |
| 7986 | } |
| 7987 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7988 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7989 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7990 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7991 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7992 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7993 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 7994 | mbedtls_md5_init( &handshake->fin_md5 ); |
| 7995 | mbedtls_sha1_init( &handshake->fin_sha1 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7996 | mbedtls_md5_starts_ret( &handshake->fin_md5 ); |
| 7997 | mbedtls_sha1_starts_ret( &handshake->fin_sha1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7998 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7999 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 8000 | #if defined(MBEDTLS_SHA256_C) |
| 8001 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 8002 | mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8003 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8004 | #if defined(MBEDTLS_SHA512_C) |
| 8005 | mbedtls_sha512_init( &handshake->fin_sha512 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 8006 | mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8007 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8008 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8009 | |
| 8010 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 8011 | |
| 8012 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 8013 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 8014 | mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs ); |
| 8015 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8016 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8017 | #if defined(MBEDTLS_DHM_C) |
| 8018 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8019 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8020 | #if defined(MBEDTLS_ECDH_C) |
| 8021 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8022 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 8023 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 8024 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 8025 | #if defined(MBEDTLS_SSL_CLI_C) |
| 8026 | handshake->ecjpake_cache = NULL; |
| 8027 | handshake->ecjpake_cache_len = 0; |
| 8028 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 8029 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 8030 | |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 8031 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 8032 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 8033 | #endif |
| 8034 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 8035 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 8036 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 8037 | #endif |
Hanno Becker | 3bf8cdf | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 8038 | |
| 8039 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 8040 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8041 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 8042 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8043 | } |
| 8044 | |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 8045 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8046 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8047 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 8048 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8049 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 8050 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 8051 | |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 8052 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8053 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 8054 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 8055 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8056 | } |
| 8057 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8058 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8059 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8060 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8061 | } |
| 8062 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8063 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8064 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8065 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8066 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8067 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8068 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8069 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8070 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 8071 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8072 | |
| 8073 | /* |
| 8074 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 8075 | * Now allocate missing structures. |
| 8076 | */ |
| 8077 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8078 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8079 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8080 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8081 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8082 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8083 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8084 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8085 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8086 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 8087 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8088 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8089 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8090 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8091 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8092 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8093 | if( ssl->handshake == NULL || |
| 8094 | ssl->transform_negotiate == NULL || |
| 8095 | ssl->session_negotiate == NULL ) |
| 8096 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 8097 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8098 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8099 | mbedtls_free( ssl->handshake ); |
| 8100 | mbedtls_free( ssl->transform_negotiate ); |
| 8101 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8102 | |
| 8103 | ssl->handshake = NULL; |
| 8104 | ssl->transform_negotiate = NULL; |
| 8105 | ssl->session_negotiate = NULL; |
| 8106 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8107 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8108 | } |
| 8109 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8110 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8111 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 8112 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 8113 | ssl_handshake_params_init( ssl->handshake ); |
| 8114 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8115 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 8116 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 8117 | { |
| 8118 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8119 | |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 8120 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 8121 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 8122 | else |
| 8123 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
| 8124 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8125 | #endif |
| 8126 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8127 | return( 0 ); |
| 8128 | } |
| 8129 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 8130 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 8131 | /* Dummy cookie callbacks for defaults */ |
| 8132 | static int ssl_cookie_write_dummy( void *ctx, |
| 8133 | unsigned char **p, unsigned char *end, |
| 8134 | const unsigned char *cli_id, size_t cli_id_len ) |
| 8135 | { |
| 8136 | ((void) ctx); |
| 8137 | ((void) p); |
| 8138 | ((void) end); |
| 8139 | ((void) cli_id); |
| 8140 | ((void) cli_id_len); |
| 8141 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8142 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 8143 | } |
| 8144 | |
| 8145 | static int ssl_cookie_check_dummy( void *ctx, |
| 8146 | const unsigned char *cookie, size_t cookie_len, |
| 8147 | const unsigned char *cli_id, size_t cli_id_len ) |
| 8148 | { |
| 8149 | ((void) ctx); |
| 8150 | ((void) cookie); |
| 8151 | ((void) cookie_len); |
| 8152 | ((void) cli_id); |
| 8153 | ((void) cli_id_len); |
| 8154 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8155 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 8156 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 8157 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 8158 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8159 | /* Once ssl->out_hdr as the address of the beginning of the |
| 8160 | * next outgoing record is set, deduce the other pointers. |
| 8161 | * |
| 8162 | * Note: For TLS, we save the implicit record sequence number |
| 8163 | * (entering MAC computation) in the 8 bytes before ssl->out_hdr, |
| 8164 | * and the caller has to make sure there's space for this. |
| 8165 | */ |
| 8166 | |
| 8167 | static void ssl_update_out_pointers( mbedtls_ssl_context *ssl, |
| 8168 | mbedtls_ssl_transform *transform ) |
| 8169 | { |
| 8170 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8171 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8172 | { |
| 8173 | ssl->out_ctr = ssl->out_hdr + 3; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8174 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8175 | ssl->out_cid = ssl->out_ctr + 8; |
| 8176 | ssl->out_len = ssl->out_cid; |
| 8177 | if( transform != NULL ) |
| 8178 | ssl->out_len += transform->out_cid_len; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8179 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8180 | ssl->out_len = ssl->out_ctr + 8; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8181 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8182 | ssl->out_iv = ssl->out_len + 2; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8183 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8184 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 8185 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 8186 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8187 | { |
| 8188 | ssl->out_ctr = ssl->out_hdr - 8; |
| 8189 | ssl->out_len = ssl->out_hdr + 3; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8190 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 9bf10ea | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 8191 | ssl->out_cid = ssl->out_len; |
| 8192 | #endif |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8193 | ssl->out_iv = ssl->out_hdr + 5; |
| 8194 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8195 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8196 | |
| 8197 | /* Adjust out_msg to make space for explicit IV, if used. */ |
| 8198 | if( transform != NULL && |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 8199 | mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8200 | { |
| 8201 | ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen; |
| 8202 | } |
| 8203 | else |
| 8204 | ssl->out_msg = ssl->out_iv; |
| 8205 | } |
| 8206 | |
| 8207 | /* Once ssl->in_hdr as the address of the beginning of the |
| 8208 | * next incoming record is set, deduce the other pointers. |
| 8209 | * |
| 8210 | * Note: For TLS, we save the implicit record sequence number |
| 8211 | * (entering MAC computation) in the 8 bytes before ssl->in_hdr, |
| 8212 | * and the caller has to make sure there's space for this. |
| 8213 | */ |
| 8214 | |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8215 | static void ssl_update_in_pointers( mbedtls_ssl_context *ssl ) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8216 | { |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8217 | /* This function sets the pointers to match the case |
Hanno Becker | c360dcc | 2019-07-12 10:00:45 +0100 | [diff] [blame] | 8218 | * of unprotected TLS/DTLS records, with ssl->in_msg |
| 8219 | * pointing to the beginning of the record content. |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8220 | * |
| 8221 | * When decrypting a protected record, ssl->in_msg |
| 8222 | * will be shifted to point to the beginning of the |
| 8223 | * record plaintext. |
| 8224 | */ |
| 8225 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8226 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8227 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8228 | { |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8229 | /* This sets the header pointers to match records |
| 8230 | * without CID. When we receive a record containing |
| 8231 | * a CID, the fields are shifted accordingly in |
| 8232 | * ssl_parse_record_header(). */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8233 | ssl->in_ctr = ssl->in_hdr + 3; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8234 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8235 | ssl->in_cid = ssl->in_ctr + 8; |
| 8236 | ssl->in_len = ssl->in_cid; /* Default: no CID */ |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8237 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8238 | ssl->in_len = ssl->in_ctr + 8; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8239 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | c360dcc | 2019-07-12 10:00:45 +0100 | [diff] [blame] | 8240 | ssl->in_msg = ssl->in_len + 2; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8241 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8242 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 8243 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 8244 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8245 | { |
| 8246 | ssl->in_ctr = ssl->in_hdr - 8; |
| 8247 | ssl->in_len = ssl->in_hdr + 3; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8248 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 9bf10ea | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 8249 | ssl->in_cid = ssl->in_len; |
| 8250 | #endif |
Hanno Becker | c360dcc | 2019-07-12 10:00:45 +0100 | [diff] [blame] | 8251 | ssl->in_msg = ssl->in_hdr + 5; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8252 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8253 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8254 | } |
| 8255 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8256 | /* |
| 8257 | * Initialize an SSL context |
| 8258 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 8259 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 8260 | { |
| 8261 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 8262 | } |
| 8263 | |
| 8264 | /* |
| 8265 | * Setup an SSL context |
| 8266 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8267 | |
| 8268 | static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl ) |
| 8269 | { |
| 8270 | /* Set the incoming and outgoing record pointers. */ |
| 8271 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8272 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8273 | { |
| 8274 | ssl->out_hdr = ssl->out_buf; |
| 8275 | ssl->in_hdr = ssl->in_buf; |
| 8276 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8277 | MBEDTLS_SSL_TRANSPORT_ELSE |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8278 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8279 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8280 | { |
| 8281 | ssl->out_hdr = ssl->out_buf + 8; |
| 8282 | ssl->in_hdr = ssl->in_buf + 8; |
| 8283 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8284 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8285 | |
| 8286 | /* Derive other internal pointers. */ |
| 8287 | ssl_update_out_pointers( ssl, NULL /* no transform enabled */ ); |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8288 | ssl_update_in_pointers ( ssl ); |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8289 | } |
| 8290 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 8291 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 8292 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8293 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8294 | int ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8295 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 8296 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 8297 | |
Hanno Becker | ef982d5 | 2019-07-23 15:56:18 +0100 | [diff] [blame] | 8298 | #if defined(MBEDTLS_USE_TINYCRYPT) |
| 8299 | uECC_set_rng( &uecc_rng_wrapper ); |
| 8300 | #endif |
| 8301 | |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 8302 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 8303 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 8304 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 8305 | |
| 8306 | /* Set to NULL in case of an error condition */ |
| 8307 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8308 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8309 | ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN ); |
| 8310 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8311 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8312 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) ); |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 8313 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8314 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8315 | } |
| 8316 | |
| 8317 | ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 8318 | if( ssl->out_buf == NULL ) |
| 8319 | { |
| 8320 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) ); |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 8321 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8322 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8323 | } |
| 8324 | |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8325 | ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 8326 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8327 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8328 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8329 | |
Hanno Becker | c8f5299 | 2019-07-25 11:15:08 +0100 | [diff] [blame] | 8330 | ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE; |
Hanno Becker | f46e1ce | 2019-07-03 13:56:59 +0100 | [diff] [blame] | 8331 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8332 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8333 | |
| 8334 | error: |
| 8335 | mbedtls_free( ssl->in_buf ); |
| 8336 | mbedtls_free( ssl->out_buf ); |
| 8337 | |
| 8338 | ssl->conf = NULL; |
| 8339 | |
| 8340 | ssl->in_buf = NULL; |
| 8341 | ssl->out_buf = NULL; |
| 8342 | |
| 8343 | ssl->in_hdr = NULL; |
| 8344 | ssl->in_ctr = NULL; |
| 8345 | ssl->in_len = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8346 | ssl->in_msg = NULL; |
| 8347 | |
| 8348 | ssl->out_hdr = NULL; |
| 8349 | ssl->out_ctr = NULL; |
| 8350 | ssl->out_len = NULL; |
| 8351 | ssl->out_iv = NULL; |
| 8352 | ssl->out_msg = NULL; |
| 8353 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 8354 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8355 | } |
| 8356 | |
| 8357 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8358 | * Reset an initialized and used SSL context for re-use while retaining |
| 8359 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8360 | * |
| 8361 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 8362 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8363 | */ |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8364 | static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8365 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8366 | int ret; |
| 8367 | |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 8368 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \ |
| 8369 | !defined(MBEDTLS_SSL_SRV_C) |
| 8370 | ((void) partial); |
| 8371 | #endif |
| 8372 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8373 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8374 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 8375 | /* Cancel any possibly running timer */ |
| 8376 | ssl_set_timer( ssl, 0 ); |
| 8377 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8378 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 8379 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8380 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8381 | |
| 8382 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8383 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 8384 | memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8385 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8386 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8387 | |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8388 | ssl->in_offt = NULL; |
Hanno Becker | f29d470 | 2018-08-10 11:31:15 +0100 | [diff] [blame] | 8389 | ssl_reset_in_out_pointers( ssl ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8390 | |
| 8391 | ssl->in_msgtype = 0; |
| 8392 | ssl->in_msglen = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8393 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 8394 | ssl->next_record_offset = 0; |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 8395 | ssl->in_epoch = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 8396 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8397 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 8398 | ssl_dtls_replay_reset( ssl ); |
| 8399 | #endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8400 | |
| 8401 | ssl->in_hslen = 0; |
| 8402 | ssl->nb_zero = 0; |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 8403 | |
| 8404 | ssl->keep_current_message = 0; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8405 | |
| 8406 | ssl->out_msgtype = 0; |
| 8407 | ssl->out_msglen = 0; |
| 8408 | ssl->out_left = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8409 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
| 8410 | if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ) |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 8411 | ssl->split_done = 0; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 8412 | #endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8413 | |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 8414 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 8415 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8416 | ssl->transform_in = NULL; |
| 8417 | ssl->transform_out = NULL; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8418 | |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 8419 | ssl->session_in = NULL; |
| 8420 | ssl->session_out = NULL; |
| 8421 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8422 | memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8423 | |
| 8424 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8425 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8426 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
| 8427 | { |
| 8428 | ssl->in_left = 0; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8429 | memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8430 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8431 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8432 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 8433 | if( mbedtls_ssl_hw_record_reset != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8434 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8435 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) ); |
| 8436 | if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8437 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8438 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret ); |
| 8439 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8440 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8441 | } |
| 8442 | #endif |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8443 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8444 | if( ssl->transform ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8445 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8446 | mbedtls_ssl_transform_free( ssl->transform ); |
| 8447 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8448 | ssl->transform = NULL; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8449 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8450 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 8451 | if( ssl->session ) |
| 8452 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8453 | mbedtls_ssl_session_free( ssl->session ); |
| 8454 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 8455 | ssl->session = NULL; |
| 8456 | } |
| 8457 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8458 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 8459 | ssl->alpn_chosen = NULL; |
| 8460 | #endif |
| 8461 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 8462 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8463 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8464 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8465 | #endif |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8466 | { |
| 8467 | mbedtls_free( ssl->cli_id ); |
| 8468 | ssl->cli_id = NULL; |
| 8469 | ssl->cli_id_len = 0; |
| 8470 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 8471 | #endif |
| 8472 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8473 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 8474 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8475 | |
| 8476 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8477 | } |
| 8478 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 8479 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8480 | * Reset an initialized and used SSL context for re-use while retaining |
| 8481 | * all application-set variables, function pointers and data. |
| 8482 | */ |
| 8483 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 8484 | { |
| 8485 | return( ssl_session_reset_int( ssl, 0 ) ); |
| 8486 | } |
| 8487 | |
| 8488 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8489 | * SSL set accessors |
| 8490 | */ |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 8491 | #if !defined(MBEDTLS_SSL_CONF_ENDPOINT) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8492 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8493 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8494 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8495 | } |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 8496 | #endif /* MBEDTLS_SSL_CONF_ENDPOINT */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8497 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 8498 | void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport ) |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 8499 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8500 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 8501 | } |
| 8502 | |
Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 8503 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \ |
| 8504 | !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8505 | void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode ) |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 8506 | { |
Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 8507 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 8508 | } |
Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 8509 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */ |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 8510 | |
Hanno Becker | de67154 | 2019-06-12 16:30:46 +0100 | [diff] [blame] | 8511 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \ |
| 8512 | !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT) |
| 8513 | void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, |
| 8514 | unsigned limit ) |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 8515 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8516 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 8517 | } |
Hanno Becker | de67154 | 2019-06-12 16:30:46 +0100 | [diff] [blame] | 8518 | #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */ |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 8519 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8520 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 8521 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 8522 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 8523 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 8524 | { |
| 8525 | ssl->disable_datagram_packing = !allow_packing; |
| 8526 | } |
| 8527 | |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 8528 | #if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \ |
| 8529 | defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 8530 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 8531 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 8532 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8533 | conf->hs_timeout_min = min; |
| 8534 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 8535 | } |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 8536 | #else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN && |
| 8537 | MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */ |
| 8538 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 8539 | uint32_t min, uint32_t max ) |
| 8540 | { |
| 8541 | ((void) conf); |
| 8542 | ((void) min); |
| 8543 | ((void) max); |
| 8544 | } |
| 8545 | #endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN && |
| 8546 | MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */ |
| 8547 | |
| 8548 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 8549 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8550 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8551 | { |
Hanno Becker | acd4fc0 | 2019-06-12 16:40:50 +0100 | [diff] [blame] | 8552 | #if !defined(MBEDTLS_SSL_CONF_AUTHMODE) |
| 8553 | conf->authmode = authmode; |
| 8554 | #else |
| 8555 | ((void) conf); |
| 8556 | ((void) authmode); |
| 8557 | #endif /* MBEDTLS_SSL_CONF_AUTHMODE */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8558 | } |
| 8559 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8560 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8561 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 8562 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 8563 | void *p_vrfy ) |
| 8564 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8565 | conf->f_vrfy = f_vrfy; |
| 8566 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 8567 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8568 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 8569 | |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 8570 | #if !defined(MBEDTLS_SSL_CONF_RNG) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8571 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 8572 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8573 | void *p_rng ) |
| 8574 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 8575 | conf->f_rng = f_rng; |
| 8576 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8577 | } |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 8578 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8579 | |
Hanno Becker | 14a4a44 | 2019-07-02 17:00:34 +0100 | [diff] [blame] | 8580 | #if defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8581 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 8582 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8583 | void *p_dbg ) |
| 8584 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8585 | conf->f_dbg = f_dbg; |
| 8586 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8587 | } |
Hanno Becker | 14a4a44 | 2019-07-02 17:00:34 +0100 | [diff] [blame] | 8588 | #endif /* MBEDTLS_DEBUG_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8589 | |
Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 8590 | #if !defined(MBEDTLS_SSL_CONF_RECV) && \ |
| 8591 | !defined(MBEDTLS_SSL_CONF_SEND) && \ |
| 8592 | !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8593 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8594 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 8595 | mbedtls_ssl_send_t *f_send, |
| 8596 | mbedtls_ssl_recv_t *f_recv, |
| 8597 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8598 | { |
Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 8599 | ssl->p_bio = p_bio; |
| 8600 | ssl->f_send = f_send; |
| 8601 | ssl->f_recv = f_recv; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8602 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 8603 | } |
Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 8604 | #else |
| 8605 | void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl, |
| 8606 | void *p_bio ) |
| 8607 | { |
| 8608 | ssl->p_bio = p_bio; |
| 8609 | } |
| 8610 | #endif |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 8611 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 8612 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8613 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 8614 | { |
| 8615 | ssl->mtu = mtu; |
| 8616 | } |
| 8617 | #endif |
| 8618 | |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 8619 | #if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8620 | void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout ) |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 8621 | { |
| 8622 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8623 | } |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 8624 | #endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */ |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8625 | |
Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 8626 | #if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \ |
| 8627 | !defined(MBEDTLS_SSL_CONF_GET_TIMER) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 8628 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 8629 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 8630 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 8631 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 8632 | { |
| 8633 | ssl->p_timer = p_timer; |
| 8634 | ssl->f_set_timer = f_set_timer; |
| 8635 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 8636 | /* Make sure we start with no timer running */ |
| 8637 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 8638 | } |
Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 8639 | #else |
| 8640 | void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl, |
| 8641 | void *p_timer ) |
| 8642 | { |
| 8643 | ssl->p_timer = p_timer; |
| 8644 | /* Make sure we start with no timer running */ |
| 8645 | ssl_set_timer( ssl, 0 ); |
| 8646 | } |
| 8647 | #endif |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 8648 | |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 8649 | #if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8650 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 8651 | void *p_cache, |
| 8652 | int (*f_get_cache)(void *, mbedtls_ssl_session *), |
| 8653 | int (*f_set_cache)(void *, const mbedtls_ssl_session *) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8654 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 8655 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8656 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8657 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8658 | } |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 8659 | #endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8660 | |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 8661 | #if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8662 | int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8663 | { |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8664 | int ret; |
| 8665 | |
| 8666 | if( ssl == NULL || |
| 8667 | session == NULL || |
| 8668 | ssl->session_negotiate == NULL || |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 8669 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8670 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8671 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8672 | } |
| 8673 | |
Hanno Becker | 58fccf2 | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 8674 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 8675 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8676 | return( ret ); |
| 8677 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 8678 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8679 | |
| 8680 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8681 | } |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 8682 | #endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8683 | |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 8684 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8685 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8686 | const int *ciphersuites ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8687 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8688 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites; |
| 8689 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites; |
| 8690 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites; |
| 8691 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 8692 | } |
| 8693 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8694 | void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 8695 | const int *ciphersuites, |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 8696 | int major, int minor ) |
| 8697 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8698 | if( major != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 8699 | return; |
| 8700 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8701 | if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 8702 | return; |
| 8703 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8704 | conf->ciphersuite_list[minor] = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8705 | } |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 8706 | #endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8707 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8708 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 8709 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 8710 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 8711 | { |
| 8712 | conf->cert_profile = profile; |
| 8713 | } |
| 8714 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8715 | /* Append a new keycert entry to a (possibly empty) list */ |
| 8716 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 8717 | mbedtls_x509_crt *cert, |
| 8718 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8719 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8720 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8721 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8722 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 8723 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8724 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8725 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8726 | new_cert->cert = cert; |
| 8727 | new_cert->key = key; |
| 8728 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8729 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8730 | /* Update head is the list was null, else add to the end */ |
| 8731 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 8732 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8733 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 8734 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8735 | else |
| 8736 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8737 | mbedtls_ssl_key_cert *cur = *head; |
| 8738 | while( cur->next != NULL ) |
| 8739 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8740 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8741 | } |
| 8742 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8743 | return( 0 ); |
| 8744 | } |
| 8745 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8746 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8747 | mbedtls_x509_crt *own_cert, |
| 8748 | mbedtls_pk_context *pk_key ) |
| 8749 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 8750 | return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8751 | } |
| 8752 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8753 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 8754 | mbedtls_x509_crt *ca_chain, |
| 8755 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8756 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 8757 | conf->ca_chain = ca_chain; |
| 8758 | conf->ca_crl = ca_crl; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8759 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8760 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 8761 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 8762 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 8763 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 8764 | mbedtls_x509_crt *own_cert, |
| 8765 | mbedtls_pk_context *pk_key ) |
| 8766 | { |
| 8767 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 8768 | own_cert, pk_key ) ); |
| 8769 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 8770 | |
| 8771 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 8772 | mbedtls_x509_crt *ca_chain, |
| 8773 | mbedtls_x509_crl *ca_crl ) |
| 8774 | { |
| 8775 | ssl->handshake->sni_ca_chain = ca_chain; |
| 8776 | ssl->handshake->sni_ca_crl = ca_crl; |
| 8777 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 8778 | |
| 8779 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 8780 | int authmode ) |
| 8781 | { |
| 8782 | ssl->handshake->sni_authmode = authmode; |
| 8783 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 8784 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 8785 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 8786 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 8787 | /* |
| 8788 | * Set EC J-PAKE password for current handshake |
| 8789 | */ |
| 8790 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 8791 | const unsigned char *pw, |
| 8792 | size_t pw_len ) |
| 8793 | { |
| 8794 | mbedtls_ecjpake_role role; |
| 8795 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 8796 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 8797 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8798 | |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 8799 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 8800 | role = MBEDTLS_ECJPAKE_SERVER; |
| 8801 | else |
| 8802 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 8803 | |
| 8804 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 8805 | role, |
| 8806 | MBEDTLS_MD_SHA256, |
| 8807 | MBEDTLS_ECP_DP_SECP256R1, |
| 8808 | pw, pw_len ) ); |
| 8809 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 8810 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 8811 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8812 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8813 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 8814 | const unsigned char *psk, size_t psk_len, |
| 8815 | const unsigned char *psk_identity, size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 8816 | { |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8817 | if( psk == NULL || psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8818 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8819 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8820 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 8821 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 8822 | |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 8823 | /* Identity len will be encoded on two bytes */ |
| 8824 | if( ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8825 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 8826 | { |
| 8827 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8828 | } |
| 8829 | |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 8830 | if( conf->psk != NULL ) |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8831 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 8832 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 8833 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 8834 | mbedtls_free( conf->psk ); |
Manuel Pégourié-Gonnard | 173c790 | 2015-10-20 19:56:45 +0200 | [diff] [blame] | 8835 | conf->psk = NULL; |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 8836 | conf->psk_len = 0; |
| 8837 | } |
| 8838 | if( conf->psk_identity != NULL ) |
| 8839 | { |
| 8840 | mbedtls_free( conf->psk_identity ); |
Manuel Pégourié-Gonnard | 173c790 | 2015-10-20 19:56:45 +0200 | [diff] [blame] | 8841 | conf->psk_identity = NULL; |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 8842 | conf->psk_identity_len = 0; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8843 | } |
| 8844 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8845 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL || |
| 8846 | ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL ) |
Mansour Moufid | f81088b | 2015-02-17 13:10:21 -0500 | [diff] [blame] | 8847 | { |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 8848 | mbedtls_free( conf->psk ); |
Manuel Pégourié-Gonnard | 24417f0 | 2015-09-28 18:09:45 +0200 | [diff] [blame] | 8849 | mbedtls_free( conf->psk_identity ); |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 8850 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | 24417f0 | 2015-09-28 18:09:45 +0200 | [diff] [blame] | 8851 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8852 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Mansour Moufid | f81088b | 2015-02-17 13:10:21 -0500 | [diff] [blame] | 8853 | } |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8854 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 8855 | conf->psk_len = psk_len; |
| 8856 | conf->psk_identity_len = psk_identity_len; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8857 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 8858 | memcpy( conf->psk, psk, conf->psk_len ); |
| 8859 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8860 | |
| 8861 | return( 0 ); |
| 8862 | } |
| 8863 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 8864 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 8865 | const unsigned char *psk, size_t psk_len ) |
| 8866 | { |
| 8867 | if( psk == NULL || ssl->handshake == NULL ) |
| 8868 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8869 | |
| 8870 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 8871 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8872 | |
| 8873 | if( ssl->handshake->psk != NULL ) |
Andres Amaya Garcia | a004988 | 2017-06-26 11:35:17 +0100 | [diff] [blame] | 8874 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 8875 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 8876 | ssl->handshake->psk_len ); |
Simon Butcher | 5b8d1d6 | 2015-10-04 22:06:51 +0100 | [diff] [blame] | 8877 | mbedtls_free( ssl->handshake->psk ); |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 8878 | ssl->handshake->psk_len = 0; |
Andres Amaya Garcia | a004988 | 2017-06-26 11:35:17 +0100 | [diff] [blame] | 8879 | } |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 8880 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8881 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8882 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 8883 | |
| 8884 | ssl->handshake->psk_len = psk_len; |
| 8885 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 8886 | |
| 8887 | return( 0 ); |
| 8888 | } |
| 8889 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8890 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8891 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8892 | size_t), |
| 8893 | void *p_psk ) |
| 8894 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8895 | conf->f_psk = f_psk; |
| 8896 | conf->p_psk = p_psk; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 8897 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8898 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 8899 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 8900 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 8901 | |
| 8902 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8903 | int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8904 | { |
| 8905 | int ret; |
| 8906 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 8907 | if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 || |
| 8908 | ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 ) |
| 8909 | { |
| 8910 | mbedtls_mpi_free( &conf->dhm_P ); |
| 8911 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8912 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 8913 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8914 | |
| 8915 | return( 0 ); |
| 8916 | } |
Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 8917 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8918 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 8919 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 8920 | const unsigned char *dhm_P, size_t P_len, |
| 8921 | const unsigned char *dhm_G, size_t G_len ) |
| 8922 | { |
| 8923 | int ret; |
| 8924 | |
| 8925 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 8926 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 8927 | { |
| 8928 | mbedtls_mpi_free( &conf->dhm_P ); |
| 8929 | mbedtls_mpi_free( &conf->dhm_G ); |
| 8930 | return( ret ); |
| 8931 | } |
| 8932 | |
| 8933 | return( 0 ); |
| 8934 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8935 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8936 | int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx ) |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 8937 | { |
| 8938 | int ret; |
| 8939 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 8940 | if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 || |
| 8941 | ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 ) |
| 8942 | { |
| 8943 | mbedtls_mpi_free( &conf->dhm_P ); |
| 8944 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 8945 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 8946 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 8947 | |
| 8948 | return( 0 ); |
| 8949 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 8950 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 8951 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 8952 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 8953 | /* |
| 8954 | * Set the minimum length for Diffie-Hellman parameters |
| 8955 | */ |
| 8956 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 8957 | unsigned int bitlen ) |
| 8958 | { |
| 8959 | conf->dhm_min_bitlen = bitlen; |
| 8960 | } |
| 8961 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 8962 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 8963 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 8964 | /* |
| 8965 | * Set allowed/preferred hashes for handshake signatures |
| 8966 | */ |
| 8967 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 8968 | const int *hashes ) |
| 8969 | { |
Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 8970 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 8971 | conf->sig_hashes = hashes; |
Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 8972 | #else |
| 8973 | ((void) conf); |
| 8974 | ((void) hashes); |
| 8975 | #endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 8976 | } |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8977 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 8978 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 8979 | #if defined(MBEDTLS_ECP_C) |
Hanno Becker | c1096e7 | 2019-06-19 12:30:41 +0100 | [diff] [blame] | 8980 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_EC) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 8981 | /* |
| 8982 | * Set the allowed elliptic curves |
| 8983 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8984 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8985 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 8986 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8987 | conf->curve_list = curve_list; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 8988 | } |
Hanno Becker | c1096e7 | 2019-06-19 12:30:41 +0100 | [diff] [blame] | 8989 | #endif /* MBEDTLS_SSL_CONF_SINGLE_EC */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8990 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 8991 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 8992 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8993 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8994 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8995 | /* Initialize to suppress unnecessary compiler warning */ |
| 8996 | size_t hostname_len = 0; |
| 8997 | |
| 8998 | /* Check if new hostname is valid before |
| 8999 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9000 | if( hostname != NULL ) |
| 9001 | { |
| 9002 | hostname_len = strlen( hostname ); |
| 9003 | |
| 9004 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 9005 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9006 | } |
| 9007 | |
| 9008 | /* Now it's clear that we will overwrite the old hostname, |
| 9009 | * so we can free it safely */ |
| 9010 | |
| 9011 | if( ssl->hostname != NULL ) |
| 9012 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 9013 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9014 | mbedtls_free( ssl->hostname ); |
| 9015 | } |
| 9016 | |
| 9017 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 9018 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9019 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9020 | { |
| 9021 | ssl->hostname = NULL; |
| 9022 | } |
| 9023 | else |
| 9024 | { |
| 9025 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9026 | if( ssl->hostname == NULL ) |
| 9027 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 9028 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9029 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 9030 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9031 | ssl->hostname[hostname_len] = '\0'; |
| 9032 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9033 | |
| 9034 | return( 0 ); |
| 9035 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 9036 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9037 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 9038 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9039 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9040 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 9041 | const unsigned char *, size_t), |
| 9042 | void *p_sni ) |
| 9043 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9044 | conf->f_sni = f_sni; |
| 9045 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 9046 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9047 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 9048 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9049 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9050 | int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos ) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9051 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9052 | size_t cur_len, tot_len; |
| 9053 | const char **p; |
| 9054 | |
| 9055 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 9056 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 9057 | * MUST NOT be truncated." |
| 9058 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9059 | */ |
| 9060 | tot_len = 0; |
| 9061 | for( p = protos; *p != NULL; p++ ) |
| 9062 | { |
| 9063 | cur_len = strlen( *p ); |
| 9064 | tot_len += cur_len; |
| 9065 | |
| 9066 | if( cur_len == 0 || cur_len > 255 || tot_len > 65535 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9067 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9068 | } |
| 9069 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9070 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9071 | |
| 9072 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9073 | } |
| 9074 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9075 | const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9076 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9077 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9078 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9079 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9080 | |
Hanno Becker | 33b9b25 | 2019-07-05 11:23:25 +0100 | [diff] [blame] | 9081 | #if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \ |
| 9082 | !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER) |
| 9083 | void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, |
| 9084 | int major, int minor ) |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 9085 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9086 | conf->max_major_ver = major; |
| 9087 | conf->max_minor_ver = minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 9088 | } |
Hanno Becker | 33b9b25 | 2019-07-05 11:23:25 +0100 | [diff] [blame] | 9089 | #endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER || |
| 9090 | MBEDTLS_SSL_CONF_MAX_MAJOR_VER */ |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 9091 | |
Hanno Becker | 33b9b25 | 2019-07-05 11:23:25 +0100 | [diff] [blame] | 9092 | #if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \ |
| 9093 | !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER) |
| 9094 | void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, |
| 9095 | int major, int minor ) |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 9096 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9097 | conf->min_major_ver = major; |
| 9098 | conf->min_minor_ver = minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 9099 | } |
Hanno Becker | 33b9b25 | 2019-07-05 11:23:25 +0100 | [diff] [blame] | 9100 | #endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER || |
| 9101 | MBEDTLS_SSL_CONF_MIN_MAJOR_VER */ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 9102 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9103 | #if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9104 | void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback ) |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 9105 | { |
Manuel Pégourié-Gonnard | 684b059 | 2015-05-06 09:27:31 +0100 | [diff] [blame] | 9106 | conf->fallback = fallback; |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 9107 | } |
| 9108 | #endif |
| 9109 | |
Hanno Becker | c2cfdaa | 2019-06-13 12:33:03 +0100 | [diff] [blame] | 9110 | #if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST) |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 9111 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 9112 | char cert_req_ca_list ) |
| 9113 | { |
| 9114 | conf->cert_req_ca_list = cert_req_ca_list; |
| 9115 | } |
| 9116 | #endif |
| 9117 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9118 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9119 | void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm ) |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 9120 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9121 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 9122 | } |
| 9123 | #endif |
| 9124 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9125 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Hanno Becker | f765ce6 | 2019-06-21 13:17:14 +0100 | [diff] [blame] | 9126 | #if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9127 | void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems ) |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 9128 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9129 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 9130 | } |
Hanno Becker | f765ce6 | 2019-06-21 13:17:14 +0100 | [diff] [blame] | 9131 | #endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */ |
| 9132 | #if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET) |
Jarno Lamsa | 7a5e2be | 2019-06-10 10:13:03 +0300 | [diff] [blame] | 9133 | void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf, |
Jarno Lamsa | 842be16 | 2019-06-10 15:05:33 +0300 | [diff] [blame] | 9134 | char ems_enf ) |
Jarno Lamsa | 7a5e2be | 2019-06-10 10:13:03 +0300 | [diff] [blame] | 9135 | { |
| 9136 | conf->enforce_extended_master_secret = ems_enf; |
| 9137 | } |
Hanno Becker | f765ce6 | 2019-06-21 13:17:14 +0100 | [diff] [blame] | 9138 | #endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */ |
Hanno Becker | aabbb58 | 2019-06-11 13:43:27 +0100 | [diff] [blame] | 9139 | #endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 9140 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 9141 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9142 | void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 ) |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 9143 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9144 | conf->arc4_disabled = arc4; |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 9145 | } |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 9146 | #endif |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 9147 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9148 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9149 | int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code ) |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9150 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9151 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9152 | ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9153 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9154 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9155 | } |
| 9156 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 9157 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9158 | |
| 9159 | return( 0 ); |
| 9160 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9161 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9162 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9163 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 9164 | void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate ) |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 9165 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9166 | conf->trunc_hmac = truncate; |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 9167 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9168 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 9169 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9170 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9171 | void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split ) |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 9172 | { |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 9173 | conf->cbc_record_splitting = split; |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 9174 | } |
| 9175 | #endif |
| 9176 | |
Hanno Becker | b0b2b67 | 2019-06-12 16:58:10 +0100 | [diff] [blame] | 9177 | #if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9178 | void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9179 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9180 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9181 | } |
Hanno Becker | b0b2b67 | 2019-06-12 16:58:10 +0100 | [diff] [blame] | 9182 | #endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9183 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9184 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9185 | void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation ) |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 9186 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9187 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 9188 | } |
| 9189 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9190 | void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records ) |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 9191 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9192 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 9193 | } |
| 9194 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9195 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 9196 | const unsigned char period[8] ) |
| 9197 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9198 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 9199 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9200 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9201 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9202 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9203 | #if defined(MBEDTLS_SSL_CLI_C) |
| 9204 | void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets ) |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 9205 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 9206 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 9207 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9208 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 9209 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9210 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 9211 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 9212 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 9213 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 9214 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 9215 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 9216 | conf->f_ticket_write = f_ticket_write; |
| 9217 | conf->f_ticket_parse = f_ticket_parse; |
| 9218 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 9219 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9220 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9221 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 9222 | |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 9223 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 9224 | void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf, |
| 9225 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 9226 | void *p_export_keys ) |
| 9227 | { |
| 9228 | conf->f_export_keys = f_export_keys; |
| 9229 | conf->p_export_keys = p_export_keys; |
| 9230 | } |
| 9231 | #endif |
| 9232 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 9233 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9234 | void mbedtls_ssl_conf_async_private_cb( |
| 9235 | mbedtls_ssl_config *conf, |
| 9236 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 9237 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 9238 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 9239 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9240 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9241 | { |
| 9242 | conf->f_async_sign_start = f_async_sign; |
| 9243 | conf->f_async_decrypt_start = f_async_decrypt; |
| 9244 | conf->f_async_resume = f_async_resume; |
| 9245 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9246 | conf->p_async_config_data = async_config_data; |
| 9247 | } |
| 9248 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 9249 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 9250 | { |
| 9251 | return( conf->p_async_config_data ); |
| 9252 | } |
| 9253 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 9254 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9255 | { |
| 9256 | if( ssl->handshake == NULL ) |
| 9257 | return( NULL ); |
| 9258 | else |
| 9259 | return( ssl->handshake->user_async_ctx ); |
| 9260 | } |
| 9261 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 9262 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9263 | void *ctx ) |
| 9264 | { |
| 9265 | if( ssl->handshake != NULL ) |
| 9266 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9267 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 9268 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9269 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9270 | /* |
| 9271 | * SSL get accessors |
| 9272 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9273 | size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9274 | { |
| 9275 | return( ssl->in_offt == NULL ? 0 : ssl->in_msglen ); |
| 9276 | } |
| 9277 | |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9278 | int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl ) |
| 9279 | { |
| 9280 | /* |
| 9281 | * Case A: We're currently holding back |
| 9282 | * a message for further processing. |
| 9283 | */ |
| 9284 | |
| 9285 | if( ssl->keep_current_message == 1 ) |
| 9286 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9287 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) ); |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9288 | return( 1 ); |
| 9289 | } |
| 9290 | |
| 9291 | /* |
| 9292 | * Case B: Further records are pending in the current datagram. |
| 9293 | */ |
| 9294 | |
| 9295 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 9296 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9297 | ssl->in_left > ssl->next_record_offset ) |
| 9298 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9299 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) ); |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9300 | return( 1 ); |
| 9301 | } |
| 9302 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 9303 | |
| 9304 | /* |
| 9305 | * Case C: A handshake message is being processed. |
| 9306 | */ |
| 9307 | |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9308 | if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen ) |
| 9309 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9310 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) ); |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9311 | return( 1 ); |
| 9312 | } |
| 9313 | |
| 9314 | /* |
| 9315 | * Case D: An application data message is being processed |
| 9316 | */ |
| 9317 | if( ssl->in_offt != NULL ) |
| 9318 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9319 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) ); |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9320 | return( 1 ); |
| 9321 | } |
| 9322 | |
| 9323 | /* |
| 9324 | * In all other cases, the rest of the message can be dropped. |
Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 9325 | * As in ssl_get_next_record, this needs to be adapted if |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9326 | * we implement support for multiple alerts in single records. |
| 9327 | */ |
| 9328 | |
| 9329 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) ); |
| 9330 | return( 0 ); |
| 9331 | } |
| 9332 | |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 9333 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9334 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 9335 | if( ssl->session != NULL ) |
| 9336 | return( ssl->session->verify_result ); |
| 9337 | |
| 9338 | if( ssl->session_negotiate != NULL ) |
| 9339 | return( ssl->session_negotiate->verify_result ); |
| 9340 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 9341 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9342 | } |
| 9343 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9344 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 9345 | { |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 9346 | int suite; |
| 9347 | |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 9348 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9349 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 9350 | |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 9351 | suite = mbedtls_ssl_session_get_ciphersuite( ssl->session ); |
| 9352 | return( mbedtls_ssl_get_ciphersuite_name( suite ) ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 9353 | } |
| 9354 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9355 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9356 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9357 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 9358 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9359 | { |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 9360 | switch( mbedtls_ssl_get_minor_ver( ssl ) ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9361 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9362 | case MBEDTLS_SSL_MINOR_VERSION_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9363 | return( "DTLSv1.0" ); |
| 9364 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9365 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9366 | return( "DTLSv1.2" ); |
| 9367 | |
| 9368 | default: |
| 9369 | return( "unknown (DTLS)" ); |
| 9370 | } |
| 9371 | } |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9372 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 9373 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 9374 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9375 | { |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 9376 | switch( mbedtls_ssl_get_minor_ver( ssl ) ) |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9377 | { |
| 9378 | case MBEDTLS_SSL_MINOR_VERSION_0: |
| 9379 | return( "SSLv3.0" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9380 | |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9381 | case MBEDTLS_SSL_MINOR_VERSION_1: |
| 9382 | return( "TLSv1.0" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9383 | |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9384 | case MBEDTLS_SSL_MINOR_VERSION_2: |
| 9385 | return( "TLSv1.1" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9386 | |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9387 | case MBEDTLS_SSL_MINOR_VERSION_3: |
| 9388 | return( "TLSv1.2" ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 9389 | |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9390 | default: |
| 9391 | return( "unknown" ); |
| 9392 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9393 | } |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9394 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9395 | } |
| 9396 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9397 | int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9398 | { |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9399 | size_t transform_expansion = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9400 | const mbedtls_ssl_transform *transform = ssl->transform_out; |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9401 | unsigned block_size; |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9402 | |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 9403 | size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl ); |
| 9404 | |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 9405 | if( transform == NULL ) |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 9406 | return( (int) out_hdr_len ); |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 9407 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9408 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 9409 | if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL ) |
| 9410 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9411 | #endif |
| 9412 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9413 | switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) ) |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9414 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9415 | case MBEDTLS_MODE_GCM: |
| 9416 | case MBEDTLS_MODE_CCM: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9417 | case MBEDTLS_MODE_CHACHAPOLY: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9418 | case MBEDTLS_MODE_STREAM: |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9419 | transform_expansion = transform->minlen; |
| 9420 | break; |
| 9421 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9422 | case MBEDTLS_MODE_CBC: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9423 | |
| 9424 | block_size = mbedtls_cipher_get_block_size( |
| 9425 | &transform->cipher_ctx_enc ); |
| 9426 | |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9427 | /* Expansion due to the addition of the MAC. */ |
| 9428 | transform_expansion += transform->maclen; |
| 9429 | |
| 9430 | /* Expansion due to the addition of CBC padding; |
| 9431 | * Theoretically up to 256 bytes, but we never use |
| 9432 | * more than the block size of the underlying cipher. */ |
| 9433 | transform_expansion += block_size; |
| 9434 | |
| 9435 | /* For TLS 1.1 or higher, an explicit IV is added |
| 9436 | * after the record header. */ |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9437 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 9438 | if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9439 | transform_expansion += block_size; |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9440 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9441 | |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9442 | break; |
| 9443 | |
| 9444 | default: |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 9445 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9446 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9447 | } |
| 9448 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 9449 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | add0190 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 9450 | if( transform->out_cid_len != 0 ) |
| 9451 | transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 9452 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | add0190 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 9453 | |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 9454 | return( (int)( out_hdr_len + transform_expansion ) ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9455 | } |
| 9456 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9457 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 9458 | size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 9459 | { |
| 9460 | size_t max_len; |
| 9461 | |
| 9462 | /* |
| 9463 | * Assume mfl_code is correct since it was checked when set |
| 9464 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9465 | max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9466 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 9467 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9468 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9469 | ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len ) |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9470 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9471 | max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9472 | } |
| 9473 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 9474 | /* During a handshake, use the value being negotiated */ |
| 9475 | if( ssl->session_negotiate != NULL && |
| 9476 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 9477 | { |
| 9478 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 9479 | } |
| 9480 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9481 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9482 | } |
| 9483 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 9484 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9485 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9486 | static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl ) |
| 9487 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 9488 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 9489 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT && |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 9490 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 9491 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 9492 | return ( 0 ); |
| 9493 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9494 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 9495 | return( ssl->mtu ); |
| 9496 | |
| 9497 | if( ssl->mtu == 0 ) |
| 9498 | return( ssl->handshake->mtu ); |
| 9499 | |
| 9500 | return( ssl->mtu < ssl->handshake->mtu ? |
| 9501 | ssl->mtu : ssl->handshake->mtu ); |
| 9502 | } |
| 9503 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 9504 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9505 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 9506 | { |
| 9507 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 9508 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 9509 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 9510 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9511 | (void) ssl; |
| 9512 | #endif |
| 9513 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9514 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 9515 | const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl ); |
| 9516 | |
| 9517 | if( max_len > mfl ) |
| 9518 | max_len = mfl; |
| 9519 | #endif |
| 9520 | |
| 9521 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9522 | if( ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9523 | { |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9524 | const size_t mtu = ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9525 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 9526 | const size_t overhead = (size_t) ret; |
| 9527 | |
| 9528 | if( ret < 0 ) |
| 9529 | return( ret ); |
| 9530 | |
| 9531 | if( mtu <= overhead ) |
| 9532 | { |
| 9533 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 9534 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 9535 | } |
| 9536 | |
| 9537 | if( max_len > mtu - overhead ) |
| 9538 | max_len = mtu - overhead; |
| 9539 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9540 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9541 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 9542 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 9543 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9544 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9545 | #endif |
| 9546 | |
| 9547 | return( (int) max_len ); |
| 9548 | } |
| 9549 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9550 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 9551 | const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl ) |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9552 | { |
| 9553 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9554 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9555 | |
Hanno Becker | bfab9df | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 9556 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9557 | return( ssl->session->peer_cert ); |
Hanno Becker | bfab9df | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 9558 | #else |
| 9559 | return( NULL ); |
| 9560 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9561 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9562 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9563 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9564 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 933b9fc | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 9565 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 9566 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9567 | { |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9568 | if( ssl == NULL || |
| 9569 | dst == NULL || |
| 9570 | ssl->session == NULL || |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 9571 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9572 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9573 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9574 | } |
| 9575 | |
Hanno Becker | 58fccf2 | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 9576 | return( mbedtls_ssl_session_copy( dst, ssl->session ) ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9577 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9578 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9579 | |
Manuel Pégourié-Gonnard | 37a5324 | 2019-05-20 11:12:28 +0200 | [diff] [blame] | 9580 | const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl ) |
| 9581 | { |
| 9582 | if( ssl == NULL ) |
| 9583 | return( NULL ); |
| 9584 | |
| 9585 | return( ssl->session ); |
| 9586 | } |
| 9587 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9588 | /* |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9589 | * Define ticket header determining Mbed TLS version |
| 9590 | * and structure of the ticket. |
| 9591 | */ |
| 9592 | |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9593 | /* |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9594 | * Define bitflag determining compile-time settings influencing |
| 9595 | * structure of serialized SSL sessions. |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9596 | */ |
| 9597 | |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9598 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9599 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9600 | #else |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9601 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9602 | #endif /* MBEDTLS_HAVE_TIME */ |
| 9603 | |
| 9604 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9605 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9606 | #else |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9607 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9608 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 9609 | |
| 9610 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9611 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9612 | #else |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9613 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9614 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 9615 | |
| 9616 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9617 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9618 | #else |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9619 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9620 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 9621 | |
| 9622 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9623 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9624 | #else |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9625 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9626 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
| 9627 | |
| 9628 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9629 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9630 | #else |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9631 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9632 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 9633 | |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9634 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 9635 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 9636 | #else |
| 9637 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 9638 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 9639 | |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 9640 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 9641 | #define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1 |
| 9642 | #else |
| 9643 | #define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0 |
| 9644 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 9645 | |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9646 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 9647 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 9648 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 9649 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
| 9650 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4 |
| 9651 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5 |
| 9652 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6 |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 9653 | #define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7 |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9654 | |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9655 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9656 | ( (uint16_t) ( \ |
| 9657 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 9658 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 9659 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 9660 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
| 9661 | ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \ |
| 9662 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 9663 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \ |
| 9664 | ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) ) |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9665 | |
Hanno Becker | 557fe9f | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 9666 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9667 | MBEDTLS_VERSION_MAJOR, |
| 9668 | MBEDTLS_VERSION_MINOR, |
| 9669 | MBEDTLS_VERSION_PATCH, |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9670 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF, |
| 9671 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF, |
Hanno Becker | 557fe9f | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 9672 | }; |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9673 | |
| 9674 | /* |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9675 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9676 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 9677 | * |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9678 | * opaque mbedtls_version[3]; // major, minor, patch |
| 9679 | * opaque session_format[2]; // version-specific 16-bit field determining |
| 9680 | * // the format of the remaining |
| 9681 | * // serialized data. |
Hanno Becker | b36db4f | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 9682 | * |
| 9683 | * Note: When updating the format, remember to keep |
| 9684 | * these version+format bytes. |
| 9685 | * |
Hanno Becker | 7bf7710 | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 9686 | * // In this version, `session_format` determines |
| 9687 | * // the setting of those compile-time |
| 9688 | * // configuration options which influence |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9689 | * // the structure of mbedtls_ssl_session. |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9690 | * uint64 start_time; |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9691 | * uint8 ciphersuite[2]; // defined by the standard |
| 9692 | * uint8 compression; // 0 or 1 |
| 9693 | * uint8 session_id_len; // at most 32 |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9694 | * opaque session_id[32]; |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9695 | * opaque master[48]; // fixed length in the standard |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9696 | * uint32 verify_result; |
Hanno Becker | 0528f82 | 2019-06-18 12:45:31 +0100 | [diff] [blame] | 9697 | * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) { |
| 9698 | * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert |
| 9699 | * case disabled: uint8_t peer_cert_digest_type; |
| 9700 | * opaque peer_cert_digest<0..2^8-1>; |
| 9701 | * } |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9702 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9703 | * uint32 ticket_lifetime; |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9704 | * uint8 mfl_code; // up to 255 according to standard |
| 9705 | * uint8 trunc_hmac; // 0 or 1 |
| 9706 | * uint8 encrypt_then_mac; // 0 or 1 |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9707 | * |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9708 | * The order is the same as in the definition of the structure, except |
| 9709 | * verify_result is put before peer_cert so that all mandatory fields come |
| 9710 | * together in one block. |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9711 | */ |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 9712 | static int ssl_session_save( const mbedtls_ssl_session *session, |
| 9713 | unsigned char omit_header, |
| 9714 | unsigned char *buf, |
| 9715 | size_t buf_len, |
| 9716 | size_t *olen ) |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9717 | { |
| 9718 | unsigned char *p = buf; |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9719 | size_t used = 0; |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9720 | #if defined(MBEDTLS_HAVE_TIME) |
| 9721 | uint64_t start; |
| 9722 | #endif |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9723 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 9724 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9725 | size_t cert_len; |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9726 | #endif |
Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 9727 | #endif |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9728 | |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 9729 | if( !omit_header ) |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9730 | { |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 9731 | /* |
| 9732 | * Add version identifier |
| 9733 | */ |
| 9734 | |
| 9735 | used += sizeof( ssl_serialized_session_header ); |
| 9736 | |
| 9737 | if( used <= buf_len ) |
| 9738 | { |
| 9739 | memcpy( p, ssl_serialized_session_header, |
| 9740 | sizeof( ssl_serialized_session_header ) ); |
| 9741 | p += sizeof( ssl_serialized_session_header ); |
| 9742 | } |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9743 | } |
| 9744 | |
| 9745 | /* |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9746 | * Time |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9747 | */ |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9748 | #if defined(MBEDTLS_HAVE_TIME) |
| 9749 | used += 8; |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9750 | |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9751 | if( used <= buf_len ) |
| 9752 | { |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9753 | start = (uint64_t) session->start; |
| 9754 | |
| 9755 | *p++ = (unsigned char)( ( start >> 56 ) & 0xFF ); |
| 9756 | *p++ = (unsigned char)( ( start >> 48 ) & 0xFF ); |
| 9757 | *p++ = (unsigned char)( ( start >> 40 ) & 0xFF ); |
| 9758 | *p++ = (unsigned char)( ( start >> 32 ) & 0xFF ); |
| 9759 | *p++ = (unsigned char)( ( start >> 24 ) & 0xFF ); |
| 9760 | *p++ = (unsigned char)( ( start >> 16 ) & 0xFF ); |
| 9761 | *p++ = (unsigned char)( ( start >> 8 ) & 0xFF ); |
| 9762 | *p++ = (unsigned char)( ( start ) & 0xFF ); |
| 9763 | } |
| 9764 | #endif /* MBEDTLS_HAVE_TIME */ |
| 9765 | |
| 9766 | /* |
| 9767 | * Basic mandatory fields |
| 9768 | */ |
| 9769 | used += 2 /* ciphersuite */ |
| 9770 | + 1 /* compression */ |
| 9771 | + 1 /* id_len */ |
| 9772 | + sizeof( session->id ) |
| 9773 | + sizeof( session->master ) |
| 9774 | + 4; /* verify_result */ |
| 9775 | |
| 9776 | if( used <= buf_len ) |
| 9777 | { |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 9778 | const int ciphersuite = |
| 9779 | mbedtls_ssl_session_get_ciphersuite( session ); |
| 9780 | *p++ = (unsigned char)( ( ciphersuite >> 8 ) & 0xFF ); |
| 9781 | *p++ = (unsigned char)( ( ciphersuite ) & 0xFF ); |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9782 | |
| 9783 | *p++ = (unsigned char)( session->compression & 0xFF ); |
| 9784 | |
| 9785 | *p++ = (unsigned char)( session->id_len & 0xFF ); |
| 9786 | memcpy( p, session->id, 32 ); |
| 9787 | p += 32; |
| 9788 | |
| 9789 | memcpy( p, session->master, 48 ); |
| 9790 | p += 48; |
| 9791 | |
| 9792 | *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF ); |
| 9793 | *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF ); |
| 9794 | *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF ); |
| 9795 | *p++ = (unsigned char)( ( session->verify_result ) & 0xFF ); |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9796 | } |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9797 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9798 | /* |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9799 | * Peer's end-entity certificate |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9800 | */ |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9801 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 9802 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9803 | if( session->peer_cert == NULL ) |
| 9804 | cert_len = 0; |
| 9805 | else |
| 9806 | cert_len = session->peer_cert->raw.len; |
| 9807 | |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9808 | used += 3 + cert_len; |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9809 | |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9810 | if( used <= buf_len ) |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9811 | { |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9812 | *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF ); |
| 9813 | *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF ); |
| 9814 | *p++ = (unsigned char)( ( cert_len ) & 0xFF ); |
| 9815 | |
| 9816 | if( session->peer_cert != NULL ) |
| 9817 | { |
| 9818 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 9819 | p += cert_len; |
| 9820 | } |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9821 | } |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 9822 | |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 9823 | #elif defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 9824 | /* Digest of peer certificate */ |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 9825 | if( session->peer_cert_digest != NULL ) |
| 9826 | { |
| 9827 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 9828 | if( used <= buf_len ) |
| 9829 | { |
| 9830 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 9831 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 9832 | memcpy( p, session->peer_cert_digest, |
| 9833 | session->peer_cert_digest_len ); |
| 9834 | p += session->peer_cert_digest_len; |
| 9835 | } |
| 9836 | } |
| 9837 | else |
| 9838 | { |
| 9839 | used += 2; |
| 9840 | if( used <= buf_len ) |
| 9841 | { |
| 9842 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 9843 | *p++ = 0; |
| 9844 | } |
| 9845 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 9846 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9847 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 9848 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9849 | /* |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9850 | * Session ticket if any, plus associated data |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9851 | */ |
| 9852 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9853 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9854 | |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9855 | if( used <= buf_len ) |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9856 | { |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9857 | *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF ); |
| 9858 | *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF ); |
| 9859 | *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF ); |
| 9860 | |
| 9861 | if( session->ticket != NULL ) |
| 9862 | { |
| 9863 | memcpy( p, session->ticket, session->ticket_len ); |
| 9864 | p += session->ticket_len; |
| 9865 | } |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9866 | |
| 9867 | *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF ); |
| 9868 | *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF ); |
| 9869 | *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF ); |
| 9870 | *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF ); |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9871 | } |
| 9872 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 9873 | |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9874 | /* |
| 9875 | * Misc extension-related info |
| 9876 | */ |
| 9877 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 9878 | used += 1; |
| 9879 | |
| 9880 | if( used <= buf_len ) |
| 9881 | *p++ = session->mfl_code; |
| 9882 | #endif |
| 9883 | |
| 9884 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 9885 | used += 1; |
| 9886 | |
| 9887 | if( used <= buf_len ) |
| 9888 | *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF ); |
| 9889 | #endif |
| 9890 | |
| 9891 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 9892 | used += 1; |
| 9893 | |
| 9894 | if( used <= buf_len ) |
| 9895 | *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF ); |
| 9896 | #endif |
| 9897 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9898 | /* Done */ |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9899 | *olen = used; |
| 9900 | |
| 9901 | if( used > buf_len ) |
| 9902 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9903 | |
| 9904 | return( 0 ); |
| 9905 | } |
| 9906 | |
| 9907 | /* |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 9908 | * Public wrapper for ssl_session_save() |
| 9909 | */ |
| 9910 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 9911 | unsigned char *buf, |
| 9912 | size_t buf_len, |
| 9913 | size_t *olen ) |
| 9914 | { |
| 9915 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); |
| 9916 | } |
| 9917 | |
| 9918 | /* |
Manuel Pégourié-Gonnard | 8175816 | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 9919 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 9920 | * |
| 9921 | * This internal version is wrapped by a public function that cleans up in |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 9922 | * case of error, and has an extra option omit_header. |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9923 | */ |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 9924 | static int ssl_session_load( mbedtls_ssl_session *session, |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 9925 | unsigned char omit_header, |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 9926 | const unsigned char *buf, |
| 9927 | size_t len ) |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9928 | { |
| 9929 | const unsigned char *p = buf; |
| 9930 | const unsigned char * const end = buf + len; |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 9931 | int ciphersuite; |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9932 | #if defined(MBEDTLS_HAVE_TIME) |
| 9933 | uint64_t start; |
| 9934 | #endif |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9935 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 9936 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9937 | size_t cert_len; |
Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 9938 | #endif |
| 9939 | #endif |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9940 | |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 9941 | if( !omit_header ) |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9942 | { |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 9943 | /* |
| 9944 | * Check version identifier |
| 9945 | */ |
| 9946 | |
| 9947 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
| 9948 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9949 | |
| 9950 | if( memcmp( p, ssl_serialized_session_header, |
| 9951 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 9952 | { |
| 9953 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 9954 | } |
| 9955 | p += sizeof( ssl_serialized_session_header ); |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9956 | } |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9957 | |
| 9958 | /* |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9959 | * Time |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9960 | */ |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9961 | #if defined(MBEDTLS_HAVE_TIME) |
| 9962 | if( 8 > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9963 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9964 | |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9965 | start = ( (uint64_t) p[0] << 56 ) | |
| 9966 | ( (uint64_t) p[1] << 48 ) | |
| 9967 | ( (uint64_t) p[2] << 40 ) | |
| 9968 | ( (uint64_t) p[3] << 32 ) | |
| 9969 | ( (uint64_t) p[4] << 24 ) | |
| 9970 | ( (uint64_t) p[5] << 16 ) | |
| 9971 | ( (uint64_t) p[6] << 8 ) | |
| 9972 | ( (uint64_t) p[7] ); |
| 9973 | p += 8; |
| 9974 | |
| 9975 | session->start = (time_t) start; |
| 9976 | #endif /* MBEDTLS_HAVE_TIME */ |
| 9977 | |
| 9978 | /* |
| 9979 | * Basic mandatory fields |
| 9980 | */ |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 9981 | |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9982 | if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
| 9983 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9984 | |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 9985 | ciphersuite = ( p[0] << 8 ) | p[1]; |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9986 | p += 2; |
| 9987 | |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 9988 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 9989 | session->ciphersuite = ciphersuite; |
| 9990 | #else |
| 9991 | if( ciphersuite != |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 9992 | MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) ) |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 9993 | { |
| 9994 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 9995 | } |
| 9996 | #endif |
| 9997 | |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9998 | session->compression = *p++; |
| 9999 | |
| 10000 | session->id_len = *p++; |
| 10001 | memcpy( session->id, p, 32 ); |
| 10002 | p += 32; |
| 10003 | |
| 10004 | memcpy( session->master, p, 48 ); |
| 10005 | p += 48; |
| 10006 | |
| 10007 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 10008 | ( (uint32_t) p[1] << 16 ) | |
| 10009 | ( (uint32_t) p[2] << 8 ) | |
| 10010 | ( (uint32_t) p[3] ); |
| 10011 | p += 4; |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10012 | |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 10013 | /* Immediately clear invalid pointer values that have been read, in case |
| 10014 | * we exit early before we replaced them with valid ones. */ |
| 10015 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 10016 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 10017 | session->peer_cert = NULL; |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 10018 | #elif defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 10019 | session->peer_cert_digest = NULL; |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 10020 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 10021 | #endif |
| 10022 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 10023 | session->ticket = NULL; |
| 10024 | #endif |
| 10025 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10026 | /* |
| 10027 | * Peer certificate |
| 10028 | */ |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10029 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 10030 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10031 | if( 3 > (size_t)( end - p ) ) |
| 10032 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10033 | |
| 10034 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 10035 | p += 3; |
| 10036 | |
| 10037 | if( cert_len == 0 ) |
| 10038 | { |
| 10039 | session->peer_cert = NULL; |
| 10040 | } |
| 10041 | else |
| 10042 | { |
| 10043 | int ret; |
| 10044 | |
| 10045 | if( cert_len > (size_t)( end - p ) ) |
| 10046 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10047 | |
| 10048 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 10049 | |
| 10050 | if( session->peer_cert == NULL ) |
| 10051 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 10052 | |
| 10053 | mbedtls_x509_crt_init( session->peer_cert ); |
| 10054 | |
| 10055 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 10056 | p, cert_len ) ) != 0 ) |
| 10057 | { |
| 10058 | mbedtls_x509_crt_free( session->peer_cert ); |
| 10059 | mbedtls_free( session->peer_cert ); |
| 10060 | session->peer_cert = NULL; |
| 10061 | return( ret ); |
| 10062 | } |
| 10063 | |
| 10064 | p += cert_len; |
| 10065 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 10066 | #elif defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 10067 | /* Deserialize CRT digest from the end of the ticket. */ |
| 10068 | if( 2 > (size_t)( end - p ) ) |
| 10069 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10070 | |
| 10071 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 10072 | session->peer_cert_digest_len = (size_t) *p++; |
| 10073 | |
| 10074 | if( session->peer_cert_digest_len != 0 ) |
| 10075 | { |
Hanno Becker | 2326d20 | 2019-06-06 14:54:55 +0100 | [diff] [blame] | 10076 | const mbedtls_md_info_t *md_info = |
| 10077 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); |
| 10078 | if( md_info == NULL ) |
| 10079 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10080 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) |
| 10081 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10082 | |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 10083 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) |
| 10084 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10085 | |
| 10086 | session->peer_cert_digest = |
| 10087 | mbedtls_calloc( 1, session->peer_cert_digest_len ); |
| 10088 | if( session->peer_cert_digest == NULL ) |
| 10089 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 10090 | |
| 10091 | memcpy( session->peer_cert_digest, p, |
| 10092 | session->peer_cert_digest_len ); |
| 10093 | p += session->peer_cert_digest_len; |
| 10094 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 10095 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10096 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 10097 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10098 | /* |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10099 | * Session ticket and associated data |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10100 | */ |
| 10101 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 10102 | if( 3 > (size_t)( end - p ) ) |
| 10103 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10104 | |
| 10105 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 10106 | p += 3; |
| 10107 | |
| 10108 | if( session->ticket_len != 0 ) |
| 10109 | { |
| 10110 | if( session->ticket_len > (size_t)( end - p ) ) |
| 10111 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10112 | |
| 10113 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 10114 | if( session->ticket == NULL ) |
| 10115 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 10116 | |
| 10117 | memcpy( session->ticket, p, session->ticket_len ); |
| 10118 | p += session->ticket_len; |
| 10119 | } |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10120 | |
| 10121 | if( 4 > (size_t)( end - p ) ) |
| 10122 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10123 | |
| 10124 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 10125 | ( (uint32_t) p[1] << 16 ) | |
| 10126 | ( (uint32_t) p[2] << 8 ) | |
| 10127 | ( (uint32_t) p[3] ); |
| 10128 | p += 4; |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10129 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 10130 | |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10131 | /* |
| 10132 | * Misc extension-related info |
| 10133 | */ |
| 10134 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 10135 | if( 1 > (size_t)( end - p ) ) |
| 10136 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10137 | |
| 10138 | session->mfl_code = *p++; |
| 10139 | #endif |
| 10140 | |
| 10141 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 10142 | if( 1 > (size_t)( end - p ) ) |
| 10143 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10144 | |
| 10145 | session->trunc_hmac = *p++; |
| 10146 | #endif |
| 10147 | |
| 10148 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 10149 | if( 1 > (size_t)( end - p ) ) |
| 10150 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10151 | |
| 10152 | session->encrypt_then_mac = *p++; |
| 10153 | #endif |
| 10154 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10155 | /* Done, should have consumed entire buffer */ |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10156 | if( p != end ) |
| 10157 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10158 | |
| 10159 | return( 0 ); |
| 10160 | } |
| 10161 | |
| 10162 | /* |
Manuel Pégourié-Gonnard | 8175816 | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 10163 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 10164 | */ |
| 10165 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 10166 | const unsigned char *buf, |
| 10167 | size_t len ) |
| 10168 | { |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 10169 | int ret = ssl_session_load( session, 0, buf, len ); |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 10170 | |
| 10171 | if( ret != 0 ) |
| 10172 | mbedtls_ssl_session_free( session ); |
| 10173 | |
| 10174 | return( ret ); |
| 10175 | } |
| 10176 | |
| 10177 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10178 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10179 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10180 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10181 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10182 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10183 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10184 | if( ssl == NULL || ssl->conf == NULL ) |
| 10185 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10186 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10187 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10188 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10189 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10190 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10191 | #if defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10192 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10193 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10194 | #endif |
| 10195 | |
Hanno Becker | b82350b | 2019-07-26 07:24:05 +0100 | [diff] [blame] | 10196 | ssl_send_pending_fatal_alert( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10197 | return( ret ); |
| 10198 | } |
| 10199 | |
| 10200 | /* |
| 10201 | * Perform the SSL handshake |
| 10202 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10203 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10204 | { |
| 10205 | int ret = 0; |
| 10206 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10207 | if( ssl == NULL || ssl->conf == NULL ) |
| 10208 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10209 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10210 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10211 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10212 | while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10213 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10214 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10215 | |
| 10216 | if( ret != 0 ) |
| 10217 | break; |
| 10218 | } |
| 10219 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10220 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10221 | |
| 10222 | return( ret ); |
| 10223 | } |
| 10224 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10225 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 10226 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10227 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10228 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10229 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10230 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10231 | { |
| 10232 | int ret; |
| 10233 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10234 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10235 | |
| 10236 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10237 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 10238 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10239 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 10240 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10241 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 10242 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10243 | return( ret ); |
| 10244 | } |
| 10245 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10246 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10247 | |
| 10248 | return( 0 ); |
| 10249 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10250 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10251 | |
| 10252 | /* |
| 10253 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10254 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 10255 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 10256 | * - server: receiving any handshake message on server during mbedtls_ssl_read() after |
Manuel Pégourié-Gonnard | 55e4ff2 | 2014-08-19 11:16:35 +0200 | [diff] [blame] | 10257 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10258 | * If the handshake doesn't complete due to waiting for I/O, it will continue |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10259 | * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively. |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10260 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10261 | static int ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10262 | { |
| 10263 | int ret; |
| 10264 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10265 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10266 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10267 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 10268 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10269 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 10270 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 10271 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10272 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 10273 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10274 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 10275 | { |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10276 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 10277 | MBEDTLS_SSL_IS_SERVER ) |
| 10278 | { |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 10279 | ssl->handshake->out_msg_seq = 1; |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10280 | } |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 10281 | else |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10282 | { |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 10283 | ssl->handshake->in_msg_seq = 1; |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10284 | } |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 10285 | } |
| 10286 | #endif |
| 10287 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10288 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 10289 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10290 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10291 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10292 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10293 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10294 | return( ret ); |
| 10295 | } |
| 10296 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10297 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10298 | |
| 10299 | return( 0 ); |
| 10300 | } |
| 10301 | |
| 10302 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10303 | * Renegotiate current connection on client, |
| 10304 | * or request renegotiation on server |
| 10305 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10306 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10307 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10308 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10309 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10310 | if( ssl == NULL || ssl->conf == NULL ) |
| 10311 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10312 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10313 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10314 | /* On server, just send the request */ |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10315 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10316 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10317 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 10318 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10319 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10320 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 10321 | |
| 10322 | /* Did we already try/start sending HelloRequest? */ |
| 10323 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10324 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 10325 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10326 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10327 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10328 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10329 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10330 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10331 | /* |
| 10332 | * On client, either start the renegotiation process or, |
| 10333 | * if already in progress, continue the handshake |
| 10334 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10335 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10336 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10337 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 10338 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10339 | |
| 10340 | if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 ) |
| 10341 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10342 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10343 | return( ret ); |
| 10344 | } |
| 10345 | } |
| 10346 | else |
| 10347 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10348 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10349 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10350 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10351 | return( ret ); |
| 10352 | } |
| 10353 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10354 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10355 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 10356 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10357 | } |
| 10358 | |
| 10359 | /* |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10360 | * Check record counters and renegotiate if they're above the limit. |
| 10361 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10362 | static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10363 | { |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 10364 | size_t ep_len = ssl_ep_len( ssl ); |
| 10365 | int in_ctr_cmp; |
| 10366 | int out_ctr_cmp; |
| 10367 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10368 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER || |
| 10369 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING || |
Manuel Pégourié-Gonnard | 18332c5 | 2019-07-29 12:17:52 +0200 | [diff] [blame] | 10370 | ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10371 | { |
| 10372 | return( 0 ); |
| 10373 | } |
| 10374 | |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 10375 | in_ctr_cmp = memcmp( ssl->in_ctr + ep_len, |
| 10376 | ssl->conf->renego_period + ep_len, 8 - ep_len ); |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 10377 | out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len, |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 10378 | ssl->conf->renego_period + ep_len, 8 - ep_len ); |
| 10379 | |
| 10380 | if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10381 | { |
| 10382 | return( 0 ); |
| 10383 | } |
| 10384 | |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 10385 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10386 | return( mbedtls_ssl_renegotiate( ssl ) ); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10387 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10388 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10389 | |
| 10390 | /* |
| 10391 | * Receive application data decrypted from the SSL layer |
| 10392 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10393 | int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10394 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10395 | int ret; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 10396 | size_t n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10397 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10398 | if( ssl == NULL || ssl->conf == NULL ) |
| 10399 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10400 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10401 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10402 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10403 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 10404 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 10405 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10406 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 10407 | return( ret ); |
| 10408 | |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10409 | if( ssl->handshake != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10410 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10411 | { |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 10412 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10413 | return( ret ); |
| 10414 | } |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 10415 | } |
| 10416 | #endif |
| 10417 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10418 | /* |
| 10419 | * Check if renegotiation is necessary and/or handshake is |
| 10420 | * in process. If yes, perform/continue, and fall through |
| 10421 | * if an unexpected packet is received while the client |
| 10422 | * is waiting for the ServerHello. |
| 10423 | * |
| 10424 | * (There is no equivalent to the last condition on |
| 10425 | * the server-side as it is not treated as within |
| 10426 | * a handshake while waiting for the ClientHello |
| 10427 | * after a renegotiation request.) |
| 10428 | */ |
| 10429 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10430 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10431 | ret = ssl_check_ctr_renegotiate( ssl ); |
| 10432 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 10433 | ret != 0 ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10434 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10435 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10436 | return( ret ); |
| 10437 | } |
| 10438 | #endif |
| 10439 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10440 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10441 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10442 | ret = mbedtls_ssl_handshake( ssl ); |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10443 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 10444 | ret != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10445 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10446 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10447 | return( ret ); |
| 10448 | } |
| 10449 | } |
| 10450 | |
Hanno Becker | e41158b | 2017-10-23 13:30:32 +0100 | [diff] [blame] | 10451 | /* Loop as long as no application data record is available */ |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10452 | while( ssl->in_offt == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10453 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 10454 | /* Start timer if not already running */ |
Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 10455 | if( mbedtls_ssl_get_get_timer( ssl ) != NULL && |
| 10456 | mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 ) |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 10457 | { |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 10458 | ssl_set_timer( ssl, |
| 10459 | mbedtls_ssl_conf_get_read_timeout( ssl->conf ) ); |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 10460 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 10461 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 10462 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10463 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10464 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) |
| 10465 | return( 0 ); |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 10466 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10467 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 10468 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10469 | } |
| 10470 | |
| 10471 | if( ssl->in_msglen == 0 && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10472 | ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10473 | { |
| 10474 | /* |
| 10475 | * OpenSSL sends empty messages to randomize the IV |
| 10476 | */ |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 10477 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10478 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10479 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 10480 | return( 0 ); |
| 10481 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10482 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10483 | return( ret ); |
| 10484 | } |
| 10485 | } |
| 10486 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10487 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10488 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10489 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10490 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10491 | /* |
| 10492 | * - For client-side, expect SERVER_HELLO_REQUEST. |
| 10493 | * - For server-side, expect CLIENT_HELLO. |
| 10494 | * - Fail (TLS) or silently drop record (DTLS) in other cases. |
| 10495 | */ |
| 10496 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10497 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10498 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 10499 | MBEDTLS_SSL_IS_CLIENT && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10500 | ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST || |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10501 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10502 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10503 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10504 | |
| 10505 | /* With DTLS, drop the packet (probably from last handshake) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10506 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 10507 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10508 | { |
| 10509 | continue; |
| 10510 | } |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 10511 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10512 | #endif |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 10513 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 10514 | { |
| 10515 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 10516 | } |
| 10517 | #endif |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10518 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10519 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10520 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10521 | #if defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10522 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 10523 | MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10524 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10525 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10526 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10527 | |
| 10528 | /* With DTLS, drop the packet (probably from last handshake) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10529 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 10530 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10531 | { |
| 10532 | continue; |
| 10533 | } |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 10534 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10535 | #endif |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 10536 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 10537 | { |
| 10538 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 10539 | } |
| 10540 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10541 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10542 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 10543 | |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 10544 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10545 | /* Determine whether renegotiation attempt should be accepted */ |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 10546 | if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
| 10547 | ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
Hanno Becker | b0b2b67 | 2019-06-12 16:58:10 +0100 | [diff] [blame] | 10548 | mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) == |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 10549 | MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) ) |
| 10550 | { |
| 10551 | /* |
| 10552 | * Accept renegotiation request |
| 10553 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10554 | |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 10555 | /* DTLS clients need to know renego is server-initiated */ |
| 10556 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 10557 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10558 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 10559 | MBEDTLS_SSL_IS_CLIENT ) |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 10560 | { |
| 10561 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
| 10562 | } |
| 10563 | #endif |
| 10564 | ret = ssl_start_renegotiation( ssl ); |
| 10565 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 10566 | ret != 0 ) |
| 10567 | { |
| 10568 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret ); |
| 10569 | return( ret ); |
| 10570 | } |
| 10571 | } |
| 10572 | else |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 10573 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10574 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10575 | /* |
| 10576 | * Refuse renegotiation |
| 10577 | */ |
| 10578 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10579 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10580 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10581 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 10582 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10583 | { |
Gilles Peskine | 92e4426 | 2017-05-10 17:27:49 +0200 | [diff] [blame] | 10584 | /* SSLv3 does not have a "no_renegotiation" warning, so |
| 10585 | we send a fatal alert and abort the connection. */ |
| 10586 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 10587 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 10588 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 10589 | } |
| 10590 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10591 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 10592 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 10593 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 10594 | if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 10595 | { |
Hanno Becker | 2e8d133 | 2019-07-25 10:27:36 +0100 | [diff] [blame] | 10596 | ret = mbedtls_ssl_send_alert_message( ssl, |
| 10597 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 10598 | MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ); |
| 10599 | if( ret != 0 ) |
| 10600 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10601 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 10602 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10603 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || |
| 10604 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 10605 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10606 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 10607 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 10608 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10609 | } |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 10610 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10611 | /* At this point, we don't know whether the renegotiation has been |
| 10612 | * completed or not. The cases to consider are the following: |
| 10613 | * 1) The renegotiation is complete. In this case, no new record |
| 10614 | * has been read yet. |
| 10615 | * 2) The renegotiation is incomplete because the client received |
| 10616 | * an application data record while awaiting the ServerHello. |
| 10617 | * 3) The renegotiation is incomplete because the client received |
| 10618 | * a non-handshake, non-application data message while awaiting |
| 10619 | * the ServerHello. |
| 10620 | * In each of these case, looping will be the proper action: |
| 10621 | * - For 1), the next iteration will read a new record and check |
| 10622 | * if it's application data. |
| 10623 | * - For 2), the loop condition isn't satisfied as application data |
| 10624 | * is present, hence continue is the same as break |
| 10625 | * - For 3), the loop condition is satisfied and read_record |
| 10626 | * will re-deliver the message that was held back by the client |
| 10627 | * when expecting the ServerHello. |
| 10628 | */ |
| 10629 | continue; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10630 | } |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 10631 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10632 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 10633 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10634 | if( ssl->conf->renego_max_records >= 0 ) |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 10635 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10636 | if( ++ssl->renego_records_seen > ssl->conf->renego_max_records ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10637 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10638 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, " |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10639 | "but not honored by client" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10640 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10641 | } |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 10642 | } |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 10643 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10644 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 10645 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10646 | /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */ |
| 10647 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 10648 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10649 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 10650 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 10651 | } |
| 10652 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10653 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10654 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10655 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) ); |
| 10656 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10657 | } |
| 10658 | |
| 10659 | ssl->in_offt = ssl->in_msg; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 10660 | |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 10661 | /* We're going to return something now, cancel timer, |
| 10662 | * except if handshake (renegotiation) is in progress */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10663 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 10664 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10665 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 10666 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10667 | /* If we requested renego but received AppData, resend HelloRequest. |
| 10668 | * Do it now, after setting in_offt, to avoid taking this branch |
| 10669 | * again if ssl_write_hello_request() returns WANT_WRITE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10670 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10671 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 10672 | MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10673 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10674 | { |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10675 | if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10676 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10677 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10678 | return( ret ); |
| 10679 | } |
| 10680 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10681 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10682 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10683 | } |
| 10684 | |
| 10685 | n = ( len < ssl->in_msglen ) |
| 10686 | ? len : ssl->in_msglen; |
| 10687 | |
| 10688 | memcpy( buf, ssl->in_offt, n ); |
| 10689 | ssl->in_msglen -= n; |
| 10690 | |
| 10691 | if( ssl->in_msglen == 0 ) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10692 | { |
| 10693 | /* all bytes consumed */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10694 | ssl->in_offt = NULL; |
Hanno Becker | bdf3905 | 2017-06-09 10:42:03 +0100 | [diff] [blame] | 10695 | ssl->keep_current_message = 0; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10696 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10697 | else |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10698 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10699 | /* more data available */ |
| 10700 | ssl->in_offt += n; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10701 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10702 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10703 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10704 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 10705 | return( (int) n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10706 | } |
| 10707 | |
| 10708 | /* |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 10709 | * Send application data to be encrypted by the SSL layer, taking care of max |
| 10710 | * fragment length and buffer size. |
| 10711 | * |
| 10712 | * According to RFC 5246 Section 6.2.1: |
| 10713 | * |
| 10714 | * Zero-length fragments of Application data MAY be sent as they are |
| 10715 | * potentially useful as a traffic analysis countermeasure. |
| 10716 | * |
| 10717 | * Therefore, it is possible that the input message length is 0 and the |
| 10718 | * corresponding return code is 0 on success. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10719 | */ |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10720 | static int ssl_write_real( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10721 | const unsigned char *buf, size_t len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10722 | { |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 10723 | int ret = mbedtls_ssl_get_max_out_record_payload( ssl ); |
| 10724 | const size_t max_len = (size_t) ret; |
| 10725 | |
| 10726 | if( ret < 0 ) |
| 10727 | { |
| 10728 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret ); |
| 10729 | return( ret ); |
| 10730 | } |
| 10731 | |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10732 | if( len > max_len ) |
| 10733 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10734 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 10735 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10736 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10737 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) " |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10738 | "maximum fragment length: %d > %d", |
| 10739 | len, max_len ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10740 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10741 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 10742 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10743 | #endif |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 10744 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 10745 | { |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10746 | len = max_len; |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 10747 | } |
| 10748 | #endif |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10749 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10750 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10751 | if( ssl->out_left != 0 ) |
| 10752 | { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 10753 | /* |
| 10754 | * The user has previously tried to send the data and |
| 10755 | * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially |
| 10756 | * written. In this case, we expect the high-level write function |
| 10757 | * (e.g. mbedtls_ssl_write()) to be called with the same parameters |
| 10758 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10759 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10760 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10761 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10762 | return( ret ); |
| 10763 | } |
| 10764 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10765 | else |
Paul Bakker | 1fd00bf | 2011-03-14 20:50:15 +0000 | [diff] [blame] | 10766 | { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 10767 | /* |
| 10768 | * The user is trying to send a message the first time, so we need to |
| 10769 | * copy the data into the internal buffers and setup the data structure |
| 10770 | * to keep track of partial writes |
| 10771 | */ |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10772 | ssl->out_msglen = len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10773 | ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10774 | memcpy( ssl->out_msg, buf, len ); |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10775 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 10776 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10777 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10778 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10779 | return( ret ); |
| 10780 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10781 | } |
| 10782 | |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10783 | return( (int) len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10784 | } |
| 10785 | |
| 10786 | /* |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10787 | * Write application data, doing 1/n-1 splitting if necessary. |
| 10788 | * |
| 10789 | * With non-blocking I/O, ssl_write_real() may return WANT_WRITE, |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 10790 | * then the caller will call us again with the same arguments, so |
Hanno Becker | 2b187c4 | 2017-09-18 14:58:11 +0100 | [diff] [blame] | 10791 | * remember whether we already did the split or not. |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10792 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10793 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10794 | static int ssl_write_split( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10795 | const unsigned char *buf, size_t len ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10796 | { |
| 10797 | int ret; |
| 10798 | |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 10799 | if( ssl->conf->cbc_record_splitting == |
| 10800 | MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED || |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 10801 | len <= 1 || |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 10802 | mbedtls_ssl_get_minor_ver( ssl ) > MBEDTLS_SSL_MINOR_VERSION_1 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10803 | mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc ) |
| 10804 | != MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10805 | { |
| 10806 | return( ssl_write_real( ssl, buf, len ) ); |
| 10807 | } |
| 10808 | |
| 10809 | if( ssl->split_done == 0 ) |
| 10810 | { |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 10811 | if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10812 | return( ret ); |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 10813 | ssl->split_done = 1; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10814 | } |
| 10815 | |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 10816 | if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 ) |
| 10817 | return( ret ); |
| 10818 | ssl->split_done = 0; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10819 | |
| 10820 | return( ret + 1 ); |
| 10821 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10822 | #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */ |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10823 | |
| 10824 | /* |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10825 | * Write application data (public-facing wrapper) |
| 10826 | */ |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10827 | int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10828 | { |
| 10829 | int ret; |
| 10830 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10831 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10832 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10833 | if( ssl == NULL || ssl->conf == NULL ) |
| 10834 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10835 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10836 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10837 | if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 ) |
| 10838 | { |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10839 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10840 | return( ret ); |
| 10841 | } |
| 10842 | #endif |
| 10843 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10844 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10845 | { |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10846 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10847 | { |
Manuel Pégourié-Gonnard | 151dc77 | 2015-05-14 13:55:51 +0200 | [diff] [blame] | 10848 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10849 | return( ret ); |
| 10850 | } |
| 10851 | } |
| 10852 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10853 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10854 | ret = ssl_write_split( ssl, buf, len ); |
| 10855 | #else |
| 10856 | ret = ssl_write_real( ssl, buf, len ); |
| 10857 | #endif |
| 10858 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10859 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10860 | |
| 10861 | return( ret ); |
| 10862 | } |
| 10863 | |
| 10864 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10865 | * Notify the peer that the connection is being closed |
| 10866 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10867 | int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10868 | { |
| 10869 | int ret; |
| 10870 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10871 | if( ssl == NULL || ssl->conf == NULL ) |
| 10872 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10873 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10874 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10875 | |
Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 10876 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10877 | return( mbedtls_ssl_flush_output( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10878 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10879 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10880 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10881 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 10882 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 10883 | MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10884 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10885 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10886 | return( ret ); |
| 10887 | } |
| 10888 | } |
| 10889 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10890 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10891 | |
Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 10892 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10893 | } |
| 10894 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10895 | void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10896 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 10897 | if( transform == NULL ) |
| 10898 | return; |
| 10899 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10900 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10901 | deflateEnd( &transform->ctx_deflate ); |
| 10902 | inflateEnd( &transform->ctx_inflate ); |
| 10903 | #endif |
| 10904 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10905 | mbedtls_cipher_free( &transform->cipher_ctx_enc ); |
| 10906 | mbedtls_cipher_free( &transform->cipher_ctx_dec ); |
Manuel Pégourié-Gonnard | f71e587 | 2013-09-23 17:12:43 +0200 | [diff] [blame] | 10907 | |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 10908 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10909 | mbedtls_md_free( &transform->md_ctx_enc ); |
| 10910 | mbedtls_md_free( &transform->md_ctx_dec ); |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 10911 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 10912 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10913 | mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10914 | } |
| 10915 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10916 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10917 | static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert ) |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 10918 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10919 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 10920 | |
| 10921 | while( cur != NULL ) |
| 10922 | { |
| 10923 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10924 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 10925 | cur = next; |
| 10926 | } |
| 10927 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10928 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 10929 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10930 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 10931 | |
| 10932 | static void ssl_buffering_free( mbedtls_ssl_context *ssl ) |
| 10933 | { |
| 10934 | unsigned offset; |
| 10935 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 10936 | |
| 10937 | if( hs == NULL ) |
| 10938 | return; |
| 10939 | |
Hanno Becker | 283f5ef | 2018-08-24 09:34:47 +0100 | [diff] [blame] | 10940 | ssl_free_buffered_record( ssl ); |
| 10941 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10942 | for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ ) |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 10943 | ssl_buffering_free_slot( ssl, offset ); |
| 10944 | } |
| 10945 | |
| 10946 | static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl, |
| 10947 | uint8_t slot ) |
| 10948 | { |
| 10949 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 10950 | mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot]; |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 10951 | |
| 10952 | if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS ) |
| 10953 | return; |
| 10954 | |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 10955 | if( hs_buf->is_valid == 1 ) |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10956 | { |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 10957 | hs->buffering.total_bytes_buffered -= hs_buf->data_len; |
Hanno Becker | 805f2e1 | 2018-10-12 16:31:41 +0100 | [diff] [blame] | 10958 | mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len ); |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 10959 | mbedtls_free( hs_buf->data ); |
| 10960 | memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10961 | } |
| 10962 | } |
| 10963 | |
| 10964 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 10965 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 10966 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10967 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 10968 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 10969 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 10970 | if( handshake == NULL ) |
| 10971 | return; |
| 10972 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 10973 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 10974 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 10975 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 10976 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 10977 | handshake->async_in_progress = 0; |
| 10978 | } |
| 10979 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 10980 | |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 10981 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 10982 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 10983 | mbedtls_md5_free( &handshake->fin_md5 ); |
| 10984 | mbedtls_sha1_free( &handshake->fin_sha1 ); |
| 10985 | #endif |
| 10986 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 10987 | #if defined(MBEDTLS_SHA256_C) |
| 10988 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 10989 | #endif |
| 10990 | #if defined(MBEDTLS_SHA512_C) |
| 10991 | mbedtls_sha512_free( &handshake->fin_sha512 ); |
| 10992 | #endif |
| 10993 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 10994 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10995 | #if defined(MBEDTLS_DHM_C) |
| 10996 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10997 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10998 | #if defined(MBEDTLS_ECDH_C) |
| 10999 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 11000 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 11001 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 11002 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 11003 | #if defined(MBEDTLS_SSL_CLI_C) |
| 11004 | mbedtls_free( handshake->ecjpake_cache ); |
| 11005 | handshake->ecjpake_cache = NULL; |
| 11006 | handshake->ecjpake_cache_len = 0; |
| 11007 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 11008 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 11009 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 11010 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 11011 | if( handshake->psk != NULL ) |
| 11012 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11013 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 11014 | mbedtls_free( handshake->psk ); |
| 11015 | } |
| 11016 | #endif |
| 11017 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11018 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 11019 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 11020 | /* |
| 11021 | * Free only the linked list wrapper, not the keys themselves |
| 11022 | * since the belong to the SNI callback |
| 11023 | */ |
| 11024 | if( handshake->sni_key_cert != NULL ) |
| 11025 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11026 | mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next; |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 11027 | |
| 11028 | while( cur != NULL ) |
| 11029 | { |
| 11030 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11031 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 11032 | cur = next; |
| 11033 | } |
| 11034 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11035 | #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 11036 | |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 11037 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 11038 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 11039 | if( handshake->ecrs_peer_cert != NULL ) |
| 11040 | { |
| 11041 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 11042 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 11043 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 11044 | #endif |
| 11045 | |
Hanno Becker | 3bf8cdf | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 11046 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 11047 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 11048 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 11049 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 11050 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11051 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11052 | mbedtls_free( handshake->verify_cookie ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 11053 | ssl_flight_free( handshake->flight ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 11054 | ssl_buffering_free( ssl ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 11055 | #endif |
| 11056 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11057 | mbedtls_platform_zeroize( handshake, |
| 11058 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11059 | } |
| 11060 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11061 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11062 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 11063 | if( session == NULL ) |
| 11064 | return; |
| 11065 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11066 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 2214159 | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 11067 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 11068 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 11069 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 11070 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11071 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 11072 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 11073 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11074 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11075 | } |
| 11076 | |
Manuel Pégourié-Gonnard | 4c1d06e | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 11077 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | a7cd483 | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 11078 | |
| 11079 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 11080 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 11081 | #else |
| 11082 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 11083 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 11084 | |
| 11085 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
| 11086 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
| 11087 | #else |
| 11088 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u |
| 11089 | #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */ |
| 11090 | |
| 11091 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 11092 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 11093 | #else |
| 11094 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 11095 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 11096 | |
| 11097 | #if defined(MBEDTLS_SSL_ALPN) |
| 11098 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 11099 | #else |
| 11100 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 11101 | #endif /* MBEDTLS_SSL_ALPN */ |
| 11102 | |
| 11103 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 11104 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 11105 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 11106 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 11107 | |
| 11108 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
| 11109 | ( (uint32_t) ( \ |
| 11110 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \ |
| 11111 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \ |
| 11112 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \ |
| 11113 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \ |
| 11114 | 0u ) ) |
| 11115 | |
Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11116 | static unsigned char ssl_serialized_context_header[] = { |
| 11117 | MBEDTLS_VERSION_MAJOR, |
| 11118 | MBEDTLS_VERSION_MINOR, |
| 11119 | MBEDTLS_VERSION_PATCH, |
| 11120 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF, |
| 11121 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF, |
Manuel Pégourié-Gonnard | a7cd483 | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 11122 | ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF, |
| 11123 | ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF, |
| 11124 | ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF, |
Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11125 | }; |
| 11126 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11127 | /* |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11128 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | b6163ef | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 11129 | * |
| 11130 | * The format of the serialized data is: |
| 11131 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 11132 | * |
| 11133 | * // header |
| 11134 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11135 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | b6163ef | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 11136 | * // the format of the remaining |
Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11137 | * // serialized data. |
Manuel Pégourié-Gonnard | a7cd483 | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 11138 | * Note: When updating the format, remember to keep these |
| 11139 | * version+format bytes. (We may make their size part of the API.) |
Manuel Pégourié-Gonnard | b6163ef | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 11140 | * |
| 11141 | * // session sub-structure |
| 11142 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 11143 | * // transform sub-structure |
| 11144 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 11145 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 11146 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 11147 | * // fields from ssl_context |
| 11148 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 11149 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 11150 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 11151 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 11152 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 11153 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 11154 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 11155 | * |
| 11156 | * Note that many fields of the ssl_context or sub-structures are not |
| 11157 | * serialized, as they fall in one of the following categories: |
| 11158 | * |
| 11159 | * 1. forced value (eg in_left must be 0) |
| 11160 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 11161 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 11162 | * 4. value was temporary (eg content of input buffer) |
| 11163 | * 5. value will be provided by the user again (eg I/O callbacks and context) |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11164 | */ |
| 11165 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 11166 | unsigned char *buf, |
| 11167 | size_t buf_len, |
| 11168 | size_t *olen ) |
| 11169 | { |
Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11170 | unsigned char *p = buf; |
| 11171 | size_t used = 0; |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11172 | size_t session_len; |
| 11173 | int ret = 0; |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11174 | |
Manuel Pégourié-Gonnard | 569ed6b | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 11175 | /* |
Manuel Pégourié-Gonnard | 69a3e41 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11176 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 11177 | * this function's documentation. |
| 11178 | * |
| 11179 | * These are due to assumptions/limitations in the implementation. Some of |
| 11180 | * them are likely to stay (no handshake in progress) some might go away |
| 11181 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 569ed6b | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 11182 | */ |
Manuel Pégourié-Gonnard | 69a3e41 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11183 | /* The initial handshake must be over */ |
| 11184 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | 569ed6b | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 11185 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 69a3e41 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11186 | if( ssl->handshake != NULL ) |
| 11187 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11188 | /* Double-check that sub-structures are indeed ready */ |
| 11189 | if( ssl->transform == NULL || ssl->session == NULL ) |
| 11190 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11191 | /* There must be no pending incoming or outgoing data */ |
| 11192 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) |
| 11193 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11194 | if( ssl->out_left != 0 ) |
| 11195 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11196 | /* Protocol must be DLTS, not TLS */ |
| 11197 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) |
| 11198 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11199 | /* Version must be 1.2 */ |
| 11200 | if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
| 11201 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11202 | if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 11203 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11204 | /* We must be using an AEAD ciphersuite */ |
| 11205 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) |
| 11206 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11207 | /* Renegotiation must not be enabled */ |
| 11208 | if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) ) |
| 11209 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11210 | |
Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11211 | /* |
| 11212 | * Version and format identifier |
| 11213 | */ |
| 11214 | used += sizeof( ssl_serialized_context_header ); |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11215 | |
Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11216 | if( used <= buf_len ) |
| 11217 | { |
| 11218 | memcpy( p, ssl_serialized_context_header, |
| 11219 | sizeof( ssl_serialized_context_header ) ); |
| 11220 | p += sizeof( ssl_serialized_context_header ); |
| 11221 | } |
| 11222 | |
| 11223 | /* |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11224 | * Session (length + data) |
| 11225 | */ |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 11226 | ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len ); |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11227 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
| 11228 | return( ret ); |
| 11229 | |
| 11230 | used += 4 + session_len; |
| 11231 | if( used <= buf_len ) |
| 11232 | { |
| 11233 | *p++ = (unsigned char)( ( session_len >> 24 ) & 0xFF ); |
| 11234 | *p++ = (unsigned char)( ( session_len >> 16 ) & 0xFF ); |
| 11235 | *p++ = (unsigned char)( ( session_len >> 8 ) & 0xFF ); |
| 11236 | *p++ = (unsigned char)( ( session_len ) & 0xFF ); |
| 11237 | |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 11238 | ret = ssl_session_save( ssl->session, 1, |
| 11239 | p, session_len, &session_len ); |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11240 | if( ret != 0 ) |
| 11241 | return( ret ); |
| 11242 | |
| 11243 | p += session_len; |
| 11244 | } |
| 11245 | |
| 11246 | /* |
Manuel Pégourié-Gonnard | 322f3c7 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11247 | * Transform |
| 11248 | */ |
| 11249 | used += sizeof( ssl->transform->randbytes ); |
| 11250 | if( used <= buf_len ) |
| 11251 | { |
| 11252 | memcpy( p, ssl->transform->randbytes, |
| 11253 | sizeof( ssl->transform->randbytes ) ); |
| 11254 | p += sizeof( ssl->transform->randbytes ); |
| 11255 | } |
| 11256 | |
| 11257 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 11258 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
| 11259 | if( used <= buf_len ) |
| 11260 | { |
| 11261 | *p++ = ssl->transform->in_cid_len; |
| 11262 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); |
| 11263 | p += ssl->transform->in_cid_len; |
| 11264 | |
| 11265 | *p++ = ssl->transform->out_cid_len; |
| 11266 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); |
| 11267 | p += ssl->transform->out_cid_len; |
| 11268 | } |
| 11269 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 11270 | |
| 11271 | /* |
Manuel Pégourié-Gonnard | 16d1485 | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 11272 | * Saved fields from top-level ssl_context structure |
| 11273 | */ |
| 11274 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
| 11275 | used += 4; |
| 11276 | if( used <= buf_len ) |
| 11277 | { |
| 11278 | *p++ = (unsigned char)( ( ssl->badmac_seen >> 24 ) & 0xFF ); |
| 11279 | *p++ = (unsigned char)( ( ssl->badmac_seen >> 16 ) & 0xFF ); |
| 11280 | *p++ = (unsigned char)( ( ssl->badmac_seen >> 8 ) & 0xFF ); |
| 11281 | *p++ = (unsigned char)( ( ssl->badmac_seen ) & 0xFF ); |
| 11282 | } |
| 11283 | #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */ |
| 11284 | |
| 11285 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 11286 | used += 16; |
| 11287 | if( used <= buf_len ) |
| 11288 | { |
| 11289 | *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF ); |
| 11290 | *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF ); |
| 11291 | *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF ); |
| 11292 | *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF ); |
| 11293 | *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF ); |
| 11294 | *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF ); |
| 11295 | *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF ); |
| 11296 | *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF ); |
| 11297 | |
| 11298 | *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF ); |
| 11299 | *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF ); |
| 11300 | *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF ); |
| 11301 | *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF ); |
| 11302 | *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF ); |
| 11303 | *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF ); |
| 11304 | *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF ); |
| 11305 | *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF ); |
| 11306 | } |
| 11307 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 11308 | |
| 11309 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11310 | used += 1; |
| 11311 | if( used <= buf_len ) |
| 11312 | { |
| 11313 | *p++ = ssl->disable_datagram_packing; |
| 11314 | } |
| 11315 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 11316 | |
| 11317 | used += 8; |
| 11318 | if( used <= buf_len ) |
| 11319 | { |
| 11320 | memcpy( p, ssl->cur_out_ctr, 8 ); |
| 11321 | p += 8; |
| 11322 | } |
| 11323 | |
| 11324 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11325 | used += 2; |
| 11326 | if( used <= buf_len ) |
| 11327 | { |
| 11328 | *p++ = (unsigned char)( ( ssl->mtu >> 8 ) & 0xFF ); |
| 11329 | *p++ = (unsigned char)( ( ssl->mtu ) & 0xFF ); |
| 11330 | } |
| 11331 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 11332 | |
| 11333 | #if defined(MBEDTLS_SSL_ALPN) |
| 11334 | { |
| 11335 | const uint8_t alpn_len = ssl->alpn_chosen |
Manuel Pégourié-Gonnard | 7af7375 | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 11336 | ? (uint8_t) strlen( ssl->alpn_chosen ) |
Manuel Pégourié-Gonnard | 16d1485 | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 11337 | : 0; |
| 11338 | |
| 11339 | used += 1 + alpn_len; |
| 11340 | if( used <= buf_len ) |
| 11341 | { |
| 11342 | *p++ = alpn_len; |
| 11343 | |
| 11344 | if( ssl->alpn_chosen != NULL ) |
| 11345 | { |
| 11346 | memcpy( p, ssl->alpn_chosen, alpn_len ); |
| 11347 | p += alpn_len; |
| 11348 | } |
| 11349 | } |
| 11350 | } |
| 11351 | #endif /* MBEDTLS_SSL_ALPN */ |
| 11352 | |
| 11353 | /* |
Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11354 | * Done |
| 11355 | */ |
| 11356 | *olen = used; |
| 11357 | |
| 11358 | if( used > buf_len ) |
| 11359 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11360 | |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11361 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); |
| 11362 | |
Manuel Pégourié-Gonnard | bc847ca | 2019-07-23 14:51:09 +0200 | [diff] [blame] | 11363 | return( ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11364 | } |
| 11365 | |
| 11366 | /* |
Manuel Pégourié-Gonnard | 322f3c7 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11367 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 11368 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 11369 | */ |
| 11370 | typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen, |
| 11371 | const char *label, |
| 11372 | const unsigned char *random, size_t rlen, |
| 11373 | unsigned char *dstbuf, size_t dlen ); |
| 11374 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) |
| 11375 | { |
| 11376 | mbedtls_ssl_ciphersuite_handle_t const info = |
| 11377 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); |
| 11378 | const mbedtls_md_type_t hash = mbedtls_ssl_suite_get_mac( info ); |
| 11379 | |
Manuel Pégourié-Gonnard | 2cc9223 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 11380 | #if defined(MBEDTLS_SHA512_C) |
| 11381 | if( hash == MBEDTLS_MD_SHA384 ) |
| 11382 | return( tls_prf_sha384 ); |
| 11383 | #else |
| 11384 | (void) hash; |
| 11385 | #endif |
| 11386 | return( tls_prf_sha256 ); |
Manuel Pégourié-Gonnard | 322f3c7 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11387 | } |
| 11388 | |
| 11389 | /* |
Manuel Pégourié-Gonnard | 8175816 | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 11390 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11391 | * |
| 11392 | * This internal version is wrapped by a public function that cleans up in |
| 11393 | * case of error. |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11394 | */ |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11395 | static int ssl_context_load( mbedtls_ssl_context *ssl, |
| 11396 | const unsigned char *buf, |
| 11397 | size_t len ) |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11398 | { |
Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11399 | const unsigned char *p = buf; |
| 11400 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11401 | size_t session_len; |
| 11402 | int ret; |
Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11403 | |
Manuel Pégourié-Gonnard | 5e534ba | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 11404 | /* |
| 11405 | * The context should have been freshly setup or reset. |
| 11406 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 14e2a8a | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 11407 | * (Checking session is useful because it won't be NULL if we're |
Manuel Pégourié-Gonnard | 5e534ba | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 11408 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 11409 | */ |
| 11410 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 11411 | ssl->session != NULL ) |
| 11412 | { |
| 11413 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11414 | } |
| 11415 | |
| 11416 | /* |
| 11417 | * We can't check that the config matches the initial one, but we can at |
| 11418 | * least check it matches the requirements for serializing. |
| 11419 | */ |
| 11420 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) || |
Manuel Pégourié-Gonnard | 73a4636 | 2019-07-23 15:16:19 +0200 | [diff] [blame] | 11421 | mbedtls_ssl_conf_get_max_major_ver( ssl->conf ) < |
| 11422 | MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 11423 | mbedtls_ssl_conf_get_min_major_ver( ssl->conf ) > |
| 11424 | MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 11425 | mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) < |
| 11426 | MBEDTLS_SSL_MINOR_VERSION_3 || |
| 11427 | mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) > |
| 11428 | MBEDTLS_SSL_MINOR_VERSION_3 || |
Manuel Pégourié-Gonnard | 18332c5 | 2019-07-29 12:17:52 +0200 | [diff] [blame] | 11429 | mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) ) |
Manuel Pégourié-Gonnard | 5e534ba | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 11430 | { |
| 11431 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11432 | } |
| 11433 | |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11434 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); |
| 11435 | |
Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11436 | /* |
| 11437 | * Check version identifier |
| 11438 | */ |
| 11439 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) |
| 11440 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11441 | |
| 11442 | if( memcmp( p, ssl_serialized_context_header, |
| 11443 | sizeof( ssl_serialized_context_header ) ) != 0 ) |
| 11444 | { |
| 11445 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 11446 | } |
| 11447 | p += sizeof( ssl_serialized_context_header ); |
| 11448 | |
| 11449 | /* |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11450 | * Session |
| 11451 | */ |
| 11452 | if( (size_t)( end - p ) < 4 ) |
| 11453 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11454 | |
| 11455 | session_len = ( (size_t) p[0] << 24 ) | |
| 11456 | ( (size_t) p[1] << 16 ) | |
| 11457 | ( (size_t) p[2] << 8 ) | |
| 11458 | ( (size_t) p[3] ); |
| 11459 | p += 4; |
| 11460 | |
Manuel Pégourié-Gonnard | ff22200 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 11461 | /* This has been allocated by ssl_handshake_init(), called by |
| 11462 | * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */ |
| 11463 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11464 | ssl->session_in = ssl->session; |
| 11465 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | ff22200 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 11466 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11467 | |
| 11468 | if( (size_t)( end - p ) < session_len ) |
| 11469 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11470 | |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 11471 | ret = ssl_session_load( ssl->session, 1, p, session_len ); |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11472 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 11473 | { |
| 11474 | mbedtls_ssl_session_free( ssl->session ); |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11475 | return( ret ); |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 11476 | } |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11477 | |
| 11478 | p += session_len; |
| 11479 | |
| 11480 | /* |
Manuel Pégourié-Gonnard | 322f3c7 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11481 | * Transform |
| 11482 | */ |
| 11483 | |
Manuel Pégourié-Gonnard | ff22200 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 11484 | /* This has been allocated by ssl_handshake_init(), called by |
| 11485 | * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */ |
| 11486 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | 322f3c7 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11487 | ssl->transform_in = ssl->transform; |
| 11488 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | ff22200 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 11489 | ssl->transform_negotiate = NULL; |
Manuel Pégourié-Gonnard | 322f3c7 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11490 | |
| 11491 | /* Read random bytes and populate structure */ |
| 11492 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) |
| 11493 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11494 | |
| 11495 | ret = ssl_populate_transform( ssl->transform, |
| 11496 | mbedtls_ssl_session_get_ciphersuite( ssl->session ), |
| 11497 | ssl->session->master, |
| 11498 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
| 11499 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 11500 | ssl->session->encrypt_then_mac, |
| 11501 | #endif |
| 11502 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 11503 | ssl->session->trunc_hmac, |
| 11504 | #endif |
| 11505 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
| 11506 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 11507 | ssl->session->compression, |
| 11508 | #endif |
| 11509 | ssl_tls12prf_from_cs( |
| 11510 | mbedtls_ssl_session_get_ciphersuite( ssl->session) ), |
| 11511 | p, /* currently pointing to randbytes */ |
| 11512 | MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */ |
| 11513 | mbedtls_ssl_conf_get_endpoint( ssl->conf ), |
| 11514 | ssl ); |
| 11515 | if( ret != 0 ) |
| 11516 | return( ret ); |
| 11517 | |
| 11518 | p += sizeof( ssl->transform->randbytes ); |
| 11519 | |
| 11520 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 11521 | /* Read connection IDs and store them */ |
| 11522 | if( (size_t)( end - p ) < 1 ) |
| 11523 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11524 | |
| 11525 | ssl->transform->in_cid_len = *p++; |
| 11526 | |
Manuel Pégourié-Gonnard | 2f3fa62 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 11527 | if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u ) |
Manuel Pégourié-Gonnard | 322f3c7 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11528 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11529 | |
| 11530 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); |
| 11531 | p += ssl->transform->in_cid_len; |
| 11532 | |
| 11533 | ssl->transform->out_cid_len = *p++; |
| 11534 | |
| 11535 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) |
| 11536 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11537 | |
| 11538 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); |
| 11539 | p += ssl->transform->out_cid_len; |
| 11540 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 11541 | |
| 11542 | /* |
Manuel Pégourié-Gonnard | 16d1485 | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 11543 | * Saved fields from top-level ssl_context structure |
| 11544 | */ |
| 11545 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
| 11546 | if( (size_t)( end - p ) < 4 ) |
| 11547 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11548 | |
| 11549 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | |
| 11550 | ( (uint32_t) p[1] << 16 ) | |
| 11551 | ( (uint32_t) p[2] << 8 ) | |
| 11552 | ( (uint32_t) p[3] ); |
| 11553 | p += 4; |
| 11554 | #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */ |
| 11555 | |
| 11556 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 11557 | if( (size_t)( end - p ) < 16 ) |
| 11558 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11559 | |
| 11560 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | |
| 11561 | ( (uint64_t) p[1] << 48 ) | |
| 11562 | ( (uint64_t) p[2] << 40 ) | |
| 11563 | ( (uint64_t) p[3] << 32 ) | |
| 11564 | ( (uint64_t) p[4] << 24 ) | |
| 11565 | ( (uint64_t) p[5] << 16 ) | |
| 11566 | ( (uint64_t) p[6] << 8 ) | |
| 11567 | ( (uint64_t) p[7] ); |
| 11568 | p += 8; |
| 11569 | |
| 11570 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | |
| 11571 | ( (uint64_t) p[1] << 48 ) | |
| 11572 | ( (uint64_t) p[2] << 40 ) | |
| 11573 | ( (uint64_t) p[3] << 32 ) | |
| 11574 | ( (uint64_t) p[4] << 24 ) | |
| 11575 | ( (uint64_t) p[5] << 16 ) | |
| 11576 | ( (uint64_t) p[6] << 8 ) | |
| 11577 | ( (uint64_t) p[7] ); |
| 11578 | p += 8; |
| 11579 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 11580 | |
| 11581 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11582 | if( (size_t)( end - p ) < 1 ) |
| 11583 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11584 | |
| 11585 | ssl->disable_datagram_packing = *p++; |
| 11586 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 11587 | |
| 11588 | if( (size_t)( end - p ) < 8 ) |
| 11589 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11590 | |
| 11591 | memcpy( ssl->cur_out_ctr, p, 8 ); |
| 11592 | p += 8; |
| 11593 | |
| 11594 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11595 | if( (size_t)( end - p ) < 2 ) |
| 11596 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11597 | |
| 11598 | ssl->mtu = ( p[0] << 8 ) | p[1]; |
| 11599 | p += 2; |
| 11600 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 11601 | |
| 11602 | #if defined(MBEDTLS_SSL_ALPN) |
| 11603 | { |
| 11604 | uint8_t alpn_len; |
| 11605 | const char **cur; |
| 11606 | |
| 11607 | if( (size_t)( end - p ) < 1 ) |
| 11608 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11609 | |
| 11610 | alpn_len = *p++; |
| 11611 | |
| 11612 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) |
| 11613 | { |
| 11614 | /* alpn_chosen should point to an item in the configured list */ |
| 11615 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 11616 | { |
| 11617 | if( strlen( *cur ) == alpn_len && |
| 11618 | memcmp( p, cur, alpn_len ) == 0 ) |
| 11619 | { |
| 11620 | ssl->alpn_chosen = *cur; |
| 11621 | break; |
| 11622 | } |
| 11623 | } |
| 11624 | } |
| 11625 | |
| 11626 | /* can only happen on conf mismatch */ |
| 11627 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) |
| 11628 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11629 | |
| 11630 | p += alpn_len; |
| 11631 | } |
| 11632 | #endif /* MBEDTLS_SSL_ALPN */ |
| 11633 | |
| 11634 | /* |
Manuel Pégourié-Gonnard | 138079d | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 11635 | * Forced fields from top-level ssl_context structure |
| 11636 | * |
| 11637 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 11638 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 11639 | */ |
| 11640 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
| 11641 | |
Manuel Pégourié-Gonnard | 2cc9223 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 11642 | #if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER) |
Manuel Pégourié-Gonnard | 138079d | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 11643 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
Manuel Pégourié-Gonnard | 2cc9223 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 11644 | #endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */ |
| 11645 | #if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER) |
Manuel Pégourié-Gonnard | 138079d | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 11646 | ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; |
Manuel Pégourié-Gonnard | 2cc9223 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 11647 | #endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */ |
Manuel Pégourié-Gonnard | 138079d | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 11648 | |
| 11649 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11650 | ssl->in_epoch = 1; |
| 11651 | #endif |
| 11652 | |
| 11653 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 11654 | * which we don't want - otherwise we'd end up freeing the wrong transform |
| 11655 | * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */ |
| 11656 | if( ssl->handshake != NULL ) |
| 11657 | { |
| 11658 | mbedtls_ssl_handshake_free( ssl ); |
| 11659 | mbedtls_free( ssl->handshake ); |
| 11660 | ssl->handshake = NULL; |
| 11661 | } |
| 11662 | |
| 11663 | /* |
Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11664 | * Done - should have consumed entire buffer |
| 11665 | */ |
| 11666 | if( p != end ) |
| 11667 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11668 | |
| 11669 | return( 0 ); |
| 11670 | } |
| 11671 | |
| 11672 | /* |
Manuel Pégourié-Gonnard | 8175816 | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 11673 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11674 | */ |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11675 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11676 | const unsigned char *buf, |
| 11677 | size_t len ) |
| 11678 | { |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11679 | int ret = ssl_context_load( context, buf, len ); |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11680 | |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11681 | if( ret != 0 ) |
| 11682 | mbedtls_ssl_free( context ); |
| 11683 | |
| 11684 | return( ret ); |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11685 | } |
Manuel Pégourié-Gonnard | 4c1d06e | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 11686 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11687 | |
| 11688 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11689 | * Free an SSL context |
| 11690 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11691 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11692 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 11693 | if( ssl == NULL ) |
| 11694 | return; |
| 11695 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11696 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11697 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 11698 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11699 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 11700 | mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11701 | mbedtls_free( ssl->out_buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11702 | } |
| 11703 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 11704 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11705 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 11706 | mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11707 | mbedtls_free( ssl->in_buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11708 | } |
| 11709 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11710 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 11711 | if( ssl->compress_buf != NULL ) |
| 11712 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 11713 | mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11714 | mbedtls_free( ssl->compress_buf ); |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 11715 | } |
| 11716 | #endif |
| 11717 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11718 | if( ssl->transform ) |
| 11719 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11720 | mbedtls_ssl_transform_free( ssl->transform ); |
| 11721 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11722 | } |
| 11723 | |
| 11724 | if( ssl->handshake ) |
| 11725 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 11726 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11727 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 11728 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11729 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11730 | mbedtls_free( ssl->handshake ); |
| 11731 | mbedtls_free( ssl->transform_negotiate ); |
| 11732 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11733 | } |
| 11734 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 11735 | if( ssl->session ) |
| 11736 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11737 | mbedtls_ssl_session_free( ssl->session ); |
| 11738 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 11739 | } |
| 11740 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 11741 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 11742 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11743 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11744 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11745 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11746 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 11747 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11748 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11749 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 11750 | if( mbedtls_ssl_hw_record_finish != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 11751 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11752 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) ); |
| 11753 | mbedtls_ssl_hw_record_finish( ssl ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 11754 | } |
| 11755 | #endif |
| 11756 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 11757 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11758 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 11759 | #endif |
| 11760 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11761 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 11762 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 11763 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11764 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11765 | } |
| 11766 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11767 | /* |
| 11768 | * Initialze mbedtls_ssl_config |
| 11769 | */ |
| 11770 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 11771 | { |
| 11772 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | e744eab | 2019-03-18 10:51:18 +0100 | [diff] [blame] | 11773 | |
| 11774 | #if !defined(MBEDTLS_SSL_PROTO_TLS) |
| 11775 | conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM; |
| 11776 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11777 | } |
| 11778 | |
Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 11779 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 11780 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 11781 | static int ssl_preset_default_hashes[] = { |
| 11782 | #if defined(MBEDTLS_SHA512_C) |
| 11783 | MBEDTLS_MD_SHA512, |
| 11784 | MBEDTLS_MD_SHA384, |
| 11785 | #endif |
| 11786 | #if defined(MBEDTLS_SHA256_C) |
| 11787 | MBEDTLS_MD_SHA256, |
| 11788 | MBEDTLS_MD_SHA224, |
| 11789 | #endif |
Gilles Peskine | 5d2511c | 2017-05-12 13:16:40 +0200 | [diff] [blame] | 11790 | #if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 11791 | MBEDTLS_MD_SHA1, |
| 11792 | #endif |
| 11793 | MBEDTLS_MD_NONE |
| 11794 | }; |
Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 11795 | #endif |
Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 11796 | #endif |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 11797 | |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 11798 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11799 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 11800 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 11801 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 11802 | 0 |
| 11803 | }; |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 11804 | #endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11805 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 11806 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 11807 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11808 | static int ssl_preset_suiteb_hashes[] = { |
| 11809 | MBEDTLS_MD_SHA256, |
| 11810 | MBEDTLS_MD_SHA384, |
| 11811 | MBEDTLS_MD_NONE |
| 11812 | }; |
| 11813 | #endif |
Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 11814 | #endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11815 | |
Hanno Becker | c1096e7 | 2019-06-19 12:30:41 +0100 | [diff] [blame] | 11816 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11817 | static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = { |
Jaeden Amero | 16529b2 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 11818 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11819 | MBEDTLS_ECP_DP_SECP256R1, |
Jaeden Amero | 16529b2 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 11820 | #endif |
| 11821 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11822 | MBEDTLS_ECP_DP_SECP384R1, |
Jaeden Amero | 16529b2 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 11823 | #endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11824 | MBEDTLS_ECP_DP_NONE |
| 11825 | }; |
| 11826 | #endif |
| 11827 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11828 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 11829 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11830 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 11831 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11832 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11833 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 11834 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11835 | int ret; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 11836 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11837 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 11838 | /* Use the functions here so that they are covered in tests, |
| 11839 | * but otherwise access member directly for efficiency */ |
| 11840 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 11841 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11842 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11843 | /* |
| 11844 | * Things that are common to all presets |
| 11845 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 11846 | #if defined(MBEDTLS_SSL_CLI_C) |
| 11847 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 11848 | { |
Hanno Becker | acd4fc0 | 2019-06-12 16:40:50 +0100 | [diff] [blame] | 11849 | #if !defined(MBEDTLS_SSL_CONF_AUTHMODE) |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 11850 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
Hanno Becker | acd4fc0 | 2019-06-12 16:40:50 +0100 | [diff] [blame] | 11851 | #endif /* !MBEDTLS_SSL_CONF_AUTHMODE */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 11852 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 11853 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 11854 | #endif |
| 11855 | } |
| 11856 | #endif |
| 11857 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 11858 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11859 | conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED; |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 11860 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11861 | |
| 11862 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 11863 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 11864 | #endif |
| 11865 | |
| 11866 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Hanno Becker | aabbb58 | 2019-06-11 13:43:27 +0100 | [diff] [blame] | 11867 | #if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11868 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
Hanno Becker | aabbb58 | 2019-06-11 13:43:27 +0100 | [diff] [blame] | 11869 | #endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */ |
| 11870 | #if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET) |
Jarno Lamsa | d9382f8 | 2019-06-10 10:27:14 +0300 | [diff] [blame] | 11871 | conf->enforce_extended_master_secret = |
Jarno Lamsa | 18b9a49 | 2019-06-10 15:23:29 +0300 | [diff] [blame] | 11872 | MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED; |
Hanno Becker | aabbb58 | 2019-06-11 13:43:27 +0100 | [diff] [blame] | 11873 | #endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11874 | #endif |
| 11875 | |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 11876 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
| 11877 | conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED; |
| 11878 | #endif |
| 11879 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 11880 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11881 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 11882 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 11883 | #endif |
| 11884 | |
Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 11885 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \ |
| 11886 | !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11887 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 11888 | #endif |
| 11889 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 11890 | #if defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | c2cfdaa | 2019-06-13 12:33:03 +0100 | [diff] [blame] | 11891 | #if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST) |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 11892 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
Hanno Becker | c2cfdaa | 2019-06-13 12:33:03 +0100 | [diff] [blame] | 11893 | #endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */ |
| 11894 | #endif /* MBEDTLS_SSL_SRV_C */ |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 11895 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11896 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 11897 | #if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11898 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 11899 | #endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */ |
| 11900 | #if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11901 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 11902 | #endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */ |
| 11903 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11904 | |
| 11905 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 11906 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 11907 | memset( conf->renego_period, 0x00, 2 ); |
| 11908 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11909 | #endif |
| 11910 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11911 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
| 11912 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 11913 | { |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 11914 | const unsigned char dhm_p[] = |
| 11915 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 11916 | const unsigned char dhm_g[] = |
| 11917 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
| 11918 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 11919 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 11920 | dhm_p, sizeof( dhm_p ), |
| 11921 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11922 | { |
| 11923 | return( ret ); |
| 11924 | } |
| 11925 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 11926 | #endif |
| 11927 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11928 | /* |
| 11929 | * Preset-specific defaults |
| 11930 | */ |
| 11931 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11932 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11933 | /* |
| 11934 | * NSA Suite B |
| 11935 | */ |
| 11936 | case MBEDTLS_SSL_PRESET_SUITEB: |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 11937 | #if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11938 | conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 11939 | #endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */ |
| 11940 | #if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11941 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 11942 | #endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */ |
| 11943 | #if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11944 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 11945 | #endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */ |
| 11946 | #if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11947 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 11948 | #endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11949 | |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 11950 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11951 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = |
| 11952 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = |
| 11953 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = |
| 11954 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = |
| 11955 | ssl_preset_suiteb_ciphersuites; |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 11956 | #endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11957 | |
| 11958 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 11959 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11960 | #endif |
| 11961 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 11962 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 11963 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11964 | conf->sig_hashes = ssl_preset_suiteb_hashes; |
| 11965 | #endif |
Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 11966 | #endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11967 | |
| 11968 | #if defined(MBEDTLS_ECP_C) |
Hanno Becker | c1096e7 | 2019-06-19 12:30:41 +0100 | [diff] [blame] | 11969 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_EC) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11970 | conf->curve_list = ssl_preset_suiteb_curves; |
| 11971 | #endif |
Hanno Becker | c1096e7 | 2019-06-19 12:30:41 +0100 | [diff] [blame] | 11972 | #endif |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 11973 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11974 | |
| 11975 | /* |
| 11976 | * Default |
| 11977 | */ |
| 11978 | default: |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 11979 | #if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER) |
Ron Eldor | 5e9f14d | 2017-05-28 10:46:38 +0300 | [diff] [blame] | 11980 | conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION > |
| 11981 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ? |
| 11982 | MBEDTLS_SSL_MIN_MAJOR_VERSION : |
| 11983 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION; |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 11984 | #endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */ |
| 11985 | #if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) |
Ron Eldor | 5e9f14d | 2017-05-28 10:46:38 +0300 | [diff] [blame] | 11986 | conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION > |
| 11987 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ? |
| 11988 | MBEDTLS_SSL_MIN_MINOR_VERSION : |
| 11989 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11990 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 11991 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) ) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11992 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2; |
| 11993 | #endif |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 11994 | #endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */ |
| 11995 | #if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER) |
| 11996 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 11997 | #endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */ |
| 11998 | #if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) |
| 11999 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 12000 | #endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12001 | |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 12002 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12003 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = |
| 12004 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = |
| 12005 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = |
| 12006 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = |
| 12007 | mbedtls_ssl_list_ciphersuites(); |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 12008 | #endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12009 | |
| 12010 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 12011 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 12012 | #endif |
| 12013 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 12014 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 12015 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 12016 | conf->sig_hashes = ssl_preset_default_hashes; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12017 | #endif |
Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 12018 | #endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12019 | |
| 12020 | #if defined(MBEDTLS_ECP_C) |
Hanno Becker | c1096e7 | 2019-06-19 12:30:41 +0100 | [diff] [blame] | 12021 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_EC) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12022 | conf->curve_list = mbedtls_ecp_grp_id_list(); |
| 12023 | #endif |
Hanno Becker | c1096e7 | 2019-06-19 12:30:41 +0100 | [diff] [blame] | 12024 | #endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12025 | |
| 12026 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 12027 | conf->dhm_min_bitlen = 1024; |
| 12028 | #endif |
| 12029 | } |
| 12030 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12031 | return( 0 ); |
| 12032 | } |
| 12033 | |
| 12034 | /* |
| 12035 | * Free mbedtls_ssl_config |
| 12036 | */ |
| 12037 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 12038 | { |
| 12039 | #if defined(MBEDTLS_DHM_C) |
| 12040 | mbedtls_mpi_free( &conf->dhm_P ); |
| 12041 | mbedtls_mpi_free( &conf->dhm_G ); |
| 12042 | #endif |
| 12043 | |
| 12044 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 12045 | if( conf->psk != NULL ) |
| 12046 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 12047 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12048 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 12049 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12050 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 12051 | } |
| 12052 | |
| 12053 | if( conf->psk_identity != NULL ) |
| 12054 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 12055 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 12056 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 12057 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12058 | conf->psk_identity_len = 0; |
| 12059 | } |
| 12060 | #endif |
| 12061 | |
| 12062 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 12063 | ssl_key_cert_free( conf->key_cert ); |
| 12064 | #endif |
| 12065 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 12066 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12067 | } |
| 12068 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 12069 | #if defined(MBEDTLS_PK_C) && \ |
| 12070 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 12071 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12072 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 12073 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12074 | unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 12075 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12076 | #if defined(MBEDTLS_RSA_C) |
| 12077 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 12078 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 12079 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12080 | #if defined(MBEDTLS_ECDSA_C) |
| 12081 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 12082 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 12083 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12084 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 12085 | } |
| 12086 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 12087 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 12088 | { |
| 12089 | switch( type ) { |
| 12090 | case MBEDTLS_PK_RSA: |
| 12091 | return( MBEDTLS_SSL_SIG_RSA ); |
| 12092 | case MBEDTLS_PK_ECDSA: |
| 12093 | case MBEDTLS_PK_ECKEY: |
| 12094 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 12095 | default: |
| 12096 | return( MBEDTLS_SSL_SIG_ANON ); |
| 12097 | } |
| 12098 | } |
| 12099 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12100 | mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig ) |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12101 | { |
| 12102 | switch( sig ) |
| 12103 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12104 | #if defined(MBEDTLS_RSA_C) |
| 12105 | case MBEDTLS_SSL_SIG_RSA: |
| 12106 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12107 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12108 | #if defined(MBEDTLS_ECDSA_C) |
| 12109 | case MBEDTLS_SSL_SIG_ECDSA: |
| 12110 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12111 | #endif |
| 12112 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12113 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12114 | } |
| 12115 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 12116 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12117 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 12118 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 12119 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 12120 | |
| 12121 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ |
| 12122 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set, |
| 12123 | mbedtls_pk_type_t sig_alg ) |
| 12124 | { |
| 12125 | switch( sig_alg ) |
| 12126 | { |
| 12127 | case MBEDTLS_PK_RSA: |
| 12128 | return( set->rsa ); |
| 12129 | case MBEDTLS_PK_ECDSA: |
| 12130 | return( set->ecdsa ); |
| 12131 | default: |
| 12132 | return( MBEDTLS_MD_NONE ); |
| 12133 | } |
| 12134 | } |
| 12135 | |
| 12136 | /* Add a signature-hash-pair to a signature-hash set */ |
| 12137 | void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set, |
| 12138 | mbedtls_pk_type_t sig_alg, |
| 12139 | mbedtls_md_type_t md_alg ) |
| 12140 | { |
| 12141 | switch( sig_alg ) |
| 12142 | { |
| 12143 | case MBEDTLS_PK_RSA: |
| 12144 | if( set->rsa == MBEDTLS_MD_NONE ) |
| 12145 | set->rsa = md_alg; |
| 12146 | break; |
| 12147 | |
| 12148 | case MBEDTLS_PK_ECDSA: |
| 12149 | if( set->ecdsa == MBEDTLS_MD_NONE ) |
| 12150 | set->ecdsa = md_alg; |
| 12151 | break; |
| 12152 | |
| 12153 | default: |
| 12154 | break; |
| 12155 | } |
| 12156 | } |
| 12157 | |
| 12158 | /* Allow exactly one hash algorithm for each signature. */ |
| 12159 | void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set, |
| 12160 | mbedtls_md_type_t md_alg ) |
| 12161 | { |
| 12162 | set->rsa = md_alg; |
| 12163 | set->ecdsa = md_alg; |
| 12164 | } |
| 12165 | |
| 12166 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2) && |
| 12167 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
| 12168 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 12169 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 12170 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 12171 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12172 | mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash ) |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12173 | { |
| 12174 | switch( hash ) |
| 12175 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12176 | #if defined(MBEDTLS_MD5_C) |
| 12177 | case MBEDTLS_SSL_HASH_MD5: |
| 12178 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12179 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12180 | #if defined(MBEDTLS_SHA1_C) |
| 12181 | case MBEDTLS_SSL_HASH_SHA1: |
| 12182 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12183 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12184 | #if defined(MBEDTLS_SHA256_C) |
| 12185 | case MBEDTLS_SSL_HASH_SHA224: |
| 12186 | return( MBEDTLS_MD_SHA224 ); |
| 12187 | case MBEDTLS_SSL_HASH_SHA256: |
| 12188 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12189 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12190 | #if defined(MBEDTLS_SHA512_C) |
| 12191 | case MBEDTLS_SSL_HASH_SHA384: |
| 12192 | return( MBEDTLS_MD_SHA384 ); |
| 12193 | case MBEDTLS_SSL_HASH_SHA512: |
| 12194 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12195 | #endif |
| 12196 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12197 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12198 | } |
| 12199 | } |
| 12200 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 12201 | /* |
| 12202 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 12203 | */ |
| 12204 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 12205 | { |
| 12206 | switch( md ) |
| 12207 | { |
| 12208 | #if defined(MBEDTLS_MD5_C) |
| 12209 | case MBEDTLS_MD_MD5: |
| 12210 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 12211 | #endif |
| 12212 | #if defined(MBEDTLS_SHA1_C) |
| 12213 | case MBEDTLS_MD_SHA1: |
| 12214 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 12215 | #endif |
| 12216 | #if defined(MBEDTLS_SHA256_C) |
| 12217 | case MBEDTLS_MD_SHA224: |
| 12218 | return( MBEDTLS_SSL_HASH_SHA224 ); |
| 12219 | case MBEDTLS_MD_SHA256: |
| 12220 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 12221 | #endif |
| 12222 | #if defined(MBEDTLS_SHA512_C) |
| 12223 | case MBEDTLS_MD_SHA384: |
| 12224 | return( MBEDTLS_SSL_HASH_SHA384 ); |
| 12225 | case MBEDTLS_MD_SHA512: |
| 12226 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 12227 | #endif |
| 12228 | default: |
| 12229 | return( MBEDTLS_SSL_HASH_NONE ); |
| 12230 | } |
| 12231 | } |
| 12232 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 12233 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 12234 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 12235 | * Check if a curve proposed by the peer is in our list. |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 12236 | * Return 0 if we're willing to use it, -1 otherwise. |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 12237 | */ |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 12238 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 12239 | { |
Hanno Becker | a4a9c69 | 2019-06-18 16:55:47 +0100 | [diff] [blame] | 12240 | MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id ) |
| 12241 | if( own_ec_id == grp_id ) |
| 12242 | return( 0 ); |
| 12243 | MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 12244 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 12245 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 12246 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 12247 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12248 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 12249 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 12250 | /* |
| 12251 | * Check if a hash proposed by the peer is in our list. |
| 12252 | * Return 0 if we're willing to use it, -1 otherwise. |
| 12253 | */ |
| 12254 | int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl, |
| 12255 | mbedtls_md_type_t md ) |
| 12256 | { |
Hanno Becker | f1bc9e1 | 2019-06-19 16:23:21 +0100 | [diff] [blame] | 12257 | MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg ) |
| 12258 | if( md_alg == md ) |
| 12259 | return( 0 ); |
| 12260 | MBEDTLS_SSL_END_FOR_EACH_SIG_HASH |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 12261 | |
| 12262 | return( -1 ); |
| 12263 | } |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 12264 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 12265 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12266 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 12267 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 12268 | mbedtls_ssl_ciphersuite_handle_t ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12269 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 12270 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12271 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12272 | int ret = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12273 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12274 | int usage = 0; |
| 12275 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12276 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12277 | const char *ext_oid; |
| 12278 | size_t ext_len; |
| 12279 | #endif |
| 12280 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12281 | #if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \ |
| 12282 | !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12283 | ((void) cert); |
| 12284 | ((void) cert_endpoint); |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12285 | ((void) flags); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12286 | #endif |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12287 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12288 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
| 12289 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12290 | { |
| 12291 | /* Server part of the key exchange */ |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 12292 | switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12293 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12294 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 12295 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 12296 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12297 | break; |
| 12298 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12299 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 12300 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 12301 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 12302 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12303 | break; |
| 12304 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12305 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 12306 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 12307 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12308 | break; |
| 12309 | |
| 12310 | /* Don't use default: we want warnings when adding new values */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12311 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 12312 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 12313 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 12314 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 12315 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12316 | usage = 0; |
| 12317 | } |
| 12318 | } |
| 12319 | else |
| 12320 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12321 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 12322 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12323 | } |
| 12324 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12325 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12326 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 12327 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12328 | ret = -1; |
| 12329 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12330 | #else |
| 12331 | ((void) ciphersuite); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12332 | #endif /* MBEDTLS_X509_CHECK_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12333 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12334 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
| 12335 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12336 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12337 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 12338 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12339 | } |
| 12340 | else |
| 12341 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12342 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 12343 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12344 | } |
| 12345 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12346 | if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12347 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 12348 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12349 | ret = -1; |
| 12350 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12351 | #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12352 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12353 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12354 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12355 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 12356 | |
Hanno Becker | 0a64170 | 2019-06-24 11:19:58 +0100 | [diff] [blame] | 12357 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 12358 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 12359 | { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 12360 | switch( md ) |
| 12361 | { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 12362 | #if defined(MBEDTLS_SHA512_C) |
| 12363 | case MBEDTLS_SSL_HASH_SHA384: |
| 12364 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 12365 | break; |
| 12366 | #endif |
| 12367 | #if defined(MBEDTLS_SHA256_C) |
| 12368 | case MBEDTLS_SSL_HASH_SHA256: |
| 12369 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 12370 | break; |
| 12371 | #endif |
Hanno Becker | 627fbee | 2019-06-24 11:21:53 +0100 | [diff] [blame] | 12372 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 12373 | default: |
Hanno Becker | 627fbee | 2019-06-24 11:21:53 +0100 | [diff] [blame] | 12374 | return( MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 12375 | } |
| 12376 | |
Hanno Becker | 627fbee | 2019-06-24 11:21:53 +0100 | [diff] [blame] | 12377 | return( 0 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 12378 | } |
Hanno Becker | 0a64170 | 2019-06-24 11:19:58 +0100 | [diff] [blame] | 12379 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 12380 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12381 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 12382 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 12383 | int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl, |
| 12384 | unsigned char *output, |
| 12385 | unsigned char *data, size_t data_len ) |
| 12386 | { |
| 12387 | int ret = 0; |
| 12388 | mbedtls_md5_context mbedtls_md5; |
| 12389 | mbedtls_sha1_context mbedtls_sha1; |
| 12390 | |
| 12391 | mbedtls_md5_init( &mbedtls_md5 ); |
| 12392 | mbedtls_sha1_init( &mbedtls_sha1 ); |
| 12393 | |
| 12394 | /* |
| 12395 | * digitally-signed struct { |
| 12396 | * opaque md5_hash[16]; |
| 12397 | * opaque sha_hash[20]; |
| 12398 | * }; |
| 12399 | * |
| 12400 | * md5_hash |
| 12401 | * MD5(ClientHello.random + ServerHello.random |
| 12402 | * + ServerParams); |
| 12403 | * sha_hash |
| 12404 | * SHA(ClientHello.random + ServerHello.random |
| 12405 | * + ServerParams); |
| 12406 | */ |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12407 | if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12408 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12409 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12410 | goto exit; |
| 12411 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12412 | if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12413 | ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 12414 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12415 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12416 | goto exit; |
| 12417 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12418 | if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12419 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12420 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12421 | goto exit; |
| 12422 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12423 | if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12424 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12425 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12426 | goto exit; |
| 12427 | } |
| 12428 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12429 | if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12430 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12431 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12432 | goto exit; |
| 12433 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12434 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12435 | ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 12436 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12437 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12438 | goto exit; |
| 12439 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12440 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12441 | data_len ) ) != 0 ) |
| 12442 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12443 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12444 | goto exit; |
| 12445 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12446 | if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12447 | output + 16 ) ) != 0 ) |
| 12448 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12449 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12450 | goto exit; |
| 12451 | } |
| 12452 | |
| 12453 | exit: |
| 12454 | mbedtls_md5_free( &mbedtls_md5 ); |
| 12455 | mbedtls_sha1_free( &mbedtls_sha1 ); |
| 12456 | |
| 12457 | if( ret != 0 ) |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 12458 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 12459 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12460 | |
| 12461 | return( ret ); |
| 12462 | |
| 12463 | } |
| 12464 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ |
| 12465 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 12466 | |
| 12467 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 12468 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 12469 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 12470 | unsigned char *hash, size_t *hashlen, |
| 12471 | unsigned char *data, size_t data_len, |
| 12472 | mbedtls_md_type_t md_alg ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12473 | { |
| 12474 | int ret = 0; |
| 12475 | mbedtls_md_context_t ctx; |
| 12476 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 12477 | *hashlen = mbedtls_md_get_size( md_info ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12478 | |
| 12479 | mbedtls_md_init( &ctx ); |
| 12480 | |
| 12481 | /* |
| 12482 | * digitally-signed struct { |
| 12483 | * opaque client_random[32]; |
| 12484 | * opaque server_random[32]; |
| 12485 | * ServerDHParams params; |
| 12486 | * }; |
| 12487 | */ |
| 12488 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 12489 | { |
| 12490 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 12491 | goto exit; |
| 12492 | } |
| 12493 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 12494 | { |
| 12495 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 12496 | goto exit; |
| 12497 | } |
| 12498 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 12499 | { |
| 12500 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 12501 | goto exit; |
| 12502 | } |
| 12503 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 12504 | { |
| 12505 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 12506 | goto exit; |
| 12507 | } |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 12508 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12509 | { |
| 12510 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 12511 | goto exit; |
| 12512 | } |
| 12513 | |
| 12514 | exit: |
| 12515 | mbedtls_md_free( &ctx ); |
| 12516 | |
| 12517 | if( ret != 0 ) |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 12518 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 12519 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12520 | |
| 12521 | return( ret ); |
| 12522 | } |
| 12523 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 12524 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 12525 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12526 | #endif /* MBEDTLS_SSL_TLS_C */ |