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 | } |
| 68 | #endif /* MBEDTLS_USE_TINYCRYPT */ |
| 69 | |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 70 | static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl ); |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 71 | 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] | 72 | |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 73 | /* Length of the "epoch" field in the record header */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 74 | 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] | 75 | { |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 76 | #if !defined(MBEDTLS_SSL_TRANSPORT__BOTH) |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 77 | ((void) ssl); |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 78 | #endif |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 79 | |
| 80 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 81 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
| 82 | return( 2 ); |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 83 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 84 | #endif |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 85 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 86 | return( 0 ); |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 87 | #endif |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Hanno Becker | b82350b | 2019-07-26 07:24:05 +0100 | [diff] [blame] | 90 | static void ssl_send_pending_fatal_alert( mbedtls_ssl_context *ssl ) |
| 91 | { |
| 92 | if( ssl->pending_fatal_alert_msg == MBEDTLS_SSL_ALERT_MSG_NONE ) |
| 93 | return; |
| 94 | |
| 95 | mbedtls_ssl_send_alert_message( ssl, |
| 96 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 97 | ssl->pending_fatal_alert_msg ); |
| 98 | ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE; |
| 99 | } |
| 100 | |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 101 | /* |
| 102 | * Start a timer. |
| 103 | * Passing millisecs = 0 cancels a running timer. |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 104 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 105 | 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] | 106 | { |
Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 107 | if( mbedtls_ssl_get_set_timer( ssl ) == NULL ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 108 | return; |
| 109 | |
| 110 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) ); |
Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 111 | mbedtls_ssl_get_set_timer( ssl )( ssl->p_timer, |
| 112 | millisecs / 4, |
| 113 | millisecs ); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Return -1 is timer is expired, 0 if it isn't. |
| 118 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 119 | static int ssl_check_timer( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 120 | { |
Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 121 | if( mbedtls_ssl_get_get_timer( ssl ) == NULL ) |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 122 | return( 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 123 | |
Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 124 | 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] | 125 | { |
| 126 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) ); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 127 | return( -1 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 128 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 129 | |
| 130 | return( 0 ); |
| 131 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 132 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 133 | static void ssl_update_out_pointers( mbedtls_ssl_context *ssl, |
| 134 | mbedtls_ssl_transform *transform ); |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 135 | static void ssl_update_in_pointers( mbedtls_ssl_context *ssl ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 136 | |
Hanno Becker | 02f2609 | 2019-07-03 16:13:00 +0100 | [diff] [blame] | 137 | #if defined(MBEDTLS_SSL_RECORD_CHECKING) |
Hanno Becker | 03e2db6 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 138 | static int ssl_parse_record_header( mbedtls_ssl_context const *ssl, |
| 139 | unsigned char *buf, |
| 140 | size_t len, |
| 141 | mbedtls_record *rec ); |
| 142 | |
Hanno Becker | 02f2609 | 2019-07-03 16:13:00 +0100 | [diff] [blame] | 143 | int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl, |
| 144 | unsigned char *buf, |
| 145 | size_t buflen ) |
| 146 | { |
Hanno Becker | 03e2db6 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 147 | int ret = 0; |
| 148 | mbedtls_record rec; |
| 149 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) ); |
| 150 | MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen ); |
| 151 | |
| 152 | /* We don't support record checking in TLS because |
| 153 | * (a) there doesn't seem to be a usecase for it, and |
| 154 | * (b) In SSLv3 and TLS 1.0, CBC record decryption has state |
| 155 | * and we'd need to backup the transform here. |
| 156 | */ |
| 157 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 158 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) |
| 159 | { |
| 160 | ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 161 | goto exit; |
| 162 | } |
| 163 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 164 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
| 165 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 166 | { |
| 167 | ret = ssl_parse_record_header( ssl, buf, buflen, &rec ); |
| 168 | if( ret != 0 ) |
| 169 | { |
| 170 | MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret ); |
| 171 | goto exit; |
| 172 | } |
| 173 | |
| 174 | if( ssl->transform_in != NULL ) |
| 175 | { |
| 176 | ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec ); |
| 177 | if( ret != 0 ) |
| 178 | { |
| 179 | MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret ); |
| 180 | goto exit; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 185 | |
| 186 | exit: |
| 187 | /* On success, we have decrypted the buffer in-place, so make |
| 188 | * sure we don't leak any plaintext data. */ |
| 189 | mbedtls_platform_zeroize( buf, buflen ); |
| 190 | |
| 191 | /* For the purpose of this API, treat messages with unexpected CID |
| 192 | * as well as such from future epochs as unexpected. */ |
| 193 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID || |
| 194 | ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 195 | { |
| 196 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
| 197 | } |
| 198 | |
| 199 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) ); |
| 200 | return( ret ); |
Hanno Becker | 02f2609 | 2019-07-03 16:13:00 +0100 | [diff] [blame] | 201 | } |
| 202 | #endif /* MBEDTLS_SSL_RECORD_CHECKING */ |
| 203 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 204 | #define SSL_DONT_FORCE_FLUSH 0 |
| 205 | #define SSL_FORCE_FLUSH 1 |
| 206 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 207 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 208 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 209 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 210 | /* Top-level Connection ID API */ |
| 211 | |
Hanno Becker | e0200da | 2019-06-13 09:23:43 +0100 | [diff] [blame] | 212 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \ |
| 213 | !defined(MBEDTLS_SSL_CONF_CID_LEN) && \ |
| 214 | !defined(MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID) |
Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 215 | int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf, |
| 216 | size_t len, |
| 217 | int ignore_other_cid ) |
Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 218 | { |
| 219 | if( len > MBEDTLS_SSL_CID_IN_LEN_MAX ) |
| 220 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 221 | |
Hanno Becker | 791ec6b | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 222 | if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && |
| 223 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 224 | { |
| 225 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 226 | } |
| 227 | |
| 228 | conf->ignore_unexpected_cid = ignore_other_cid; |
Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 229 | conf->cid_len = len; |
| 230 | return( 0 ); |
| 231 | } |
Hanno Becker | e0200da | 2019-06-13 09:23:43 +0100 | [diff] [blame] | 232 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID && |
| 233 | !MBEDTLS_SSL_CONF_CID_LEN && |
| 234 | !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */ |
| 235 | |
| 236 | #if MBEDTLS_SSL_CONF_CID_LEN > MBEDTLS_SSL_CID_IN_LEN_MAX |
| 237 | #error "Invalid hardcoded value for MBEDTLS_SSL_CONF_CID_LEN" |
| 238 | #endif |
| 239 | #if MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE && \ |
| 240 | MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_FAIL |
| 241 | #error "Invalid hardcoded value for MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID" |
| 242 | #endif |
| 243 | |
| 244 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID && |
| 245 | !MBEDTLS_SSL_CONF_CID_LEN && |
| 246 | !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */ |
Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 247 | |
Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 248 | int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl, |
| 249 | int enable, |
| 250 | unsigned char const *own_cid, |
| 251 | size_t own_cid_len ) |
| 252 | { |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 253 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) |
Hanno Becker | 78c4302 | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 254 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 255 | |
Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 256 | ssl->negotiate_cid = enable; |
| 257 | if( enable == MBEDTLS_SSL_CID_DISABLED ) |
| 258 | { |
| 259 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) ); |
| 260 | return( 0 ); |
| 261 | } |
| 262 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) ); |
Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 263 | MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len ); |
Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 264 | |
Hanno Becker | e0200da | 2019-06-13 09:23:43 +0100 | [diff] [blame] | 265 | if( own_cid_len != mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) |
Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 266 | { |
Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 267 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config", |
| 268 | (unsigned) own_cid_len, |
Hanno Becker | e0200da | 2019-06-13 09:23:43 +0100 | [diff] [blame] | 269 | (unsigned) mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) ); |
Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 270 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 271 | } |
| 272 | |
| 273 | memcpy( ssl->own_cid, own_cid, own_cid_len ); |
Hanno Becker | b4a5606 | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 274 | /* Truncation is not an issue here because |
| 275 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ |
| 276 | ssl->own_cid_len = (uint8_t) own_cid_len; |
Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 277 | |
Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 278 | return( 0 ); |
| 279 | } |
| 280 | |
| 281 | int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl, |
| 282 | int *enabled, |
| 283 | unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ], |
| 284 | size_t *peer_cid_len ) |
| 285 | { |
Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 286 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 287 | |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 288 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) || |
Hanno Becker | 78c4302 | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 289 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 290 | { |
Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 291 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 78c4302 | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 292 | } |
Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 293 | |
Hanno Becker | cb063f5 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 294 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions |
| 295 | * were used, but client and server requested the empty CID. |
| 296 | * This is indistinguishable from not using the CID extension |
| 297 | * in the first place. */ |
Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 298 | if( ssl->transform_in->in_cid_len == 0 && |
| 299 | ssl->transform_in->out_cid_len == 0 ) |
| 300 | { |
| 301 | return( 0 ); |
| 302 | } |
| 303 | |
Hanno Becker | 633d604 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 304 | if( peer_cid_len != NULL ) |
| 305 | { |
| 306 | *peer_cid_len = ssl->transform_in->out_cid_len; |
| 307 | if( peer_cid != NULL ) |
| 308 | { |
| 309 | memcpy( peer_cid, ssl->transform_in->out_cid, |
| 310 | ssl->transform_in->out_cid_len ); |
| 311 | } |
| 312 | } |
Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 313 | |
| 314 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 315 | |
Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 316 | return( 0 ); |
| 317 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 318 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 319 | |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 320 | /* Forward declarations for functions related to message buffering. */ |
| 321 | static void ssl_buffering_free( mbedtls_ssl_context *ssl ); |
| 322 | static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl, |
| 323 | uint8_t slot ); |
| 324 | static void ssl_free_buffered_record( mbedtls_ssl_context *ssl ); |
| 325 | static int ssl_load_buffered_message( mbedtls_ssl_context *ssl ); |
| 326 | static int ssl_load_buffered_record( mbedtls_ssl_context *ssl ); |
| 327 | static int ssl_buffer_message( mbedtls_ssl_context *ssl ); |
Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 328 | static int ssl_buffer_future_record( mbedtls_ssl_context *ssl, |
| 329 | mbedtls_record const *rec ); |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 330 | static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl ); |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 331 | |
Hanno Becker | a67dee2 | 2018-08-22 10:05:20 +0100 | [diff] [blame] | 332 | static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl ); |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 333 | 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] | 334 | { |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 335 | size_t mtu = ssl_get_current_mtu( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 336 | |
| 337 | if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN ) |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 338 | return( mtu ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 339 | |
| 340 | return( MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 341 | } |
| 342 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 343 | static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl ) |
| 344 | { |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 345 | size_t const bytes_written = ssl->out_left; |
| 346 | size_t const mtu = ssl_get_maximum_datagram_size( ssl ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 347 | |
| 348 | /* Double-check that the write-index hasn't gone |
| 349 | * past what we can transmit in a single datagram. */ |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 350 | if( bytes_written > mtu ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 351 | { |
| 352 | /* Should never happen... */ |
| 353 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 354 | } |
| 355 | |
| 356 | return( (int) ( mtu - bytes_written ) ); |
| 357 | } |
| 358 | |
| 359 | static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl ) |
| 360 | { |
| 361 | int ret; |
| 362 | size_t remaining, expansion; |
Andrzej Kurek | 748face | 2018-10-11 07:20:19 -0400 | [diff] [blame] | 363 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 364 | |
| 365 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 366 | const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl ); |
| 367 | |
| 368 | if( max_len > mfl ) |
| 369 | max_len = mfl; |
Hanno Becker | f4b010e | 2018-08-24 10:47:29 +0100 | [diff] [blame] | 370 | |
| 371 | /* By the standard (RFC 6066 Sect. 4), the MFL extension |
| 372 | * only limits the maximum record payload size, so in theory |
| 373 | * we would be allowed to pack multiple records of payload size |
| 374 | * MFL into a single datagram. However, this would mean that there's |
| 375 | * no way to explicitly communicate MTU restrictions to the peer. |
| 376 | * |
| 377 | * The following reduction of max_len makes sure that we never |
| 378 | * write datagrams larger than MFL + Record Expansion Overhead. |
| 379 | */ |
| 380 | if( max_len <= ssl->out_left ) |
| 381 | return( 0 ); |
| 382 | |
| 383 | max_len -= ssl->out_left; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 384 | #endif |
| 385 | |
| 386 | ret = ssl_get_remaining_space_in_datagram( ssl ); |
| 387 | if( ret < 0 ) |
| 388 | return( ret ); |
| 389 | remaining = (size_t) ret; |
| 390 | |
| 391 | ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 392 | if( ret < 0 ) |
| 393 | return( ret ); |
| 394 | expansion = (size_t) ret; |
| 395 | |
| 396 | if( remaining <= expansion ) |
| 397 | return( 0 ); |
| 398 | |
| 399 | remaining -= expansion; |
| 400 | if( remaining >= max_len ) |
| 401 | remaining = max_len; |
| 402 | |
| 403 | return( (int) remaining ); |
| 404 | } |
| 405 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 406 | /* |
| 407 | * Double the retransmit timeout value, within the allowed range, |
| 408 | * returning -1 if the maximum value has already been reached. |
| 409 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 410 | static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 411 | { |
| 412 | uint32_t new_timeout; |
| 413 | |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 414 | if( ssl->handshake->retransmit_timeout >= |
| 415 | mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) ) |
| 416 | { |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 417 | return( -1 ); |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 418 | } |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 419 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 420 | /* Implement the final paragraph of RFC 6347 section 4.1.1.1 |
| 421 | * in the following way: after the initial transmission and a first |
| 422 | * retransmission, back off to a temporary estimated MTU of 508 bytes. |
| 423 | * This value is guaranteed to be deliverable (if not guaranteed to be |
| 424 | * delivered) of any compliant IPv4 (and IPv6) network, and should work |
| 425 | * on most non-IP stacks too. */ |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 426 | if( ssl->handshake->retransmit_timeout != |
| 427 | mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) ) |
Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 428 | { |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 429 | ssl->handshake->mtu = 508; |
Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 430 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) ); |
| 431 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 432 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 433 | new_timeout = 2 * ssl->handshake->retransmit_timeout; |
| 434 | |
| 435 | /* Avoid arithmetic overflow and range overflow */ |
| 436 | if( new_timeout < ssl->handshake->retransmit_timeout || |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 437 | 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] | 438 | { |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 439 | 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] | 440 | } |
| 441 | |
| 442 | ssl->handshake->retransmit_timeout = new_timeout; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 443 | 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] | 444 | ssl->handshake->retransmit_timeout ) ); |
| 445 | |
| 446 | return( 0 ); |
| 447 | } |
| 448 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 449 | static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 450 | { |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 451 | 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] | 452 | 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] | 453 | ssl->handshake->retransmit_timeout ) ); |
| 454 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 455 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 456 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 457 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 458 | /* |
| 459 | * Convert max_fragment_length codes to length. |
| 460 | * RFC 6066 says: |
| 461 | * enum{ |
| 462 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 463 | * } MaxFragmentLength; |
| 464 | * and we add 0 -> extension unused |
| 465 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 466 | static unsigned int ssl_mfl_code_to_length( int mfl ) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 467 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 468 | switch( mfl ) |
| 469 | { |
| 470 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: |
| 471 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 472 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: |
| 473 | return 512; |
| 474 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: |
| 475 | return 1024; |
| 476 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: |
| 477 | return 2048; |
| 478 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: |
| 479 | return 4096; |
| 480 | default: |
| 481 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 482 | } |
| 483 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 484 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 485 | |
Hanno Becker | 58fccf2 | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 486 | int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst, |
| 487 | const mbedtls_ssl_session *src ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 488 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 489 | mbedtls_ssl_session_free( dst ); |
| 490 | memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 491 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 492 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | d5258fa | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 493 | |
| 494 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 495 | if( src->peer_cert != NULL ) |
| 496 | { |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 497 | int ret; |
| 498 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 499 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 500 | if( dst->peer_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 501 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 502 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 503 | mbedtls_x509_crt_init( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 504 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 505 | 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] | 506 | src->peer_cert->raw.len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 507 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 508 | mbedtls_free( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 509 | dst->peer_cert = NULL; |
| 510 | return( ret ); |
| 511 | } |
| 512 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 513 | #elif defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 9fb6e2e | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 514 | if( src->peer_cert_digest != NULL ) |
| 515 | { |
Hanno Becker | 9fb6e2e | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 516 | dst->peer_cert_digest = |
Hanno Becker | 9d64b78 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 517 | mbedtls_calloc( 1, src->peer_cert_digest_len ); |
Hanno Becker | 9fb6e2e | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 518 | if( dst->peer_cert_digest == NULL ) |
| 519 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 520 | |
| 521 | memcpy( dst->peer_cert_digest, src->peer_cert_digest, |
| 522 | src->peer_cert_digest_len ); |
| 523 | dst->peer_cert_digest_type = src->peer_cert_digest_type; |
Hanno Becker | 9d64b78 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 524 | dst->peer_cert_digest_len = src->peer_cert_digest_len; |
Hanno Becker | 9fb6e2e | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 525 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 526 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 9fb6e2e | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 527 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 528 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 529 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 530 | #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] | 531 | if( src->ticket != NULL ) |
| 532 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 533 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 534 | if( dst->ticket == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 535 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 536 | |
| 537 | memcpy( dst->ticket, src->ticket, src->ticket_len ); |
| 538 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 539 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 540 | |
| 541 | return( 0 ); |
| 542 | } |
| 543 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 544 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 545 | int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl, |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 546 | const unsigned char *key_enc, const unsigned char *key_dec, |
| 547 | size_t keylen, |
| 548 | const unsigned char *iv_enc, const unsigned char *iv_dec, |
| 549 | size_t ivlen, |
| 550 | const unsigned char *mac_enc, const unsigned char *mac_dec, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 551 | size_t maclen ) = NULL; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 552 | int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL; |
| 553 | int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL; |
| 554 | int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL; |
| 555 | int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL; |
| 556 | int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL; |
| 557 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 558 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 559 | /* |
| 560 | * Key material generation |
| 561 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 562 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 563 | static int ssl3_prf( const unsigned char *secret, size_t slen, |
| 564 | const char *label, |
| 565 | const unsigned char *random, size_t rlen, |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 566 | unsigned char *dstbuf, size_t dlen ) |
| 567 | { |
Andres Amaya Garcia | 3395250 | 2017-07-20 16:29:16 +0100 | [diff] [blame] | 568 | int ret = 0; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 569 | size_t i; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 570 | mbedtls_md5_context md5; |
| 571 | mbedtls_sha1_context sha1; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 572 | unsigned char padding[16]; |
| 573 | unsigned char sha1sum[20]; |
| 574 | ((void)label); |
| 575 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 576 | mbedtls_md5_init( &md5 ); |
| 577 | mbedtls_sha1_init( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 578 | |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 579 | /* |
| 580 | * SSLv3: |
| 581 | * block = |
| 582 | * MD5( secret + SHA1( 'A' + secret + random ) ) + |
| 583 | * MD5( secret + SHA1( 'BB' + secret + random ) ) + |
| 584 | * MD5( secret + SHA1( 'CCC' + secret + random ) ) + |
| 585 | * ... |
| 586 | */ |
| 587 | for( i = 0; i < dlen / 16; i++ ) |
| 588 | { |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 589 | memset( padding, (unsigned char) ('A' + i), 1 + i ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 590 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 591 | if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 592 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 593 | if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 594 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 595 | if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 596 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 597 | if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 598 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 599 | if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 600 | goto exit; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 601 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 602 | if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 603 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 604 | if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 605 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 606 | if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 607 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 608 | if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 609 | goto exit; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 612 | exit: |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 613 | mbedtls_md5_free( &md5 ); |
| 614 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 615 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 616 | mbedtls_platform_zeroize( padding, sizeof( padding ) ); |
| 617 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 618 | |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 619 | return( ret ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 620 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 621 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 622 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 623 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 624 | static int tls1_prf( const unsigned char *secret, size_t slen, |
| 625 | const char *label, |
| 626 | const unsigned char *random, size_t rlen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 627 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 628 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 629 | size_t nb, hs; |
| 630 | size_t i, j, k; |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 631 | const unsigned char *S1, *S2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 632 | unsigned char tmp[128]; |
| 633 | unsigned char h_i[20]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 634 | const mbedtls_md_info_t *md_info; |
| 635 | mbedtls_md_context_t md_ctx; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 636 | int ret; |
| 637 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 638 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 639 | |
| 640 | if( sizeof( tmp ) < 20 + strlen( label ) + rlen ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 641 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 642 | |
| 643 | hs = ( slen + 1 ) / 2; |
| 644 | S1 = secret; |
| 645 | S2 = secret + slen - hs; |
| 646 | |
| 647 | nb = strlen( label ); |
| 648 | memcpy( tmp + 20, label, nb ); |
| 649 | memcpy( tmp + 20 + nb, random, rlen ); |
| 650 | nb += rlen; |
| 651 | |
| 652 | /* |
| 653 | * First compute P_md5(secret,label+random)[0..dlen] |
| 654 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 655 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL ) |
| 656 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 657 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 658 | 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] | 659 | return( ret ); |
| 660 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 661 | mbedtls_md_hmac_starts( &md_ctx, S1, hs ); |
| 662 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); |
| 663 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 664 | |
| 665 | for( i = 0; i < dlen; i += 16 ) |
| 666 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 667 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 668 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb ); |
| 669 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 670 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 671 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 672 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 ); |
| 673 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 674 | |
| 675 | k = ( i + 16 > dlen ) ? dlen % 16 : 16; |
| 676 | |
| 677 | for( j = 0; j < k; j++ ) |
| 678 | dstbuf[i + j] = h_i[j]; |
| 679 | } |
| 680 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 681 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 682 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 683 | /* |
| 684 | * XOR out with P_sha1(secret,label+random)[0..dlen] |
| 685 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 686 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL ) |
| 687 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 688 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 689 | 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] | 690 | return( ret ); |
| 691 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 692 | mbedtls_md_hmac_starts( &md_ctx, S2, hs ); |
| 693 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); |
| 694 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 695 | |
| 696 | for( i = 0; i < dlen; i += 20 ) |
| 697 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 698 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 699 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb ); |
| 700 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 701 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 702 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 703 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 ); |
| 704 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 705 | |
| 706 | k = ( i + 20 > dlen ) ? dlen % 20 : 20; |
| 707 | |
| 708 | for( j = 0; j < k; j++ ) |
| 709 | dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] ); |
| 710 | } |
| 711 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 712 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 713 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 714 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 715 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 716 | |
| 717 | return( 0 ); |
| 718 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 719 | #endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 720 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 721 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 722 | 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] | 723 | const unsigned char *secret, size_t slen, |
| 724 | const char *label, |
| 725 | const unsigned char *random, size_t rlen, |
| 726 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 727 | { |
| 728 | size_t nb; |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 729 | size_t i, j, k, md_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 730 | unsigned char tmp[128]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 731 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 732 | const mbedtls_md_info_t *md_info; |
| 733 | mbedtls_md_context_t md_ctx; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 734 | int ret; |
| 735 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 736 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 737 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 738 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 739 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 740 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 741 | md_len = mbedtls_md_get_size( md_info ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 742 | |
| 743 | if( sizeof( tmp ) < md_len + strlen( label ) + rlen ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 744 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 745 | |
| 746 | nb = strlen( label ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 747 | memcpy( tmp + md_len, label, nb ); |
| 748 | memcpy( tmp + md_len + nb, random, rlen ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 749 | nb += rlen; |
| 750 | |
| 751 | /* |
| 752 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 753 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 754 | 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] | 755 | return( ret ); |
| 756 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 757 | mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 758 | mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 759 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 760 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 761 | for( i = 0; i < dlen; i += md_len ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 762 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 763 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 764 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 765 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 766 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 767 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 768 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 769 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 770 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 771 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 772 | |
| 773 | for( j = 0; j < k; j++ ) |
| 774 | dstbuf[i + j] = h_i[j]; |
| 775 | } |
| 776 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 777 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 778 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 779 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 780 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 781 | |
| 782 | return( 0 ); |
| 783 | } |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 784 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 785 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 786 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 787 | const char *label, |
| 788 | const unsigned char *random, size_t rlen, |
| 789 | unsigned char *dstbuf, size_t dlen ) |
| 790 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 791 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 792 | label, random, rlen, dstbuf, dlen ) ); |
| 793 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 794 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 795 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 796 | #if defined(MBEDTLS_SHA512_C) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 797 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 798 | const char *label, |
| 799 | const unsigned char *random, size_t rlen, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 800 | unsigned char *dstbuf, size_t dlen ) |
| 801 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 802 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 803 | label, random, rlen, dstbuf, dlen ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 804 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 805 | #endif /* MBEDTLS_SHA512_C */ |
| 806 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 807 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 808 | 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] | 809 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 810 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 811 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 812 | 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] | 813 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 814 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 815 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 816 | 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] | 817 | static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 818 | #endif |
| 819 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 820 | #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] | 821 | 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] | 822 | static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 823 | #endif |
| 824 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 825 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 826 | #if defined(MBEDTLS_SHA256_C) |
| 827 | 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] | 828 | 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] | 829 | 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] | 830 | #endif |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 831 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 832 | #if defined(MBEDTLS_SHA512_C) |
| 833 | 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] | 834 | 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] | 835 | 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] | 836 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 837 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 838 | |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 839 | /* Type for the TLS PRF */ |
| 840 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, |
| 841 | const unsigned char *, size_t, |
| 842 | unsigned char *, size_t); |
| 843 | |
Manuel Pégourié-Gonnard | 5ed5e90 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 844 | /* |
Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 845 | * Populate a transform structure with session keys and all the other |
| 846 | * necessary information. |
Manuel Pégourié-Gonnard | 5ed5e90 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 847 | * |
Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 848 | * Parameters: |
| 849 | * - [in/out]: transform: structure to populate |
| 850 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 851 | * [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] | 852 | * - [in] ciphersuite |
| 853 | * - [in] master |
| 854 | * - [in] encrypt_then_mac |
| 855 | * - [in] trunc_hmac |
| 856 | * - [in] compression |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 857 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 858 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 859 | * - [in] minor_ver: SSL/TLS minor version |
| 860 | * - [in] endpoint: client or server |
| 861 | * - [in] ssl: optionally used for: |
| 862 | * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context |
| 863 | * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys |
| 864 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
Manuel Pégourié-Gonnard | 5ed5e90 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 865 | */ |
Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 866 | static int ssl_populate_transform( mbedtls_ssl_transform *transform, |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 867 | int ciphersuite, |
| 868 | const unsigned char master[48], |
Jaeden Amero | 2eaf2c7 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 869 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 870 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 871 | int encrypt_then_mac, |
| 872 | #endif |
| 873 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 874 | int trunc_hmac, |
| 875 | #endif |
Jaeden Amero | 2eaf2c7 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 876 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 877 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 878 | int compression, |
| 879 | #endif |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 880 | ssl_tls_prf_t tls_prf, |
| 881 | const unsigned char randbytes[64], |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 882 | int minor_ver, |
| 883 | unsigned endpoint, |
Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 884 | const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 885 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 886 | int ret = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 887 | unsigned char keyblk[256]; |
| 888 | unsigned char *key1; |
| 889 | unsigned char *key2; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 890 | unsigned char *mac_enc; |
| 891 | unsigned char *mac_dec; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 892 | size_t mac_key_len; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 893 | size_t iv_copy_len; |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 894 | unsigned keylen; |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 895 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 896 | const mbedtls_cipher_info_t *cipher_info; |
| 897 | const mbedtls_md_info_t *md_info; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 898 | |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 899 | #if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \ |
| 900 | !defined(MBEDTLS_SSL_EXPORT_KEYS) && \ |
| 901 | !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | 86e48c2 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 902 | 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] | 903 | (void) ssl; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 904 | #endif |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 905 | |
Manuel Pégourié-Gonnard | a3024ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 906 | /* |
| 907 | * Some data just needs copying into the structure |
| 908 | */ |
Jaeden Amero | 2eaf2c7 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 909 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
| 910 | defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 911 | transform->encrypt_then_mac = encrypt_then_mac; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 912 | #endif |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 913 | |
| 914 | #if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER) |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 915 | transform->minor_ver = minor_ver; |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 916 | #else |
| 917 | ((void) minor_ver); |
| 918 | #endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 919 | |
Manuel Pégourié-Gonnard | a3024ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 920 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 921 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); |
| 922 | #endif |
| 923 | |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 924 | /* |
| 925 | * Get various info structures |
| 926 | */ |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 927 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 928 | if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ) |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 929 | { |
| 930 | 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] | 931 | ciphersuite ) ); |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 932 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 933 | } |
| 934 | |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 935 | cipher_info = mbedtls_cipher_info_from_type( |
| 936 | mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 937 | if( cipher_info == NULL ) |
| 938 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 939 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found", |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 940 | mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 941 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 942 | } |
| 943 | |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 944 | md_info = mbedtls_md_info_from_type( |
| 945 | mbedtls_ssl_suite_get_mac( ciphersuite_info ) ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 946 | if( md_info == NULL ) |
| 947 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 948 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found", |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 949 | mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 950 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 951 | } |
| 952 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 953 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | dd0afca | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 954 | /* Copy own and peer's CID if the use of the CID |
| 955 | * extension has been negotiated. */ |
| 956 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 957 | { |
| 958 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
Hanno Becker | d91dc37 | 2019-04-30 13:52:29 +0100 | [diff] [blame] | 959 | |
Hanno Becker | 4932f9f | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 960 | transform->in_cid_len = ssl->own_cid_len; |
Hanno Becker | 4932f9f | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 961 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
Hanno Becker | 8013b27 | 2019-05-03 12:55:51 +0100 | [diff] [blame] | 962 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
Hanno Becker | dd0afca | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 963 | transform->in_cid_len ); |
Hanno Becker | e582d12 | 2019-05-15 10:21:55 +0100 | [diff] [blame] | 964 | |
| 965 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 966 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 967 | ssl->handshake->peer_cid_len ); |
| 968 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 969 | transform->out_cid_len ); |
Hanno Becker | dd0afca | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 970 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 971 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | dd0afca | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 972 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 973 | /* |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 974 | * Compute key block using the PRF |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 975 | */ |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 976 | 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] | 977 | if( ret != 0 ) |
| 978 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 979 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 980 | return( ret ); |
| 981 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 982 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 983 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
Manuel Pégourié-Gonnard | 762d011 | 2019-05-20 10:27:20 +0200 | [diff] [blame] | 984 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 985 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 986 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 987 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 988 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 989 | /* |
| 990 | * Determine the appropriate key, IV and MAC length. |
| 991 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 992 | |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 993 | keylen = cipher_info->key_bitlen / 8; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 994 | |
Hanno Becker | f122944 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 995 | #if defined(MBEDTLS_GCM_C) || \ |
| 996 | defined(MBEDTLS_CCM_C) || \ |
| 997 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 998 | if( cipher_info->mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 999 | cipher_info->mode == MBEDTLS_MODE_CCM || |
| 1000 | cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1001 | { |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1002 | size_t explicit_ivlen; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1003 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1004 | transform->maclen = 0; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1005 | mac_key_len = 0; |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 1006 | transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) & |
| 1007 | MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1008 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1009 | /* All modes haves 96-bit IVs; |
| 1010 | * GCM and CCM has 4 implicit and 8 explicit bytes |
| 1011 | * ChachaPoly has all 12 bytes implicit |
| 1012 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1013 | transform->ivlen = 12; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1014 | if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) |
| 1015 | transform->fixed_ivlen = 12; |
| 1016 | else |
| 1017 | transform->fixed_ivlen = 4; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1018 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1019 | /* Minimum length of encrypted record */ |
| 1020 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1021 | transform->minlen = explicit_ivlen + transform->taglen; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1022 | } |
| 1023 | else |
Hanno Becker | f122944 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1024 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 1025 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
| 1026 | if( cipher_info->mode == MBEDTLS_MODE_STREAM || |
| 1027 | cipher_info->mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1028 | { |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1029 | /* Initialize HMAC contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1030 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 1031 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1032 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1033 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1034 | return( ret ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1035 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1036 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1037 | /* Get MAC length */ |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1038 | mac_key_len = mbedtls_md_get_size( md_info ); |
| 1039 | transform->maclen = mac_key_len; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1040 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1041 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1042 | /* |
| 1043 | * If HMAC is to be truncated, we shall keep the leftmost bytes, |
| 1044 | * (rfc 6066 page 13 or rfc 2104 section 4), |
| 1045 | * so we only need to adjust the length here. |
| 1046 | */ |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1047 | if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED ) |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1048 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1049 | transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN; |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1050 | |
| 1051 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT) |
| 1052 | /* Fall back to old, non-compliant version of the truncated |
Hanno Becker | 563423f | 2017-11-21 17:20:17 +0000 | [diff] [blame] | 1053 | * HMAC implementation which also truncates the key |
| 1054 | * (Mbed TLS versions from 1.3 to 2.6.0) */ |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1055 | mac_key_len = transform->maclen; |
| 1056 | #endif |
| 1057 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1058 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1059 | |
| 1060 | /* IV length */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1061 | transform->ivlen = cipher_info->iv_size; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1062 | |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1063 | /* Minimum length */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1064 | if( cipher_info->mode == MBEDTLS_MODE_STREAM ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1065 | transform->minlen = transform->maclen; |
| 1066 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1067 | { |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1068 | /* |
| 1069 | * GenericBlockCipher: |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 1070 | * 1. if EtM is in use: one block plus MAC |
| 1071 | * otherwise: * first multiple of blocklen greater than maclen |
| 1072 | * 2. IV except for SSL3 and TLS 1.0 |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1073 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1074 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1075 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 1076 | { |
| 1077 | transform->minlen = transform->maclen |
| 1078 | + cipher_info->block_size; |
| 1079 | } |
| 1080 | else |
| 1081 | #endif |
| 1082 | { |
| 1083 | transform->minlen = transform->maclen |
| 1084 | + cipher_info->block_size |
| 1085 | - transform->maclen % cipher_info->block_size; |
| 1086 | } |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1087 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1088 | #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] | 1089 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 || |
| 1090 | minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1091 | ; /* No need to adjust minlen */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1092 | else |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1093 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1094 | #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] | 1095 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 || |
| 1096 | minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1097 | { |
| 1098 | transform->minlen += transform->ivlen; |
| 1099 | } |
| 1100 | else |
| 1101 | #endif |
| 1102 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1103 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1104 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1105 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1106 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1107 | } |
Hanno Becker | f122944 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1108 | else |
| 1109 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
| 1110 | { |
| 1111 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1112 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1113 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1114 | |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1115 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 1116 | (unsigned) keylen, |
| 1117 | (unsigned) transform->minlen, |
| 1118 | (unsigned) transform->ivlen, |
| 1119 | (unsigned) transform->maclen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1120 | |
| 1121 | /* |
| 1122 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 1123 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1124 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1125 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1126 | { |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1127 | key1 = keyblk + mac_key_len * 2; |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1128 | key2 = keyblk + mac_key_len * 2 + keylen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1129 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1130 | mac_enc = keyblk; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1131 | mac_dec = keyblk + mac_key_len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1132 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1133 | /* |
| 1134 | * This is not used in TLS v1.1. |
| 1135 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1136 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 1137 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1138 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 1139 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1140 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1141 | } |
| 1142 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1143 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 1144 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1145 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1146 | { |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1147 | key1 = keyblk + mac_key_len * 2 + keylen; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1148 | key2 = keyblk + mac_key_len * 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1149 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1150 | mac_enc = keyblk + mac_key_len; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1151 | mac_dec = keyblk; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1152 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1153 | /* |
| 1154 | * This is not used in TLS v1.1. |
| 1155 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1156 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 1157 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1158 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 1159 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1160 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1161 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1162 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1163 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1164 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1165 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1166 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1167 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1168 | |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1169 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1170 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1171 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1172 | { |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1173 | if( mac_key_len > sizeof( transform->mac_enc ) ) |
Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1174 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1175 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1176 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1177 | } |
| 1178 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1179 | memcpy( transform->mac_enc, mac_enc, mac_key_len ); |
| 1180 | memcpy( transform->mac_dec, mac_dec, mac_key_len ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1181 | } |
| 1182 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1183 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 1184 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 1185 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1186 | if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1187 | { |
Gilles Peskine | 039fd12 | 2018-03-19 19:06:08 +0100 | [diff] [blame] | 1188 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 1189 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 1190 | if( mac_key_len != 0 ) |
| 1191 | { |
| 1192 | mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 1193 | mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 1194 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1195 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1196 | else |
| 1197 | #endif |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1198 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1199 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1200 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1201 | } |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1202 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1203 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1204 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 1205 | if( mbedtls_ssl_hw_record_init != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1206 | { |
| 1207 | int ret = 0; |
| 1208 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1209 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1210 | |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1211 | if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen, |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 1212 | transform->iv_enc, transform->iv_dec, |
| 1213 | iv_copy_len, |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1214 | mac_enc, mac_dec, |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1215 | mac_key_len ) ) != 0 ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1216 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1217 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret ); |
| 1218 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1219 | } |
| 1220 | } |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1221 | #else |
| 1222 | ((void) mac_dec); |
| 1223 | ((void) mac_enc); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1224 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1225 | |
Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1226 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 1227 | if( ssl->conf->f_export_keys != NULL ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1228 | { |
Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1229 | ssl->conf->f_export_keys( ssl->conf->p_export_keys, |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1230 | master, keyblk, |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1231 | mac_key_len, keylen, |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1232 | iv_copy_len ); |
| 1233 | } |
| 1234 | #endif |
| 1235 | |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1236 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1237 | cipher_info ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1238 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1239 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1240 | return( ret ); |
| 1241 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1242 | |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1243 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1244 | cipher_info ) ) != 0 ) |
| 1245 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1246 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1247 | return( ret ); |
| 1248 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1249 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1250 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1251 | cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1252 | MBEDTLS_ENCRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1253 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1254 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1255 | return( ret ); |
| 1256 | } |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1257 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1258 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1259 | cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1260 | MBEDTLS_DECRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1261 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1262 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1263 | return( ret ); |
| 1264 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1265 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1266 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 1267 | if( cipher_info->mode == MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1268 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1269 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 1270 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1271 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1272 | 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] | 1273 | return( ret ); |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1274 | } |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1275 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1276 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 1277 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1278 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1279 | 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] | 1280 | return( ret ); |
| 1281 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1282 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1283 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1284 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 1285 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1286 | |
Manuel Pégourié-Gonnard | a1abb26 | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1287 | /* Initialize Zlib contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1288 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1289 | if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1290 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1291 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1292 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1293 | memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) ); |
| 1294 | memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1295 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1296 | if( deflateInit( &transform->ctx_deflate, |
| 1297 | Z_DEFAULT_COMPRESSION ) != Z_OK || |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1298 | inflateInit( &transform->ctx_inflate ) != Z_OK ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1299 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1300 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) ); |
| 1301 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1302 | } |
| 1303 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1304 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1305 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1306 | return( 0 ); |
| 1307 | } |
| 1308 | |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1309 | /* |
Manuel Pégourié-Gonnard | 42c814f | 2019-05-20 10:10:17 +0200 | [diff] [blame] | 1310 | * 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] | 1311 | * |
| 1312 | * Inputs: |
| 1313 | * - SSL/TLS minor version |
| 1314 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 1315 | * |
Manuel Pégourié-Gonnard | cf31216 | 2019-05-10 10:25:00 +0200 | [diff] [blame] | 1316 | * Outputs: |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1317 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 1318 | */ |
| 1319 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
| 1320 | int minor_ver, |
| 1321 | mbedtls_md_type_t hash ) |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1322 | { |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1323 | #if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C) |
| 1324 | (void) hash; |
| 1325 | #endif |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1326 | |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1327 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1328 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1329 | { |
| 1330 | handshake->tls_prf = ssl3_prf; |
| 1331 | handshake->calc_verify = ssl_calc_verify_ssl; |
| 1332 | handshake->calc_finished = ssl_calc_finished_ssl; |
| 1333 | } |
| 1334 | else |
| 1335 | #endif |
| 1336 | #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] | 1337 | if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1338 | { |
| 1339 | handshake->tls_prf = tls1_prf; |
| 1340 | handshake->calc_verify = ssl_calc_verify_tls; |
| 1341 | handshake->calc_finished = ssl_calc_finished_tls; |
| 1342 | } |
| 1343 | else |
| 1344 | #endif |
| 1345 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1346 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1347 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
| 1348 | hash == MBEDTLS_MD_SHA384 ) |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1349 | { |
| 1350 | handshake->tls_prf = tls_prf_sha384; |
| 1351 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 1352 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 1353 | } |
| 1354 | else |
| 1355 | #endif |
| 1356 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1357 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1358 | { |
| 1359 | handshake->tls_prf = tls_prf_sha256; |
| 1360 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 1361 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 1362 | } |
| 1363 | else |
| 1364 | #endif |
| 1365 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1366 | { |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1367 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1368 | } |
| 1369 | |
| 1370 | return( 0 ); |
| 1371 | } |
| 1372 | |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1373 | /* |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1374 | * Compute master secret if needed |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1375 | * |
| 1376 | * Parameters: |
| 1377 | * [in/out] handshake |
| 1378 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
| 1379 | * [out] premaster (cleared) |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1380 | * [out] master |
| 1381 | * [in] ssl: optionally used for debugging and calc_verify |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1382 | */ |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1383 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1384 | unsigned char *master, |
Manuel Pégourié-Gonnard | ed3b7a9 | 2019-05-03 09:58:33 +0200 | [diff] [blame] | 1385 | const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1386 | { |
| 1387 | int ret; |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1388 | |
| 1389 | #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] | 1390 | 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] | 1391 | (void) ssl; |
| 1392 | #endif |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1393 | |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 1394 | #if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION) |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1395 | if( handshake->resume != 0 ) |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1396 | { |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1397 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
| 1398 | return( 0 ); |
| 1399 | } |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 1400 | #endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */ |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1401 | |
| 1402 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster, |
| 1403 | handshake->pmslen ); |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1404 | |
| 1405 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Hanno Becker | a49ec56 | 2019-06-11 14:47:55 +0100 | [diff] [blame] | 1406 | if( mbedtls_ssl_hs_get_extended_ms( handshake ) |
| 1407 | == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1408 | { |
| 1409 | unsigned char session_hash[48]; |
| 1410 | size_t hash_len; |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1411 | |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1412 | handshake->calc_verify( ssl, session_hash, &hash_len ); |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1413 | |
Manuel Pégourié-Gonnard | 9c5bcc9 | 2019-05-20 12:09:50 +0200 | [diff] [blame] | 1414 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
| 1415 | session_hash, hash_len ); |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1416 | |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1417 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1418 | "extended master secret", |
| 1419 | session_hash, hash_len, |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1420 | master, 48 ); |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1421 | } |
| 1422 | else |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1423 | #endif |
Manuel Pégourié-Gonnard | bcf258e | 2019-05-03 11:46:27 +0200 | [diff] [blame] | 1424 | { |
| 1425 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
| 1426 | "master secret", |
| 1427 | handshake->randbytes, 64, |
| 1428 | master, 48 ); |
| 1429 | } |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1430 | if( ret != 0 ) |
| 1431 | { |
| 1432 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 1433 | return( ret ); |
| 1434 | } |
| 1435 | |
| 1436 | mbedtls_platform_zeroize( handshake->premaster, |
| 1437 | sizeof(handshake->premaster) ); |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1438 | |
| 1439 | return( 0 ); |
| 1440 | } |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1441 | |
Manuel Pégourié-Gonnard | 5ed5e90 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1442 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 1443 | { |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1444 | int ret; |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 1445 | mbedtls_ssl_ciphersuite_handle_t const ciphersuite_info = |
| 1446 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1447 | |
Manuel Pégourié-Gonnard | 707728d | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1448 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 1449 | |
| 1450 | /* Set PRF, calc_verify and calc_finished function pointers */ |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1451 | ret = ssl_set_handshake_prfs( ssl->handshake, |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 1452 | mbedtls_ssl_get_minor_ver( ssl ), |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 1453 | mbedtls_ssl_suite_get_mac( ciphersuite_info ) ); |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1454 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1455 | { |
| 1456 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1457 | return( ret ); |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1458 | } |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1459 | |
Manuel Pégourié-Gonnard | 707728d | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1460 | /* Compute master secret if needed */ |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1461 | ret = ssl_compute_master( ssl->handshake, |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1462 | ssl->session_negotiate->master, |
| 1463 | ssl ); |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1464 | if( ret != 0 ) |
| 1465 | { |
| 1466 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 1467 | return( ret ); |
| 1468 | } |
| 1469 | |
Manuel Pégourié-Gonnard | 707728d | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1470 | /* Swap the client and server random values: |
| 1471 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 1472 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 1473 | { |
| 1474 | unsigned char tmp[64]; |
| 1475 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 1476 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 1477 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 1478 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 1479 | } |
| 1480 | |
| 1481 | /* Populate transform structure */ |
Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1482 | ret = ssl_populate_transform( ssl->transform_negotiate, |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1483 | mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ), |
| 1484 | ssl->session_negotiate->master, |
Jaeden Amero | 2eaf2c7 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 1485 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1486 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1487 | ssl->session_negotiate->encrypt_then_mac, |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1488 | #endif |
| 1489 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1490 | ssl->session_negotiate->trunc_hmac, |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1491 | #endif |
Jaeden Amero | 2eaf2c7 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 1492 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1493 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1494 | ssl->session_negotiate->compression, |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1495 | #endif |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1496 | ssl->handshake->tls_prf, |
| 1497 | ssl->handshake->randbytes, |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 1498 | mbedtls_ssl_get_minor_ver( ssl ), |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1499 | mbedtls_ssl_conf_get_endpoint( ssl->conf ), |
| 1500 | ssl ); |
Manuel Pégourié-Gonnard | 707728d | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1501 | if( ret != 0 ) |
| 1502 | { |
| 1503 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret ); |
| 1504 | return( ret ); |
| 1505 | } |
| 1506 | |
| 1507 | /* We no longer need Server/ClientHello.random values */ |
| 1508 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 1509 | sizeof( ssl->handshake->randbytes ) ); |
| 1510 | |
Manuel Pégourié-Gonnard | a1abb26 | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1511 | /* Allocate compression buffer */ |
| 1512 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 1513 | if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE && |
| 1514 | ssl->compress_buf == NULL ) |
| 1515 | { |
| 1516 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) ); |
| 1517 | ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN ); |
| 1518 | if( ssl->compress_buf == NULL ) |
| 1519 | { |
| 1520 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
Manuel Pégourié-Gonnard | 762d011 | 2019-05-20 10:27:20 +0200 | [diff] [blame] | 1521 | MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) ); |
Manuel Pégourié-Gonnard | a1abb26 | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1522 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1523 | } |
| 1524 | } |
| 1525 | #endif |
| 1526 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1527 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 1528 | |
| 1529 | return( 0 ); |
| 1530 | } |
| 1531 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1532 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1533 | void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl, |
| 1534 | unsigned char hash[36], |
| 1535 | size_t *hlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1536 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1537 | mbedtls_md5_context md5; |
| 1538 | mbedtls_sha1_context sha1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1539 | unsigned char pad_1[48]; |
| 1540 | unsigned char pad_2[48]; |
| 1541 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1542 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1543 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1544 | mbedtls_md5_init( &md5 ); |
| 1545 | mbedtls_sha1_init( &sha1 ); |
| 1546 | |
| 1547 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 1548 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1549 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1550 | memset( pad_1, 0x36, 48 ); |
| 1551 | memset( pad_2, 0x5C, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1552 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1553 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); |
| 1554 | mbedtls_md5_update_ret( &md5, pad_1, 48 ); |
| 1555 | mbedtls_md5_finish_ret( &md5, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1556 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1557 | mbedtls_md5_starts_ret( &md5 ); |
| 1558 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); |
| 1559 | mbedtls_md5_update_ret( &md5, pad_2, 48 ); |
| 1560 | mbedtls_md5_update_ret( &md5, hash, 16 ); |
| 1561 | mbedtls_md5_finish_ret( &md5, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1562 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1563 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); |
| 1564 | mbedtls_sha1_update_ret( &sha1, pad_1, 40 ); |
| 1565 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1566 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1567 | mbedtls_sha1_starts_ret( &sha1 ); |
| 1568 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); |
| 1569 | mbedtls_sha1_update_ret( &sha1, pad_2, 40 ); |
| 1570 | mbedtls_sha1_update_ret( &sha1, hash + 16, 20 ); |
| 1571 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1572 | |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1573 | *hlen = 36; |
| 1574 | |
| 1575 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1576 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1577 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1578 | mbedtls_md5_free( &md5 ); |
| 1579 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1580 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1581 | return; |
| 1582 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1583 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1584 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1585 | #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] | 1586 | void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl, |
| 1587 | unsigned char hash[36], |
| 1588 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1589 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1590 | mbedtls_md5_context md5; |
| 1591 | mbedtls_sha1_context sha1; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1592 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1593 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1594 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1595 | mbedtls_md5_init( &md5 ); |
| 1596 | mbedtls_sha1_init( &sha1 ); |
| 1597 | |
| 1598 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 1599 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1600 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1601 | mbedtls_md5_finish_ret( &md5, hash ); |
| 1602 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1603 | |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1604 | *hlen = 36; |
| 1605 | |
| 1606 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1607 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1608 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1609 | mbedtls_md5_free( &md5 ); |
| 1610 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1611 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1612 | return; |
| 1613 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1614 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1615 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1616 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1617 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1618 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
| 1619 | unsigned char hash[32], |
| 1620 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1621 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1622 | mbedtls_sha256_context sha256; |
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_sha256_init( &sha256 ); |
| 1625 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1626 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1627 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1628 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1629 | mbedtls_sha256_finish_ret( &sha256, hash ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1630 | |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1631 | *hlen = 32; |
| 1632 | |
| 1633 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1634 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1635 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1636 | mbedtls_sha256_free( &sha256 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1637 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1638 | return; |
| 1639 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1640 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1641 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1642 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1643 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
| 1644 | unsigned char hash[48], |
| 1645 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1646 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1647 | mbedtls_sha512_context sha512; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1648 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1649 | mbedtls_sha512_init( &sha512 ); |
| 1650 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1651 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1652 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1653 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1654 | mbedtls_sha512_finish_ret( &sha512, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1655 | |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1656 | *hlen = 48; |
| 1657 | |
| 1658 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1659 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1660 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1661 | mbedtls_sha512_free( &sha512 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1662 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1663 | return; |
| 1664 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1665 | #endif /* MBEDTLS_SHA512_C */ |
| 1666 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1667 | |
Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 1668 | int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl ) |
| 1669 | { |
| 1670 | int ret; |
| 1671 | |
| 1672 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
| 1673 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
| 1674 | |
| 1675 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) |
| 1676 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 1677 | == MBEDTLS_KEY_EXCHANGE_DHE_RSA ) |
| 1678 | { |
| 1679 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
| 1680 | ssl->handshake->premaster, |
| 1681 | MBEDTLS_PREMASTER_SIZE, |
| 1682 | &ssl->handshake->pmslen, |
| 1683 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 1684 | mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 ) |
Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 1685 | { |
| 1686 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
| 1687 | return( ret ); |
| 1688 | } |
| 1689 | |
| 1690 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
| 1691 | } |
| 1692 | else |
| 1693 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */ |
| 1694 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 1695 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ |
| 1696 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 1697 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
| 1698 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 1699 | == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || |
| 1700 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 1701 | == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || |
| 1702 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 1703 | == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || |
| 1704 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 1705 | == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) |
| 1706 | { |
| 1707 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, |
| 1708 | &ssl->handshake->pmslen, |
| 1709 | ssl->handshake->premaster, |
| 1710 | MBEDTLS_MPI_MAX_SIZE, |
| 1711 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 1712 | mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 ) |
Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 1713 | { |
| 1714 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
| 1715 | return( ret ); |
| 1716 | } |
| 1717 | |
| 1718 | MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z ); |
| 1719 | } |
| 1720 | else |
| 1721 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
| 1722 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || |
| 1723 | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || |
| 1724 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
| 1725 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 1726 | if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) ) |
| 1727 | { |
| 1728 | if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, |
Manuel Pégourié-Gonnard | fb02e96 | 2019-08-01 10:48:49 +0200 | [diff] [blame] | 1729 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ) ) != 0 ) |
Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 1730 | { |
| 1731 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret ); |
| 1732 | return( ret ); |
| 1733 | } |
| 1734 | } |
| 1735 | else |
| 1736 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
| 1737 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 1738 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == |
| 1739 | MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
| 1740 | { |
| 1741 | ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx, |
| 1742 | ssl->handshake->premaster, 32, &ssl->handshake->pmslen, |
| 1743 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 1744 | mbedtls_ssl_conf_get_prng( ssl->conf ) ); |
Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 1745 | if( ret != 0 ) |
| 1746 | { |
| 1747 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret ); |
| 1748 | return( ret ); |
| 1749 | } |
| 1750 | } |
| 1751 | else |
| 1752 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
| 1753 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) |
| 1754 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 1755 | == MBEDTLS_KEY_EXCHANGE_RSA ) |
| 1756 | { |
| 1757 | ((void) ret); |
Manuel Pégourié-Gonnard | 8793fab | 2019-08-01 10:44:07 +0200 | [diff] [blame] | 1758 | /* The premaster secret has already been set by |
Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 1759 | * ssl_rsa_generate_partial_pms(). Only the |
| 1760 | * PMS length needs to be set. */ |
| 1761 | ssl->handshake->pmslen = 48; |
| 1762 | } |
| 1763 | else |
| 1764 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
| 1765 | { |
| 1766 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1767 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1768 | } |
| 1769 | |
| 1770 | return( 0 ); |
| 1771 | } |
| 1772 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1773 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 1774 | 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] | 1775 | { |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1776 | unsigned char *p = ssl->handshake->premaster; |
| 1777 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1778 | const unsigned char *psk = ssl->conf->psk; |
| 1779 | size_t psk_len = ssl->conf->psk_len; |
| 1780 | |
| 1781 | /* If the psk callback was called, use its result */ |
| 1782 | if( ssl->handshake->psk != NULL ) |
| 1783 | { |
| 1784 | psk = ssl->handshake->psk; |
| 1785 | psk_len = ssl->handshake->psk_len; |
| 1786 | } |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1787 | |
| 1788 | /* |
| 1789 | * PMS = struct { |
| 1790 | * opaque other_secret<0..2^16-1>; |
| 1791 | * opaque psk<0..2^16-1>; |
| 1792 | * }; |
| 1793 | * with "other_secret" depending on the particular key exchange |
| 1794 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1795 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1796 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1797 | { |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1798 | if( end - p < 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1799 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1800 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1801 | *(p++) = (unsigned char)( psk_len >> 8 ); |
| 1802 | *(p++) = (unsigned char)( psk_len ); |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1803 | |
| 1804 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 1805 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1806 | |
| 1807 | memset( p, 0, psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1808 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1809 | } |
| 1810 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1811 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 1812 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 1813 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1814 | { |
| 1815 | /* |
| 1816 | * other_secret already set by the ClientKeyExchange message, |
| 1817 | * and is 48 bytes long |
| 1818 | */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1819 | if( end - p < 2 ) |
| 1820 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1821 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1822 | *p++ = 0; |
| 1823 | *p++ = 48; |
| 1824 | p += 48; |
| 1825 | } |
| 1826 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1827 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 1828 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 1829 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1830 | { |
Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 1831 | int ret; |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1832 | size_t len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1833 | |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1834 | /* Write length only when we know the actual value */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1835 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1836 | p + 2, end - ( p + 2 ), &len, |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 1837 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 1838 | mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1839 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1840 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1841 | return( ret ); |
| 1842 | } |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1843 | *(p++) = (unsigned char)( len >> 8 ); |
| 1844 | *(p++) = (unsigned char)( len ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1845 | p += len; |
| 1846 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1847 | 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] | 1848 | } |
| 1849 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1850 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 1851 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 1852 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1853 | { |
Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 1854 | int ret; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1855 | size_t zlen; |
| 1856 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1857 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1858 | p + 2, end - ( p + 2 ), |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 1859 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 1860 | mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1861 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1862 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1863 | return( ret ); |
| 1864 | } |
| 1865 | |
| 1866 | *(p++) = (unsigned char)( zlen >> 8 ); |
| 1867 | *(p++) = (unsigned char)( zlen ); |
| 1868 | p += zlen; |
| 1869 | |
Janos Follath | 3fbdada | 2018-08-15 10:26:53 +0100 | [diff] [blame] | 1870 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 1871 | MBEDTLS_DEBUG_ECDH_Z ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1872 | } |
| 1873 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1874 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1875 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1876 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1877 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | /* opaque psk<0..2^16-1>; */ |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1881 | if( end - p < 2 ) |
| 1882 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 1883 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1884 | *(p++) = (unsigned char)( psk_len >> 8 ); |
| 1885 | *(p++) = (unsigned char)( psk_len ); |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1886 | |
| 1887 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 1888 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1889 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1890 | memcpy( p, psk, psk_len ); |
| 1891 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1892 | |
| 1893 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 1894 | |
| 1895 | return( 0 ); |
| 1896 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1897 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1898 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1899 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1900 | /* |
| 1901 | * SSLv3.0 MAC functions |
| 1902 | */ |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 1903 | #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] | 1904 | static void ssl_mac( mbedtls_md_context_t *md_ctx, |
| 1905 | const unsigned char *secret, |
| 1906 | const unsigned char *buf, size_t len, |
| 1907 | const unsigned char *ctr, int type, |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 1908 | unsigned char out[SSL_MAC_MAX_BYTES] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1909 | { |
| 1910 | unsigned char header[11]; |
| 1911 | unsigned char padding[48]; |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 1912 | int padlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1913 | int md_size = mbedtls_md_get_size( md_ctx->md_info ); |
| 1914 | int md_type = mbedtls_md_get_type( md_ctx->md_info ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1915 | |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 1916 | /* Only MD5 and SHA-1 supported */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1917 | if( md_type == MBEDTLS_MD_MD5 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1918 | padlen = 48; |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 1919 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1920 | padlen = 40; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1921 | |
| 1922 | memcpy( header, ctr, 8 ); |
| 1923 | header[ 8] = (unsigned char) type; |
| 1924 | header[ 9] = (unsigned char)( len >> 8 ); |
| 1925 | header[10] = (unsigned char)( len ); |
| 1926 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1927 | memset( padding, 0x36, padlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1928 | mbedtls_md_starts( md_ctx ); |
| 1929 | mbedtls_md_update( md_ctx, secret, md_size ); |
| 1930 | mbedtls_md_update( md_ctx, padding, padlen ); |
| 1931 | mbedtls_md_update( md_ctx, header, 11 ); |
| 1932 | mbedtls_md_update( md_ctx, buf, len ); |
Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 1933 | mbedtls_md_finish( md_ctx, out ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1934 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1935 | memset( padding, 0x5C, padlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1936 | mbedtls_md_starts( md_ctx ); |
| 1937 | mbedtls_md_update( md_ctx, secret, md_size ); |
| 1938 | mbedtls_md_update( md_ctx, padding, padlen ); |
Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 1939 | mbedtls_md_update( md_ctx, out, md_size ); |
| 1940 | mbedtls_md_finish( md_ctx, out ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 1941 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1942 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 1943 | |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1944 | /* 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] | 1945 | * 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] | 1946 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \ |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1947 | ( defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 1948 | defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 1949 | defined(MBEDTLS_SSL_PROTO_TLS1_2) ) |
| 1950 | /* This function makes sure every byte in the memory region is accessed |
| 1951 | * (in ascending addresses order) */ |
| 1952 | static void ssl_read_memory( unsigned char *p, size_t len ) |
| 1953 | { |
| 1954 | unsigned char acc = 0; |
| 1955 | volatile unsigned char force; |
| 1956 | |
| 1957 | for( ; len != 0; p++, len-- ) |
| 1958 | acc ^= *p; |
| 1959 | |
| 1960 | force = acc; |
| 1961 | (void) force; |
| 1962 | } |
| 1963 | #endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */ |
| 1964 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 1965 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1966 | * Encryption/decryption functions |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 1967 | */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1968 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1969 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 8969369 | 2019-05-20 15:06:12 +0100 | [diff] [blame] | 1970 | /* This functions transforms a DTLS plaintext fragment and a record content |
| 1971 | * type into an instance of the DTLSInnerPlaintext structure: |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1972 | * |
| 1973 | * struct { |
| 1974 | * opaque content[DTLSPlaintext.length]; |
| 1975 | * ContentType real_type; |
| 1976 | * uint8 zeros[length_of_padding]; |
| 1977 | * } DTLSInnerPlaintext; |
| 1978 | * |
| 1979 | * Input: |
| 1980 | * - `content`: The beginning of the buffer holding the |
| 1981 | * plaintext to be wrapped. |
| 1982 | * - `*content_size`: The length of the plaintext in Bytes. |
| 1983 | * - `max_len`: The number of Bytes available starting from |
| 1984 | * `content`. This must be `>= *content_size`. |
| 1985 | * - `rec_type`: The desired record content type. |
| 1986 | * |
| 1987 | * Output: |
| 1988 | * - `content`: The beginning of the resulting DTLSInnerPlaintext structure. |
| 1989 | * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure. |
| 1990 | * |
| 1991 | * Returns: |
| 1992 | * - `0` on success. |
| 1993 | * - A negative error code if `max_len` didn't offer enough space |
| 1994 | * for the expansion. |
| 1995 | */ |
| 1996 | static int ssl_cid_build_inner_plaintext( unsigned char *content, |
| 1997 | size_t *content_size, |
| 1998 | size_t remaining, |
| 1999 | uint8_t rec_type ) |
| 2000 | { |
| 2001 | size_t len = *content_size; |
Hanno Becker | 7842609 | 2019-05-13 15:31:17 +0100 | [diff] [blame] | 2002 | size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY - |
| 2003 | ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) % |
| 2004 | MBEDTLS_SSL_CID_PADDING_GRANULARITY; |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2005 | |
| 2006 | /* Write real content type */ |
| 2007 | if( remaining == 0 ) |
| 2008 | return( -1 ); |
| 2009 | content[ len ] = rec_type; |
| 2010 | len++; |
| 2011 | remaining--; |
| 2012 | |
| 2013 | if( remaining < pad ) |
| 2014 | return( -1 ); |
| 2015 | memset( content + len, 0, pad ); |
| 2016 | len += pad; |
| 2017 | remaining -= pad; |
| 2018 | |
| 2019 | *content_size = len; |
| 2020 | return( 0 ); |
| 2021 | } |
| 2022 | |
Hanno Becker | 7dc2577 | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 2023 | /* This function parses a DTLSInnerPlaintext structure. |
| 2024 | * See ssl_cid_build_inner_plaintext() for details. */ |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2025 | static int ssl_cid_parse_inner_plaintext( unsigned char const *content, |
| 2026 | size_t *content_size, |
| 2027 | uint8_t *rec_type ) |
| 2028 | { |
| 2029 | size_t remaining = *content_size; |
| 2030 | |
| 2031 | /* Determine length of padding by skipping zeroes from the back. */ |
| 2032 | do |
| 2033 | { |
| 2034 | if( remaining == 0 ) |
| 2035 | return( -1 ); |
| 2036 | remaining--; |
| 2037 | } while( content[ remaining ] == 0 ); |
| 2038 | |
| 2039 | *content_size = remaining; |
| 2040 | *rec_type = content[ remaining ]; |
| 2041 | |
| 2042 | return( 0 ); |
| 2043 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2044 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2045 | |
Hanno Becker | 99abf51 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2046 | /* `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] | 2047 | * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2048 | static void ssl_extract_add_data_from_record( unsigned char* add_data, |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2049 | size_t *add_data_len, |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2050 | mbedtls_record *rec ) |
| 2051 | { |
Hanno Becker | 99abf51 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2052 | /* Quoting RFC 5246 (TLS 1.2): |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2053 | * |
| 2054 | * additional_data = seq_num + TLSCompressed.type + |
| 2055 | * TLSCompressed.version + TLSCompressed.length; |
| 2056 | * |
Hanno Becker | 99abf51 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2057 | * For the CID extension, this is extended as follows |
| 2058 | * (quoting draft-ietf-tls-dtls-connection-id-05, |
| 2059 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05): |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2060 | * |
| 2061 | * additional_data = seq_num + DTLSPlaintext.type + |
| 2062 | * DTLSPlaintext.version + |
Hanno Becker | 99abf51 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2063 | * cid + |
| 2064 | * cid_length + |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2065 | * length_of_DTLSInnerPlaintext; |
| 2066 | */ |
| 2067 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2068 | memcpy( add_data, rec->ctr, sizeof( rec->ctr ) ); |
| 2069 | add_data[8] = rec->type; |
Hanno Becker | 24ce1eb | 2019-05-20 15:01:46 +0100 | [diff] [blame] | 2070 | memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) ); |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2071 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2072 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 1f02f05 | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 2073 | if( rec->cid_len != 0 ) |
| 2074 | { |
| 2075 | memcpy( add_data + 11, rec->cid, rec->cid_len ); |
| 2076 | add_data[11 + rec->cid_len + 0] = rec->cid_len; |
| 2077 | add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF; |
| 2078 | add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF; |
| 2079 | *add_data_len = 13 + 1 + rec->cid_len; |
| 2080 | } |
| 2081 | else |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2082 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 1f02f05 | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 2083 | { |
| 2084 | add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF; |
| 2085 | add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF; |
| 2086 | *add_data_len = 13; |
| 2087 | } |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2090 | int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl, |
| 2091 | mbedtls_ssl_transform *transform, |
| 2092 | mbedtls_record *rec, |
| 2093 | int (*f_rng)(void *, unsigned char *, size_t), |
| 2094 | void *p_rng ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2095 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2096 | mbedtls_cipher_mode_t mode; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2097 | int auth_done = 0; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2098 | unsigned char * data; |
Hanno Becker | 28a0c4e | 2019-05-20 14:54:26 +0100 | [diff] [blame] | 2099 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ]; |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2100 | size_t add_data_len; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2101 | size_t post_avail; |
| 2102 | |
| 2103 | /* The SSL context is only used for debugging purposes! */ |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2104 | #if !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | 86e48c2 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 2105 | ssl = NULL; /* make sure we don't use it except for debug */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2106 | ((void) ssl); |
| 2107 | #endif |
| 2108 | |
| 2109 | /* The PRNG is used for dynamic IV generation that's used |
| 2110 | * for CBC transformations in TLS 1.1 and TLS 1.2. */ |
| 2111 | #if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
| 2112 | ( defined(MBEDTLS_AES_C) || \ |
| 2113 | defined(MBEDTLS_ARIA_C) || \ |
| 2114 | defined(MBEDTLS_CAMELLIA_C) ) && \ |
| 2115 | ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) ) |
| 2116 | ((void) f_rng); |
| 2117 | ((void) p_rng); |
| 2118 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2119 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2120 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2121 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2122 | if( transform == NULL ) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2123 | { |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2124 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) ); |
| 2125 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2126 | } |
Hanno Becker | 505089d | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 2127 | if( rec == NULL |
| 2128 | || rec->buf == NULL |
| 2129 | || rec->buf_len < rec->data_offset |
| 2130 | || rec->buf_len - rec->data_offset < rec->data_len |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2131 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 505089d | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 2132 | || rec->cid_len != 0 |
| 2133 | #endif |
| 2134 | ) |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2135 | { |
| 2136 | 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] | 2137 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2138 | } |
| 2139 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2140 | data = rec->buf + rec->data_offset; |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2141 | 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] | 2142 | MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload", |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2143 | data, rec->data_len ); |
| 2144 | |
| 2145 | mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ); |
| 2146 | |
| 2147 | if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
| 2148 | { |
| 2149 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d", |
| 2150 | (unsigned) rec->data_len, |
| 2151 | MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 2152 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2153 | } |
Manuel Pégourié-Gonnard | 60346be | 2014-11-21 11:38:37 +0100 | [diff] [blame] | 2154 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2155 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2156 | /* |
| 2157 | * Add CID information |
| 2158 | */ |
| 2159 | rec->cid_len = transform->out_cid_len; |
| 2160 | memcpy( rec->cid, transform->out_cid, transform->out_cid_len ); |
| 2161 | MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len ); |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2162 | |
| 2163 | if( rec->cid_len != 0 ) |
| 2164 | { |
| 2165 | /* |
Hanno Becker | 7dc2577 | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 2166 | * Wrap plaintext into DTLSInnerPlaintext structure. |
| 2167 | * See ssl_cid_build_inner_plaintext() for more information. |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2168 | * |
Hanno Becker | 7dc2577 | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 2169 | * Note that this changes `rec->data_len`, and hence |
| 2170 | * `post_avail` needs to be recalculated afterwards. |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2171 | */ |
| 2172 | if( ssl_cid_build_inner_plaintext( data, |
| 2173 | &rec->data_len, |
| 2174 | post_avail, |
| 2175 | rec->type ) != 0 ) |
| 2176 | { |
| 2177 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2178 | } |
| 2179 | |
| 2180 | rec->type = MBEDTLS_SSL_MSG_CID; |
| 2181 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2182 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2183 | |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2184 | post_avail = rec->buf_len - ( rec->data_len + rec->data_offset ); |
| 2185 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2186 | /* |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 2187 | * Add MAC before if needed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2188 | */ |
Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2189 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2190 | if( mode == MBEDTLS_MODE_STREAM || |
| 2191 | ( mode == MBEDTLS_MODE_CBC |
| 2192 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2193 | && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2194 | #endif |
| 2195 | ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2196 | { |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2197 | if( post_avail < transform->maclen ) |
| 2198 | { |
| 2199 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2200 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2201 | } |
| 2202 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2203 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 2204 | if( mbedtls_ssl_transform_get_minor_ver( transform ) == |
| 2205 | MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2206 | { |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 2207 | unsigned char mac[SSL_MAC_MAX_BYTES]; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2208 | ssl_mac( &transform->md_ctx_enc, transform->mac_enc, |
| 2209 | data, rec->data_len, rec->ctr, rec->type, mac ); |
| 2210 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2211 | } |
| 2212 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2213 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2214 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2215 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 2216 | if( mbedtls_ssl_transform_get_minor_ver( transform ) >= |
| 2217 | MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2218 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2219 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
| 2220 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2221 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2222 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2223 | mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data, |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2224 | add_data_len ); |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2225 | mbedtls_md_hmac_update( &transform->md_ctx_enc, |
| 2226 | data, rec->data_len ); |
| 2227 | mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac ); |
| 2228 | mbedtls_md_hmac_reset( &transform->md_ctx_enc ); |
| 2229 | |
| 2230 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2231 | } |
| 2232 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2233 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2234 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2235 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2236 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2237 | } |
| 2238 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2239 | MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len, |
| 2240 | transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2241 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2242 | rec->data_len += transform->maclen; |
| 2243 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2244 | auth_done++; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2245 | } |
Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2246 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2247 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2248 | /* |
| 2249 | * Encrypt |
| 2250 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2251 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
| 2252 | if( mode == MBEDTLS_MODE_STREAM ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2253 | { |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2254 | int ret; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2255 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2256 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2257 | "including %d bytes of padding", |
| 2258 | rec->data_len, 0 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2259 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2260 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc, |
| 2261 | transform->iv_enc, transform->ivlen, |
| 2262 | data, rec->data_len, |
| 2263 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2264 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2265 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2266 | return( ret ); |
| 2267 | } |
| 2268 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2269 | if( rec->data_len != olen ) |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2270 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2271 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2272 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2273 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2274 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2275 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2276 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2277 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2278 | #if defined(MBEDTLS_GCM_C) || \ |
| 2279 | defined(MBEDTLS_CCM_C) || \ |
| 2280 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2281 | if( mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2282 | mode == MBEDTLS_MODE_CCM || |
| 2283 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2284 | { |
Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 2285 | int ret; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2286 | unsigned char iv[12]; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2287 | size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2288 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2289 | /* Check that there's space for both the authentication tag |
| 2290 | * and the explicit IV before and after the record content. */ |
| 2291 | if( post_avail < transform->taglen || |
| 2292 | rec->data_offset < explicit_iv_len ) |
| 2293 | { |
| 2294 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2295 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2296 | } |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2297 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2298 | /* |
| 2299 | * Generate IV |
| 2300 | */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2301 | if( transform->ivlen == 12 && transform->fixed_ivlen == 4 ) |
| 2302 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2303 | /* GCM and CCM: fixed || explicit (=seqnum) */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2304 | memcpy( iv, transform->iv_enc, transform->fixed_ivlen ); |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2305 | memcpy( iv + transform->fixed_ivlen, rec->ctr, |
| 2306 | explicit_iv_len ); |
| 2307 | /* Prefix record content with explicit IV. */ |
| 2308 | memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len ); |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2309 | } |
| 2310 | else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 ) |
| 2311 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2312 | /* ChachaPoly: fixed XOR sequence number */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2313 | unsigned char i; |
| 2314 | |
| 2315 | memcpy( iv, transform->iv_enc, transform->fixed_ivlen ); |
| 2316 | |
| 2317 | for( i = 0; i < 8; i++ ) |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2318 | iv[i+4] ^= rec->ctr[i]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2319 | } |
| 2320 | else |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 2321 | { |
| 2322 | /* 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] | 2323 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2324 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 2325 | } |
| 2326 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2327 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 0888581 | 2019-04-26 13:34:37 +0100 | [diff] [blame] | 2328 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2329 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)", |
| 2330 | iv, transform->ivlen ); |
| 2331 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)", |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2332 | data - explicit_iv_len, explicit_iv_len ); |
| 2333 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD", |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2334 | add_data, add_data_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2335 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2336 | "including 0 bytes of padding", |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2337 | rec->data_len ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2338 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2339 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2340 | * Encrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2341 | */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2342 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2343 | if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc, |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2344 | iv, transform->ivlen, |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2345 | add_data, add_data_len, /* add data */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2346 | data, rec->data_len, /* source */ |
| 2347 | data, &rec->data_len, /* destination */ |
| 2348 | data + rec->data_len, transform->taglen ) ) != 0 ) |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2349 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2350 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2351 | return( ret ); |
| 2352 | } |
| 2353 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2354 | MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", |
| 2355 | data + rec->data_len, transform->taglen ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2356 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2357 | rec->data_len += transform->taglen + explicit_iv_len; |
| 2358 | rec->data_offset -= explicit_iv_len; |
| 2359 | post_avail -= transform->taglen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2360 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2361 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2362 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2363 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
| 2364 | #if defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2365 | ( 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] | 2366 | if( mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2367 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2368 | int ret; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2369 | size_t padlen, i; |
| 2370 | size_t olen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2371 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2372 | /* Currently we're always using minimal padding |
| 2373 | * (up to 255 bytes would be allowed). */ |
| 2374 | padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen; |
| 2375 | if( padlen == transform->ivlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2376 | padlen = 0; |
| 2377 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2378 | /* Check there's enough space in the buffer for the padding. */ |
| 2379 | if( post_avail < padlen + 1 ) |
| 2380 | { |
| 2381 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2382 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2383 | } |
| 2384 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2385 | for( i = 0; i <= padlen; i++ ) |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2386 | data[rec->data_len + i] = (unsigned char) padlen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2387 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2388 | rec->data_len += padlen + 1; |
| 2389 | post_avail -= padlen + 1; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2390 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2391 | #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] | 2392 | /* |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2393 | * Prepend per-record IV for block cipher in TLS v1.1 and up as per |
| 2394 | * Method 1 (6.2.3.2. in RFC4346 and RFC5246) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2395 | */ |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 2396 | if( mbedtls_ssl_transform_get_minor_ver( transform ) >= |
| 2397 | MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2398 | { |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2399 | if( f_rng == NULL ) |
| 2400 | { |
| 2401 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) ); |
| 2402 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2403 | } |
| 2404 | |
| 2405 | if( rec->data_offset < transform->ivlen ) |
| 2406 | { |
| 2407 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2408 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2409 | } |
| 2410 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2411 | /* |
| 2412 | * Generate IV |
| 2413 | */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2414 | ret = f_rng( p_rng, transform->iv_enc, transform->ivlen ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2415 | if( ret != 0 ) |
| 2416 | return( ret ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2417 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2418 | memcpy( data - transform->ivlen, transform->iv_enc, |
| 2419 | transform->ivlen ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2420 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2421 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2422 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2423 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2424 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2425 | "including %d bytes of IV and %d bytes of padding", |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2426 | rec->data_len, transform->ivlen, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 2427 | padlen + 1 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2428 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2429 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc, |
| 2430 | transform->iv_enc, |
| 2431 | transform->ivlen, |
| 2432 | data, rec->data_len, |
| 2433 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2434 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2435 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2436 | return( ret ); |
| 2437 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2438 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2439 | if( rec->data_len != olen ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2440 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2441 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2442 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2443 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2444 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2445 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 2446 | if( mbedtls_ssl_transform_get_minor_ver( transform ) < |
| 2447 | MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2448 | { |
| 2449 | /* |
| 2450 | * Save IV in SSL3 and TLS1 |
| 2451 | */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2452 | memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv, |
| 2453 | transform->ivlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2454 | } |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2455 | else |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2456 | #endif |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2457 | { |
| 2458 | data -= transform->ivlen; |
| 2459 | rec->data_offset -= transform->ivlen; |
| 2460 | rec->data_len += transform->ivlen; |
| 2461 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2462 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2463 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2464 | if( auth_done == 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2465 | { |
Hanno Becker | 3d8c907 | 2018-01-05 16:24:22 +0000 | [diff] [blame] | 2466 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
| 2467 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2468 | /* |
| 2469 | * MAC(MAC_write_key, seq_num + |
| 2470 | * TLSCipherText.type + |
| 2471 | * TLSCipherText.version + |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2472 | * length_of( (IV +) ENC(...) ) + |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2473 | * IV + // except for TLS 1.0 |
| 2474 | * ENC(content + padding + padding_length)); |
| 2475 | */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2476 | |
| 2477 | if( post_avail < transform->maclen) |
| 2478 | { |
| 2479 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2480 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2481 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2482 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2483 | 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] | 2484 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2485 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2486 | MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2487 | add_data_len ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2488 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2489 | mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data, |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2490 | add_data_len ); |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2491 | mbedtls_md_hmac_update( &transform->md_ctx_enc, |
| 2492 | data, rec->data_len ); |
| 2493 | mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac ); |
| 2494 | mbedtls_md_hmac_reset( &transform->md_ctx_enc ); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2495 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2496 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2497 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2498 | rec->data_len += transform->maclen; |
| 2499 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2500 | auth_done++; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2501 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2502 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2503 | } |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2504 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2505 | #endif /* MBEDTLS_CIPHER_MODE_CBC && |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2506 | ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2507 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2508 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2509 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2510 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2511 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2512 | /* Make extra sure authentication was performed, exactly once */ |
| 2513 | if( auth_done != 1 ) |
| 2514 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2515 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2516 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2517 | } |
| 2518 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2519 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2520 | |
| 2521 | return( 0 ); |
| 2522 | } |
| 2523 | |
Hanno Becker | 40478be | 2019-07-12 08:23:59 +0100 | [diff] [blame] | 2524 | int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl, |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2525 | mbedtls_ssl_transform *transform, |
| 2526 | mbedtls_record *rec ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2527 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2528 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2529 | mbedtls_cipher_mode_t mode; |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2530 | int ret, auth_done = 0; |
Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2531 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 2532 | size_t padlen = 0, correct = 1; |
| 2533 | #endif |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2534 | unsigned char* data; |
Hanno Becker | 28a0c4e | 2019-05-20 14:54:26 +0100 | [diff] [blame] | 2535 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ]; |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2536 | size_t add_data_len; |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2537 | |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2538 | #if !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | 86e48c2 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 2539 | ssl = NULL; /* make sure we don't use it except for debug */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2540 | ((void) ssl); |
| 2541 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2542 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2543 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2544 | if( rec == NULL || |
| 2545 | rec->buf == NULL || |
| 2546 | rec->buf_len < rec->data_offset || |
| 2547 | rec->buf_len - rec->data_offset < rec->data_len ) |
| 2548 | { |
| 2549 | 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] | 2550 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2551 | } |
| 2552 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2553 | data = rec->buf + rec->data_offset; |
| 2554 | mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2555 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2556 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2557 | /* |
| 2558 | * Match record's CID with incoming CID. |
| 2559 | */ |
Hanno Becker | abd7c89 | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 2560 | if( rec->cid_len != transform->in_cid_len || |
| 2561 | memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) |
| 2562 | { |
Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 2563 | return( MBEDTLS_ERR_SSL_UNEXPECTED_CID ); |
Hanno Becker | abd7c89 | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 2564 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2565 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2566 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2567 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
| 2568 | if( mode == MBEDTLS_MODE_STREAM ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2569 | { |
| 2570 | padlen = 0; |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2571 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec, |
| 2572 | transform->iv_dec, |
| 2573 | transform->ivlen, |
| 2574 | data, rec->data_len, |
| 2575 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2576 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2577 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2578 | return( ret ); |
| 2579 | } |
| 2580 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2581 | if( rec->data_len != olen ) |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2582 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2583 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2584 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2585 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2586 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2587 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2588 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2589 | #if defined(MBEDTLS_GCM_C) || \ |
| 2590 | defined(MBEDTLS_CCM_C) || \ |
| 2591 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2592 | if( mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2593 | mode == MBEDTLS_MODE_CCM || |
| 2594 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2595 | { |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2596 | unsigned char iv[12]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2597 | size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2598 | |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2599 | /* |
| 2600 | * Prepare IV from explicit and implicit data. |
| 2601 | */ |
| 2602 | |
| 2603 | /* Check that there's enough space for the explicit IV |
| 2604 | * (at the beginning of the record) and the MAC (at the |
| 2605 | * end of the record). */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2606 | if( rec->data_len < explicit_iv_len + transform->taglen ) |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 2607 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2608 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) " |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2609 | "+ taglen (%d)", rec->data_len, |
| 2610 | explicit_iv_len, transform->taglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2611 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 2612 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2613 | |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2614 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2615 | if( transform->ivlen == 12 && transform->fixed_ivlen == 4 ) |
| 2616 | { |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2617 | /* GCM and CCM: fixed || explicit */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2618 | |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2619 | /* Fixed */ |
| 2620 | memcpy( iv, transform->iv_dec, transform->fixed_ivlen ); |
| 2621 | /* Explicit */ |
| 2622 | memcpy( iv + transform->fixed_ivlen, data, 8 ); |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2623 | } |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2624 | else |
| 2625 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
| 2626 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 2627 | if( transform->ivlen == 12 && transform->fixed_ivlen == 12 ) |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2628 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2629 | /* ChachaPoly: fixed XOR sequence number */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2630 | unsigned char i; |
| 2631 | |
| 2632 | memcpy( iv, transform->iv_dec, transform->fixed_ivlen ); |
| 2633 | |
| 2634 | for( i = 0; i < 8; i++ ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2635 | iv[i+4] ^= rec->ctr[i]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2636 | } |
| 2637 | else |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2638 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2639 | { |
| 2640 | /* Reminder if we ever add an AEAD mode with a different size */ |
| 2641 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2642 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2643 | } |
| 2644 | |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2645 | /* Group changes to data, data_len, and add_data, because |
| 2646 | * add_data depends on data_len. */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2647 | data += explicit_iv_len; |
| 2648 | rec->data_offset += explicit_iv_len; |
| 2649 | rec->data_len -= explicit_iv_len + transform->taglen; |
| 2650 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2651 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2652 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD", |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2653 | add_data, add_data_len ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2654 | |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2655 | /* Because of the check above, we know that there are |
| 2656 | * explicit_iv_len Bytes preceeding data, and taglen |
| 2657 | * bytes following data + data_len. This justifies |
Hanno Becker | 07d420d | 2019-07-10 11:44:13 +0100 | [diff] [blame] | 2658 | * the debug message and the invocation of |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2659 | * mbedtls_cipher_auth_decrypt() below. */ |
| 2660 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2661 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2662 | MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len, |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2663 | transform->taglen ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2664 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2665 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2666 | * Decrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2667 | */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2668 | if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec, |
| 2669 | iv, transform->ivlen, |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2670 | add_data, add_data_len, |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2671 | data, rec->data_len, |
| 2672 | data, &olen, |
| 2673 | data + rec->data_len, |
| 2674 | transform->taglen ) ) != 0 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2675 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2676 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret ); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2677 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2678 | if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED ) |
| 2679 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2680 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2681 | return( ret ); |
| 2682 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2683 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2684 | |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2685 | /* Double-check that AEAD decryption doesn't change content length. */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2686 | if( olen != rec->data_len ) |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2687 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2688 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2689 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2690 | } |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2691 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2692 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2693 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
| 2694 | #if defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2695 | ( 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] | 2696 | if( mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2697 | { |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2698 | size_t minlen = 0; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2699 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2700 | /* |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2701 | * Check immediate ciphertext sanity |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2702 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2703 | #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] | 2704 | if( mbedtls_ssl_transform_get_minor_ver( transform ) >= |
| 2705 | MBEDTLS_SSL_MINOR_VERSION_2 ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2706 | { |
| 2707 | /* The ciphertext is prefixed with the CBC IV. */ |
| 2708 | minlen += transform->ivlen; |
| 2709 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2710 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2711 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2712 | /* Size considerations: |
| 2713 | * |
| 2714 | * - The CBC cipher text must not be empty and hence |
| 2715 | * at least of size transform->ivlen. |
| 2716 | * |
| 2717 | * Together with the potential IV-prefix, this explains |
| 2718 | * the first of the two checks below. |
| 2719 | * |
| 2720 | * - The record must contain a MAC, either in plain or |
| 2721 | * encrypted, depending on whether Encrypt-then-MAC |
| 2722 | * is used or not. |
| 2723 | * - If it is, the message contains the IV-prefix, |
| 2724 | * the CBC ciphertext, and the MAC. |
| 2725 | * - If it is not, the padded plaintext, and hence |
| 2726 | * the CBC ciphertext, has at least length maclen + 1 |
| 2727 | * because there is at least the padding length byte. |
| 2728 | * |
| 2729 | * As the CBC ciphertext is not empty, both cases give the |
| 2730 | * lower bound minlen + maclen + 1 on the record size, which |
| 2731 | * we test for in the second check below. |
| 2732 | */ |
| 2733 | if( rec->data_len < minlen + transform->ivlen || |
| 2734 | rec->data_len < minlen + transform->maclen + 1 ) |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2735 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2736 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) " |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2737 | "+ 1 ) ( + expl IV )", rec->data_len, |
| 2738 | transform->ivlen, |
| 2739 | transform->maclen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2740 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2741 | } |
| 2742 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2743 | /* |
| 2744 | * Authenticate before decrypt if enabled |
| 2745 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2746 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2747 | if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2748 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2749 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2750 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2751 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2752 | |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2753 | /* Update data_len in tandem with add_data. |
| 2754 | * |
| 2755 | * The subtraction is safe because of the previous check |
| 2756 | * data_len >= minlen + maclen + 1. |
| 2757 | * |
| 2758 | * Afterwards, we know that data + data_len is followed by at |
| 2759 | * least maclen Bytes, which justifies the call to |
| 2760 | * mbedtls_ssl_safer_memcmp() below. |
| 2761 | * |
| 2762 | * Further, we still know that data_len > minlen */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2763 | rec->data_len -= transform->maclen; |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2764 | 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] | 2765 | |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2766 | /* Calculate expected MAC. */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2767 | MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, |
| 2768 | add_data_len ); |
| 2769 | mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, |
| 2770 | add_data_len ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2771 | mbedtls_md_hmac_update( &transform->md_ctx_dec, |
| 2772 | data, rec->data_len ); |
| 2773 | mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect ); |
| 2774 | mbedtls_md_hmac_reset( &transform->md_ctx_dec ); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2775 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2776 | MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, |
| 2777 | transform->maclen ); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2778 | MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2779 | transform->maclen ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2780 | |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2781 | /* Compare expected MAC with MAC at the end of the record. */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2782 | if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect, |
| 2783 | transform->maclen ) != 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2784 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2785 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2786 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2787 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2788 | auth_done++; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2789 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2790 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2791 | |
| 2792 | /* |
| 2793 | * Check length sanity |
| 2794 | */ |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2795 | |
| 2796 | /* We know from above that data_len > minlen >= 0, |
| 2797 | * so the following check in particular implies that |
| 2798 | * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2799 | if( rec->data_len % transform->ivlen != 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2800 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2801 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0", |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2802 | rec->data_len, transform->ivlen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2803 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2804 | } |
| 2805 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2806 | #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] | 2807 | /* |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2808 | * 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] | 2809 | */ |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 2810 | if( mbedtls_ssl_transform_get_minor_ver( transform ) >= |
| 2811 | MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2812 | { |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2813 | /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2814 | memcpy( transform->iv_dec, data, transform->ivlen ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2815 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2816 | data += transform->ivlen; |
| 2817 | rec->data_offset += transform->ivlen; |
| 2818 | rec->data_len -= transform->ivlen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2819 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2820 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2821 | |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2822 | /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */ |
| 2823 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2824 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec, |
| 2825 | transform->iv_dec, transform->ivlen, |
| 2826 | data, rec->data_len, data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2827 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2828 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2829 | return( ret ); |
| 2830 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2831 | |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2832 | /* Double-check that length hasn't changed during decryption. */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2833 | if( rec->data_len != olen ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2834 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2835 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2836 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2837 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2838 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2839 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 2840 | if( mbedtls_ssl_transform_get_minor_ver( transform ) < |
| 2841 | MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2842 | { |
| 2843 | /* |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2844 | * Save IV in SSL3 and TLS1, where CBC decryption of consecutive |
| 2845 | * records is equivalent to CBC decryption of the concatenation |
| 2846 | * of the records; in other words, IVs are maintained across |
| 2847 | * record decryptions. |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2848 | */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2849 | memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv, |
| 2850 | transform->ivlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2851 | } |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2852 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2853 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2854 | /* Safe since data_len >= minlen + maclen + 1, so after having |
| 2855 | * subtracted at most minlen and maclen up to this point, |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2856 | * data_len > 0 (because of data_len % ivlen == 0, it's actually |
| 2857 | * >= ivlen ). */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2858 | padlen = data[rec->data_len - 1]; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2859 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2860 | if( auth_done == 1 ) |
| 2861 | { |
| 2862 | correct *= ( rec->data_len >= padlen + 1 ); |
| 2863 | padlen *= ( rec->data_len >= padlen + 1 ); |
| 2864 | } |
| 2865 | else |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2866 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2867 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2868 | if( rec->data_len < transform->maclen + padlen + 1 ) |
| 2869 | { |
| 2870 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)", |
| 2871 | rec->data_len, |
| 2872 | transform->maclen, |
| 2873 | padlen + 1 ) ); |
| 2874 | } |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 2875 | #endif |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2876 | |
| 2877 | correct *= ( rec->data_len >= transform->maclen + padlen + 1 ); |
| 2878 | padlen *= ( rec->data_len >= transform->maclen + padlen + 1 ); |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2879 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2880 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2881 | padlen++; |
| 2882 | |
| 2883 | /* Regardless of the validity of the padding, |
| 2884 | * we have data_len >= padlen here. */ |
| 2885 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2886 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 2887 | if( mbedtls_ssl_transform_get_minor_ver( transform ) == |
| 2888 | MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2889 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2890 | if( padlen > transform->ivlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2891 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2892 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 2893 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, " |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2894 | "should be no more than %d", |
| 2895 | padlen, transform->ivlen ) ); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 2896 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2897 | correct = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2898 | } |
| 2899 | } |
| 2900 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2901 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 2902 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2903 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 2904 | if( mbedtls_ssl_transform_get_minor_ver( transform ) > |
| 2905 | MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2906 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2907 | /* The padding check involves a series of up to 256 |
| 2908 | * consecutive memory reads at the end of the record |
| 2909 | * plaintext buffer. In order to hide the length and |
| 2910 | * validity of the padding, always perform exactly |
| 2911 | * `min(256,plaintext_len)` reads (but take into account |
| 2912 | * only the last `padlen` bytes for the padding check). */ |
| 2913 | size_t pad_count = 0; |
| 2914 | size_t real_count = 0; |
| 2915 | volatile unsigned char* const check = data; |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2916 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2917 | /* Index of first padding byte; it has been ensured above |
| 2918 | * that the subtraction is safe. */ |
| 2919 | size_t const padding_idx = rec->data_len - padlen; |
| 2920 | size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256; |
| 2921 | size_t const start_idx = rec->data_len - num_checks; |
| 2922 | size_t idx; |
Paul Bakker | 956c9e0 | 2013-12-19 14:42:28 +0100 | [diff] [blame] | 2923 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2924 | for( idx = start_idx; idx < rec->data_len; idx++ ) |
Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 2925 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2926 | real_count |= ( idx >= padding_idx ); |
| 2927 | pad_count += real_count * ( check[idx] == padlen - 1 ); |
Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 2928 | } |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2929 | correct &= ( pad_count == padlen ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2930 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2931 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 2932 | if( padlen > 0 && correct == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2933 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) ); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 2934 | #endif |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2935 | padlen &= correct * 0x1FF; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2936 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2937 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2938 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 2939 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2940 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2941 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2942 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2943 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2944 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2945 | /* If the padding was found to be invalid, padlen == 0 |
| 2946 | * and the subtraction is safe. If the padding was found valid, |
| 2947 | * padlen hasn't been changed and the previous assertion |
| 2948 | * data_len >= padlen still holds. */ |
| 2949 | rec->data_len -= padlen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2950 | } |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2951 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2952 | #endif /* MBEDTLS_CIPHER_MODE_CBC && |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2953 | ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2954 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2955 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2956 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2957 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2958 | |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 2959 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2960 | MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption", |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2961 | data, rec->data_len ); |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 2962 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2963 | |
| 2964 | /* |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2965 | * Authenticate if not done yet. |
| 2966 | * Compute the MAC regardless of the padding result (RFC4346, CBCTIME). |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2967 | */ |
Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2968 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2969 | if( auth_done == 0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2970 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2971 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 2972 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2973 | /* If the initial value of padlen was such that |
| 2974 | * data_len < maclen + padlen + 1, then padlen |
| 2975 | * got reset to 1, and the initial check |
| 2976 | * data_len >= minlen + maclen + 1 |
| 2977 | * guarantees that at this point we still |
| 2978 | * have at least data_len >= maclen. |
| 2979 | * |
| 2980 | * If the initial value of padlen was such that |
| 2981 | * data_len >= maclen + padlen + 1, then we have |
| 2982 | * subtracted either padlen + 1 (if the padding was correct) |
| 2983 | * or 0 (if the padding was incorrect) since then, |
| 2984 | * hence data_len >= maclen in any case. |
| 2985 | */ |
| 2986 | rec->data_len -= transform->maclen; |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2987 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2988 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2989 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 2990 | if( mbedtls_ssl_transform_get_minor_ver( transform ) == |
| 2991 | MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2992 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2993 | ssl_mac( &transform->md_ctx_dec, |
| 2994 | transform->mac_dec, |
| 2995 | data, rec->data_len, |
| 2996 | rec->ctr, rec->type, |
| 2997 | mac_expect ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2998 | } |
| 2999 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3000 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 3001 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 3002 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 3003 | if( mbedtls_ssl_transform_get_minor_ver( transform ) > |
| 3004 | MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3005 | { |
| 3006 | /* |
| 3007 | * Process MAC and always update for padlen afterwards to make |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3008 | * total time independent of padlen. |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3009 | * |
| 3010 | * Known timing attacks: |
| 3011 | * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf) |
| 3012 | * |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3013 | * To compensate for different timings for the MAC calculation |
| 3014 | * depending on how much padding was removed (which is determined |
| 3015 | * by padlen), process extra_run more blocks through the hash |
| 3016 | * function. |
| 3017 | * |
| 3018 | * The formula in the paper is |
| 3019 | * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 ) |
| 3020 | * where L1 is the size of the header plus the decrypted message |
| 3021 | * plus CBC padding and L2 is the size of the header plus the |
| 3022 | * decrypted message. This is for an underlying hash function |
| 3023 | * with 64-byte blocks. |
| 3024 | * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values |
| 3025 | * correctly. We round down instead of up, so -56 is the correct |
| 3026 | * value for our calculations instead of -55. |
| 3027 | * |
Gilles Peskine | 1bd9d58 | 2018-06-04 11:58:44 +0200 | [diff] [blame] | 3028 | * Repeat the formula rather than defining a block_size variable. |
| 3029 | * This avoids requiring division by a variable at runtime |
| 3030 | * (which would be marginally less efficient and would require |
| 3031 | * linking an extra division function in some builds). |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3032 | */ |
| 3033 | size_t j, extra_run = 0; |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3034 | unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE]; |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3035 | |
| 3036 | /* |
| 3037 | * The next two sizes are the minimum and maximum values of |
| 3038 | * in_msglen over all padlen values. |
| 3039 | * |
| 3040 | * They're independent of padlen, since we previously did |
Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3041 | * data_len -= padlen. |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3042 | * |
| 3043 | * Note that max_len + maclen is never more than the buffer |
| 3044 | * length, as we previously did in_msglen -= maclen too. |
| 3045 | */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3046 | const size_t max_len = rec->data_len + padlen; |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3047 | const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0; |
| 3048 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3049 | memset( tmp, 0, sizeof( tmp ) ); |
| 3050 | |
| 3051 | switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) ) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3052 | { |
Gilles Peskine | d0e55a4 | 2018-06-04 12:03:30 +0200 | [diff] [blame] | 3053 | #if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \ |
| 3054 | defined(MBEDTLS_SHA256_C) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3055 | case MBEDTLS_MD_MD5: |
| 3056 | case MBEDTLS_MD_SHA1: |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3057 | case MBEDTLS_MD_SHA256: |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3058 | /* 8 bytes of message size, 64-byte compression blocks */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3059 | extra_run = |
| 3060 | ( add_data_len + rec->data_len + padlen + 8 ) / 64 - |
| 3061 | ( add_data_len + rec->data_len + 8 ) / 64; |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3062 | break; |
| 3063 | #endif |
Gilles Peskine | a7fe25d | 2018-06-04 12:01:18 +0200 | [diff] [blame] | 3064 | #if defined(MBEDTLS_SHA512_C) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3065 | case MBEDTLS_MD_SHA384: |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3066 | /* 16 bytes of message size, 128-byte compression blocks */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3067 | extra_run = |
| 3068 | ( add_data_len + rec->data_len + padlen + 16 ) / 128 - |
| 3069 | ( add_data_len + rec->data_len + 16 ) / 128; |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3070 | break; |
| 3071 | #endif |
| 3072 | default: |
Gilles Peskine | 5c38984 | 2018-06-04 12:02:43 +0200 | [diff] [blame] | 3073 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3074 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3075 | } |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3076 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3077 | extra_run &= correct * 0xFF; |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3078 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3079 | mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, |
| 3080 | add_data_len ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3081 | mbedtls_md_hmac_update( &transform->md_ctx_dec, data, |
| 3082 | rec->data_len ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3083 | /* Make sure we access everything even when padlen > 0. This |
| 3084 | * makes the synchronisation requirements for just-in-time |
| 3085 | * Prime+Probe attacks much tighter and hopefully impractical. */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3086 | ssl_read_memory( data + rec->data_len, padlen ); |
| 3087 | mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3088 | |
| 3089 | /* Call mbedtls_md_process at least once due to cache attacks |
| 3090 | * that observe whether md_process() was called of not */ |
Manuel Pégourié-Gonnard | 47fede0 | 2015-04-29 01:35:48 +0200 | [diff] [blame] | 3091 | for( j = 0; j < extra_run + 1; j++ ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3092 | mbedtls_md_process( &transform->md_ctx_dec, tmp ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3093 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3094 | mbedtls_md_hmac_reset( &transform->md_ctx_dec ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3095 | |
| 3096 | /* Make sure we access all the memory that could contain the MAC, |
| 3097 | * before we check it in the next code block. This makes the |
| 3098 | * synchronisation requirements for just-in-time Prime+Probe |
| 3099 | * attacks much tighter and hopefully impractical. */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3100 | ssl_read_memory( data + min_len, |
| 3101 | max_len - min_len + transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3102 | } |
| 3103 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3104 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 3105 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3106 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3107 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3108 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3109 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3110 | |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3111 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3112 | MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen ); |
| 3113 | 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] | 3114 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3115 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3116 | if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect, |
| 3117 | transform->maclen ) != 0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3118 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3119 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 3120 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3121 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3122 | correct = 0; |
| 3123 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3124 | auth_done++; |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3125 | } |
Hanno Becker | dd3ab13 | 2018-10-17 14:43:14 +0100 | [diff] [blame] | 3126 | |
| 3127 | /* |
| 3128 | * Finally check the correct flag |
| 3129 | */ |
| 3130 | if( correct == 0 ) |
| 3131 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 3132 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3133 | |
| 3134 | /* Make extra sure authentication was performed, exactly once */ |
| 3135 | if( auth_done != 1 ) |
| 3136 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3137 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3138 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3139 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3140 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3141 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 3142 | if( rec->cid_len != 0 ) |
| 3143 | { |
| 3144 | ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len, |
| 3145 | &rec->type ); |
| 3146 | if( ret != 0 ) |
| 3147 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 3148 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3149 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 3150 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3151 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3152 | |
| 3153 | return( 0 ); |
| 3154 | } |
| 3155 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 3156 | #undef MAC_NONE |
| 3157 | #undef MAC_PLAINTEXT |
| 3158 | #undef MAC_CIPHERTEXT |
| 3159 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3160 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3161 | /* |
| 3162 | * Compression/decompression functions |
| 3163 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3164 | static int ssl_compress_buf( mbedtls_ssl_context *ssl ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3165 | { |
| 3166 | int ret; |
| 3167 | unsigned char *msg_post = ssl->out_msg; |
Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 3168 | ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3169 | size_t len_pre = ssl->out_msglen; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 3170 | unsigned char *msg_pre = ssl->compress_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3171 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3172 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3173 | |
Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 3174 | if( len_pre == 0 ) |
| 3175 | return( 0 ); |
| 3176 | |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3177 | memcpy( msg_pre, ssl->out_msg, len_pre ); |
| 3178 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3179 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3180 | ssl->out_msglen ) ); |
| 3181 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3182 | MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3183 | ssl->out_msg, ssl->out_msglen ); |
| 3184 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3185 | ssl->transform_out->ctx_deflate.next_in = msg_pre; |
| 3186 | ssl->transform_out->ctx_deflate.avail_in = len_pre; |
| 3187 | ssl->transform_out->ctx_deflate.next_out = msg_post; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3188 | 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] | 3189 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3190 | ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3191 | if( ret != Z_OK ) |
| 3192 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3193 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) ); |
| 3194 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3195 | } |
| 3196 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3197 | ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN - |
Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 3198 | ssl->transform_out->ctx_deflate.avail_out - bytes_written; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3199 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3200 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3201 | ssl->out_msglen ) ); |
| 3202 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3203 | MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3204 | ssl->out_msg, ssl->out_msglen ); |
| 3205 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3206 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3207 | |
| 3208 | return( 0 ); |
| 3209 | } |
| 3210 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3211 | static int ssl_decompress_buf( mbedtls_ssl_context *ssl ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3212 | { |
| 3213 | int ret; |
| 3214 | unsigned char *msg_post = ssl->in_msg; |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 3215 | ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3216 | size_t len_pre = ssl->in_msglen; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 3217 | unsigned char *msg_pre = ssl->compress_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3218 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3219 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3220 | |
Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 3221 | if( len_pre == 0 ) |
| 3222 | return( 0 ); |
| 3223 | |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3224 | memcpy( msg_pre, ssl->in_msg, len_pre ); |
| 3225 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3226 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3227 | ssl->in_msglen ) ); |
| 3228 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3229 | MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3230 | ssl->in_msg, ssl->in_msglen ); |
| 3231 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3232 | ssl->transform_in->ctx_inflate.next_in = msg_pre; |
| 3233 | ssl->transform_in->ctx_inflate.avail_in = len_pre; |
| 3234 | ssl->transform_in->ctx_inflate.next_out = msg_post; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3235 | ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN - |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 3236 | header_bytes; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3237 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3238 | ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3239 | if( ret != Z_OK ) |
| 3240 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3241 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) ); |
| 3242 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3243 | } |
| 3244 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3245 | ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN - |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 3246 | ssl->transform_in->ctx_inflate.avail_out - header_bytes; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3247 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3248 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3249 | ssl->in_msglen ) ); |
| 3250 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3251 | MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3252 | ssl->in_msg, ssl->in_msglen ); |
| 3253 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3254 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3255 | |
| 3256 | return( 0 ); |
| 3257 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3258 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3259 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3260 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3261 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3262 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3263 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3264 | static int ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3265 | { |
| 3266 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 3267 | * timeout if we were using the usual handshake doubling scheme */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3268 | if( ssl->conf->renego_max_records < 0 ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3269 | { |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 3270 | uint32_t ratio = |
| 3271 | mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) / |
| 3272 | mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1; |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3273 | unsigned char doublings = 1; |
| 3274 | |
| 3275 | while( ratio != 0 ) |
| 3276 | { |
| 3277 | ++doublings; |
| 3278 | ratio >>= 1; |
| 3279 | } |
| 3280 | |
| 3281 | if( ++ssl->renego_records_seen > doublings ) |
| 3282 | { |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 3283 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3284 | return( 0 ); |
| 3285 | } |
| 3286 | } |
| 3287 | |
| 3288 | return( ssl_write_hello_request( ssl ) ); |
| 3289 | } |
| 3290 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3291 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3292 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3293 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3294 | * Fill the input message buffer by appending data to it. |
| 3295 | * 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] | 3296 | * |
| 3297 | * If we return 0, is it guaranteed that (at least) nb_want bytes are |
| 3298 | * available (from this read and/or a previous one). Otherwise, an error code |
| 3299 | * is returned (possibly EOF or WANT_READ). |
| 3300 | * |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3301 | * With stream transport (TLS) on success ssl->in_left == nb_want, but |
| 3302 | * with datagram transport (DTLS) on success ssl->in_left >= nb_want, |
| 3303 | * since we always read a whole datagram at once. |
| 3304 | * |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 3305 | * 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] | 3306 | * they're done reading a record. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3307 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3308 | 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] | 3309 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 3310 | int ret; |
| 3311 | size_t len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3312 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3313 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3314 | |
Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 3315 | if( mbedtls_ssl_get_recv( ssl ) == NULL && |
| 3316 | mbedtls_ssl_get_recv_timeout( ssl ) == NULL ) |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3317 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3318 | 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] | 3319 | "or mbedtls_ssl_set_bio()" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3320 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3321 | } |
| 3322 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3323 | 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] | 3324 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3325 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) ); |
| 3326 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 3327 | } |
| 3328 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3329 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 25838b7 | 2019-03-19 10:23:56 +0100 | [diff] [blame] | 3330 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3331 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3332 | uint32_t timeout; |
| 3333 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3334 | /* Just to be sure */ |
Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 3335 | if( mbedtls_ssl_get_set_timer( ssl ) == NULL || |
| 3336 | mbedtls_ssl_get_get_timer( ssl ) == NULL ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3337 | { |
| 3338 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 3339 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 3340 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3341 | } |
| 3342 | |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3343 | /* |
| 3344 | * The point is, we need to always read a full datagram at once, so we |
| 3345 | * sometimes read more then requested, and handle the additional data. |
| 3346 | * It could be the rest of the current record (while fetching the |
| 3347 | * header) and/or some other records in the same datagram. |
| 3348 | */ |
| 3349 | |
| 3350 | /* |
| 3351 | * Move to the next record in the already read datagram if applicable |
| 3352 | */ |
| 3353 | if( ssl->next_record_offset != 0 ) |
| 3354 | { |
| 3355 | if( ssl->in_left < ssl->next_record_offset ) |
| 3356 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3357 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3358 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3359 | } |
| 3360 | |
| 3361 | ssl->in_left -= ssl->next_record_offset; |
| 3362 | |
| 3363 | if( ssl->in_left != 0 ) |
| 3364 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3365 | 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] | 3366 | ssl->next_record_offset ) ); |
| 3367 | memmove( ssl->in_hdr, |
| 3368 | ssl->in_hdr + ssl->next_record_offset, |
| 3369 | ssl->in_left ); |
| 3370 | } |
| 3371 | |
| 3372 | ssl->next_record_offset = 0; |
| 3373 | } |
| 3374 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3375 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3376 | ssl->in_left, nb_want ) ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3377 | |
| 3378 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3379 | * Done if we already have enough data. |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3380 | */ |
| 3381 | if( nb_want <= ssl->in_left) |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3382 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3383 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3384 | return( 0 ); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3385 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3386 | |
| 3387 | /* |
Antonin Décimo | d5f4759 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 3388 | * 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] | 3389 | * are not at the beginning of a new record, the caller did something |
| 3390 | * wrong. |
| 3391 | */ |
| 3392 | if( ssl->in_left != 0 ) |
| 3393 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3394 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3395 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3396 | } |
| 3397 | |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3398 | /* |
| 3399 | * Don't even try to read if time's out already. |
| 3400 | * This avoids by-passing the timer when repeatedly receiving messages |
| 3401 | * that will end up being dropped. |
| 3402 | */ |
| 3403 | if( ssl_check_timer( ssl ) != 0 ) |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 3404 | { |
| 3405 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3406 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 3407 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3408 | else |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3409 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3410 | 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] | 3411 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3412 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3413 | timeout = ssl->handshake->retransmit_timeout; |
| 3414 | else |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 3415 | timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3416 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3417 | 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] | 3418 | |
Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 3419 | if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL ) |
| 3420 | { |
| 3421 | ret = mbedtls_ssl_get_recv_timeout( ssl ) |
| 3422 | ( ssl->p_bio, ssl->in_hdr, len, timeout ); |
| 3423 | } |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3424 | else |
Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 3425 | { |
| 3426 | ret = mbedtls_ssl_get_recv( ssl ) |
| 3427 | ( ssl->p_bio, ssl->in_hdr, len ); |
| 3428 | } |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3429 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3430 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3431 | |
| 3432 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3433 | return( MBEDTLS_ERR_SSL_CONN_EOF ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3434 | } |
| 3435 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3436 | if( ret == MBEDTLS_ERR_SSL_TIMEOUT ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3437 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3438 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3439 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3440 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3441 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3442 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3443 | if( ssl_double_retransmit_timeout( ssl ) != 0 ) |
| 3444 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3445 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3446 | return( MBEDTLS_ERR_SSL_TIMEOUT ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3447 | } |
| 3448 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3449 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3450 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3451 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3452 | return( ret ); |
| 3453 | } |
| 3454 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3455 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3456 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3457 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 3458 | else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 3459 | MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3460 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3461 | { |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3462 | if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3463 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3464 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3465 | return( ret ); |
| 3466 | } |
| 3467 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3468 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3469 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3470 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3471 | } |
| 3472 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3473 | if( ret < 0 ) |
| 3474 | return( ret ); |
| 3475 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3476 | ssl->in_left = ret; |
| 3477 | } |
Manuel Pégourié-Gonnard | 25838b7 | 2019-03-19 10:23:56 +0100 | [diff] [blame] | 3478 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 3479 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3480 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3481 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3482 | 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] | 3483 | ssl->in_left, nb_want ) ); |
| 3484 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3485 | while( ssl->in_left < nb_want ) |
| 3486 | { |
| 3487 | len = nb_want - ssl->in_left; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3488 | |
| 3489 | if( ssl_check_timer( ssl ) != 0 ) |
| 3490 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
| 3491 | else |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3492 | { |
Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 3493 | if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL ) |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3494 | { |
Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 3495 | ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio, |
| 3496 | ssl->in_hdr + ssl->in_left, len, |
| 3497 | mbedtls_ssl_conf_get_read_timeout( ssl->conf ) ); |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3498 | } |
| 3499 | else |
| 3500 | { |
Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 3501 | ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio, |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3502 | ssl->in_hdr + ssl->in_left, len ); |
| 3503 | } |
| 3504 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3505 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3506 | 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] | 3507 | ssl->in_left, nb_want ) ); |
| 3508 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3509 | |
| 3510 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3511 | return( MBEDTLS_ERR_SSL_CONN_EOF ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3512 | |
| 3513 | if( ret < 0 ) |
| 3514 | return( ret ); |
| 3515 | |
mohammad1603 | 52aecb9 | 2018-03-28 23:41:40 -0700 | [diff] [blame] | 3516 | if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) ) |
mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 3517 | { |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3518 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3519 | ( "f_recv returned %d bytes but only %lu were requested", |
mohammad1603 | 19d392b | 2018-04-02 07:25:26 -0700 | [diff] [blame] | 3520 | ret, (unsigned long)len ) ); |
mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 3521 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3522 | } |
| 3523 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3524 | ssl->in_left += ret; |
| 3525 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3526 | } |
Manuel Pégourié-Gonnard | 25838b7 | 2019-03-19 10:23:56 +0100 | [diff] [blame] | 3527 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3528 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3529 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3530 | |
| 3531 | return( 0 ); |
| 3532 | } |
| 3533 | |
| 3534 | /* |
| 3535 | * Flush any data not yet written |
| 3536 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3537 | int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3538 | { |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 3539 | int ret; |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 3540 | unsigned char *buf; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3541 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3542 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3543 | |
Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 3544 | if( mbedtls_ssl_get_send( ssl ) == NULL ) |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3545 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3546 | 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] | 3547 | "or mbedtls_ssl_set_bio()" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3548 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3549 | } |
| 3550 | |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3551 | /* Avoid incrementing counter if data is flushed */ |
| 3552 | if( ssl->out_left == 0 ) |
| 3553 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3554 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3555 | return( 0 ); |
| 3556 | } |
| 3557 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3558 | while( ssl->out_left > 0 ) |
| 3559 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3560 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d", |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 3561 | mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3562 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3563 | buf = ssl->out_hdr - ssl->out_left; |
Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 3564 | 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] | 3565 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3566 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3567 | |
| 3568 | if( ret <= 0 ) |
| 3569 | return( ret ); |
| 3570 | |
mohammad1603 | 52aecb9 | 2018-03-28 23:41:40 -0700 | [diff] [blame] | 3571 | 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] | 3572 | { |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3573 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3574 | ( "f_send returned %d bytes but only %lu bytes were sent", |
mohammad1603 | 19d392b | 2018-04-02 07:25:26 -0700 | [diff] [blame] | 3575 | ret, (unsigned long)ssl->out_left ) ); |
mohammad1603 | 4bbaeb4 | 2018-02-22 04:29:04 -0800 | [diff] [blame] | 3576 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3577 | } |
| 3578 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3579 | ssl->out_left -= ret; |
| 3580 | } |
| 3581 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3582 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 3583 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3584 | { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3585 | ssl->out_hdr = ssl->out_buf; |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3586 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 3587 | MBEDTLS_SSL_TRANSPORT_ELSE |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3588 | #endif |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 3589 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3590 | { |
| 3591 | ssl->out_hdr = ssl->out_buf + 8; |
| 3592 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 3593 | #endif |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3594 | ssl_update_out_pointers( ssl, ssl->transform_out ); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3595 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3596 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3597 | |
| 3598 | return( 0 ); |
| 3599 | } |
| 3600 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3601 | /* |
| 3602 | * Functions to handle the DTLS retransmission state machine |
| 3603 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3604 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3605 | /* |
| 3606 | * Append current handshake message to current outgoing flight |
| 3607 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3608 | static int ssl_flight_append( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3609 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3610 | mbedtls_ssl_flight_item *msg; |
Hanno Becker | 3b23590 | 2018-08-06 09:54:53 +0100 | [diff] [blame] | 3611 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) ); |
| 3612 | MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight", |
| 3613 | ssl->out_msg, ssl->out_msglen ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3614 | |
| 3615 | /* Allocate space for current message */ |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3616 | 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] | 3617 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 3618 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3619 | sizeof( mbedtls_ssl_flight_item ) ) ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3620 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3621 | } |
| 3622 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3623 | if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3624 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 3625 | 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] | 3626 | mbedtls_free( msg ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3627 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3628 | } |
| 3629 | |
| 3630 | /* Copy current handshake message with headers */ |
| 3631 | memcpy( msg->p, ssl->out_msg, ssl->out_msglen ); |
| 3632 | msg->len = ssl->out_msglen; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3633 | msg->type = ssl->out_msgtype; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3634 | msg->next = NULL; |
| 3635 | |
| 3636 | /* Append to the current flight */ |
| 3637 | if( ssl->handshake->flight == NULL ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3638 | ssl->handshake->flight = msg; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3639 | else |
| 3640 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3641 | mbedtls_ssl_flight_item *cur = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3642 | while( cur->next != NULL ) |
| 3643 | cur = cur->next; |
| 3644 | cur->next = msg; |
| 3645 | } |
| 3646 | |
Hanno Becker | 3b23590 | 2018-08-06 09:54:53 +0100 | [diff] [blame] | 3647 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3648 | return( 0 ); |
| 3649 | } |
| 3650 | |
| 3651 | /* |
| 3652 | * Free the current flight of handshake messages |
| 3653 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3654 | static void ssl_flight_free( mbedtls_ssl_flight_item *flight ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3655 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3656 | mbedtls_ssl_flight_item *cur = flight; |
| 3657 | mbedtls_ssl_flight_item *next; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3658 | |
| 3659 | while( cur != NULL ) |
| 3660 | { |
| 3661 | next = cur->next; |
| 3662 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3663 | mbedtls_free( cur->p ); |
| 3664 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3665 | |
| 3666 | cur = next; |
| 3667 | } |
| 3668 | } |
| 3669 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3670 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3671 | static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 3672 | #endif |
| 3673 | |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3674 | /* |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3675 | * Swap transform_out and out_ctr with the alternative ones |
| 3676 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3677 | static void ssl_swap_epochs( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3678 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3679 | mbedtls_ssl_transform *tmp_transform; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3680 | unsigned char tmp_out_ctr[8]; |
| 3681 | |
| 3682 | if( ssl->transform_out == ssl->handshake->alt_transform_out ) |
| 3683 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3684 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3685 | return; |
| 3686 | } |
| 3687 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3688 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3689 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3690 | /* Swap transforms */ |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3691 | tmp_transform = ssl->transform_out; |
| 3692 | ssl->transform_out = ssl->handshake->alt_transform_out; |
| 3693 | ssl->handshake->alt_transform_out = tmp_transform; |
| 3694 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3695 | /* Swap epoch + sequence_number */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3696 | memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 ); |
| 3697 | 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] | 3698 | memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3699 | |
| 3700 | /* Adjust to the newly activated transform */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 3701 | ssl_update_out_pointers( ssl, ssl->transform_out ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3702 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3703 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 3704 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3705 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3706 | 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] | 3707 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3708 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
| 3709 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3710 | } |
| 3711 | } |
| 3712 | #endif |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3713 | } |
| 3714 | |
| 3715 | /* |
| 3716 | * Retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3717 | */ |
| 3718 | int mbedtls_ssl_resend( mbedtls_ssl_context *ssl ) |
| 3719 | { |
| 3720 | int ret = 0; |
| 3721 | |
| 3722 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) ); |
| 3723 | |
| 3724 | ret = mbedtls_ssl_flight_transmit( ssl ); |
| 3725 | |
| 3726 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) ); |
| 3727 | |
| 3728 | return( ret ); |
| 3729 | } |
| 3730 | |
| 3731 | /* |
| 3732 | * Transmit or retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3733 | * |
| 3734 | * Need to remember the current message in case flush_output returns |
| 3735 | * 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] | 3736 | * 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] | 3737 | */ |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3738 | int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3739 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3740 | int ret; |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3741 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3742 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3743 | if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3744 | { |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 3745 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3746 | |
| 3747 | ssl->handshake->cur_msg = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3748 | ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3749 | ssl_swap_epochs( ssl ); |
| 3750 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3751 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3752 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3753 | |
| 3754 | while( ssl->handshake->cur_msg != NULL ) |
| 3755 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3756 | size_t max_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3757 | const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3758 | |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3759 | int const is_finished = |
| 3760 | ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3761 | cur->p[0] == MBEDTLS_SSL_HS_FINISHED ); |
| 3762 | |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3763 | uint8_t const force_flush = ssl->disable_datagram_packing == 1 ? |
| 3764 | SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH; |
| 3765 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3766 | /* Swap epochs before sending Finished: we can't do it after |
| 3767 | * sending ChangeCipherSpec, in case write returns WANT_READ. |
| 3768 | * Must be done before copying, may change out_msg pointer */ |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3769 | 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] | 3770 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3771 | 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] | 3772 | ssl_swap_epochs( ssl ); |
| 3773 | } |
| 3774 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3775 | ret = ssl_get_remaining_payload_in_datagram( ssl ); |
| 3776 | if( ret < 0 ) |
| 3777 | return( ret ); |
| 3778 | max_frag_len = (size_t) ret; |
| 3779 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3780 | /* CCS is copied as is, while HS messages may need fragmentation */ |
| 3781 | if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
| 3782 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3783 | if( max_frag_len == 0 ) |
| 3784 | { |
| 3785 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3786 | return( ret ); |
| 3787 | |
| 3788 | continue; |
| 3789 | } |
| 3790 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3791 | memcpy( ssl->out_msg, cur->p, cur->len ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3792 | ssl->out_msglen = cur->len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3793 | ssl->out_msgtype = cur->type; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3794 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3795 | /* Update position inside current message */ |
| 3796 | ssl->handshake->cur_msg_p += cur->len; |
| 3797 | } |
| 3798 | else |
| 3799 | { |
| 3800 | const unsigned char * const p = ssl->handshake->cur_msg_p; |
| 3801 | const size_t hs_len = cur->len - 12; |
| 3802 | const size_t frag_off = p - ( cur->p + 12 ); |
| 3803 | const size_t rem_len = hs_len - frag_off; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3804 | size_t cur_hs_frag_len, max_hs_frag_len; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3805 | |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3806 | 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] | 3807 | { |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3808 | if( is_finished ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3809 | ssl_swap_epochs( ssl ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3810 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3811 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3812 | return( ret ); |
| 3813 | |
| 3814 | continue; |
| 3815 | } |
| 3816 | max_hs_frag_len = max_frag_len - 12; |
| 3817 | |
| 3818 | cur_hs_frag_len = rem_len > max_hs_frag_len ? |
| 3819 | max_hs_frag_len : rem_len; |
| 3820 | |
| 3821 | if( frag_off == 0 && cur_hs_frag_len != hs_len ) |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 3822 | { |
| 3823 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)", |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3824 | (unsigned) cur_hs_frag_len, |
| 3825 | (unsigned) max_hs_frag_len ) ); |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 3826 | } |
Manuel Pégourié-Gonnard | b747c6c | 2018-08-12 13:28:53 +0200 | [diff] [blame] | 3827 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3828 | /* Messages are stored with handshake headers as if not fragmented, |
| 3829 | * copy beginning of headers then fill fragmentation fields. |
| 3830 | * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */ |
| 3831 | memcpy( ssl->out_msg, cur->p, 6 ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3832 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3833 | ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff ); |
| 3834 | ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff ); |
| 3835 | ssl->out_msg[8] = ( ( frag_off ) & 0xff ); |
| 3836 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3837 | ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff ); |
| 3838 | ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff ); |
| 3839 | ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff ); |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3840 | |
| 3841 | MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 ); |
| 3842 | |
Hanno Becker | 3f7b973 | 2018-08-28 09:53:25 +0100 | [diff] [blame] | 3843 | /* Copy the handshake message content and set records fields */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3844 | memcpy( ssl->out_msg + 12, p, cur_hs_frag_len ); |
| 3845 | ssl->out_msglen = cur_hs_frag_len + 12; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3846 | ssl->out_msgtype = cur->type; |
| 3847 | |
| 3848 | /* Update position inside current message */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3849 | ssl->handshake->cur_msg_p += cur_hs_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3850 | } |
| 3851 | |
| 3852 | /* If done with the current message move to the next one if any */ |
| 3853 | if( ssl->handshake->cur_msg_p >= cur->p + cur->len ) |
| 3854 | { |
| 3855 | if( cur->next != NULL ) |
| 3856 | { |
| 3857 | ssl->handshake->cur_msg = cur->next; |
| 3858 | ssl->handshake->cur_msg_p = cur->next->p + 12; |
| 3859 | } |
| 3860 | else |
| 3861 | { |
| 3862 | ssl->handshake->cur_msg = NULL; |
| 3863 | ssl->handshake->cur_msg_p = NULL; |
| 3864 | } |
| 3865 | } |
| 3866 | |
| 3867 | /* Actually send the message out */ |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3868 | if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3869 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3870 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3871 | return( ret ); |
| 3872 | } |
| 3873 | } |
| 3874 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3875 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3876 | return( ret ); |
| 3877 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3878 | /* Update state and set timer */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3879 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 3880 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 23b7b70 | 2014-09-25 13:50:12 +0200 | [diff] [blame] | 3881 | else |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3882 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3883 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3884 | ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); |
| 3885 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3886 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3887 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3888 | |
| 3889 | return( 0 ); |
| 3890 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3891 | |
| 3892 | /* |
| 3893 | * To be called when the last message of an incoming flight is received. |
| 3894 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3895 | void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3896 | { |
| 3897 | /* We won't need to resend that one any more */ |
| 3898 | ssl_flight_free( ssl->handshake->flight ); |
| 3899 | ssl->handshake->flight = NULL; |
| 3900 | ssl->handshake->cur_msg = NULL; |
| 3901 | |
| 3902 | /* The next incoming flight will start with this msg_seq */ |
| 3903 | ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq; |
| 3904 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3905 | /* We don't want to remember CCS's across flight boundaries. */ |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 3906 | ssl->handshake->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3907 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3908 | /* Clear future message buffering structure. */ |
| 3909 | ssl_buffering_free( ssl ); |
| 3910 | |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 3911 | /* Cancel timer */ |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3912 | ssl_set_timer( ssl, 0 ); |
| 3913 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3914 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3915 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3916 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3917 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3918 | } |
| 3919 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3920 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3921 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3922 | |
| 3923 | /* |
| 3924 | * To be called when the last message of an outgoing flight is send. |
| 3925 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3926 | void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3927 | { |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 3928 | ssl_reset_retransmit_timeout( ssl ); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3929 | ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3930 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3931 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3932 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3933 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3934 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3935 | } |
| 3936 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3937 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3938 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3939 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3940 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3941 | /* |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3942 | * Handshake layer functions |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3943 | */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3944 | |
| 3945 | /* |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3946 | * Write (DTLS: or queue) current handshake (including CCS) message. |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3947 | * |
| 3948 | * - fill in handshake headers |
| 3949 | * - update handshake checksum |
| 3950 | * - DTLS: save message for resending |
| 3951 | * - then pass to the record layer |
| 3952 | * |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3953 | * DTLS: except for HelloRequest, messages are only queued, and will only be |
| 3954 | * actually sent when calling flight_transmit() or resend(). |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3955 | * |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3956 | * Inputs: |
| 3957 | * - ssl->out_msglen: 4 + actual handshake message len |
| 3958 | * (4 is the size of handshake headers for TLS) |
| 3959 | * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc) |
| 3960 | * - ssl->out_msg + 4: the handshake message body |
| 3961 | * |
Manuel Pégourié-Gonnard | 065a2a3 | 2018-08-20 11:09:26 +0200 | [diff] [blame] | 3962 | * 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] | 3963 | * - ssl->out_msglen: the length of the record contents |
| 3964 | * (including handshake headers but excluding record headers) |
| 3965 | * - ssl->out_msg: the record contents (handshake headers + content) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3966 | */ |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3967 | int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3968 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3969 | int ret; |
| 3970 | const size_t hs_len = ssl->out_msglen - 4; |
| 3971 | const unsigned char hs_type = ssl->out_msg[0]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3972 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3973 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) ); |
| 3974 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3975 | /* |
| 3976 | * Sanity checks |
| 3977 | */ |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 3978 | if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE && |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3979 | ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
| 3980 | { |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 3981 | /* In SSLv3, the client might send a NoCertificate alert. */ |
| 3982 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 3983 | if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 && |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 3984 | ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT && |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 3985 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 3986 | MBEDTLS_SSL_IS_CLIENT ) ) |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 3987 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ |
| 3988 | { |
| 3989 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3990 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3991 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3992 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3993 | |
Hanno Becker | f6d6e30 | 2018-11-07 11:57:51 +0000 | [diff] [blame] | 3994 | /* Whenever we send anything different from a |
| 3995 | * HelloRequest we should be in a handshake - double check. */ |
| 3996 | if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3997 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) && |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3998 | ssl->handshake == NULL ) |
| 3999 | { |
| 4000 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4001 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4002 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4003 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4004 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4005 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4006 | ssl->handshake != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4007 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4008 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4009 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4010 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4011 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4012 | #endif |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4013 | |
Hanno Becker | b50a253 | 2018-08-06 11:52:54 +0100 | [diff] [blame] | 4014 | /* Double-check that we did not exceed the bounds |
| 4015 | * of the outgoing record buffer. |
| 4016 | * This should never fail as the various message |
| 4017 | * writing functions must obey the bounds of the |
| 4018 | * outgoing record buffer, but better be safe. |
| 4019 | * |
| 4020 | * Note: We deliberately do not check for the MTU or MFL here. |
| 4021 | */ |
| 4022 | if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
| 4023 | { |
| 4024 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: " |
| 4025 | "size %u, maximum %u", |
| 4026 | (unsigned) ssl->out_msglen, |
| 4027 | (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 4028 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4029 | } |
| 4030 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4031 | /* |
| 4032 | * Fill handshake headers |
| 4033 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4034 | if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4035 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4036 | ssl->out_msg[1] = (unsigned char)( hs_len >> 16 ); |
| 4037 | ssl->out_msg[2] = (unsigned char)( hs_len >> 8 ); |
| 4038 | ssl->out_msg[3] = (unsigned char)( hs_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4039 | |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4040 | /* |
| 4041 | * DTLS has additional fields in the Handshake layer, |
| 4042 | * between the length field and the actual payload: |
| 4043 | * uint16 message_seq; |
| 4044 | * uint24 fragment_offset; |
| 4045 | * uint24 fragment_length; |
| 4046 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4047 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4048 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4049 | { |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 4050 | /* Make room for the additional DTLS fields */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4051 | if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 ) |
Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 4052 | { |
| 4053 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: " |
| 4054 | "size %u, maximum %u", |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4055 | (unsigned) ( hs_len ), |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4056 | (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) ); |
Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 4057 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4058 | } |
| 4059 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4060 | 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] | 4061 | ssl->out_msglen += 8; |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4062 | |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 4063 | /* Write message_seq and update it, except for HelloRequest */ |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4064 | if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 4065 | { |
Manuel Pégourié-Gonnard | d9ba0d9 | 2014-09-02 18:30:26 +0200 | [diff] [blame] | 4066 | ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF; |
| 4067 | ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF; |
| 4068 | ++( ssl->handshake->out_msg_seq ); |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 4069 | } |
| 4070 | else |
| 4071 | { |
| 4072 | ssl->out_msg[4] = 0; |
| 4073 | ssl->out_msg[5] = 0; |
| 4074 | } |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 4075 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4076 | /* Handshake hashes are computed without fragmentation, |
| 4077 | * 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] | 4078 | memset( ssl->out_msg + 6, 0x00, 3 ); |
| 4079 | memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 ); |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4080 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4081 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4082 | |
Hanno Becker | 0207e53 | 2018-08-28 10:28:28 +0100 | [diff] [blame] | 4083 | /* Update running hashes of handshake messages seen */ |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4084 | if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) |
| 4085 | ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4086 | } |
| 4087 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4088 | /* 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] | 4089 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4090 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | f6d6e30 | 2018-11-07 11:57:51 +0000 | [diff] [blame] | 4091 | ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4092 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4093 | { |
| 4094 | if( ( ret = ssl_flight_append( ssl ) ) != 0 ) |
| 4095 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4096 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4097 | return( ret ); |
| 4098 | } |
| 4099 | } |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4100 | else |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4101 | #endif |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4102 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4103 | 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] | 4104 | { |
| 4105 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret ); |
| 4106 | return( ret ); |
| 4107 | } |
| 4108 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4109 | |
| 4110 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) ); |
| 4111 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4112 | return( 0 ); |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4113 | } |
| 4114 | |
| 4115 | /* |
| 4116 | * Record layer functions |
| 4117 | */ |
| 4118 | |
| 4119 | /* |
| 4120 | * Write current record. |
| 4121 | * |
| 4122 | * Uses: |
| 4123 | * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS) |
| 4124 | * - ssl->out_msglen: length of the record content (excl headers) |
| 4125 | * - ssl->out_msg: record content |
| 4126 | */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4127 | 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] | 4128 | { |
| 4129 | int ret, done = 0; |
| 4130 | size_t len = ssl->out_msglen; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4131 | uint8_t flush = force_flush; |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4132 | |
| 4133 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4134 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4135 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4136 | if( ssl->transform_out != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4137 | ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4138 | { |
| 4139 | if( ( ret = ssl_compress_buf( ssl ) ) != 0 ) |
| 4140 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4141 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4142 | return( ret ); |
| 4143 | } |
| 4144 | |
| 4145 | len = ssl->out_msglen; |
| 4146 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4147 | #endif /*MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4148 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4149 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 4150 | if( mbedtls_ssl_hw_record_write != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4151 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4152 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4153 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4154 | ret = mbedtls_ssl_hw_record_write( ssl ); |
| 4155 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4156 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4157 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret ); |
| 4158 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4159 | } |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 4160 | |
| 4161 | if( ret == 0 ) |
| 4162 | done = 1; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4163 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4164 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4165 | if( !done ) |
| 4166 | { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4167 | unsigned i; |
| 4168 | size_t protected_record_size; |
| 4169 | |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4170 | /* Skip writing the record content type to after the encryption, |
| 4171 | * as it may change when using the CID extension. */ |
| 4172 | |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 4173 | mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ), |
| 4174 | mbedtls_ssl_get_minor_ver( ssl ), |
| 4175 | ssl->conf->transport, ssl->out_hdr + 1 ); |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 4176 | |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 4177 | memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 4178 | ssl->out_len[0] = (unsigned char)( len >> 8 ); |
| 4179 | ssl->out_len[1] = (unsigned char)( len ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4180 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4181 | if( ssl->transform_out != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4182 | { |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4183 | mbedtls_record rec; |
| 4184 | |
| 4185 | rec.buf = ssl->out_iv; |
| 4186 | rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - |
| 4187 | ( ssl->out_iv - ssl->out_buf ); |
| 4188 | rec.data_len = ssl->out_msglen; |
| 4189 | rec.data_offset = ssl->out_msg - rec.buf; |
| 4190 | |
| 4191 | memcpy( &rec.ctr[0], ssl->out_ctr, 8 ); |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 4192 | mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ), |
| 4193 | mbedtls_ssl_get_minor_ver( ssl ), |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4194 | ssl->conf->transport, rec.ver ); |
| 4195 | rec.type = ssl->out_msgtype; |
| 4196 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4197 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 505089d | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 4198 | /* The CID is set by mbedtls_ssl_encrypt_buf(). */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 4199 | rec.cid_len = 0; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4200 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 4201 | |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 4202 | if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec, |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 4203 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 4204 | mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 ) |
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, "ssl_encrypt_buf", ret ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4207 | return( ret ); |
| 4208 | } |
| 4209 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4210 | if( rec.data_offset != 0 ) |
| 4211 | { |
| 4212 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4213 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4214 | } |
| 4215 | |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4216 | /* Update the record content type and CID. */ |
| 4217 | ssl->out_msgtype = rec.type; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4218 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID ) |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4219 | memcpy( ssl->out_cid, rec.cid, rec.cid_len ); |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4220 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | c5aee96 | 2019-03-14 12:56:23 +0000 | [diff] [blame] | 4221 | ssl->out_msglen = len = rec.data_len; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4222 | ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 ); |
| 4223 | ssl->out_len[1] = (unsigned char)( rec.data_len ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4224 | } |
| 4225 | |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 4226 | protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4227 | |
| 4228 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4229 | /* In case of DTLS, double-check that we don't exceed |
| 4230 | * the remaining space in the datagram. */ |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4231 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4232 | { |
Hanno Becker | 554b0af | 2018-08-22 20:33:41 +0100 | [diff] [blame] | 4233 | ret = ssl_get_remaining_space_in_datagram( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4234 | if( ret < 0 ) |
| 4235 | return( ret ); |
| 4236 | |
| 4237 | if( protected_record_size > (size_t) ret ) |
| 4238 | { |
| 4239 | /* Should never happen */ |
| 4240 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4241 | } |
| 4242 | } |
| 4243 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4244 | |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4245 | /* Now write the potentially updated record content type. */ |
| 4246 | ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype; |
| 4247 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4248 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, " |
Hanno Becker | ecbdf1c | 2018-08-28 09:53:54 +0100 | [diff] [blame] | 4249 | "version = [%d:%d], msglen = %d", |
| 4250 | ssl->out_hdr[0], ssl->out_hdr[1], |
| 4251 | ssl->out_hdr[2], len ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4252 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4253 | MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network", |
Hanno Becker | ecbdf1c | 2018-08-28 09:53:54 +0100 | [diff] [blame] | 4254 | ssl->out_hdr, protected_record_size ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4255 | |
| 4256 | ssl->out_left += protected_record_size; |
| 4257 | ssl->out_hdr += protected_record_size; |
| 4258 | ssl_update_out_pointers( ssl, ssl->transform_out ); |
| 4259 | |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 4260 | for( i = 8; i > ssl_ep_len( ssl ); i-- ) |
| 4261 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
| 4262 | break; |
| 4263 | |
| 4264 | /* The loop goes to its end iff the counter is wrapping */ |
| 4265 | if( i == ssl_ep_len( ssl ) ) |
| 4266 | { |
| 4267 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) ); |
| 4268 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 4269 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4270 | } |
| 4271 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4272 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4273 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | 47db877 | 2018-08-21 13:32:13 +0100 | [diff] [blame] | 4274 | flush == SSL_DONT_FORCE_FLUSH ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4275 | { |
Hanno Becker | 1f5a15d | 2018-08-21 13:31:31 +0100 | [diff] [blame] | 4276 | size_t remaining; |
| 4277 | ret = ssl_get_remaining_payload_in_datagram( ssl ); |
| 4278 | if( ret < 0 ) |
| 4279 | { |
| 4280 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram", |
| 4281 | ret ); |
| 4282 | return( ret ); |
| 4283 | } |
| 4284 | |
| 4285 | remaining = (size_t) ret; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4286 | if( remaining == 0 ) |
Hanno Becker | f0da667 | 2018-08-28 09:55:10 +0100 | [diff] [blame] | 4287 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4288 | flush = SSL_FORCE_FLUSH; |
Hanno Becker | f0da667 | 2018-08-28 09:55:10 +0100 | [diff] [blame] | 4289 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4290 | else |
| 4291 | { |
Hanno Becker | 513815a | 2018-08-20 11:56:09 +0100 | [diff] [blame] | 4292 | 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] | 4293 | } |
| 4294 | } |
| 4295 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4296 | |
| 4297 | if( ( flush == SSL_FORCE_FLUSH ) && |
| 4298 | ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4299 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4300 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4301 | return( ret ); |
| 4302 | } |
| 4303 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4304 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4305 | |
| 4306 | return( 0 ); |
| 4307 | } |
| 4308 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4309 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4310 | |
| 4311 | static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl ) |
| 4312 | { |
| 4313 | if( ssl->in_msglen < ssl->in_hslen || |
| 4314 | memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 || |
| 4315 | memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ) |
| 4316 | { |
| 4317 | return( 1 ); |
| 4318 | } |
| 4319 | return( 0 ); |
| 4320 | } |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4321 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4322 | 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] | 4323 | { |
| 4324 | return( ( ssl->in_msg[9] << 16 ) | |
| 4325 | ( ssl->in_msg[10] << 8 ) | |
| 4326 | ssl->in_msg[11] ); |
| 4327 | } |
| 4328 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4329 | 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] | 4330 | { |
| 4331 | return( ( ssl->in_msg[6] << 16 ) | |
| 4332 | ( ssl->in_msg[7] << 8 ) | |
| 4333 | ssl->in_msg[8] ); |
| 4334 | } |
| 4335 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4336 | static int ssl_check_hs_header( mbedtls_ssl_context const *ssl ) |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4337 | { |
| 4338 | uint32_t msg_len, frag_off, frag_len; |
| 4339 | |
| 4340 | msg_len = ssl_get_hs_total_len( ssl ); |
| 4341 | frag_off = ssl_get_hs_frag_off( ssl ); |
| 4342 | frag_len = ssl_get_hs_frag_len( ssl ); |
| 4343 | |
| 4344 | if( frag_off > msg_len ) |
| 4345 | return( -1 ); |
| 4346 | |
| 4347 | if( frag_len > msg_len - frag_off ) |
| 4348 | return( -1 ); |
| 4349 | |
| 4350 | if( frag_len + 12 > ssl->in_msglen ) |
| 4351 | return( -1 ); |
| 4352 | |
| 4353 | return( 0 ); |
| 4354 | } |
| 4355 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4356 | /* |
| 4357 | * Mark bits in bitmask (used for DTLS HS reassembly) |
| 4358 | */ |
| 4359 | static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len ) |
| 4360 | { |
| 4361 | unsigned int start_bits, end_bits; |
| 4362 | |
| 4363 | start_bits = 8 - ( offset % 8 ); |
| 4364 | if( start_bits != 8 ) |
| 4365 | { |
| 4366 | size_t first_byte_idx = offset / 8; |
| 4367 | |
Manuel Pégourié-Gonnard | ac03052 | 2014-09-02 14:23:40 +0200 | [diff] [blame] | 4368 | /* Special case */ |
| 4369 | if( len <= start_bits ) |
| 4370 | { |
| 4371 | for( ; len != 0; len-- ) |
| 4372 | mask[first_byte_idx] |= 1 << ( start_bits - len ); |
| 4373 | |
| 4374 | /* Avoid potential issues with offset or len becoming invalid */ |
| 4375 | return; |
| 4376 | } |
| 4377 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4378 | offset += start_bits; /* Now offset % 8 == 0 */ |
| 4379 | len -= start_bits; |
| 4380 | |
| 4381 | for( ; start_bits != 0; start_bits-- ) |
| 4382 | mask[first_byte_idx] |= 1 << ( start_bits - 1 ); |
| 4383 | } |
| 4384 | |
| 4385 | end_bits = len % 8; |
| 4386 | if( end_bits != 0 ) |
| 4387 | { |
| 4388 | size_t last_byte_idx = ( offset + len ) / 8; |
| 4389 | |
| 4390 | len -= end_bits; /* Now len % 8 == 0 */ |
| 4391 | |
| 4392 | for( ; end_bits != 0; end_bits-- ) |
| 4393 | mask[last_byte_idx] |= 1 << ( 8 - end_bits ); |
| 4394 | } |
| 4395 | |
| 4396 | memset( mask + offset / 8, 0xFF, len / 8 ); |
| 4397 | } |
| 4398 | |
| 4399 | /* |
| 4400 | * Check that bitmask is full |
| 4401 | */ |
| 4402 | static int ssl_bitmask_check( unsigned char *mask, size_t len ) |
| 4403 | { |
| 4404 | size_t i; |
| 4405 | |
| 4406 | for( i = 0; i < len / 8; i++ ) |
| 4407 | if( mask[i] != 0xFF ) |
| 4408 | return( -1 ); |
| 4409 | |
| 4410 | for( i = 0; i < len % 8; i++ ) |
| 4411 | if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 ) |
| 4412 | return( -1 ); |
| 4413 | |
| 4414 | return( 0 ); |
| 4415 | } |
| 4416 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4417 | /* msg_len does not include the handshake header */ |
Hanno Becker | 65dc885 | 2018-08-23 09:40:49 +0100 | [diff] [blame] | 4418 | static size_t ssl_get_reassembly_buffer_size( size_t msg_len, |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4419 | unsigned add_bitmap ) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4420 | { |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4421 | size_t alloc_len; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4422 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4423 | alloc_len = 12; /* Handshake header */ |
| 4424 | alloc_len += msg_len; /* Content buffer */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4425 | |
Hanno Becker | d07df86 | 2018-08-16 09:14:58 +0100 | [diff] [blame] | 4426 | if( add_bitmap ) |
| 4427 | alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */ |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4428 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4429 | return( alloc_len ); |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4430 | } |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4431 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4432 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4433 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4434 | 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] | 4435 | { |
| 4436 | return( ( ssl->in_msg[1] << 16 ) | |
| 4437 | ( ssl->in_msg[2] << 8 ) | |
| 4438 | ssl->in_msg[3] ); |
| 4439 | } |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4440 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4441 | int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4442 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4443 | if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) ) |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4444 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4445 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d", |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4446 | ssl->in_msglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4447 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4448 | } |
| 4449 | |
Hanno Becker | 12555c6 | 2018-08-16 12:47:53 +0100 | [diff] [blame] | 4450 | 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] | 4451 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4452 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen =" |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4453 | " %d, type = %d, hslen = %d", |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4454 | ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) ); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4455 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4456 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4457 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4458 | { |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4459 | int ret; |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4460 | 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] | 4461 | |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4462 | if( ssl_check_hs_header( ssl ) != 0 ) |
| 4463 | { |
| 4464 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) ); |
| 4465 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4466 | } |
| 4467 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4468 | if( ssl->handshake != NULL && |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 4469 | ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && |
| 4470 | recv_msg_seq != ssl->handshake->in_msg_seq ) || |
| 4471 | ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 4472 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4473 | { |
Hanno Becker | 9e1ec22 | 2018-08-15 15:54:43 +0100 | [diff] [blame] | 4474 | if( recv_msg_seq > ssl->handshake->in_msg_seq ) |
| 4475 | { |
| 4476 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)", |
| 4477 | recv_msg_seq, |
| 4478 | ssl->handshake->in_msg_seq ) ); |
| 4479 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 4480 | } |
| 4481 | |
Manuel Pégourié-Gonnard | fc572dd | 2014-10-09 17:56:57 +0200 | [diff] [blame] | 4482 | /* Retransmit only on last message from previous flight, to avoid |
| 4483 | * too many retransmissions. |
| 4484 | * Besides, No sane server ever retransmits HelloVerifyRequest */ |
| 4485 | 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] | 4486 | ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST ) |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4487 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4488 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, " |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4489 | "message_seq = %d, start_of_flight = %d", |
| 4490 | recv_msg_seq, |
| 4491 | ssl->handshake->in_flight_start_seq ) ); |
| 4492 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4493 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4494 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4495 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4496 | return( ret ); |
| 4497 | } |
| 4498 | } |
| 4499 | else |
| 4500 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4501 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: " |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4502 | "message_seq = %d, expected = %d", |
| 4503 | recv_msg_seq, |
| 4504 | ssl->handshake->in_msg_seq ) ); |
| 4505 | } |
| 4506 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 4507 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4508 | } |
| 4509 | /* Wait until message completion to increment in_msg_seq */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4510 | |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4511 | /* Message reassembly is handled alongside buffering of future |
| 4512 | * messages; the commonality is that both handshake fragments and |
Hanno Becker | 83ab41c | 2018-08-28 17:19:38 +0100 | [diff] [blame] | 4513 | * future messages cannot be forwarded immediately to the |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4514 | * handshake logic layer. */ |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4515 | if( ssl_hs_is_proper_fragment( ssl ) == 1 ) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4516 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4517 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) ); |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4518 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4519 | } |
| 4520 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4521 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4522 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4523 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4524 | { |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4525 | /* With TLS we don't handle fragmentation (for now) */ |
| 4526 | if( ssl->in_msglen < ssl->in_hslen ) |
| 4527 | { |
| 4528 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) ); |
| 4529 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4530 | } |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4531 | } |
Manuel Pégourié-Gonnard | ec1c222 | 2019-06-12 10:18:26 +0200 | [diff] [blame] | 4532 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4533 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4534 | return( 0 ); |
| 4535 | } |
| 4536 | |
| 4537 | void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl ) |
| 4538 | { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4539 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4540 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4541 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL ) |
Manuel Pégourié-Gonnard | 14bf706 | 2015-06-23 14:07:13 +0200 | [diff] [blame] | 4542 | { |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4543 | 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] | 4544 | } |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4545 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4546 | /* Handshake message is complete, increment counter */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4547 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4548 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4549 | ssl->handshake != NULL ) |
| 4550 | { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4551 | unsigned offset; |
| 4552 | mbedtls_ssl_hs_buffer *hs_buf; |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4553 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4554 | /* Increment handshake sequence number */ |
| 4555 | hs->in_msg_seq++; |
| 4556 | |
| 4557 | /* |
| 4558 | * Clear up handshake buffering and reassembly structure. |
| 4559 | */ |
| 4560 | |
| 4561 | /* Free first entry */ |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 4562 | ssl_buffering_free_slot( ssl, 0 ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4563 | |
| 4564 | /* Shift all other entries */ |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 4565 | for( offset = 0, hs_buf = &hs->buffering.hs[0]; |
| 4566 | offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS; |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4567 | offset++, hs_buf++ ) |
| 4568 | { |
| 4569 | *hs_buf = *(hs_buf + 1); |
| 4570 | } |
| 4571 | |
| 4572 | /* Create a fresh last entry */ |
| 4573 | memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4574 | } |
| 4575 | #endif |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4576 | } |
| 4577 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4578 | /* |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4579 | * DTLS anti-replay: RFC 6347 4.1.2.6 |
| 4580 | * |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4581 | * in_window is a field of bits numbered from 0 (lsb) to 63 (msb). |
| 4582 | * Bit n is set iff record number in_window_top - n has been seen. |
| 4583 | * |
| 4584 | * Usually, in_window_top is the last record number seen and the lsb of |
| 4585 | * in_window is set. The only exception is the initial state (record number 0 |
| 4586 | * not seen yet). |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4587 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4588 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4589 | static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4590 | { |
| 4591 | ssl->in_window_top = 0; |
| 4592 | ssl->in_window = 0; |
| 4593 | } |
| 4594 | |
| 4595 | static inline uint64_t ssl_load_six_bytes( unsigned char *buf ) |
| 4596 | { |
| 4597 | return( ( (uint64_t) buf[0] << 40 ) | |
| 4598 | ( (uint64_t) buf[1] << 32 ) | |
| 4599 | ( (uint64_t) buf[2] << 24 ) | |
| 4600 | ( (uint64_t) buf[3] << 16 ) | |
| 4601 | ( (uint64_t) buf[4] << 8 ) | |
| 4602 | ( (uint64_t) buf[5] ) ); |
| 4603 | } |
| 4604 | |
| 4605 | /* |
| 4606 | * Return 0 if sequence number is acceptable, -1 otherwise |
| 4607 | */ |
Hanno Becker | fc55172 | 2019-07-12 08:50:37 +0100 | [diff] [blame] | 4608 | 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] | 4609 | { |
| 4610 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); |
| 4611 | uint64_t bit; |
| 4612 | |
Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 4613 | if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) == |
| 4614 | MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) |
| 4615 | { |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4616 | return( 0 ); |
Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 4617 | } |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4618 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4619 | if( rec_seqnum > ssl->in_window_top ) |
| 4620 | return( 0 ); |
| 4621 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4622 | bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4623 | |
| 4624 | if( bit >= 64 ) |
| 4625 | return( -1 ); |
| 4626 | |
| 4627 | if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 ) |
| 4628 | return( -1 ); |
| 4629 | |
| 4630 | return( 0 ); |
| 4631 | } |
| 4632 | |
| 4633 | /* |
| 4634 | * Update replay window on new validated record |
| 4635 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4636 | void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4637 | { |
| 4638 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); |
| 4639 | |
Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 4640 | if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) == |
| 4641 | MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) |
| 4642 | { |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4643 | return; |
Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 4644 | } |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4645 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4646 | if( rec_seqnum > ssl->in_window_top ) |
| 4647 | { |
| 4648 | /* Update window_top and the contents of the window */ |
| 4649 | uint64_t shift = rec_seqnum - ssl->in_window_top; |
| 4650 | |
| 4651 | if( shift >= 64 ) |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4652 | ssl->in_window = 1; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4653 | else |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4654 | { |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4655 | ssl->in_window <<= shift; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4656 | ssl->in_window |= 1; |
| 4657 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4658 | |
| 4659 | ssl->in_window_top = rec_seqnum; |
| 4660 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4661 | else |
| 4662 | { |
| 4663 | /* Mark that number as seen in the current window */ |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4664 | uint64_t bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4665 | |
| 4666 | if( bit < 64 ) /* Always true, but be extra sure */ |
| 4667 | ssl->in_window |= (uint64_t) 1 << bit; |
| 4668 | } |
| 4669 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4670 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4671 | |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 4672 | #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] | 4673 | /* Forward declaration */ |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 4674 | static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ); |
| 4675 | |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4676 | /* |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4677 | * Without any SSL context, check if a datagram looks like a ClientHello with |
| 4678 | * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message. |
Simon Butcher | 0789aed | 2015-09-11 17:15:17 +0100 | [diff] [blame] | 4679 | * Both input and output include full DTLS headers. |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4680 | * |
| 4681 | * - if cookie is valid, return 0 |
| 4682 | * - if ClientHello looks superficially valid but cookie is not, |
| 4683 | * fill obuf and set olen, then |
| 4684 | * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED |
| 4685 | * - otherwise return a specific error code |
| 4686 | */ |
| 4687 | static int ssl_check_dtls_clihlo_cookie( |
| 4688 | mbedtls_ssl_cookie_write_t *f_cookie_write, |
| 4689 | mbedtls_ssl_cookie_check_t *f_cookie_check, |
| 4690 | void *p_cookie, |
| 4691 | const unsigned char *cli_id, size_t cli_id_len, |
| 4692 | const unsigned char *in, size_t in_len, |
| 4693 | unsigned char *obuf, size_t buf_len, size_t *olen ) |
| 4694 | { |
| 4695 | size_t sid_len, cookie_len; |
| 4696 | unsigned char *p; |
| 4697 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4698 | /* |
| 4699 | * Structure of ClientHello with record and handshake headers, |
| 4700 | * and expected values. We don't need to check a lot, more checks will be |
| 4701 | * done when actually parsing the ClientHello - skipping those checks |
| 4702 | * avoids code duplication and does not make cookie forging any easier. |
| 4703 | * |
| 4704 | * 0-0 ContentType type; copied, must be handshake |
| 4705 | * 1-2 ProtocolVersion version; copied |
| 4706 | * 3-4 uint16 epoch; copied, must be 0 |
| 4707 | * 5-10 uint48 sequence_number; copied |
| 4708 | * 11-12 uint16 length; (ignored) |
| 4709 | * |
| 4710 | * 13-13 HandshakeType msg_type; (ignored) |
| 4711 | * 14-16 uint24 length; (ignored) |
| 4712 | * 17-18 uint16 message_seq; copied |
| 4713 | * 19-21 uint24 fragment_offset; copied, must be 0 |
| 4714 | * 22-24 uint24 fragment_length; (ignored) |
| 4715 | * |
| 4716 | * 25-26 ProtocolVersion client_version; (ignored) |
| 4717 | * 27-58 Random random; (ignored) |
| 4718 | * 59-xx SessionID session_id; 1 byte len + sid_len content |
| 4719 | * 60+ opaque cookie<0..2^8-1>; 1 byte len + content |
| 4720 | * ... |
| 4721 | * |
| 4722 | * Minimum length is 61 bytes. |
| 4723 | */ |
| 4724 | if( in_len < 61 || |
| 4725 | in[0] != MBEDTLS_SSL_MSG_HANDSHAKE || |
| 4726 | in[3] != 0 || in[4] != 0 || |
| 4727 | in[19] != 0 || in[20] != 0 || in[21] != 0 ) |
| 4728 | { |
| 4729 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 4730 | } |
| 4731 | |
| 4732 | sid_len = in[59]; |
| 4733 | if( sid_len > in_len - 61 ) |
| 4734 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 4735 | |
| 4736 | cookie_len = in[60 + sid_len]; |
| 4737 | if( cookie_len > in_len - 60 ) |
| 4738 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 4739 | |
| 4740 | if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len, |
| 4741 | cli_id, cli_id_len ) == 0 ) |
| 4742 | { |
| 4743 | /* Valid cookie */ |
| 4744 | return( 0 ); |
| 4745 | } |
| 4746 | |
| 4747 | /* |
| 4748 | * If we get here, we've got an invalid cookie, let's prepare HVR. |
| 4749 | * |
| 4750 | * 0-0 ContentType type; copied |
| 4751 | * 1-2 ProtocolVersion version; copied |
| 4752 | * 3-4 uint16 epoch; copied |
| 4753 | * 5-10 uint48 sequence_number; copied |
| 4754 | * 11-12 uint16 length; olen - 13 |
| 4755 | * |
| 4756 | * 13-13 HandshakeType msg_type; hello_verify_request |
| 4757 | * 14-16 uint24 length; olen - 25 |
| 4758 | * 17-18 uint16 message_seq; copied |
| 4759 | * 19-21 uint24 fragment_offset; copied |
| 4760 | * 22-24 uint24 fragment_length; olen - 25 |
| 4761 | * |
| 4762 | * 25-26 ProtocolVersion server_version; 0xfe 0xff |
| 4763 | * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie |
| 4764 | * |
| 4765 | * Minimum length is 28. |
| 4766 | */ |
| 4767 | if( buf_len < 28 ) |
| 4768 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 4769 | |
| 4770 | /* Copy most fields and adapt others */ |
| 4771 | memcpy( obuf, in, 25 ); |
| 4772 | obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST; |
| 4773 | obuf[25] = 0xfe; |
| 4774 | obuf[26] = 0xff; |
| 4775 | |
| 4776 | /* Generate and write actual cookie */ |
| 4777 | p = obuf + 28; |
| 4778 | if( f_cookie_write( p_cookie, |
| 4779 | &p, obuf + buf_len, cli_id, cli_id_len ) != 0 ) |
| 4780 | { |
| 4781 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4782 | } |
| 4783 | |
| 4784 | *olen = p - obuf; |
| 4785 | |
| 4786 | /* Go back and fill length fields */ |
| 4787 | obuf[27] = (unsigned char)( *olen - 28 ); |
| 4788 | |
| 4789 | obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 ); |
| 4790 | obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 ); |
| 4791 | obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) ); |
| 4792 | |
| 4793 | obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 ); |
| 4794 | obuf[12] = (unsigned char)( ( *olen - 13 ) ); |
| 4795 | |
| 4796 | return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); |
| 4797 | } |
| 4798 | |
| 4799 | /* |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4800 | * Handle possible client reconnect with the same UDP quadruplet |
| 4801 | * (RFC 6347 Section 4.2.8). |
| 4802 | * |
| 4803 | * Called by ssl_parse_record_header() in case we receive an epoch 0 record |
| 4804 | * that looks like a ClientHello. |
| 4805 | * |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4806 | * - if the input looks like a ClientHello without cookies, |
| 4807 | * send back HelloVerifyRequest, then |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4808 | * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4809 | * - if the input looks like a ClientHello with a valid cookie, |
| 4810 | * reset the session of the current context, and |
Manuel Pégourié-Gonnard | be619c1 | 2015-09-08 11:21:21 +0200 | [diff] [blame] | 4811 | * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4812 | * - if anything goes wrong, return a specific error code |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4813 | * |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4814 | * mbedtls_ssl_read_record() will ignore the record if anything else than |
Simon Butcher | d0bf6a3 | 2015-09-11 17:34:49 +0100 | [diff] [blame] | 4815 | * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function |
| 4816 | * cannot not return 0. |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4817 | */ |
| 4818 | static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl ) |
| 4819 | { |
| 4820 | int ret; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4821 | size_t len; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4822 | |
Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4823 | if( ssl->conf->f_cookie_write == NULL || |
| 4824 | ssl->conf->f_cookie_check == NULL ) |
| 4825 | { |
| 4826 | /* If we can't use cookies to verify reachability of the peer, |
| 4827 | * drop the record. */ |
| 4828 | return( 0 ); |
| 4829 | } |
| 4830 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4831 | ret = ssl_check_dtls_clihlo_cookie( |
| 4832 | ssl->conf->f_cookie_write, |
| 4833 | ssl->conf->f_cookie_check, |
| 4834 | ssl->conf->p_cookie, |
| 4835 | ssl->cli_id, ssl->cli_id_len, |
| 4836 | ssl->in_buf, ssl->in_left, |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4837 | ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4838 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4839 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret ); |
| 4840 | |
| 4841 | if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4842 | { |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 4843 | /* 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] | 4844 | * If the error is permanent we'll catch it later, |
| 4845 | * if it's not, then hopefully it'll work next time. */ |
Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 4846 | (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] | 4847 | ret = 0; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4848 | } |
| 4849 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4850 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4851 | { |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4852 | /* Got a valid cookie, partially reset context */ |
| 4853 | if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 ) |
| 4854 | { |
| 4855 | MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret ); |
| 4856 | return( ret ); |
| 4857 | } |
| 4858 | |
| 4859 | return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4860 | } |
| 4861 | |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4862 | return( ret ); |
| 4863 | } |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 4864 | #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] | 4865 | |
Hanno Becker | 46483f1 | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 4866 | static int ssl_check_record_type( uint8_t record_type ) |
| 4867 | { |
| 4868 | if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4869 | record_type != MBEDTLS_SSL_MSG_ALERT && |
| 4870 | record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC && |
| 4871 | record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
| 4872 | { |
| 4873 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4874 | } |
| 4875 | |
| 4876 | return( 0 ); |
| 4877 | } |
| 4878 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4879 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4880 | * ContentType type; |
| 4881 | * ProtocolVersion version; |
| 4882 | * uint16 epoch; // DTLS only |
| 4883 | * uint48 sequence_number; // DTLS only |
| 4884 | * uint16 length; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4885 | * |
| 4886 | * 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] | 4887 | * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad, |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4888 | * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected. |
| 4889 | * |
| 4890 | * With DTLS, mbedtls_ssl_read_record() will: |
Simon Butcher | 207990d | 2015-12-16 01:51:30 +0000 | [diff] [blame] | 4891 | * 1. proceed with the record if this function returns 0 |
| 4892 | * 2. drop only the current record if this function returns UNEXPECTED_RECORD |
| 4893 | * 3. return CLIENT_RECONNECT if this function return that value |
| 4894 | * 4. drop the whole datagram if this function returns anything else. |
| 4895 | * Point 2 is needed when the peer is resending, and we have already received |
| 4896 | * 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] | 4897 | */ |
Hanno Becker | 21fc61c | 2019-07-12 11:10:16 +0100 | [diff] [blame] | 4898 | static int ssl_parse_record_header( mbedtls_ssl_context const *ssl, |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4899 | unsigned char *buf, |
| 4900 | size_t len, |
| 4901 | mbedtls_record *rec ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4902 | { |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 4903 | int major_ver, minor_ver; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4904 | |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4905 | size_t const rec_hdr_type_offset = 0; |
| 4906 | size_t const rec_hdr_type_len = 1; |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 4907 | |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4908 | size_t const rec_hdr_version_offset = rec_hdr_type_offset + |
| 4909 | rec_hdr_type_len; |
| 4910 | size_t const rec_hdr_version_len = 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4911 | |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4912 | size_t const rec_hdr_ctr_len = 8; |
| 4913 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 6181761 | 2019-07-25 10:13:02 +0100 | [diff] [blame] | 4914 | uint32_t rec_epoch; |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4915 | size_t const rec_hdr_ctr_offset = rec_hdr_version_offset + |
| 4916 | rec_hdr_version_len; |
| 4917 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4918 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4919 | size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset + |
| 4920 | rec_hdr_ctr_len; |
Hanno Becker | 6181761 | 2019-07-25 10:13:02 +0100 | [diff] [blame] | 4921 | size_t rec_hdr_cid_len = 0; |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4922 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4923 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4924 | |
| 4925 | size_t rec_hdr_len_offset; /* To be determined */ |
| 4926 | size_t const rec_hdr_len_len = 2; |
| 4927 | |
| 4928 | /* |
| 4929 | * Check minimum lengths for record header. |
| 4930 | */ |
| 4931 | |
| 4932 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4933 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
| 4934 | { |
| 4935 | rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len; |
| 4936 | } |
| 4937 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 4938 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4939 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 4940 | { |
| 4941 | rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len; |
| 4942 | } |
| 4943 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4944 | |
| 4945 | if( len < rec_hdr_len_offset + rec_hdr_len_len ) |
| 4946 | { |
| 4947 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u", |
| 4948 | (unsigned) len, |
| 4949 | (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) ); |
| 4950 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4951 | } |
| 4952 | |
| 4953 | /* |
| 4954 | * Parse and validate record content type |
| 4955 | */ |
| 4956 | |
| 4957 | rec->type = buf[ rec_hdr_type_offset ]; |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4958 | |
| 4959 | /* Check record content type */ |
| 4960 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4961 | rec->cid_len = 0; |
| 4962 | |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4963 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4964 | mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 && |
| 4965 | rec->type == MBEDTLS_SSL_MSG_CID ) |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4966 | { |
| 4967 | /* Shift pointers to account for record header including CID |
| 4968 | * struct { |
| 4969 | * ContentType special_type = tls12_cid; |
| 4970 | * ProtocolVersion version; |
| 4971 | * uint16 epoch; |
| 4972 | * uint48 sequence_number; |
Hanno Becker | 3b2bf5b | 2019-05-23 17:03:19 +0100 | [diff] [blame] | 4973 | * opaque cid[cid_length]; // Additional field compared to |
| 4974 | * // default DTLS record format |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4975 | * uint16 length; |
| 4976 | * opaque enc_content[DTLSCiphertext.length]; |
| 4977 | * } DTLSCiphertext; |
| 4978 | */ |
| 4979 | |
| 4980 | /* So far, we only support static CID lengths |
| 4981 | * fixed in the configuration. */ |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4982 | rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf ); |
| 4983 | rec_hdr_len_offset += rec_hdr_cid_len; |
Hanno Becker | de7d6d3 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 4984 | |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4985 | if( len < rec_hdr_len_offset + rec_hdr_len_len ) |
Hanno Becker | de7d6d3 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 4986 | { |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4987 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u", |
| 4988 | (unsigned) len, |
| 4989 | (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) ); |
Hanno Becker | 2982346 | 2019-07-10 14:53:43 +0100 | [diff] [blame] | 4990 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Hanno Becker | de7d6d3 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 4991 | } |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4992 | |
Manuel Pégourié-Gonnard | f3a15b3 | 2019-08-02 10:17:15 +0200 | [diff] [blame] | 4993 | /* configured CID len is guaranteed at most 255, see |
| 4994 | * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */ |
| 4995 | rec->cid_len = (uint8_t) rec_hdr_cid_len; |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4996 | memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len ); |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4997 | } |
| 4998 | else |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4999 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 5000 | { |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5001 | if( ssl_check_record_type( rec->type ) ) |
| 5002 | { |
Hanno Becker | 03e2db6 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 5003 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u", |
| 5004 | (unsigned) rec->type ) ); |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5005 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5006 | } |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 5007 | } |
| 5008 | |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5009 | /* |
| 5010 | * Parse and validate record version |
| 5011 | */ |
| 5012 | |
Hanno Becker | 8061c6e | 2019-07-26 08:07:03 +0100 | [diff] [blame] | 5013 | rec->ver[0] = buf[ rec_hdr_version_offset + 0 ]; |
| 5014 | rec->ver[1] = buf[ rec_hdr_version_offset + 1 ]; |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5015 | mbedtls_ssl_read_version( &major_ver, &minor_ver, |
| 5016 | ssl->conf->transport, |
Hanno Becker | 8061c6e | 2019-07-26 08:07:03 +0100 | [diff] [blame] | 5017 | &rec->ver[0] ); |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5018 | |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 5019 | if( major_ver != mbedtls_ssl_get_major_ver( ssl ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5020 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5021 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) ); |
| 5022 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5023 | } |
| 5024 | |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 5025 | if( minor_ver > mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5026 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5027 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) ); |
| 5028 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5029 | } |
| 5030 | |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5031 | /* |
| 5032 | * Parse/Copy record sequence number. |
| 5033 | */ |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5034 | |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5035 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5036 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
| 5037 | { |
| 5038 | /* Copy explicit record sequence number from input buffer. */ |
| 5039 | memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset, |
| 5040 | rec_hdr_ctr_len ); |
| 5041 | } |
| 5042 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 5043 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5044 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 5045 | { |
| 5046 | /* Copy implicit record sequence number from SSL context structure. */ |
| 5047 | memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len ); |
| 5048 | } |
| 5049 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5050 | |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5051 | /* |
| 5052 | * Parse record length. |
| 5053 | */ |
| 5054 | |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5055 | rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len; |
Hanno Becker | 7b5ba84 | 2019-07-25 10:16:37 +0100 | [diff] [blame] | 5056 | rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) | |
| 5057 | ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 ); |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5058 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset ); |
| 5059 | |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5060 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, " |
Hanno Becker | d8f7c4a | 2019-05-23 17:03:44 +0100 | [diff] [blame] | 5061 | "version = [%d:%d], msglen = %d", |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5062 | rec->type, |
| 5063 | major_ver, minor_ver, rec->data_len ) ); |
| 5064 | |
| 5065 | rec->buf = buf; |
| 5066 | rec->buf_len = rec->data_offset + rec->data_len; |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5067 | |
Hanno Becker | ec01408 | 2019-07-26 08:20:27 +0100 | [diff] [blame] | 5068 | if( rec->data_len == 0 ) |
| 5069 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5070 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5071 | /* |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 5072 | * DTLS-related tests. |
| 5073 | * Check epoch before checking length constraint because |
| 5074 | * the latter varies with the epoch. E.g., if a ChangeCipherSpec |
| 5075 | * message gets duplicated before the corresponding Finished message, |
| 5076 | * the second ChangeCipherSpec should be discarded because it belongs |
| 5077 | * to an old epoch, but not because its length is shorter than |
| 5078 | * the minimum record length for packets using the new record transform. |
| 5079 | * Note that these two kinds of failures are handled differently, |
| 5080 | * as an unexpected record is silently skipped but an invalid |
| 5081 | * record leads to the entire datagram being dropped. |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5082 | */ |
| 5083 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 5084 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5085 | { |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5086 | rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1]; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5087 | |
Hanno Becker | e045277 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 5088 | /* Check that the datagram is large enough to contain a record |
| 5089 | * of the advertised length. */ |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5090 | if( len < rec->data_offset + rec->data_len ) |
Hanno Becker | e045277 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 5091 | { |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5092 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.", |
| 5093 | (unsigned) len, |
| 5094 | (unsigned)( rec->data_offset + rec->data_len ) ) ); |
Hanno Becker | e045277 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 5095 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5096 | } |
Hanno Becker | 6c0e53c | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 5097 | |
Hanno Becker | 6c0e53c | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 5098 | /* Records from other, non-matching epochs are silently discarded. |
| 5099 | * (The case of same-port Client reconnects must be considered in |
| 5100 | * the caller). */ |
Hanno Becker | c1c173c | 2019-07-19 10:59:12 +0100 | [diff] [blame] | 5101 | if( rec_epoch != ssl->in_epoch ) |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5102 | { |
| 5103 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: " |
| 5104 | "expected %d, received %d", |
| 5105 | ssl->in_epoch, rec_epoch ) ); |
Hanno Becker | c1c173c | 2019-07-19 10:59:12 +0100 | [diff] [blame] | 5106 | |
| 5107 | /* Records from the next epoch are considered for buffering |
| 5108 | * (concretely: early Finished messages). */ |
| 5109 | if( rec_epoch == (unsigned) ssl->in_epoch + 1 ) |
| 5110 | { |
| 5111 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) ); |
| 5112 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 5113 | } |
| 5114 | |
Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5115 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5116 | } |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5117 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Hanno Becker | 6c0e53c | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 5118 | /* For records from the correct epoch, check whether their |
| 5119 | * sequence number has been seen before. */ |
Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5120 | else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5121 | { |
| 5122 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) ); |
| 5123 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
| 5124 | } |
| 5125 | #endif |
| 5126 | } |
| 5127 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5128 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5129 | return( 0 ); |
| 5130 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5131 | |
Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5132 | |
| 5133 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
| 5134 | static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl ) |
| 5135 | { |
| 5136 | unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1]; |
| 5137 | |
| 5138 | /* |
| 5139 | * Check for an epoch 0 ClientHello. We can't use in_msg here to |
| 5140 | * access the first byte of record content (handshake type), as we |
| 5141 | * have an active transform (possibly iv_len != 0), so use the |
| 5142 | * fact that the record header len is 13 instead. |
| 5143 | */ |
| 5144 | if( rec_epoch == 0 && |
| 5145 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 5146 | MBEDTLS_SSL_IS_SERVER && |
| 5147 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 5148 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 5149 | ssl->in_left > 13 && |
| 5150 | ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO ) |
| 5151 | { |
| 5152 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect " |
| 5153 | "from the same port" ) ); |
| 5154 | return( ssl_handle_possible_reconnect( ssl ) ); |
| 5155 | } |
| 5156 | |
| 5157 | return( 0 ); |
| 5158 | } |
| 5159 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
| 5160 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5161 | /* |
| 5162 | * If applicable, decrypt (and decompress) record content |
| 5163 | */ |
Hanno Becker | a89610a | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 5164 | static int ssl_prepare_record_content( mbedtls_ssl_context *ssl, |
| 5165 | mbedtls_record *rec ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5166 | { |
| 5167 | int ret, done = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 5168 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5169 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network", |
Hanno Becker | a89610a | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 5170 | rec->buf, rec->buf_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5171 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5172 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 5173 | if( mbedtls_ssl_hw_record_read != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5174 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5175 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5176 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5177 | ret = mbedtls_ssl_hw_record_read( ssl ); |
| 5178 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5179 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5180 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret ); |
| 5181 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5182 | } |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 5183 | |
| 5184 | if( ret == 0 ) |
| 5185 | done = 1; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5186 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5187 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5188 | if( !done && ssl->transform_in != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5189 | { |
Hanno Becker | 106f3da | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5190 | unsigned char const old_msg_type = rec->type; |
| 5191 | |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 5192 | if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, |
Hanno Becker | a89610a | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 5193 | rec ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5194 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5195 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret ); |
Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 5196 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5197 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 5198 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID && |
Hanno Becker | e0200da | 2019-06-13 09:23:43 +0100 | [diff] [blame] | 5199 | mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf ) |
| 5200 | == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 5201 | { |
Hanno Becker | 675c4d6 | 2019-05-24 10:11:06 +0100 | [diff] [blame] | 5202 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) ); |
Hanno Becker | 687e0fb | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 5203 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 5204 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5205 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 687e0fb | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 5206 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5207 | return( ret ); |
| 5208 | } |
| 5209 | |
Hanno Becker | 106f3da | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5210 | if( old_msg_type != rec->type ) |
Hanno Becker | 93012fe | 2018-08-07 14:30:18 +0100 | [diff] [blame] | 5211 | { |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5212 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d", |
Hanno Becker | 106f3da | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5213 | old_msg_type, rec->type ) ); |
Hanno Becker | 93012fe | 2018-08-07 14:30:18 +0100 | [diff] [blame] | 5214 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5215 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5216 | MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt", |
Hanno Becker | 106f3da | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5217 | rec->buf + rec->data_offset, rec->data_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5218 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5219 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5220 | /* We have already checked the record content type |
| 5221 | * in ssl_parse_record_header(), failing or silently |
| 5222 | * dropping the record in the case of an unknown type. |
| 5223 | * |
| 5224 | * Since with the use of CIDs, the record content type |
| 5225 | * might change during decryption, re-check the record |
| 5226 | * content type, but treat a failure as fatal this time. */ |
Hanno Becker | 106f3da | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5227 | if( ssl_check_record_type( rec->type ) ) |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5228 | { |
| 5229 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) ); |
| 5230 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5231 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5232 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5233 | |
Hanno Becker | 106f3da | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5234 | if( rec->data_len == 0 ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5235 | { |
| 5236 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 5237 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3 |
Hanno Becker | 106f3da | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5238 | && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5239 | { |
| 5240 | /* TLS v1.2 explicitly disallows zero-length messages which are not application data */ |
| 5241 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) ); |
| 5242 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5243 | } |
| 5244 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5245 | |
| 5246 | ssl->nb_zero++; |
| 5247 | |
| 5248 | /* |
| 5249 | * Three or more empty messages may be a DoS attack |
| 5250 | * (excessive CPU consumption). |
| 5251 | */ |
| 5252 | if( ssl->nb_zero > 3 ) |
| 5253 | { |
| 5254 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty " |
Hanno Becker | 70463db | 2019-05-08 10:38:32 +0100 | [diff] [blame] | 5255 | "messages, possible DoS attack" ) ); |
| 5256 | /* Treat the records as if they were not properly authenticated, |
| 5257 | * thereby failing the connection if we see more than allowed |
| 5258 | * by the configured bad MAC threshold. */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5259 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
| 5260 | } |
| 5261 | } |
| 5262 | else |
| 5263 | ssl->nb_zero = 0; |
| 5264 | |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5265 | /* Only needed for TLS, as with DTLS in_ctr is read from the header */ |
| 5266 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 5267 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5268 | { |
| 5269 | unsigned i; |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5270 | for( i = 8; i > 0; i-- ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5271 | if( ++ssl->in_ctr[i - 1] != 0 ) |
| 5272 | break; |
| 5273 | |
Manuel Pégourié-Gonnard | 8794a42 | 2019-06-11 10:04:57 +0200 | [diff] [blame] | 5274 | /* 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] | 5275 | if( i == 0 ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5276 | { |
| 5277 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) ); |
| 5278 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 5279 | } |
| 5280 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5281 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5282 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5283 | } |
| 5284 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5285 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5286 | if( ssl->transform_in != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5287 | ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5288 | { |
| 5289 | if( ( ret = ssl_decompress_buf( ssl ) ) != 0 ) |
| 5290 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5291 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5292 | return( ret ); |
| 5293 | } |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5294 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5295 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5296 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5297 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 5298 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 5299 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5300 | mbedtls_ssl_dtls_replay_update( ssl ); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 5301 | } |
| 5302 | #endif |
| 5303 | |
Hanno Becker | f024285 | 2019-07-09 17:30:02 +0100 | [diff] [blame] | 5304 | /* Check actual (decrypted) record content length against |
| 5305 | * configured maximum. */ |
| 5306 | if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN ) |
| 5307 | { |
| 5308 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 5309 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5310 | } |
| 5311 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5312 | return( 0 ); |
| 5313 | } |
| 5314 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5315 | 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] | 5316 | |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5317 | /* |
| 5318 | * Read a record. |
| 5319 | * |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 5320 | * Silently ignore non-fatal alert (and for DTLS, invalid records as well, |
| 5321 | * RFC 6347 4.1.2.7) and continue reading until a valid record is found. |
| 5322 | * |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5323 | */ |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5324 | |
| 5325 | /* Helper functions for mbedtls_ssl_read_record(). */ |
| 5326 | static int ssl_consume_current_message( mbedtls_ssl_context *ssl ); |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5327 | static int ssl_get_next_record( mbedtls_ssl_context *ssl ); |
| 5328 | static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl ); |
Hanno Becker | 4162b11 | 2018-08-15 14:05:04 +0100 | [diff] [blame] | 5329 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 5330 | int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl, |
Hanno Becker | 3a0aad1 | 2018-08-20 09:44:02 +0100 | [diff] [blame] | 5331 | unsigned update_hs_digest ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5332 | { |
| 5333 | int ret; |
| 5334 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5335 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5336 | |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5337 | if( ssl->keep_current_message == 0 ) |
| 5338 | { |
| 5339 | do { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5340 | |
Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 5341 | ret = ssl_consume_current_message( ssl ); |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5342 | if( ret != 0 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5343 | return( ret ); |
Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 5344 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5345 | if( ssl_record_is_in_progress( ssl ) == 0 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5346 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5347 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5348 | int have_buffered = 0; |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5349 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5350 | /* We only check for buffered messages if the |
| 5351 | * current datagram is fully consumed. */ |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 5352 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 5353 | ssl_next_record_is_in_datagram( ssl ) == 0 ) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5354 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5355 | if( ssl_load_buffered_message( ssl ) == 0 ) |
| 5356 | have_buffered = 1; |
| 5357 | } |
| 5358 | |
| 5359 | if( have_buffered == 0 ) |
| 5360 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5361 | { |
| 5362 | ret = ssl_get_next_record( ssl ); |
| 5363 | if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ) |
| 5364 | continue; |
| 5365 | |
| 5366 | if( ret != 0 ) |
| 5367 | { |
Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 5368 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret ); |
Hanno Becker | 42a6b04 | 2019-07-26 07:25:20 +0100 | [diff] [blame] | 5369 | ssl_send_pending_fatal_alert( ssl ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5370 | return( ret ); |
| 5371 | } |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5372 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5373 | } |
| 5374 | |
| 5375 | ret = mbedtls_ssl_handle_message_type( ssl ); |
| 5376 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5377 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5378 | if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 5379 | { |
| 5380 | /* Buffer future message */ |
| 5381 | ret = ssl_buffer_message( ssl ); |
| 5382 | if( ret != 0 ) |
| 5383 | return( ret ); |
| 5384 | |
| 5385 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
| 5386 | } |
| 5387 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5388 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5389 | } while( MBEDTLS_ERR_SSL_NON_FATAL == ret || |
| 5390 | MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5391 | |
| 5392 | if( 0 != ret ) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5393 | { |
Hanno Becker | 05c4fc8 | 2017-11-09 14:34:06 +0000 | [diff] [blame] | 5394 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5395 | return( ret ); |
| 5396 | } |
| 5397 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 5398 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
Hanno Becker | 3a0aad1 | 2018-08-20 09:44:02 +0100 | [diff] [blame] | 5399 | update_hs_digest == 1 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5400 | { |
| 5401 | mbedtls_ssl_update_handshake_status( ssl ); |
| 5402 | } |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5403 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5404 | else |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5405 | { |
Hanno Becker | 02f5907 | 2018-08-15 14:00:24 +0100 | [diff] [blame] | 5406 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5407 | ssl->keep_current_message = 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5408 | } |
| 5409 | |
| 5410 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) ); |
| 5411 | |
| 5412 | return( 0 ); |
| 5413 | } |
| 5414 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5415 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 5416 | static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl ) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5417 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5418 | if( ssl->in_left > ssl->next_record_offset ) |
| 5419 | return( 1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5420 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5421 | return( 0 ); |
| 5422 | } |
| 5423 | |
| 5424 | static int ssl_load_buffered_message( mbedtls_ssl_context *ssl ) |
| 5425 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5426 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5427 | mbedtls_ssl_hs_buffer * hs_buf; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5428 | int ret = 0; |
| 5429 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5430 | if( hs == NULL ) |
| 5431 | return( -1 ); |
| 5432 | |
Hanno Becker | e00ae37 | 2018-08-20 09:39:42 +0100 | [diff] [blame] | 5433 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) ); |
| 5434 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5435 | if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC || |
| 5436 | ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) |
| 5437 | { |
| 5438 | /* Check if we have seen a ChangeCipherSpec before. |
| 5439 | * If yes, synthesize a CCS record. */ |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5440 | if( !hs->buffering.seen_ccs ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5441 | { |
| 5442 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) ); |
| 5443 | ret = -1; |
Hanno Becker | 0d4b376 | 2018-08-20 09:36:59 +0100 | [diff] [blame] | 5444 | goto exit; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5445 | } |
| 5446 | |
Hanno Becker | 39b8bc9 | 2018-08-28 17:17:13 +0100 | [diff] [blame] | 5447 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) ); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5448 | ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
| 5449 | ssl->in_msglen = 1; |
| 5450 | ssl->in_msg[0] = 1; |
| 5451 | |
| 5452 | /* As long as they are equal, the exact value doesn't matter. */ |
| 5453 | ssl->in_left = 0; |
| 5454 | ssl->next_record_offset = 0; |
| 5455 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 5456 | hs->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5457 | goto exit; |
| 5458 | } |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5459 | |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 5460 | #if defined(MBEDTLS_DEBUG_C) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5461 | /* Debug only */ |
| 5462 | { |
| 5463 | unsigned offset; |
| 5464 | for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ ) |
| 5465 | { |
| 5466 | hs_buf = &hs->buffering.hs[offset]; |
| 5467 | if( hs_buf->is_valid == 1 ) |
| 5468 | { |
| 5469 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.", |
| 5470 | hs->in_msg_seq + offset, |
Hanno Becker | a591c48 | 2018-08-28 17:20:00 +0100 | [diff] [blame] | 5471 | hs_buf->is_complete ? "fully" : "partially" ) ); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5472 | } |
| 5473 | } |
| 5474 | } |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 5475 | #endif /* MBEDTLS_DEBUG_C */ |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5476 | |
| 5477 | /* Check if we have buffered and/or fully reassembled the |
| 5478 | * next handshake message. */ |
| 5479 | hs_buf = &hs->buffering.hs[0]; |
| 5480 | if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) ) |
| 5481 | { |
| 5482 | /* Synthesize a record containing the buffered HS message. */ |
| 5483 | size_t msg_len = ( hs_buf->data[1] << 16 ) | |
| 5484 | ( hs_buf->data[2] << 8 ) | |
| 5485 | hs_buf->data[3]; |
| 5486 | |
| 5487 | /* Double-check that we haven't accidentally buffered |
| 5488 | * a message that doesn't fit into the input buffer. */ |
| 5489 | if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN ) |
| 5490 | { |
| 5491 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5492 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5493 | } |
| 5494 | |
| 5495 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) ); |
| 5496 | MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)", |
| 5497 | hs_buf->data, msg_len + 12 ); |
| 5498 | |
| 5499 | ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 5500 | ssl->in_hslen = msg_len + 12; |
| 5501 | ssl->in_msglen = msg_len + 12; |
| 5502 | memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen ); |
| 5503 | |
| 5504 | ret = 0; |
| 5505 | goto exit; |
| 5506 | } |
| 5507 | else |
| 5508 | { |
| 5509 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered", |
| 5510 | hs->in_msg_seq ) ); |
| 5511 | } |
| 5512 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5513 | ret = -1; |
| 5514 | |
| 5515 | exit: |
| 5516 | |
| 5517 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) ); |
| 5518 | return( ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5519 | } |
| 5520 | |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5521 | static int ssl_buffer_make_space( mbedtls_ssl_context *ssl, |
| 5522 | size_t desired ) |
| 5523 | { |
| 5524 | int offset; |
| 5525 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5526 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available", |
| 5527 | (unsigned) desired ) ); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5528 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5529 | /* Get rid of future records epoch first, if such exist. */ |
| 5530 | ssl_free_buffered_record( ssl ); |
| 5531 | |
| 5532 | /* Check if we have enough space available now. */ |
| 5533 | if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5534 | hs->buffering.total_bytes_buffered ) ) |
| 5535 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5536 | 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] | 5537 | return( 0 ); |
| 5538 | } |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5539 | |
Hanno Becker | 4f432ad | 2018-08-28 10:02:32 +0100 | [diff] [blame] | 5540 | /* We don't have enough space to buffer the next expected handshake |
| 5541 | * message. Remove buffers used for future messages to gain space, |
| 5542 | * starting with the most distant one. */ |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5543 | for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1; |
| 5544 | offset >= 0; offset-- ) |
| 5545 | { |
| 5546 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message", |
| 5547 | offset ) ); |
| 5548 | |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 5549 | ssl_buffering_free_slot( ssl, (uint8_t) offset ); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5550 | |
| 5551 | /* Check if we have enough space available now. */ |
| 5552 | if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5553 | hs->buffering.total_bytes_buffered ) ) |
| 5554 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5555 | 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] | 5556 | return( 0 ); |
| 5557 | } |
| 5558 | } |
| 5559 | |
| 5560 | return( -1 ); |
| 5561 | } |
| 5562 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5563 | static int ssl_buffer_message( mbedtls_ssl_context *ssl ) |
| 5564 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5565 | int ret = 0; |
| 5566 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5567 | |
| 5568 | if( hs == NULL ) |
| 5569 | return( 0 ); |
| 5570 | |
| 5571 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) ); |
| 5572 | |
| 5573 | switch( ssl->in_msgtype ) |
| 5574 | { |
| 5575 | case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: |
| 5576 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) ); |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5577 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 5578 | hs->buffering.seen_ccs = 1; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5579 | break; |
| 5580 | |
| 5581 | case MBEDTLS_SSL_MSG_HANDSHAKE: |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5582 | { |
| 5583 | unsigned recv_msg_seq_offset; |
| 5584 | unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5]; |
| 5585 | mbedtls_ssl_hs_buffer *hs_buf; |
| 5586 | size_t msg_len = ssl->in_hslen - 12; |
| 5587 | |
| 5588 | /* We should never receive an old handshake |
| 5589 | * message - double-check nonetheless. */ |
| 5590 | if( recv_msg_seq < ssl->handshake->in_msg_seq ) |
| 5591 | { |
| 5592 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5593 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5594 | } |
| 5595 | |
| 5596 | recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq; |
| 5597 | if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS ) |
| 5598 | { |
| 5599 | /* Silently ignore -- message too far in the future */ |
| 5600 | MBEDTLS_SSL_DEBUG_MSG( 2, |
| 5601 | ( "Ignore future HS message with sequence number %u, " |
| 5602 | "buffering window %u - %u", |
| 5603 | recv_msg_seq, ssl->handshake->in_msg_seq, |
| 5604 | ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) ); |
| 5605 | |
| 5606 | goto exit; |
| 5607 | } |
| 5608 | |
| 5609 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ", |
| 5610 | recv_msg_seq, recv_msg_seq_offset ) ); |
| 5611 | |
| 5612 | hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ]; |
| 5613 | |
| 5614 | /* Check if the buffering for this seq nr has already commenced. */ |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5615 | if( !hs_buf->is_valid ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5616 | { |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 5617 | size_t reassembly_buf_sz; |
| 5618 | |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5619 | hs_buf->is_fragmented = |
| 5620 | ( ssl_hs_is_proper_fragment( ssl ) == 1 ); |
| 5621 | |
| 5622 | /* We copy the message back into the input buffer |
| 5623 | * after reassembly, so check that it's not too large. |
| 5624 | * This is an implementation-specific limitation |
| 5625 | * and not one from the standard, hence it is not |
| 5626 | * checked in ssl_check_hs_header(). */ |
Hanno Becker | 96a6c69 | 2018-08-21 15:56:03 +0100 | [diff] [blame] | 5627 | if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5628 | { |
| 5629 | /* Ignore message */ |
| 5630 | goto exit; |
| 5631 | } |
| 5632 | |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5633 | /* Check if we have enough space to buffer the message. */ |
| 5634 | if( hs->buffering.total_bytes_buffered > |
| 5635 | MBEDTLS_SSL_DTLS_MAX_BUFFERING ) |
| 5636 | { |
| 5637 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5638 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5639 | } |
| 5640 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 5641 | reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len, |
| 5642 | hs_buf->is_fragmented ); |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5643 | |
| 5644 | if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5645 | hs->buffering.total_bytes_buffered ) ) |
| 5646 | { |
| 5647 | if( recv_msg_seq_offset > 0 ) |
| 5648 | { |
| 5649 | /* If we can't buffer a future message because |
| 5650 | * of space limitations -- ignore. */ |
| 5651 | 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", |
| 5652 | (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 5653 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
| 5654 | goto exit; |
| 5655 | } |
Hanno Becker | e180139 | 2018-08-21 16:51:05 +0100 | [diff] [blame] | 5656 | else |
| 5657 | { |
| 5658 | 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", |
| 5659 | (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 5660 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
| 5661 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5662 | |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5663 | if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 ) |
Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 5664 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5665 | 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", |
| 5666 | (unsigned) msg_len, |
| 5667 | (unsigned) reassembly_buf_sz, |
| 5668 | MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5669 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 5670 | ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 5671 | goto exit; |
| 5672 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5673 | } |
| 5674 | |
| 5675 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d", |
| 5676 | msg_len ) ); |
| 5677 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 5678 | hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz ); |
| 5679 | if( hs_buf->data == NULL ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5680 | { |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5681 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5682 | goto exit; |
| 5683 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5684 | hs_buf->data_len = reassembly_buf_sz; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5685 | |
| 5686 | /* Prepare final header: copy msg_type, length and message_seq, |
| 5687 | * then add standardised fragment_offset and fragment_length */ |
| 5688 | memcpy( hs_buf->data, ssl->in_msg, 6 ); |
| 5689 | memset( hs_buf->data + 6, 0, 3 ); |
| 5690 | memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 ); |
| 5691 | |
| 5692 | hs_buf->is_valid = 1; |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5693 | |
| 5694 | hs->buffering.total_bytes_buffered += reassembly_buf_sz; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5695 | } |
| 5696 | else |
| 5697 | { |
| 5698 | /* Make sure msg_type and length are consistent */ |
| 5699 | if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 ) |
| 5700 | { |
| 5701 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) ); |
| 5702 | /* Ignore */ |
| 5703 | goto exit; |
| 5704 | } |
| 5705 | } |
| 5706 | |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5707 | if( !hs_buf->is_complete ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5708 | { |
| 5709 | size_t frag_len, frag_off; |
| 5710 | unsigned char * const msg = hs_buf->data + 12; |
| 5711 | |
| 5712 | /* |
| 5713 | * Check and copy current fragment |
| 5714 | */ |
| 5715 | |
| 5716 | /* Validation of header fields already done in |
| 5717 | * mbedtls_ssl_prepare_handshake_record(). */ |
| 5718 | frag_off = ssl_get_hs_frag_off( ssl ); |
| 5719 | frag_len = ssl_get_hs_frag_len( ssl ); |
| 5720 | |
| 5721 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d", |
| 5722 | frag_off, frag_len ) ); |
| 5723 | memcpy( msg + frag_off, ssl->in_msg + 12, frag_len ); |
| 5724 | |
| 5725 | if( hs_buf->is_fragmented ) |
| 5726 | { |
| 5727 | unsigned char * const bitmask = msg + msg_len; |
| 5728 | ssl_bitmask_set( bitmask, frag_off, frag_len ); |
| 5729 | hs_buf->is_complete = ( ssl_bitmask_check( bitmask, |
| 5730 | msg_len ) == 0 ); |
| 5731 | } |
| 5732 | else |
| 5733 | { |
| 5734 | hs_buf->is_complete = 1; |
| 5735 | } |
| 5736 | |
| 5737 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete", |
| 5738 | hs_buf->is_complete ? "" : "not yet " ) ); |
| 5739 | } |
| 5740 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5741 | break; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5742 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5743 | |
| 5744 | default: |
Hanno Becker | 360bef3 | 2018-08-28 10:04:33 +0100 | [diff] [blame] | 5745 | /* We don't buffer other types of messages. */ |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5746 | break; |
| 5747 | } |
| 5748 | |
| 5749 | exit: |
| 5750 | |
| 5751 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) ); |
| 5752 | return( ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5753 | } |
| 5754 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5755 | |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5756 | static int ssl_consume_current_message( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5757 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5758 | /* |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5759 | * Consume last content-layer message and potentially |
| 5760 | * update in_msglen which keeps track of the contents' |
| 5761 | * consumption state. |
| 5762 | * |
| 5763 | * (1) Handshake messages: |
| 5764 | * Remove last handshake message, move content |
| 5765 | * and adapt in_msglen. |
| 5766 | * |
| 5767 | * (2) Alert messages: |
| 5768 | * Consume whole record content, in_msglen = 0. |
| 5769 | * |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5770 | * (3) Change cipher spec: |
| 5771 | * Consume whole record content, in_msglen = 0. |
| 5772 | * |
| 5773 | * (4) Application data: |
| 5774 | * Don't do anything - the record layer provides |
| 5775 | * the application data as a stream transport |
| 5776 | * and consumes through mbedtls_ssl_read only. |
| 5777 | * |
| 5778 | */ |
| 5779 | |
| 5780 | /* Case (1): Handshake messages */ |
| 5781 | if( ssl->in_hslen != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5782 | { |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 5783 | /* Hard assertion to be sure that no application data |
| 5784 | * is in flight, as corrupting ssl->in_msglen during |
| 5785 | * ssl->in_offt != NULL is fatal. */ |
| 5786 | if( ssl->in_offt != NULL ) |
| 5787 | { |
| 5788 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5789 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5790 | } |
| 5791 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5792 | /* |
| 5793 | * Get next Handshake message in the current record |
| 5794 | */ |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5795 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5796 | /* Notes: |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 5797 | * (1) in_hslen is not necessarily the size of the |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5798 | * current handshake content: If DTLS handshake |
| 5799 | * fragmentation is used, that's the fragment |
| 5800 | * size instead. Using the total handshake message |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 5801 | * size here is faulty and should be changed at |
| 5802 | * some point. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5803 | * (2) While it doesn't seem to cause problems, one |
| 5804 | * has to be very careful not to assume that in_hslen |
| 5805 | * is always <= in_msglen in a sensible communication. |
| 5806 | * Again, it's wrong for DTLS handshake fragmentation. |
| 5807 | * The following check is therefore mandatory, and |
| 5808 | * should not be treated as a silently corrected assertion. |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 5809 | * Additionally, ssl->in_hslen might be arbitrarily out of |
| 5810 | * bounds after handling a DTLS message with an unexpected |
| 5811 | * sequence number, see mbedtls_ssl_prepare_handshake_record. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5812 | */ |
| 5813 | if( ssl->in_hslen < ssl->in_msglen ) |
| 5814 | { |
| 5815 | ssl->in_msglen -= ssl->in_hslen; |
| 5816 | memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen, |
| 5817 | ssl->in_msglen ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5818 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5819 | MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record", |
| 5820 | ssl->in_msg, ssl->in_msglen ); |
| 5821 | } |
| 5822 | else |
| 5823 | { |
| 5824 | ssl->in_msglen = 0; |
| 5825 | } |
Manuel Pégourié-Gonnard | 4a17536 | 2014-09-09 17:45:31 +0200 | [diff] [blame] | 5826 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5827 | ssl->in_hslen = 0; |
| 5828 | } |
| 5829 | /* Case (4): Application data */ |
| 5830 | else if( ssl->in_offt != NULL ) |
| 5831 | { |
| 5832 | return( 0 ); |
| 5833 | } |
| 5834 | /* Everything else (CCS & Alerts) */ |
| 5835 | else |
| 5836 | { |
| 5837 | ssl->in_msglen = 0; |
| 5838 | } |
| 5839 | |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5840 | return( 0 ); |
| 5841 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5842 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5843 | static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl ) |
| 5844 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5845 | if( ssl->in_msglen > 0 ) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5846 | return( 1 ); |
| 5847 | |
| 5848 | return( 0 ); |
| 5849 | } |
| 5850 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5851 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5852 | |
| 5853 | static void ssl_free_buffered_record( mbedtls_ssl_context *ssl ) |
| 5854 | { |
| 5855 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5856 | if( hs == NULL ) |
| 5857 | return; |
| 5858 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5859 | if( hs->buffering.future_record.data != NULL ) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5860 | { |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5861 | hs->buffering.total_bytes_buffered -= |
| 5862 | hs->buffering.future_record.len; |
| 5863 | |
| 5864 | mbedtls_free( hs->buffering.future_record.data ); |
| 5865 | hs->buffering.future_record.data = NULL; |
| 5866 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5867 | } |
| 5868 | |
| 5869 | static int ssl_load_buffered_record( mbedtls_ssl_context *ssl ) |
| 5870 | { |
| 5871 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5872 | unsigned char * rec; |
| 5873 | size_t rec_len; |
| 5874 | unsigned rec_epoch; |
| 5875 | |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 5876 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5877 | return( 0 ); |
| 5878 | |
| 5879 | if( hs == NULL ) |
| 5880 | return( 0 ); |
| 5881 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5882 | rec = hs->buffering.future_record.data; |
| 5883 | rec_len = hs->buffering.future_record.len; |
| 5884 | rec_epoch = hs->buffering.future_record.epoch; |
| 5885 | |
| 5886 | if( rec == NULL ) |
| 5887 | return( 0 ); |
| 5888 | |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 5889 | /* Only consider loading future records if the |
| 5890 | * input buffer is empty. */ |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 5891 | if( ssl_next_record_is_in_datagram( ssl ) == 1 ) |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 5892 | return( 0 ); |
| 5893 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5894 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) ); |
| 5895 | |
| 5896 | if( rec_epoch != ssl->in_epoch ) |
| 5897 | { |
| 5898 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) ); |
| 5899 | goto exit; |
| 5900 | } |
| 5901 | |
| 5902 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) ); |
| 5903 | |
| 5904 | /* Double-check that the record is not too large */ |
| 5905 | if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN - |
| 5906 | (size_t)( ssl->in_hdr - ssl->in_buf ) ) |
| 5907 | { |
| 5908 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5909 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5910 | } |
| 5911 | |
| 5912 | memcpy( ssl->in_hdr, rec, rec_len ); |
| 5913 | ssl->in_left = rec_len; |
| 5914 | ssl->next_record_offset = 0; |
| 5915 | |
| 5916 | ssl_free_buffered_record( ssl ); |
| 5917 | |
| 5918 | exit: |
| 5919 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) ); |
| 5920 | return( 0 ); |
| 5921 | } |
| 5922 | |
Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 5923 | static int ssl_buffer_future_record( mbedtls_ssl_context *ssl, |
| 5924 | mbedtls_record const *rec ) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5925 | { |
| 5926 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5927 | |
| 5928 | /* Don't buffer future records outside handshakes. */ |
| 5929 | if( hs == NULL ) |
| 5930 | return( 0 ); |
| 5931 | |
| 5932 | /* Only buffer handshake records (we are only interested |
| 5933 | * in Finished messages). */ |
Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 5934 | if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5935 | return( 0 ); |
| 5936 | |
| 5937 | /* Don't buffer more than one future epoch record. */ |
| 5938 | if( hs->buffering.future_record.data != NULL ) |
| 5939 | return( 0 ); |
| 5940 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5941 | /* Don't buffer record if there's not enough buffering space remaining. */ |
Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 5942 | if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5943 | hs->buffering.total_bytes_buffered ) ) |
| 5944 | { |
| 5945 | 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] | 5946 | (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5947 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5948 | return( 0 ); |
| 5949 | } |
| 5950 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5951 | /* Buffer record */ |
| 5952 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u", |
| 5953 | ssl->in_epoch + 1 ) ); |
Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 5954 | MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5955 | |
| 5956 | /* ssl_parse_record_header() only considers records |
| 5957 | * of the next epoch as candidates for buffering. */ |
| 5958 | hs->buffering.future_record.epoch = ssl->in_epoch + 1; |
Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 5959 | hs->buffering.future_record.len = rec->buf_len; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5960 | |
| 5961 | hs->buffering.future_record.data = |
| 5962 | mbedtls_calloc( 1, hs->buffering.future_record.len ); |
| 5963 | if( hs->buffering.future_record.data == NULL ) |
| 5964 | { |
| 5965 | /* If we run out of RAM trying to buffer a |
| 5966 | * record from the next epoch, just ignore. */ |
| 5967 | return( 0 ); |
| 5968 | } |
| 5969 | |
Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 5970 | memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5971 | |
Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 5972 | hs->buffering.total_bytes_buffered += rec->buf_len; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5973 | return( 0 ); |
| 5974 | } |
| 5975 | |
| 5976 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5977 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5978 | static int ssl_get_next_record( mbedtls_ssl_context *ssl ) |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5979 | { |
| 5980 | int ret; |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5981 | mbedtls_record rec; |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5982 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5983 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5984 | /* We might have buffered a future record; if so, |
| 5985 | * and if the epoch matches now, load it. |
| 5986 | * On success, this call will set ssl->in_left to |
| 5987 | * the length of the buffered record, so that |
| 5988 | * the calls to ssl_fetch_input() below will |
| 5989 | * essentially be no-ops. */ |
| 5990 | ret = ssl_load_buffered_record( ssl ); |
| 5991 | if( ret != 0 ) |
| 5992 | return( ret ); |
| 5993 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5994 | |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5995 | /* Ensure that we have enough space available for the default form |
| 5996 | * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS, |
| 5997 | * with no space for CIDs counted in). */ |
| 5998 | ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) ); |
| 5999 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6000 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6001 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6002 | return( ret ); |
| 6003 | } |
| 6004 | |
Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 6005 | ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec ); |
| 6006 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6007 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6008 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6009 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6010 | { |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6011 | if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 6012 | { |
Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6013 | ret = ssl_buffer_future_record( ssl, &rec ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6014 | if( ret != 0 ) |
| 6015 | return( ret ); |
| 6016 | |
| 6017 | /* Fall through to handling of unexpected records */ |
| 6018 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
| 6019 | } |
| 6020 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 6021 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ) |
| 6022 | { |
Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6023 | #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] | 6024 | /* Reset in pointers to default state for TLS/DTLS records, |
| 6025 | * assuming no CID and no offset between record content and |
| 6026 | * record plaintext. */ |
| 6027 | ssl_update_in_pointers( ssl ); |
| 6028 | |
Hanno Becker | 6941245 | 2019-07-12 08:33:49 +0100 | [diff] [blame] | 6029 | /* Setup internal message pointers from record structure. */ |
| 6030 | ssl->in_msgtype = rec.type; |
| 6031 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6032 | ssl->in_len = ssl->in_cid + rec.cid_len; |
| 6033 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | c360dcc | 2019-07-12 10:00:45 +0100 | [diff] [blame] | 6034 | ssl->in_msg = ssl->in_len + 2; |
Hanno Becker | 6941245 | 2019-07-12 08:33:49 +0100 | [diff] [blame] | 6035 | ssl->in_msglen = rec.data_len; |
| 6036 | |
Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6037 | ret = ssl_check_client_reconnect( ssl ); |
| 6038 | if( ret != 0 ) |
| 6039 | return( ret ); |
| 6040 | #endif |
| 6041 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 6042 | /* Skip unexpected record (but not whole datagram) */ |
Hanno Becker | 2528ee0 | 2019-07-11 12:48:53 +0100 | [diff] [blame] | 6043 | ssl->next_record_offset = rec.buf_len; |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6044 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 6045 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record " |
| 6046 | "(header)" ) ); |
| 6047 | } |
| 6048 | else |
| 6049 | { |
| 6050 | /* Skip invalid record and the rest of the datagram */ |
| 6051 | ssl->next_record_offset = 0; |
| 6052 | ssl->in_left = 0; |
| 6053 | |
| 6054 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record " |
| 6055 | "(header)" ) ); |
| 6056 | } |
| 6057 | |
| 6058 | /* Get next record */ |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 6059 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6060 | } |
Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6061 | else |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6062 | #endif |
Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6063 | { |
| 6064 | return( ret ); |
| 6065 | } |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6066 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6067 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6068 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6069 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 6070 | { |
Hanno Becker | dc4d627 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 6071 | /* Remember offset of next record within datagram. */ |
Hanno Becker | 2720f4c | 2019-07-11 12:50:10 +0100 | [diff] [blame] | 6072 | ssl->next_record_offset = rec.buf_len; |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 6073 | if( ssl->next_record_offset < ssl->in_left ) |
| 6074 | { |
| 6075 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) ); |
| 6076 | } |
| 6077 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6078 | MBEDTLS_SSL_TRANSPORT_ELSE |
Hanno Becker | dc4d627 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 6079 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6080 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 6081 | { |
Hanno Becker | e045277 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 6082 | /* |
| 6083 | * Fetch record contents from underlying transport. |
| 6084 | */ |
Hanno Becker | 9babbf7 | 2019-07-11 12:50:29 +0100 | [diff] [blame] | 6085 | ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len ); |
Hanno Becker | dc4d627 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 6086 | if( ret != 0 ) |
| 6087 | { |
| 6088 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
| 6089 | return( ret ); |
| 6090 | } |
| 6091 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6092 | ssl->in_left = 0; |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6093 | } |
Hanno Becker | dc4d627 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 6094 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
| 6095 | |
| 6096 | /* |
| 6097 | * Decrypt record contents. |
| 6098 | */ |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6099 | |
Hanno Becker | a89610a | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 6100 | if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6101 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6102 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6103 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6104 | { |
| 6105 | /* Silently discard invalid records */ |
Hanno Becker | 16e9ae2 | 2019-05-03 16:36:59 +0100 | [diff] [blame] | 6106 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6107 | { |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 6108 | /* Except when waiting for Finished as a bad mac here |
| 6109 | * probably means something went wrong in the handshake |
| 6110 | * (eg wrong psk used, mitm downgrade attempt, etc.) */ |
| 6111 | if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED || |
| 6112 | ssl->state == MBEDTLS_SSL_SERVER_FINISHED ) |
| 6113 | { |
| 6114 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
| 6115 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
| 6116 | { |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 6117 | mbedtls_ssl_pend_fatal_alert( ssl, |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 6118 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); |
| 6119 | } |
| 6120 | #endif |
| 6121 | return( ret ); |
| 6122 | } |
| 6123 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6124 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
Hanno Becker | de67154 | 2019-06-12 16:30:46 +0100 | [diff] [blame] | 6125 | if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 && |
| 6126 | ++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] | 6127 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6128 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) ); |
| 6129 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 6130 | } |
| 6131 | #endif |
| 6132 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6133 | /* As above, invalid records cause |
| 6134 | * dismissal of the whole datagram. */ |
| 6135 | |
| 6136 | ssl->next_record_offset = 0; |
| 6137 | ssl->in_left = 0; |
| 6138 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6139 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) ); |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 6140 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6141 | } |
| 6142 | |
| 6143 | return( ret ); |
| 6144 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6145 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 6146 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6147 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6148 | { |
| 6149 | /* Error out (and send alert) on invalid records */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6150 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
| 6151 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6152 | { |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 6153 | mbedtls_ssl_pend_fatal_alert( ssl, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6154 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6155 | } |
| 6156 | #endif |
| 6157 | return( ret ); |
| 6158 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6159 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6160 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6161 | |
Hanno Becker | bd70c8e | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 6162 | |
| 6163 | /* Reset in pointers to default state for TLS/DTLS records, |
| 6164 | * assuming no CID and no offset between record content and |
| 6165 | * record plaintext. */ |
| 6166 | ssl_update_in_pointers( ssl ); |
Hanno Becker | bd70c8e | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 6167 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6168 | ssl->in_len = ssl->in_cid + rec.cid_len; |
| 6169 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | c360dcc | 2019-07-12 10:00:45 +0100 | [diff] [blame] | 6170 | ssl->in_msg = ssl->in_len + 2; |
Hanno Becker | bd70c8e | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 6171 | |
Hanno Becker | bf256cd | 2019-07-12 09:37:30 +0100 | [diff] [blame] | 6172 | /* The record content type may change during decryption, |
| 6173 | * so re-read it. */ |
| 6174 | ssl->in_msgtype = rec.type; |
| 6175 | /* Also update the input buffer, because unfortunately |
| 6176 | * the server-side ssl_parse_client_hello() reparses the |
| 6177 | * record header when receiving a ClientHello initiating |
| 6178 | * a renegotiation. */ |
| 6179 | ssl->in_hdr[0] = rec.type; |
| 6180 | ssl->in_msg = rec.buf + rec.data_offset; |
| 6181 | ssl->in_msglen = rec.data_len; |
| 6182 | ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 ); |
| 6183 | ssl->in_len[1] = (unsigned char)( rec.data_len ); |
| 6184 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6185 | return( 0 ); |
| 6186 | } |
| 6187 | |
| 6188 | int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl ) |
| 6189 | { |
| 6190 | int ret; |
| 6191 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6192 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6193 | * Handle particular types of records |
| 6194 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6195 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6196 | { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6197 | if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 ) |
| 6198 | { |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 6199 | return( ret ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6200 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6201 | } |
| 6202 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6203 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6204 | { |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6205 | if( ssl->in_msglen != 1 ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6206 | { |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6207 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d", |
| 6208 | ssl->in_msglen ) ); |
| 6209 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6210 | } |
| 6211 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6212 | if( ssl->in_msg[0] != 1 ) |
| 6213 | { |
| 6214 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x", |
| 6215 | ssl->in_msg[0] ) ); |
| 6216 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 6217 | } |
| 6218 | |
| 6219 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 6220 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6221 | ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC && |
| 6222 | ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) |
| 6223 | { |
| 6224 | if( ssl->handshake == NULL ) |
| 6225 | { |
| 6226 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) ); |
| 6227 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
| 6228 | } |
| 6229 | |
| 6230 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) ); |
| 6231 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 6232 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6233 | #endif |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6234 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6235 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6236 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6237 | { |
Angus Gratton | 1a7a17e | 2018-06-20 15:43:50 +1000 | [diff] [blame] | 6238 | if( ssl->in_msglen != 2 ) |
| 6239 | { |
| 6240 | /* Note: Standard allows for more than one 2 byte alert |
| 6241 | to be packed in a single message, but Mbed TLS doesn't |
| 6242 | currently support this. */ |
| 6243 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d", |
| 6244 | ssl->in_msglen ) ); |
| 6245 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 6246 | } |
| 6247 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6248 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6249 | ssl->in_msg[0], ssl->in_msg[1] ) ); |
| 6250 | |
| 6251 | /* |
Simon Butcher | 459a950 | 2015-10-27 16:09:03 +0000 | [diff] [blame] | 6252 | * Ignore non-fatal alerts, except close_notify and no_renegotiation |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6253 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6254 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6255 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6256 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 6257 | ssl->in_msg[1] ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6258 | return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6259 | } |
| 6260 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6261 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6262 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6263 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6264 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) ); |
| 6265 | return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6266 | } |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 6267 | |
| 6268 | #if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED) |
| 6269 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6270 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) |
| 6271 | { |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 6272 | 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] | 6273 | /* Will be handled when trying to parse ServerHello */ |
| 6274 | return( 0 ); |
| 6275 | } |
| 6276 | #endif |
| 6277 | |
| 6278 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 6279 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 && |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 6280 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 6281 | MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 6282 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6283 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) |
| 6284 | { |
| 6285 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) ); |
| 6286 | /* Will be handled in mbedtls_ssl_parse_certificate() */ |
| 6287 | return( 0 ); |
| 6288 | } |
| 6289 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ |
| 6290 | |
| 6291 | /* Silently ignore: fetch new message */ |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6292 | return MBEDTLS_ERR_SSL_NON_FATAL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6293 | } |
| 6294 | |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6295 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 6296 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6297 | { |
Hanno Becker | 74dd3a7 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 6298 | /* Drop unexpected ApplicationData records, |
| 6299 | * except at the beginning of renegotiations */ |
| 6300 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA && |
| 6301 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER |
| 6302 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6303 | && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 6304 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6305 | #endif |
Hanno Becker | 74dd3a7 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 6306 | ) |
| 6307 | { |
| 6308 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) ); |
| 6309 | return( MBEDTLS_ERR_SSL_NON_FATAL ); |
| 6310 | } |
| 6311 | |
| 6312 | if( ssl->handshake != NULL && |
| 6313 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 6314 | { |
| 6315 | ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 6316 | } |
| 6317 | } |
Hanno Becker | f65ad82 | 2019-05-08 16:26:21 +0100 | [diff] [blame] | 6318 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6319 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6320 | return( 0 ); |
| 6321 | } |
| 6322 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6323 | int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 6324 | { |
| 6325 | int ret; |
| 6326 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6327 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 6328 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6329 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 6330 | { |
| 6331 | return( ret ); |
| 6332 | } |
| 6333 | |
| 6334 | return( 0 ); |
| 6335 | } |
| 6336 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6337 | int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl, |
Hanno Becker | 1facd55 | 2019-07-03 13:57:23 +0100 | [diff] [blame] | 6338 | unsigned char level, |
| 6339 | unsigned char message ) |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6340 | { |
| 6341 | int ret; |
| 6342 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 6343 | if( ssl == NULL || ssl->conf == NULL ) |
| 6344 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6345 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6346 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6347 | 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] | 6348 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6349 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6350 | ssl->out_msglen = 2; |
| 6351 | ssl->out_msg[0] = level; |
| 6352 | ssl->out_msg[1] = message; |
| 6353 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 6354 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6355 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6356 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6357 | return( ret ); |
| 6358 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6359 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) ); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6360 | |
| 6361 | return( 0 ); |
| 6362 | } |
| 6363 | |
Hanno Becker | 1757247 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 6364 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6365 | static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) |
| 6366 | { |
| 6367 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6368 | if( session->peer_cert != NULL ) |
| 6369 | { |
| 6370 | mbedtls_x509_crt_free( session->peer_cert ); |
| 6371 | mbedtls_free( session->peer_cert ); |
| 6372 | session->peer_cert = NULL; |
| 6373 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 6374 | #elif defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 1757247 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 6375 | if( session->peer_cert_digest != NULL ) |
| 6376 | { |
| 6377 | /* Zeroization is not necessary. */ |
| 6378 | mbedtls_free( session->peer_cert_digest ); |
| 6379 | session->peer_cert_digest = NULL; |
| 6380 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 6381 | session->peer_cert_digest_len = 0; |
| 6382 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 6383 | #else |
| 6384 | ((void) session); |
| 6385 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 1757247 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 6386 | } |
| 6387 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 6388 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6389 | /* |
| 6390 | * Handshake functions |
| 6391 | */ |
Hanno Becker | b71e90a | 2019-02-05 13:20:55 +0000 | [diff] [blame] | 6392 | #if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6393 | /* No certificate support -> dummy functions */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6394 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6395 | { |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 6396 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
| 6397 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6398 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6399 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6400 | |
Hanno Becker | 5097cba | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 6401 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 6402 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6403 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 6404 | ssl->state++; |
| 6405 | return( 0 ); |
| 6406 | } |
| 6407 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6408 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6409 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6410 | } |
| 6411 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6412 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6413 | { |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 6414 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
| 6415 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6416 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6417 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6418 | |
Hanno Becker | 5097cba | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 6419 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6420 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6421 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6422 | ssl->state++; |
| 6423 | return( 0 ); |
| 6424 | } |
| 6425 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6426 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6427 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6428 | } |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6429 | |
Hanno Becker | b71e90a | 2019-02-05 13:20:55 +0000 | [diff] [blame] | 6430 | #else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6431 | /* Some certificate support -> implement write and parse */ |
| 6432 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6433 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6434 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6435 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6436 | size_t i, n; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6437 | const mbedtls_x509_crt *crt; |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 6438 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
| 6439 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6440 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6441 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6442 | |
Hanno Becker | 5097cba | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 6443 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6444 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6445 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6446 | ssl->state++; |
| 6447 | return( 0 ); |
| 6448 | } |
| 6449 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6450 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 6451 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 6452 | MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6453 | { |
| 6454 | if( ssl->client_auth == 0 ) |
| 6455 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6456 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6457 | ssl->state++; |
| 6458 | return( 0 ); |
| 6459 | } |
| 6460 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6461 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6462 | /* |
| 6463 | * If using SSLv3 and got no cert, send an Alert message |
| 6464 | * (otherwise an empty Certificate message will be sent). |
| 6465 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6466 | if( mbedtls_ssl_own_cert( ssl ) == NULL && |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 6467 | mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6468 | { |
| 6469 | ssl->out_msglen = 2; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6470 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
| 6471 | ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING; |
| 6472 | ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6473 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6474 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6475 | goto write_msg; |
| 6476 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6477 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6478 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6479 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 6480 | #if defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 6481 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6482 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6483 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6484 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6485 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) ); |
| 6486 | return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6487 | } |
| 6488 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 6489 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6490 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6491 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6492 | |
| 6493 | /* |
| 6494 | * 0 . 0 handshake type |
| 6495 | * 1 . 3 handshake length |
| 6496 | * 4 . 6 length of all certs |
| 6497 | * 7 . 9 length of cert. 1 |
| 6498 | * 10 . n-1 peer certificate |
| 6499 | * n . n+2 length of cert. 2 |
| 6500 | * n+3 . ... upper level cert, etc. |
| 6501 | */ |
| 6502 | i = 7; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6503 | crt = mbedtls_ssl_own_cert( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6504 | |
Paul Bakker | 2908713 | 2010-03-21 21:03:34 +0000 | [diff] [blame] | 6505 | while( crt != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6506 | { |
| 6507 | n = crt->raw.len; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6508 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6509 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6510 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d", |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6511 | i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6512 | return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6513 | } |
| 6514 | |
| 6515 | ssl->out_msg[i ] = (unsigned char)( n >> 16 ); |
| 6516 | ssl->out_msg[i + 1] = (unsigned char)( n >> 8 ); |
| 6517 | ssl->out_msg[i + 2] = (unsigned char)( n ); |
| 6518 | |
| 6519 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 6520 | i += n; crt = crt->next; |
| 6521 | } |
| 6522 | |
| 6523 | ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 ); |
| 6524 | ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 ); |
| 6525 | ssl->out_msg[6] = (unsigned char)( ( i - 7 ) ); |
| 6526 | |
| 6527 | ssl->out_msglen = i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6528 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6529 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6530 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 6531 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6532 | write_msg: |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6533 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6534 | |
| 6535 | ssl->state++; |
| 6536 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 6537 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6538 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 6539 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6540 | return( ret ); |
| 6541 | } |
| 6542 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6543 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6544 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 6545 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6546 | } |
| 6547 | |
Hanno Becker | 285ff0c | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 6548 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | df75938 | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 6549 | |
| 6550 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6551 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6552 | unsigned char *crt_buf, |
| 6553 | size_t crt_buf_len ) |
| 6554 | { |
| 6555 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 6556 | |
| 6557 | if( peer_crt == NULL ) |
| 6558 | return( -1 ); |
| 6559 | |
| 6560 | if( peer_crt->raw.len != crt_buf_len ) |
| 6561 | return( -1 ); |
| 6562 | |
Hanno Becker | 68b856d | 2019-02-08 14:00:04 +0000 | [diff] [blame] | 6563 | return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) ); |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6564 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 6565 | #elif defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | df75938 | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 6566 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6567 | unsigned char *crt_buf, |
| 6568 | size_t crt_buf_len ) |
| 6569 | { |
| 6570 | int ret; |
| 6571 | unsigned char const * const peer_cert_digest = |
| 6572 | ssl->session->peer_cert_digest; |
| 6573 | mbedtls_md_type_t const peer_cert_digest_type = |
| 6574 | ssl->session->peer_cert_digest_type; |
| 6575 | mbedtls_md_info_t const * const digest_info = |
| 6576 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 6577 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 6578 | size_t digest_len; |
| 6579 | |
| 6580 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 6581 | return( -1 ); |
| 6582 | |
| 6583 | digest_len = mbedtls_md_get_size( digest_info ); |
| 6584 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 6585 | return( -1 ); |
| 6586 | |
| 6587 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 6588 | if( ret != 0 ) |
| 6589 | return( -1 ); |
| 6590 | |
| 6591 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 6592 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 6593 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 285ff0c | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 6594 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6595 | |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6596 | /* |
| 6597 | * Once the certificate message is read, parse it into a cert chain and |
| 6598 | * perform basic checks, but leave actual verification to the caller |
| 6599 | */ |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6600 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 6601 | mbedtls_x509_crt *chain ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6602 | { |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6603 | int ret; |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6604 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6605 | int crt_cnt=0; |
| 6606 | #endif |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 6607 | size_t i, n; |
Gilles Peskine | 064a85c | 2017-05-10 10:46:40 +0200 | [diff] [blame] | 6608 | uint8_t alert; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6609 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6610 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6611 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6612 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 6613 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 6614 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6615 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6616 | } |
| 6617 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6618 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE || |
| 6619 | ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6620 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6621 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 6622 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 6623 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6624 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6625 | } |
| 6626 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6627 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 6628 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6629 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6630 | * Same message structure as in mbedtls_ssl_write_certificate() |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6631 | */ |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 6632 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6633 | |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 6634 | if( ssl->in_msg[i] != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6635 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6636 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6637 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 6638 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 6639 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6640 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6641 | } |
| 6642 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6643 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 6644 | i += 3; |
| 6645 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6646 | /* Iterate through and parse the CRTs in the provided chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6647 | while( i < ssl->in_hslen ) |
| 6648 | { |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6649 | /* Check that there's room for the next CRT's length fields. */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 6650 | if ( i + 3 > ssl->in_hslen ) { |
| 6651 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 6652 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 6653 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 6654 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
| 6655 | } |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6656 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 6657 | * anything beyond 2**16 ~ 64K. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6658 | if( ssl->in_msg[i] != 0 ) |
| 6659 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6660 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 6661 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 6662 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6663 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6664 | } |
| 6665 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6666 | /* Read length of the next CRT in the chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6667 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 6668 | | (unsigned int) ssl->in_msg[i + 2]; |
| 6669 | i += 3; |
| 6670 | |
| 6671 | if( n < 128 || i + n > ssl->in_hslen ) |
| 6672 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6673 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 6674 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 6675 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6676 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6677 | } |
| 6678 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6679 | /* Check if we're handling the first CRT in the chain. */ |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6680 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6681 | if( crt_cnt++ == 0 && |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 6682 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 6683 | MBEDTLS_SSL_IS_CLIENT && |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6684 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6685 | { |
Hanno Becker | 68b856d | 2019-02-08 14:00:04 +0000 | [diff] [blame] | 6686 | /* During client-side renegotiation, check that the server's |
| 6687 | * end-CRTs hasn't changed compared to the initial handshake, |
| 6688 | * mitigating the triple handshake attack. On success, reuse |
| 6689 | * the original end-CRT instead of parsing it again. */ |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6690 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 6691 | if( ssl_check_peer_crt_unchanged( ssl, |
| 6692 | &ssl->in_msg[i], |
| 6693 | n ) != 0 ) |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6694 | { |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6695 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 6696 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 6697 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6698 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6699 | } |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6700 | |
| 6701 | /* Now we can safely free the original chain. */ |
| 6702 | ssl_clear_peer_cert( ssl->session ); |
| 6703 | } |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6704 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6705 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6706 | /* Parse the next certificate in the chain. */ |
Hanno Becker | 0cc7af5 | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 6707 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6708 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
Hanno Becker | 0cc7af5 | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 6709 | #else |
Hanno Becker | 42de8f8 | 2019-02-26 11:51:34 +0000 | [diff] [blame] | 6710 | /* If we don't need to store the CRT chain permanently, parse |
Hanno Becker | 0cc7af5 | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 6711 | * it in-place from the input buffer instead of making a copy. */ |
| 6712 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 6713 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6714 | switch( ret ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6715 | { |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6716 | case 0: /*ok*/ |
| 6717 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 6718 | /* Ignore certificate with an unknown algorithm: maybe a |
| 6719 | prior certificate was already trusted. */ |
| 6720 | break; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6721 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6722 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 6723 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 6724 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6725 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6726 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 6727 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6728 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6729 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6730 | default: |
| 6731 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6732 | crt_parse_der_failed: |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 6733 | mbedtls_ssl_pend_fatal_alert( ssl, alert ); |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6734 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 6735 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6736 | } |
| 6737 | |
| 6738 | i += n; |
| 6739 | } |
| 6740 | |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6741 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6742 | return( 0 ); |
| 6743 | } |
| 6744 | |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6745 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6746 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 6747 | { |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 6748 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT ) |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6749 | return( -1 ); |
| 6750 | |
| 6751 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 6752 | /* |
| 6753 | * Check if the client sent an empty certificate |
| 6754 | */ |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 6755 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6756 | { |
| 6757 | if( ssl->in_msglen == 2 && |
| 6758 | ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT && |
| 6759 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6760 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) |
| 6761 | { |
| 6762 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) ); |
| 6763 | return( 0 ); |
| 6764 | } |
| 6765 | |
| 6766 | return( -1 ); |
| 6767 | } |
| 6768 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 6769 | |
| 6770 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 6771 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6772 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 6773 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 6774 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 6775 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 6776 | { |
| 6777 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) ); |
| 6778 | return( 0 ); |
| 6779 | } |
| 6780 | |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6781 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 6782 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 17daaa5 | 2019-06-18 12:31:45 +0100 | [diff] [blame] | 6783 | |
| 6784 | return( -1 ); |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6785 | } |
| 6786 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6787 | |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6788 | /* Check if a certificate message is expected. |
| 6789 | * Return either |
| 6790 | * - SSL_CERTIFICATE_EXPECTED, or |
| 6791 | * - SSL_CERTIFICATE_SKIP |
| 6792 | * indicating whether a Certificate message is expected or not. |
| 6793 | */ |
| 6794 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 6795 | #define SSL_CERTIFICATE_SKIP 1 |
| 6796 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 6797 | int authmode ) |
| 6798 | { |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 6799 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 6800 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6801 | |
| 6802 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6803 | return( SSL_CERTIFICATE_SKIP ); |
| 6804 | |
| 6805 | #if defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 6806 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER ) |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6807 | { |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 6808 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == |
| 6809 | MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 6810 | { |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6811 | return( SSL_CERTIFICATE_SKIP ); |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 6812 | } |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6813 | |
| 6814 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6815 | { |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6816 | ssl->session_negotiate->verify_result = |
| 6817 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 6818 | return( SSL_CERTIFICATE_SKIP ); |
| 6819 | } |
| 6820 | } |
Hanno Becker | fd5dc8a | 2019-03-01 08:10:46 +0000 | [diff] [blame] | 6821 | #else |
| 6822 | ((void) authmode); |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6823 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6824 | |
| 6825 | return( SSL_CERTIFICATE_EXPECTED ); |
| 6826 | } |
| 6827 | |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6828 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 6829 | int authmode, |
| 6830 | mbedtls_x509_crt *chain, |
| 6831 | void *rs_ctx ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6832 | { |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6833 | int verify_ret; |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 6834 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 6835 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6836 | mbedtls_x509_crt *ca_chain; |
| 6837 | mbedtls_x509_crl *ca_crl; |
| 6838 | |
| 6839 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6840 | return( 0 ); |
| 6841 | |
| 6842 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6843 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 6844 | { |
| 6845 | ca_chain = ssl->handshake->sni_ca_chain; |
| 6846 | ca_crl = ssl->handshake->sni_ca_crl; |
| 6847 | } |
| 6848 | else |
| 6849 | #endif |
| 6850 | { |
| 6851 | ca_chain = ssl->conf->ca_chain; |
| 6852 | ca_crl = ssl->conf->ca_crl; |
| 6853 | } |
| 6854 | |
| 6855 | /* |
| 6856 | * Main check: verify certificate |
| 6857 | */ |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6858 | verify_ret = mbedtls_x509_crt_verify_restartable( |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6859 | chain, |
| 6860 | ca_chain, ca_crl, |
| 6861 | ssl->conf->cert_profile, |
| 6862 | ssl->hostname, |
| 6863 | &ssl->session_negotiate->verify_result, |
| 6864 | ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx ); |
| 6865 | |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6866 | if( verify_ret != 0 ) |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6867 | { |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6868 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret ); |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6869 | } |
| 6870 | |
| 6871 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6872 | if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6873 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 6874 | #endif |
| 6875 | |
| 6876 | /* |
| 6877 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 6878 | */ |
| 6879 | |
| 6880 | #if defined(MBEDTLS_ECP_C) |
| 6881 | { |
Manuel Pégourié-Gonnard | b391766 | 2019-07-03 11:19:30 +0200 | [diff] [blame] | 6882 | int ret; |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6883 | mbedtls_pk_context *pk; |
| 6884 | ret = mbedtls_x509_crt_pk_acquire( chain, &pk ); |
| 6885 | if( ret != 0 ) |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 6886 | { |
| 6887 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret ); |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6888 | return( ret ); |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 6889 | } |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6890 | |
| 6891 | /* If certificate uses an EC key, make sure the curve is OK */ |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6892 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) ) |
| 6893 | ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ); |
| 6894 | |
Hanno Becker | c6d1c3e | 2019-03-05 13:50:56 +0000 | [diff] [blame] | 6895 | mbedtls_x509_crt_pk_release( chain ); |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6896 | |
| 6897 | if( ret != 0 ) |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6898 | { |
| 6899 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; |
| 6900 | |
| 6901 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6902 | if( verify_ret == 0 ) |
| 6903 | verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6904 | } |
| 6905 | } |
| 6906 | #endif /* MBEDTLS_ECP_C */ |
| 6907 | |
| 6908 | if( mbedtls_ssl_check_cert_usage( chain, |
| 6909 | ciphersuite_info, |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 6910 | ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 6911 | MBEDTLS_SSL_IS_CLIENT ), |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6912 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 6913 | { |
| 6914 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6915 | if( verify_ret == 0 ) |
| 6916 | verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6917 | } |
| 6918 | |
| 6919 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 6920 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 6921 | * with details encoded in the verification flags. All other kinds |
| 6922 | * of error codes, including those from the user provided f_vrfy |
| 6923 | * functions, are treated as fatal and lead to a failure of |
| 6924 | * ssl_parse_certificate even if verification was optional. */ |
| 6925 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6926 | ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 6927 | verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) ) |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6928 | { |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6929 | verify_ret = 0; |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6930 | } |
| 6931 | |
| 6932 | if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
| 6933 | { |
| 6934 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6935 | verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6936 | } |
| 6937 | |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6938 | if( verify_ret != 0 ) |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6939 | { |
| 6940 | uint8_t alert; |
| 6941 | |
| 6942 | /* The certificate may have been rejected for several reasons. |
| 6943 | Pick one and send the corresponding alert. Which alert to send |
| 6944 | may be a subject of debate in some cases. */ |
| 6945 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 6946 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 6947 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 6948 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6949 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 6950 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6951 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 6952 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6953 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 6954 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6955 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 6956 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6957 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 6958 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6959 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 6960 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 6961 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 6962 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 6963 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 6964 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 6965 | else |
| 6966 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 6967 | mbedtls_ssl_pend_fatal_alert( ssl, alert ); |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6968 | } |
| 6969 | |
| 6970 | #if defined(MBEDTLS_DEBUG_C) |
| 6971 | if( ssl->session_negotiate->verify_result != 0 ) |
| 6972 | { |
| 6973 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x", |
| 6974 | ssl->session_negotiate->verify_result ) ); |
| 6975 | } |
| 6976 | else |
| 6977 | { |
| 6978 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 6979 | } |
| 6980 | #endif /* MBEDTLS_DEBUG_C */ |
| 6981 | |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6982 | return( verify_ret ); |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6983 | } |
| 6984 | |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 6985 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 6986 | |
| 6987 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 6988 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 6989 | unsigned char *start, size_t len ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6990 | { |
| 6991 | int ret; |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 6992 | /* Remember digest of the peer's end-CRT. */ |
| 6993 | ssl->session_negotiate->peer_cert_digest = |
| 6994 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 6995 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 6996 | { |
| 6997 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 6998 | sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 6999 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 7000 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7001 | |
| 7002 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 7003 | } |
| 7004 | |
| 7005 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 7006 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 7007 | start, len, |
| 7008 | ssl->session_negotiate->peer_cert_digest ); |
| 7009 | |
| 7010 | ssl->session_negotiate->peer_cert_digest_type = |
| 7011 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 7012 | ssl->session_negotiate->peer_cert_digest_len = |
| 7013 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 7014 | |
| 7015 | return( ret ); |
| 7016 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 7017 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7018 | |
| 7019 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 7020 | unsigned char *start, size_t len ) |
| 7021 | { |
| 7022 | unsigned char *end = start + len; |
| 7023 | int ret; |
| 7024 | |
| 7025 | /* Make a copy of the peer's raw public key. */ |
| 7026 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 7027 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 7028 | &ssl->handshake->peer_pubkey ); |
| 7029 | if( ret != 0 ) |
| 7030 | { |
| 7031 | /* We should have parsed the public key before. */ |
| 7032 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7033 | } |
| 7034 | |
| 7035 | return( 0 ); |
| 7036 | } |
| 7037 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7038 | |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7039 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 7040 | { |
| 7041 | int ret = 0; |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7042 | int crt_expected; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7043 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7044 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 7045 | ? ssl->handshake->sni_authmode |
Hanno Becker | acd4fc0 | 2019-06-12 16:40:50 +0100 | [diff] [blame] | 7046 | : mbedtls_ssl_conf_get_authmode( ssl->conf ); |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7047 | #else |
Hanno Becker | acd4fc0 | 2019-06-12 16:40:50 +0100 | [diff] [blame] | 7048 | const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf ); |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7049 | #endif |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7050 | void *rs_ctx = NULL; |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7051 | mbedtls_x509_crt *chain = NULL; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7052 | |
| 7053 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 7054 | |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7055 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 7056 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7057 | { |
| 7058 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Hanno Becker | 613d490 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7059 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7060 | } |
| 7061 | |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7062 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 7063 | if( ssl->handshake->ecrs_enabled && |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 7064 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7065 | { |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7066 | chain = ssl->handshake->ecrs_peer_cert; |
| 7067 | ssl->handshake->ecrs_peer_cert = NULL; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7068 | goto crt_verify; |
| 7069 | } |
| 7070 | #endif |
| 7071 | |
Manuel Pégourié-Gonnard | 125af94 | 2018-09-11 11:08:12 +0200 | [diff] [blame] | 7072 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7073 | { |
| 7074 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 7075 | let it decide whether to alert. */ |
| 7076 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7077 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7078 | } |
| 7079 | |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7080 | #if defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7081 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 7082 | { |
| 7083 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7084 | |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7085 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ) |
Hanno Becker | 613d490 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7086 | ret = 0; |
| 7087 | else |
| 7088 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7089 | |
Hanno Becker | 613d490 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7090 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7091 | } |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7092 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7093 | |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7094 | /* Clear existing peer CRT structure in case we tried to |
| 7095 | * reuse a session but it failed, and allocate a new one. */ |
Hanno Becker | a46c287 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 7096 | ssl_clear_peer_cert( ssl->session_negotiate ); |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7097 | |
| 7098 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 7099 | if( chain == NULL ) |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7100 | { |
| 7101 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 7102 | sizeof( mbedtls_x509_crt ) ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7103 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 7104 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Hanno Becker | a46c287 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 7105 | |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7106 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 7107 | goto exit; |
| 7108 | } |
| 7109 | mbedtls_x509_crt_init( chain ); |
| 7110 | |
| 7111 | ret = ssl_parse_certificate_chain( ssl, chain ); |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7112 | if( ret != 0 ) |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7113 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7114 | |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7115 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 7116 | if( ssl->handshake->ecrs_enabled) |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 7117 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7118 | |
| 7119 | crt_verify: |
| 7120 | if( ssl->handshake->ecrs_enabled) |
| 7121 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 7122 | #endif |
| 7123 | |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7124 | ret = ssl_parse_certificate_verify( ssl, authmode, |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7125 | chain, rs_ctx ); |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7126 | if( ret != 0 ) |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7127 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7128 | |
Hanno Becker | 3008d28 | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 7129 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7130 | { |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 7131 | size_t pk_len; |
| 7132 | unsigned char *pk_start; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7133 | |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7134 | /* We parse the CRT chain without copying, so |
| 7135 | * these pointers point into the input buffer, |
| 7136 | * and are hence still valid after freeing the |
| 7137 | * CRT chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7138 | |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 7139 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7140 | unsigned char *crt_start; |
| 7141 | size_t crt_len; |
| 7142 | |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7143 | crt_start = chain->raw.p; |
| 7144 | crt_len = chain->raw.len; |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 7145 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7146 | |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 7147 | pk_start = chain->cache->pk_raw.p; |
| 7148 | pk_len = chain->cache->pk_raw.len; |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7149 | |
| 7150 | /* Free the CRT structures before computing |
| 7151 | * digest and copying the peer's public key. */ |
| 7152 | mbedtls_x509_crt_free( chain ); |
| 7153 | mbedtls_free( chain ); |
| 7154 | chain = NULL; |
| 7155 | |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 7156 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7157 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7158 | if( ret != 0 ) |
Hanno Becker | cf291d6 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 7159 | goto exit; |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 7160 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7161 | |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7162 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7163 | if( ret != 0 ) |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7164 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7165 | } |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7166 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7167 | /* Pass ownership to session structure. */ |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7168 | ssl->session_negotiate->peer_cert = chain; |
| 7169 | chain = NULL; |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7170 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7171 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7172 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7173 | |
Hanno Becker | 613d490 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7174 | exit: |
| 7175 | |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7176 | if( ret == 0 ) |
| 7177 | ssl->state++; |
| 7178 | |
| 7179 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 7180 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 7181 | { |
| 7182 | ssl->handshake->ecrs_peer_cert = chain; |
| 7183 | chain = NULL; |
| 7184 | } |
| 7185 | #endif |
| 7186 | |
| 7187 | if( chain != NULL ) |
| 7188 | { |
| 7189 | mbedtls_x509_crt_free( chain ); |
| 7190 | mbedtls_free( chain ); |
| 7191 | } |
| 7192 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7193 | return( ret ); |
| 7194 | } |
Hanno Becker | b71e90a | 2019-02-05 13:20:55 +0000 | [diff] [blame] | 7195 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7196 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7197 | int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7198 | { |
| 7199 | int ret; |
| 7200 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7201 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7202 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7203 | ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7204 | ssl->out_msglen = 1; |
| 7205 | ssl->out_msg[0] = 1; |
| 7206 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7207 | ssl->state++; |
| 7208 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7209 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7210 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7211 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7212 | return( ret ); |
| 7213 | } |
| 7214 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7215 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7216 | |
| 7217 | return( 0 ); |
| 7218 | } |
| 7219 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7220 | int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7221 | { |
| 7222 | int ret; |
| 7223 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7224 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7225 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 7226 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7227 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7228 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7229 | return( ret ); |
| 7230 | } |
| 7231 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7232 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7233 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7234 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7235 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 7236 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7237 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7238 | } |
| 7239 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 7240 | /* CCS records are only accepted if they have length 1 and content '1', |
| 7241 | * so we don't need to check this here. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7242 | |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7243 | /* |
| 7244 | * Switch to our negotiated transform and session parameters for inbound |
| 7245 | * data. |
| 7246 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7247 | 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] | 7248 | ssl->transform_in = ssl->transform_negotiate; |
| 7249 | ssl->session_in = ssl->session_negotiate; |
| 7250 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7251 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7252 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7253 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7254 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7255 | ssl_dtls_replay_reset( ssl ); |
| 7256 | #endif |
| 7257 | |
| 7258 | /* Increment epoch */ |
| 7259 | if( ++ssl->in_epoch == 0 ) |
| 7260 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7261 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7262 | /* This is highly unlikely to happen for legitimate reasons, so |
| 7263 | 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] | 7264 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7265 | } |
| 7266 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7267 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7268 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7269 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 7270 | { |
| 7271 | memset( ssl->in_ctr, 0, 8 ); |
| 7272 | } |
| 7273 | #endif |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7274 | |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 7275 | ssl_update_in_pointers( ssl ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7276 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7277 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 7278 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7279 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7280 | 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] | 7281 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7282 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7283 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 7284 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7285 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7286 | } |
| 7287 | } |
| 7288 | #endif |
| 7289 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7290 | ssl->state++; |
| 7291 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7292 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7293 | |
| 7294 | return( 0 ); |
| 7295 | } |
| 7296 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7297 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 7298 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7299 | { |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 7300 | ((void) ciphersuite_info); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 7301 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7302 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7303 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 7304 | if( mbedtls_ssl_get_minor_ver( ssl ) < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7305 | ssl->handshake->update_checksum = ssl_update_checksum_md5sha1; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7306 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7307 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7308 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7309 | #if defined(MBEDTLS_SHA512_C) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 7310 | if( mbedtls_ssl_suite_get_mac( ciphersuite_info ) == MBEDTLS_MD_SHA384 ) |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7311 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 7312 | else |
| 7313 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7314 | #if defined(MBEDTLS_SHA256_C) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 7315 | if( mbedtls_ssl_suite_get_mac( ciphersuite_info ) != MBEDTLS_MD_SHA384 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7316 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7317 | else |
| 7318 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7319 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 7320 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7321 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7322 | return; |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 7323 | } |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7324 | } |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 7325 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7326 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7327 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7328 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7329 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7330 | mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 ); |
| 7331 | mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7332 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7333 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7334 | #if defined(MBEDTLS_SHA256_C) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7335 | mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7336 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7337 | #if defined(MBEDTLS_SHA512_C) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7338 | mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7339 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7340 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7341 | } |
| 7342 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7343 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7344 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7345 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7346 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7347 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7348 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); |
| 7349 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7350 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7351 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7352 | #if defined(MBEDTLS_SHA256_C) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7353 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7354 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7355 | #if defined(MBEDTLS_SHA512_C) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7356 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 7357 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7358 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7359 | } |
| 7360 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7361 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7362 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 7363 | static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7364 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7365 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7366 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); |
| 7367 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7368 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7369 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7370 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7371 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7372 | #if defined(MBEDTLS_SHA256_C) |
| 7373 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7374 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7375 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7376 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7377 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7378 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7379 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7380 | #if defined(MBEDTLS_SHA512_C) |
| 7381 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7382 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7383 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7384 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7385 | } |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 7386 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7387 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7388 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7389 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7390 | static void ssl_calc_finished_ssl( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7391 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7392 | { |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7393 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7394 | mbedtls_md5_context md5; |
| 7395 | mbedtls_sha1_context sha1; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7396 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7397 | unsigned char padbuf[48]; |
| 7398 | unsigned char md5sum[16]; |
| 7399 | unsigned char sha1sum[20]; |
| 7400 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7401 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7402 | if( !session ) |
| 7403 | session = ssl->session; |
| 7404 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7405 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7406 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7407 | mbedtls_md5_init( &md5 ); |
| 7408 | mbedtls_sha1_init( &sha1 ); |
| 7409 | |
| 7410 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 7411 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7412 | |
| 7413 | /* |
| 7414 | * SSLv3: |
| 7415 | * hash = |
| 7416 | * MD5( master + pad2 + |
| 7417 | * MD5( handshake + sender + master + pad1 ) ) |
| 7418 | * + SHA1( master + pad2 + |
| 7419 | * SHA1( handshake + sender + master + pad1 ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7420 | */ |
| 7421 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7422 | #if !defined(MBEDTLS_MD5_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7423 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *) |
| 7424 | md5.state, sizeof( md5.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7425 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7426 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7427 | #if !defined(MBEDTLS_SHA1_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7428 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) |
| 7429 | sha1.state, sizeof( sha1.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7430 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7431 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7432 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT" |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7433 | : "SRVR"; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7434 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7435 | memset( padbuf, 0x36, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7436 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7437 | mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 ); |
| 7438 | mbedtls_md5_update_ret( &md5, session->master, 48 ); |
| 7439 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); |
| 7440 | mbedtls_md5_finish_ret( &md5, md5sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7441 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7442 | mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 ); |
| 7443 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); |
| 7444 | mbedtls_sha1_update_ret( &sha1, padbuf, 40 ); |
| 7445 | mbedtls_sha1_finish_ret( &sha1, sha1sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7446 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7447 | memset( padbuf, 0x5C, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7448 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7449 | mbedtls_md5_starts_ret( &md5 ); |
| 7450 | mbedtls_md5_update_ret( &md5, session->master, 48 ); |
| 7451 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); |
| 7452 | mbedtls_md5_update_ret( &md5, md5sum, 16 ); |
| 7453 | mbedtls_md5_finish_ret( &md5, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7454 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7455 | mbedtls_sha1_starts_ret( &sha1 ); |
| 7456 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); |
| 7457 | mbedtls_sha1_update_ret( &sha1, padbuf , 40 ); |
| 7458 | mbedtls_sha1_update_ret( &sha1, sha1sum, 20 ); |
| 7459 | mbedtls_sha1_finish_ret( &sha1, buf + 16 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7460 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7461 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7462 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7463 | mbedtls_md5_free( &md5 ); |
| 7464 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7465 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7466 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 7467 | mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) ); |
| 7468 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7469 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7470 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7471 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7472 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7473 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7474 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7475 | static void ssl_calc_finished_tls( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7476 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7477 | { |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7478 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7479 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7480 | mbedtls_md5_context md5; |
| 7481 | mbedtls_sha1_context sha1; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7482 | unsigned char padbuf[36]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7483 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7484 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7485 | if( !session ) |
| 7486 | session = ssl->session; |
| 7487 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7488 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7489 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7490 | mbedtls_md5_init( &md5 ); |
| 7491 | mbedtls_sha1_init( &sha1 ); |
| 7492 | |
| 7493 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 7494 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
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 | /* |
| 7497 | * TLSv1: |
| 7498 | * hash = PRF( master, finished_label, |
| 7499 | * MD5( handshake ) + SHA1( handshake ) )[0..11] |
| 7500 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7501 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7502 | #if !defined(MBEDTLS_MD5_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7503 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *) |
| 7504 | md5.state, sizeof( md5.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7505 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7506 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7507 | #if !defined(MBEDTLS_SHA1_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7508 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) |
| 7509 | sha1.state, sizeof( sha1.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7510 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7511 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7512 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7513 | ? "client finished" |
| 7514 | : "server finished"; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7515 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7516 | mbedtls_md5_finish_ret( &md5, padbuf ); |
| 7517 | mbedtls_sha1_finish_ret( &sha1, padbuf + 16 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7518 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7519 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7520 | padbuf, 36, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7521 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7522 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7523 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7524 | mbedtls_md5_free( &md5 ); |
| 7525 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7526 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7527 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7528 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7529 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7530 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7531 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7532 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7533 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7534 | #if defined(MBEDTLS_SHA256_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7535 | static void ssl_calc_finished_tls_sha256( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7536 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7537 | { |
| 7538 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7539 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7540 | mbedtls_sha256_context sha256; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7541 | unsigned char padbuf[32]; |
| 7542 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7543 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7544 | if( !session ) |
| 7545 | session = ssl->session; |
| 7546 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7547 | mbedtls_sha256_init( &sha256 ); |
| 7548 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7549 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7550 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7551 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7552 | |
| 7553 | /* |
| 7554 | * TLSv1.2: |
| 7555 | * hash = PRF( master, finished_label, |
| 7556 | * Hash( handshake ) )[0.11] |
| 7557 | */ |
| 7558 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7559 | #if !defined(MBEDTLS_SHA256_ALT) |
| 7560 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7561 | sha256.state, sizeof( sha256.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7562 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7563 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7564 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7565 | ? "client finished" |
| 7566 | : "server finished"; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7567 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7568 | mbedtls_sha256_finish_ret( &sha256, padbuf ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7569 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7570 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7571 | padbuf, 32, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7572 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7573 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7574 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7575 | mbedtls_sha256_free( &sha256 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7576 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7577 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7578 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7579 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7580 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7581 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7582 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7583 | #if defined(MBEDTLS_SHA512_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7584 | static void ssl_calc_finished_tls_sha384( |
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 | ca4ab49 | 2012-04-18 14:23:57 +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_sha512_context sha512; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7590 | unsigned char padbuf[48]; |
| 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_sha512_init( &sha512 ); |
| 7597 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7598 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7599 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7600 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +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_SHA512_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7609 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 7610 | sha512.state, sizeof( sha512.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7611 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +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 | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7616 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7617 | mbedtls_sha512_finish_ret( &sha512, padbuf ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +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, 48, buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +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 | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7623 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7624 | mbedtls_sha512_free( &sha512 ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +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 | ca4ab49 | 2012-04-18 14:23:57 +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 | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7629 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7630 | #endif /* MBEDTLS_SHA512_C */ |
| 7631 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7632 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7633 | static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7634 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7635 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7636 | |
| 7637 | /* |
| 7638 | * Free our handshake params |
| 7639 | */ |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 7640 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7641 | mbedtls_free( ssl->handshake ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7642 | ssl->handshake = NULL; |
| 7643 | |
| 7644 | /* |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7645 | * Free the previous transform and swith in the current one |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7646 | */ |
| 7647 | if( ssl->transform ) |
| 7648 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7649 | mbedtls_ssl_transform_free( ssl->transform ); |
| 7650 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7651 | } |
| 7652 | ssl->transform = ssl->transform_negotiate; |
| 7653 | ssl->transform_negotiate = NULL; |
| 7654 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7655 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7656 | } |
| 7657 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7658 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7659 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7660 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7661 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7662 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7663 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7664 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7665 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7666 | ssl->renego_records_seen = 0; |
| 7667 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 7668 | #endif |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7669 | |
| 7670 | /* |
| 7671 | * Free the previous session and switch in the current one |
| 7672 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7673 | if( ssl->session ) |
| 7674 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7675 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 1a03473 | 2014-11-04 17:36:18 +0100 | [diff] [blame] | 7676 | /* RFC 7366 3.1: keep the EtM state */ |
| 7677 | ssl->session_negotiate->encrypt_then_mac = |
| 7678 | ssl->session->encrypt_then_mac; |
| 7679 | #endif |
| 7680 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7681 | mbedtls_ssl_session_free( ssl->session ); |
| 7682 | mbedtls_free( ssl->session ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7683 | } |
| 7684 | ssl->session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7685 | ssl->session_negotiate = NULL; |
| 7686 | |
Manuel Pégourié-Gonnard | 7b80c64 | 2019-07-02 16:21:30 +0200 | [diff] [blame] | 7687 | #if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7688 | /* |
| 7689 | * Add cache entry |
| 7690 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7691 | if( ssl->conf->f_set_cache != NULL && |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 7692 | ssl->session->id_len != 0 && |
Manuel Pégourié-Gonnard | 7b80c64 | 2019-07-02 16:21:30 +0200 | [diff] [blame] | 7693 | ssl->handshake->resume == 0 ) |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 7694 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 7695 | 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] | 7696 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 7697 | } |
Manuel Pégourié-Gonnard | 7b80c64 | 2019-07-02 16:21:30 +0200 | [diff] [blame] | 7698 | #endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7699 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7700 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 7701 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7702 | ssl->handshake->flight != NULL ) |
| 7703 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 7704 | /* Cancel handshake timer */ |
| 7705 | ssl_set_timer( ssl, 0 ); |
| 7706 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7707 | /* Keep last flight around in case we need to resend it: |
| 7708 | * we need the handshake and transform structures for that */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7709 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7710 | } |
| 7711 | else |
| 7712 | #endif |
| 7713 | ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 7714 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7715 | ssl->state++; |
| 7716 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7717 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7718 | } |
| 7719 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7720 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7721 | { |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7722 | int ret, hash_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7723 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7724 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7725 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7726 | ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 7727 | |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7728 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, |
| 7729 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7730 | |
Manuel Pégourié-Gonnard | 214a848 | 2016-02-22 11:27:26 +0100 | [diff] [blame] | 7731 | /* |
| 7732 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 7733 | * may define some other value. Currently (early 2016), no defined |
| 7734 | * ciphersuite does this (and this is unlikely to change as activity has |
| 7735 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 7736 | */ |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 7737 | 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] | 7738 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7739 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7740 | ssl->verify_data_len = hash_len; |
| 7741 | 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] | 7742 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7743 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7744 | ssl->out_msglen = 4 + hash_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7745 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 7746 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7747 | |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 7748 | #if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7749 | /* |
| 7750 | * In case of session resuming, invert the client and server |
| 7751 | * ChangeCipherSpec messages order. |
| 7752 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7753 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7754 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7755 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7756 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 7757 | MBEDTLS_SSL_IS_CLIENT ) |
| 7758 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7759 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7760 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 7761 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7762 | #if defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7763 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 7764 | MBEDTLS_SSL_IS_SERVER ) |
| 7765 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7766 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7767 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 7768 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7769 | } |
| 7770 | else |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 7771 | #endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7772 | ssl->state++; |
| 7773 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7774 | /* |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 7775 | * Switch to our negotiated transform and session parameters for outbound |
| 7776 | * data. |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7777 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7778 | 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] | 7779 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7780 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7781 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7782 | { |
| 7783 | unsigned char i; |
| 7784 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 7785 | /* Remember current epoch settings for resending */ |
| 7786 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 7787 | 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] | 7788 | |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7789 | /* Set sequence_number to zero */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 7790 | memset( ssl->cur_out_ctr + 2, 0, 6 ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7791 | |
| 7792 | /* Increment epoch */ |
| 7793 | for( i = 2; i > 0; i-- ) |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 7794 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7795 | break; |
| 7796 | |
| 7797 | /* The loop goes to its end iff the counter is wrapping */ |
| 7798 | if( i == 0 ) |
| 7799 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7800 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 7801 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7802 | } |
| 7803 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7804 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7805 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7806 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 7807 | { |
| 7808 | memset( ssl->cur_out_ctr, 0, 8 ); |
| 7809 | } |
| 7810 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7811 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 7812 | ssl->transform_out = ssl->transform_negotiate; |
| 7813 | ssl->session_out = ssl->session_negotiate; |
| 7814 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7815 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 7816 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 7817 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7818 | 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] | 7819 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7820 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
| 7821 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 7822 | } |
| 7823 | } |
| 7824 | #endif |
| 7825 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7826 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 7827 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7828 | mbedtls_ssl_send_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 7829 | #endif |
| 7830 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7831 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7832 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7833 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7834 | return( ret ); |
| 7835 | } |
| 7836 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 7837 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 7838 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 7839 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 7840 | { |
| 7841 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 7842 | return( ret ); |
| 7843 | } |
| 7844 | #endif |
| 7845 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7846 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7847 | |
| 7848 | return( 0 ); |
| 7849 | } |
| 7850 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7851 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 7852 | #define SSL_MAX_HASH_LEN 36 |
| 7853 | #else |
| 7854 | #define SSL_MAX_HASH_LEN 12 |
| 7855 | #endif |
| 7856 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7857 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7858 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 7859 | int ret; |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7860 | unsigned int hash_len; |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 7861 | unsigned char buf[SSL_MAX_HASH_LEN]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7862 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7863 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7864 | |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7865 | ssl->handshake->calc_finished( ssl, buf, |
| 7866 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7867 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 7868 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7869 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7870 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7871 | return( ret ); |
| 7872 | } |
| 7873 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7874 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7875 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7876 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7877 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 7878 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7879 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7880 | } |
| 7881 | |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 7882 | /* 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] | 7883 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 7884 | 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] | 7885 | hash_len = 36; |
| 7886 | else |
| 7887 | #endif |
| 7888 | hash_len = 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7889 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7890 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED || |
| 7891 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7892 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7893 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7894 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 7895 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7896 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7897 | } |
| 7898 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7899 | 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] | 7900 | buf, hash_len ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7901 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7902 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7903 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 7904 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7905 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7906 | } |
| 7907 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7908 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7909 | ssl->verify_data_len = hash_len; |
| 7910 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 7911 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7912 | |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 7913 | #if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7914 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7915 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7916 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7917 | 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] | 7918 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 7919 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7920 | #if defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7921 | 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] | 7922 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 7923 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7924 | } |
| 7925 | else |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 7926 | #endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7927 | ssl->state++; |
| 7928 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7929 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 7930 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7931 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 7932 | #endif |
| 7933 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7934 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7935 | |
| 7936 | return( 0 ); |
| 7937 | } |
| 7938 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7939 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7940 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7941 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7942 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7943 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7944 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 7945 | mbedtls_md5_init( &handshake->fin_md5 ); |
| 7946 | mbedtls_sha1_init( &handshake->fin_sha1 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7947 | mbedtls_md5_starts_ret( &handshake->fin_md5 ); |
| 7948 | mbedtls_sha1_starts_ret( &handshake->fin_sha1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7949 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7950 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7951 | #if defined(MBEDTLS_SHA256_C) |
| 7952 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7953 | mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7954 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7955 | #if defined(MBEDTLS_SHA512_C) |
| 7956 | mbedtls_sha512_init( &handshake->fin_sha512 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7957 | mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7958 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7959 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7960 | |
| 7961 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 7962 | |
| 7963 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 7964 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 7965 | mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs ); |
| 7966 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7967 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7968 | #if defined(MBEDTLS_DHM_C) |
| 7969 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7970 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7971 | #if defined(MBEDTLS_ECDH_C) |
| 7972 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7973 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 7974 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 7975 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 7976 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7977 | handshake->ecjpake_cache = NULL; |
| 7978 | handshake->ecjpake_cache_len = 0; |
| 7979 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 7980 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 7981 | |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 7982 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 7983 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 7984 | #endif |
| 7985 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 7986 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7987 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 7988 | #endif |
Hanno Becker | 3bf8cdf | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 7989 | |
| 7990 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 7991 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7992 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 7993 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7994 | } |
| 7995 | |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 7996 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7997 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7998 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 7999 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8000 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 8001 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 8002 | |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 8003 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8004 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 8005 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 8006 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8007 | } |
| 8008 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8009 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8010 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8011 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8012 | } |
| 8013 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8014 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8015 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8016 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8017 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8018 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8019 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8020 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8021 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 8022 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8023 | |
| 8024 | /* |
| 8025 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 8026 | * Now allocate missing structures. |
| 8027 | */ |
| 8028 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8029 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8030 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8031 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8032 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8033 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8034 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8035 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8036 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8037 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 8038 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8039 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8040 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8041 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8042 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8043 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8044 | if( ssl->handshake == NULL || |
| 8045 | ssl->transform_negotiate == NULL || |
| 8046 | ssl->session_negotiate == NULL ) |
| 8047 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 8048 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8049 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8050 | mbedtls_free( ssl->handshake ); |
| 8051 | mbedtls_free( ssl->transform_negotiate ); |
| 8052 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8053 | |
| 8054 | ssl->handshake = NULL; |
| 8055 | ssl->transform_negotiate = NULL; |
| 8056 | ssl->session_negotiate = NULL; |
| 8057 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8058 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8059 | } |
| 8060 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8061 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8062 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 8063 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 8064 | ssl_handshake_params_init( ssl->handshake ); |
| 8065 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8066 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 8067 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 8068 | { |
| 8069 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8070 | |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 8071 | 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] | 8072 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 8073 | else |
| 8074 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
| 8075 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8076 | #endif |
| 8077 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8078 | return( 0 ); |
| 8079 | } |
| 8080 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 8081 | #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] | 8082 | /* Dummy cookie callbacks for defaults */ |
| 8083 | static int ssl_cookie_write_dummy( void *ctx, |
| 8084 | unsigned char **p, unsigned char *end, |
| 8085 | const unsigned char *cli_id, size_t cli_id_len ) |
| 8086 | { |
| 8087 | ((void) ctx); |
| 8088 | ((void) p); |
| 8089 | ((void) end); |
| 8090 | ((void) cli_id); |
| 8091 | ((void) cli_id_len); |
| 8092 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8093 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 8094 | } |
| 8095 | |
| 8096 | static int ssl_cookie_check_dummy( void *ctx, |
| 8097 | const unsigned char *cookie, size_t cookie_len, |
| 8098 | const unsigned char *cli_id, size_t cli_id_len ) |
| 8099 | { |
| 8100 | ((void) ctx); |
| 8101 | ((void) cookie); |
| 8102 | ((void) cookie_len); |
| 8103 | ((void) cli_id); |
| 8104 | ((void) cli_id_len); |
| 8105 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8106 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 8107 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 8108 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 8109 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8110 | /* Once ssl->out_hdr as the address of the beginning of the |
| 8111 | * next outgoing record is set, deduce the other pointers. |
| 8112 | * |
| 8113 | * Note: For TLS, we save the implicit record sequence number |
| 8114 | * (entering MAC computation) in the 8 bytes before ssl->out_hdr, |
| 8115 | * and the caller has to make sure there's space for this. |
| 8116 | */ |
| 8117 | |
| 8118 | static void ssl_update_out_pointers( mbedtls_ssl_context *ssl, |
| 8119 | mbedtls_ssl_transform *transform ) |
| 8120 | { |
| 8121 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8122 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8123 | { |
| 8124 | ssl->out_ctr = ssl->out_hdr + 3; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8125 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8126 | ssl->out_cid = ssl->out_ctr + 8; |
| 8127 | ssl->out_len = ssl->out_cid; |
| 8128 | if( transform != NULL ) |
| 8129 | ssl->out_len += transform->out_cid_len; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8130 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8131 | ssl->out_len = ssl->out_ctr + 8; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8132 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8133 | ssl->out_iv = ssl->out_len + 2; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8134 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8135 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 8136 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 8137 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8138 | { |
| 8139 | ssl->out_ctr = ssl->out_hdr - 8; |
| 8140 | ssl->out_len = ssl->out_hdr + 3; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8141 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 9bf10ea | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 8142 | ssl->out_cid = ssl->out_len; |
| 8143 | #endif |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8144 | ssl->out_iv = ssl->out_hdr + 5; |
| 8145 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8146 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8147 | |
| 8148 | /* Adjust out_msg to make space for explicit IV, if used. */ |
| 8149 | if( transform != NULL && |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 8150 | mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8151 | { |
| 8152 | ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen; |
| 8153 | } |
| 8154 | else |
| 8155 | ssl->out_msg = ssl->out_iv; |
| 8156 | } |
| 8157 | |
| 8158 | /* Once ssl->in_hdr as the address of the beginning of the |
| 8159 | * next incoming record is set, deduce the other pointers. |
| 8160 | * |
| 8161 | * Note: For TLS, we save the implicit record sequence number |
| 8162 | * (entering MAC computation) in the 8 bytes before ssl->in_hdr, |
| 8163 | * and the caller has to make sure there's space for this. |
| 8164 | */ |
| 8165 | |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8166 | static void ssl_update_in_pointers( mbedtls_ssl_context *ssl ) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8167 | { |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8168 | /* This function sets the pointers to match the case |
Hanno Becker | c360dcc | 2019-07-12 10:00:45 +0100 | [diff] [blame] | 8169 | * of unprotected TLS/DTLS records, with ssl->in_msg |
| 8170 | * pointing to the beginning of the record content. |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8171 | * |
| 8172 | * When decrypting a protected record, ssl->in_msg |
| 8173 | * will be shifted to point to the beginning of the |
| 8174 | * record plaintext. |
| 8175 | */ |
| 8176 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8177 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8178 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8179 | { |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8180 | /* This sets the header pointers to match records |
| 8181 | * without CID. When we receive a record containing |
| 8182 | * a CID, the fields are shifted accordingly in |
| 8183 | * ssl_parse_record_header(). */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8184 | ssl->in_ctr = ssl->in_hdr + 3; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8185 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8186 | ssl->in_cid = ssl->in_ctr + 8; |
| 8187 | ssl->in_len = ssl->in_cid; /* Default: no CID */ |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8188 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8189 | ssl->in_len = ssl->in_ctr + 8; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8190 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | c360dcc | 2019-07-12 10:00:45 +0100 | [diff] [blame] | 8191 | ssl->in_msg = ssl->in_len + 2; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8192 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8193 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 8194 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 8195 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8196 | { |
| 8197 | ssl->in_ctr = ssl->in_hdr - 8; |
| 8198 | ssl->in_len = ssl->in_hdr + 3; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8199 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 9bf10ea | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 8200 | ssl->in_cid = ssl->in_len; |
| 8201 | #endif |
Hanno Becker | c360dcc | 2019-07-12 10:00:45 +0100 | [diff] [blame] | 8202 | ssl->in_msg = ssl->in_hdr + 5; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8203 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8204 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8205 | } |
| 8206 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8207 | /* |
| 8208 | * Initialize an SSL context |
| 8209 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 8210 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 8211 | { |
| 8212 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 8213 | } |
| 8214 | |
| 8215 | /* |
| 8216 | * Setup an SSL context |
| 8217 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8218 | |
| 8219 | static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl ) |
| 8220 | { |
| 8221 | /* Set the incoming and outgoing record pointers. */ |
| 8222 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8223 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8224 | { |
| 8225 | ssl->out_hdr = ssl->out_buf; |
| 8226 | ssl->in_hdr = ssl->in_buf; |
| 8227 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8228 | MBEDTLS_SSL_TRANSPORT_ELSE |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8229 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8230 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8231 | { |
| 8232 | ssl->out_hdr = ssl->out_buf + 8; |
| 8233 | ssl->in_hdr = ssl->in_buf + 8; |
| 8234 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8235 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8236 | |
| 8237 | /* Derive other internal pointers. */ |
| 8238 | ssl_update_out_pointers( ssl, NULL /* no transform enabled */ ); |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8239 | ssl_update_in_pointers ( ssl ); |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8240 | } |
| 8241 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 8242 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 8243 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8244 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8245 | int ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8246 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 8247 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 8248 | |
Hanno Becker | ef982d5 | 2019-07-23 15:56:18 +0100 | [diff] [blame] | 8249 | #if defined(MBEDTLS_USE_TINYCRYPT) |
| 8250 | uECC_set_rng( &uecc_rng_wrapper ); |
| 8251 | #endif |
| 8252 | |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 8253 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 8254 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 8255 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 8256 | |
| 8257 | /* Set to NULL in case of an error condition */ |
| 8258 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8259 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8260 | ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN ); |
| 8261 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8262 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8263 | 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] | 8264 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8265 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8266 | } |
| 8267 | |
| 8268 | ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 8269 | if( ssl->out_buf == NULL ) |
| 8270 | { |
| 8271 | 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] | 8272 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8273 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8274 | } |
| 8275 | |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8276 | ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 8277 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8278 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8279 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8280 | |
Hanno Becker | c8f5299 | 2019-07-25 11:15:08 +0100 | [diff] [blame] | 8281 | ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE; |
Hanno Becker | f46e1ce | 2019-07-03 13:56:59 +0100 | [diff] [blame] | 8282 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8283 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8284 | |
| 8285 | error: |
| 8286 | mbedtls_free( ssl->in_buf ); |
| 8287 | mbedtls_free( ssl->out_buf ); |
| 8288 | |
| 8289 | ssl->conf = NULL; |
| 8290 | |
| 8291 | ssl->in_buf = NULL; |
| 8292 | ssl->out_buf = NULL; |
| 8293 | |
| 8294 | ssl->in_hdr = NULL; |
| 8295 | ssl->in_ctr = NULL; |
| 8296 | ssl->in_len = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8297 | ssl->in_msg = NULL; |
| 8298 | |
| 8299 | ssl->out_hdr = NULL; |
| 8300 | ssl->out_ctr = NULL; |
| 8301 | ssl->out_len = NULL; |
| 8302 | ssl->out_iv = NULL; |
| 8303 | ssl->out_msg = NULL; |
| 8304 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 8305 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8306 | } |
| 8307 | |
| 8308 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8309 | * Reset an initialized and used SSL context for re-use while retaining |
| 8310 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8311 | * |
| 8312 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 8313 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8314 | */ |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8315 | static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8316 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8317 | int ret; |
| 8318 | |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 8319 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \ |
| 8320 | !defined(MBEDTLS_SSL_SRV_C) |
| 8321 | ((void) partial); |
| 8322 | #endif |
| 8323 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8324 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8325 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 8326 | /* Cancel any possibly running timer */ |
| 8327 | ssl_set_timer( ssl, 0 ); |
| 8328 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8329 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 8330 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8331 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8332 | |
| 8333 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8334 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 8335 | 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] | 8336 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8337 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8338 | |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8339 | ssl->in_offt = NULL; |
Hanno Becker | f29d470 | 2018-08-10 11:31:15 +0100 | [diff] [blame] | 8340 | ssl_reset_in_out_pointers( ssl ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8341 | |
| 8342 | ssl->in_msgtype = 0; |
| 8343 | ssl->in_msglen = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8344 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 8345 | ssl->next_record_offset = 0; |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 8346 | ssl->in_epoch = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 8347 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8348 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 8349 | ssl_dtls_replay_reset( ssl ); |
| 8350 | #endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8351 | |
| 8352 | ssl->in_hslen = 0; |
| 8353 | ssl->nb_zero = 0; |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 8354 | |
| 8355 | ssl->keep_current_message = 0; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8356 | |
| 8357 | ssl->out_msgtype = 0; |
| 8358 | ssl->out_msglen = 0; |
| 8359 | ssl->out_left = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8360 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
| 8361 | if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ) |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 8362 | ssl->split_done = 0; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 8363 | #endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8364 | |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 8365 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 8366 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8367 | ssl->transform_in = NULL; |
| 8368 | ssl->transform_out = NULL; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8369 | |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 8370 | ssl->session_in = NULL; |
| 8371 | ssl->session_out = NULL; |
| 8372 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8373 | memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8374 | |
| 8375 | #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] | 8376 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8377 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
| 8378 | { |
| 8379 | ssl->in_left = 0; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8380 | memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8381 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8382 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8383 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 8384 | if( mbedtls_ssl_hw_record_reset != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8385 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8386 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) ); |
| 8387 | if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8388 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8389 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret ); |
| 8390 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8391 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8392 | } |
| 8393 | #endif |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8394 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8395 | if( ssl->transform ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8396 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8397 | mbedtls_ssl_transform_free( ssl->transform ); |
| 8398 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8399 | ssl->transform = NULL; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8400 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8401 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 8402 | if( ssl->session ) |
| 8403 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8404 | mbedtls_ssl_session_free( ssl->session ); |
| 8405 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 8406 | ssl->session = NULL; |
| 8407 | } |
| 8408 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8409 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 8410 | ssl->alpn_chosen = NULL; |
| 8411 | #endif |
| 8412 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 8413 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8414 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8415 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8416 | #endif |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8417 | { |
| 8418 | mbedtls_free( ssl->cli_id ); |
| 8419 | ssl->cli_id = NULL; |
| 8420 | ssl->cli_id_len = 0; |
| 8421 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 8422 | #endif |
| 8423 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8424 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 8425 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8426 | |
| 8427 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8428 | } |
| 8429 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 8430 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8431 | * Reset an initialized and used SSL context for re-use while retaining |
| 8432 | * all application-set variables, function pointers and data. |
| 8433 | */ |
| 8434 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 8435 | { |
| 8436 | return( ssl_session_reset_int( ssl, 0 ) ); |
| 8437 | } |
| 8438 | |
| 8439 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8440 | * SSL set accessors |
| 8441 | */ |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 8442 | #if !defined(MBEDTLS_SSL_CONF_ENDPOINT) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8443 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8444 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8445 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8446 | } |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 8447 | #endif /* MBEDTLS_SSL_CONF_ENDPOINT */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8448 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 8449 | 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] | 8450 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8451 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 8452 | } |
| 8453 | |
Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 8454 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \ |
| 8455 | !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8456 | 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] | 8457 | { |
Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 8458 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 8459 | } |
Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 8460 | #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] | 8461 | |
Hanno Becker | de67154 | 2019-06-12 16:30:46 +0100 | [diff] [blame] | 8462 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \ |
| 8463 | !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT) |
| 8464 | void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, |
| 8465 | unsigned limit ) |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 8466 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8467 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 8468 | } |
Hanno Becker | de67154 | 2019-06-12 16:30:46 +0100 | [diff] [blame] | 8469 | #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] | 8470 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8471 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 8472 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 8473 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 8474 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 8475 | { |
| 8476 | ssl->disable_datagram_packing = !allow_packing; |
| 8477 | } |
| 8478 | |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 8479 | #if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \ |
| 8480 | defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 8481 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 8482 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 8483 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8484 | conf->hs_timeout_min = min; |
| 8485 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 8486 | } |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 8487 | #else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN && |
| 8488 | MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */ |
| 8489 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 8490 | uint32_t min, uint32_t max ) |
| 8491 | { |
| 8492 | ((void) conf); |
| 8493 | ((void) min); |
| 8494 | ((void) max); |
| 8495 | } |
| 8496 | #endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN && |
| 8497 | MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */ |
| 8498 | |
| 8499 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 8500 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8501 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8502 | { |
Hanno Becker | acd4fc0 | 2019-06-12 16:40:50 +0100 | [diff] [blame] | 8503 | #if !defined(MBEDTLS_SSL_CONF_AUTHMODE) |
| 8504 | conf->authmode = authmode; |
| 8505 | #else |
| 8506 | ((void) conf); |
| 8507 | ((void) authmode); |
| 8508 | #endif /* MBEDTLS_SSL_CONF_AUTHMODE */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8509 | } |
| 8510 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8511 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8512 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 8513 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 8514 | void *p_vrfy ) |
| 8515 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8516 | conf->f_vrfy = f_vrfy; |
| 8517 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 8518 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8519 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 8520 | |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 8521 | #if !defined(MBEDTLS_SSL_CONF_RNG) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8522 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 8523 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8524 | void *p_rng ) |
| 8525 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 8526 | conf->f_rng = f_rng; |
| 8527 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8528 | } |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 8529 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8530 | |
Hanno Becker | 14a4a44 | 2019-07-02 17:00:34 +0100 | [diff] [blame] | 8531 | #if defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8532 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 8533 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8534 | void *p_dbg ) |
| 8535 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8536 | conf->f_dbg = f_dbg; |
| 8537 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8538 | } |
Hanno Becker | 14a4a44 | 2019-07-02 17:00:34 +0100 | [diff] [blame] | 8539 | #endif /* MBEDTLS_DEBUG_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8540 | |
Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 8541 | #if !defined(MBEDTLS_SSL_CONF_RECV) && \ |
| 8542 | !defined(MBEDTLS_SSL_CONF_SEND) && \ |
| 8543 | !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8544 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8545 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 8546 | mbedtls_ssl_send_t *f_send, |
| 8547 | mbedtls_ssl_recv_t *f_recv, |
| 8548 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8549 | { |
Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 8550 | ssl->p_bio = p_bio; |
| 8551 | ssl->f_send = f_send; |
| 8552 | ssl->f_recv = f_recv; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8553 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 8554 | } |
Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 8555 | #else |
| 8556 | void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl, |
| 8557 | void *p_bio ) |
| 8558 | { |
| 8559 | ssl->p_bio = p_bio; |
| 8560 | } |
| 8561 | #endif |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 8562 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 8563 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8564 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 8565 | { |
| 8566 | ssl->mtu = mtu; |
| 8567 | } |
| 8568 | #endif |
| 8569 | |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 8570 | #if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8571 | 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] | 8572 | { |
| 8573 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8574 | } |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 8575 | #endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */ |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8576 | |
Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 8577 | #if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \ |
| 8578 | !defined(MBEDTLS_SSL_CONF_GET_TIMER) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 8579 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 8580 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 8581 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 8582 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 8583 | { |
| 8584 | ssl->p_timer = p_timer; |
| 8585 | ssl->f_set_timer = f_set_timer; |
| 8586 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 8587 | /* Make sure we start with no timer running */ |
| 8588 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 8589 | } |
Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 8590 | #else |
| 8591 | void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl, |
| 8592 | void *p_timer ) |
| 8593 | { |
| 8594 | ssl->p_timer = p_timer; |
| 8595 | /* Make sure we start with no timer running */ |
| 8596 | ssl_set_timer( ssl, 0 ); |
| 8597 | } |
| 8598 | #endif |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 8599 | |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 8600 | #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] | 8601 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 8602 | void *p_cache, |
| 8603 | int (*f_get_cache)(void *, mbedtls_ssl_session *), |
| 8604 | int (*f_set_cache)(void *, const mbedtls_ssl_session *) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8605 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 8606 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8607 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8608 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8609 | } |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 8610 | #endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8611 | |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 8612 | #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] | 8613 | 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] | 8614 | { |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8615 | int ret; |
| 8616 | |
| 8617 | if( ssl == NULL || |
| 8618 | session == NULL || |
| 8619 | ssl->session_negotiate == NULL || |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 8620 | 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] | 8621 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8622 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8623 | } |
| 8624 | |
Hanno Becker | 58fccf2 | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 8625 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 8626 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8627 | return( ret ); |
| 8628 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 8629 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8630 | |
| 8631 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8632 | } |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 8633 | #endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8634 | |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 8635 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8636 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8637 | const int *ciphersuites ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8638 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8639 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites; |
| 8640 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites; |
| 8641 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites; |
| 8642 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 8643 | } |
| 8644 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8645 | void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 8646 | const int *ciphersuites, |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 8647 | int major, int minor ) |
| 8648 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8649 | if( major != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 8650 | return; |
| 8651 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8652 | 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] | 8653 | return; |
| 8654 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8655 | conf->ciphersuite_list[minor] = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8656 | } |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 8657 | #endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8658 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8659 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 8660 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 8661 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 8662 | { |
| 8663 | conf->cert_profile = profile; |
| 8664 | } |
| 8665 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8666 | /* Append a new keycert entry to a (possibly empty) list */ |
| 8667 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 8668 | mbedtls_x509_crt *cert, |
| 8669 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8670 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8671 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8672 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8673 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 8674 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8675 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8676 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8677 | new_cert->cert = cert; |
| 8678 | new_cert->key = key; |
| 8679 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8680 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8681 | /* Update head is the list was null, else add to the end */ |
| 8682 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 8683 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8684 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 8685 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8686 | else |
| 8687 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8688 | mbedtls_ssl_key_cert *cur = *head; |
| 8689 | while( cur->next != NULL ) |
| 8690 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8691 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8692 | } |
| 8693 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8694 | return( 0 ); |
| 8695 | } |
| 8696 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8697 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8698 | mbedtls_x509_crt *own_cert, |
| 8699 | mbedtls_pk_context *pk_key ) |
| 8700 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 8701 | 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] | 8702 | } |
| 8703 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8704 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 8705 | mbedtls_x509_crt *ca_chain, |
| 8706 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8707 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 8708 | conf->ca_chain = ca_chain; |
| 8709 | conf->ca_crl = ca_crl; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8710 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8711 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 8712 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 8713 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 8714 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 8715 | mbedtls_x509_crt *own_cert, |
| 8716 | mbedtls_pk_context *pk_key ) |
| 8717 | { |
| 8718 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 8719 | own_cert, pk_key ) ); |
| 8720 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 8721 | |
| 8722 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 8723 | mbedtls_x509_crt *ca_chain, |
| 8724 | mbedtls_x509_crl *ca_crl ) |
| 8725 | { |
| 8726 | ssl->handshake->sni_ca_chain = ca_chain; |
| 8727 | ssl->handshake->sni_ca_crl = ca_crl; |
| 8728 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 8729 | |
| 8730 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 8731 | int authmode ) |
| 8732 | { |
| 8733 | ssl->handshake->sni_authmode = authmode; |
| 8734 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 8735 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 8736 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 8737 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 8738 | /* |
| 8739 | * Set EC J-PAKE password for current handshake |
| 8740 | */ |
| 8741 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 8742 | const unsigned char *pw, |
| 8743 | size_t pw_len ) |
| 8744 | { |
| 8745 | mbedtls_ecjpake_role role; |
| 8746 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 8747 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 8748 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8749 | |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 8750 | 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] | 8751 | role = MBEDTLS_ECJPAKE_SERVER; |
| 8752 | else |
| 8753 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 8754 | |
| 8755 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 8756 | role, |
| 8757 | MBEDTLS_MD_SHA256, |
| 8758 | MBEDTLS_ECP_DP_SECP256R1, |
| 8759 | pw, pw_len ) ); |
| 8760 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 8761 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 8762 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8763 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8764 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 8765 | const unsigned char *psk, size_t psk_len, |
| 8766 | const unsigned char *psk_identity, size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 8767 | { |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8768 | if( psk == NULL || psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8769 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8770 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8771 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 8772 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 8773 | |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 8774 | /* Identity len will be encoded on two bytes */ |
| 8775 | if( ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8776 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 8777 | { |
| 8778 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8779 | } |
| 8780 | |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 8781 | if( conf->psk != NULL ) |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8782 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 8783 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 8784 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 8785 | mbedtls_free( conf->psk ); |
Manuel Pégourié-Gonnard | 173c790 | 2015-10-20 19:56:45 +0200 | [diff] [blame] | 8786 | conf->psk = NULL; |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 8787 | conf->psk_len = 0; |
| 8788 | } |
| 8789 | if( conf->psk_identity != NULL ) |
| 8790 | { |
| 8791 | mbedtls_free( conf->psk_identity ); |
Manuel Pégourié-Gonnard | 173c790 | 2015-10-20 19:56:45 +0200 | [diff] [blame] | 8792 | conf->psk_identity = NULL; |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 8793 | conf->psk_identity_len = 0; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8794 | } |
| 8795 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8796 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL || |
| 8797 | ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL ) |
Mansour Moufid | f81088b | 2015-02-17 13:10:21 -0500 | [diff] [blame] | 8798 | { |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 8799 | mbedtls_free( conf->psk ); |
Manuel Pégourié-Gonnard | 24417f0 | 2015-09-28 18:09:45 +0200 | [diff] [blame] | 8800 | mbedtls_free( conf->psk_identity ); |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 8801 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | 24417f0 | 2015-09-28 18:09:45 +0200 | [diff] [blame] | 8802 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8803 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Mansour Moufid | f81088b | 2015-02-17 13:10:21 -0500 | [diff] [blame] | 8804 | } |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8805 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 8806 | conf->psk_len = psk_len; |
| 8807 | conf->psk_identity_len = psk_identity_len; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8808 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 8809 | memcpy( conf->psk, psk, conf->psk_len ); |
| 8810 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8811 | |
| 8812 | return( 0 ); |
| 8813 | } |
| 8814 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 8815 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 8816 | const unsigned char *psk, size_t psk_len ) |
| 8817 | { |
| 8818 | if( psk == NULL || ssl->handshake == NULL ) |
| 8819 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8820 | |
| 8821 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 8822 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8823 | |
| 8824 | if( ssl->handshake->psk != NULL ) |
Andres Amaya Garcia | a004988 | 2017-06-26 11:35:17 +0100 | [diff] [blame] | 8825 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 8826 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 8827 | ssl->handshake->psk_len ); |
Simon Butcher | 5b8d1d6 | 2015-10-04 22:06:51 +0100 | [diff] [blame] | 8828 | mbedtls_free( ssl->handshake->psk ); |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 8829 | ssl->handshake->psk_len = 0; |
Andres Amaya Garcia | a004988 | 2017-06-26 11:35:17 +0100 | [diff] [blame] | 8830 | } |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 8831 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8832 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8833 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 8834 | |
| 8835 | ssl->handshake->psk_len = psk_len; |
| 8836 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 8837 | |
| 8838 | return( 0 ); |
| 8839 | } |
| 8840 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8841 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8842 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8843 | size_t), |
| 8844 | void *p_psk ) |
| 8845 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8846 | conf->f_psk = f_psk; |
| 8847 | conf->p_psk = p_psk; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 8848 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8849 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 8850 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 8851 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 8852 | |
| 8853 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8854 | 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] | 8855 | { |
| 8856 | int ret; |
| 8857 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 8858 | if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 || |
| 8859 | ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 ) |
| 8860 | { |
| 8861 | mbedtls_mpi_free( &conf->dhm_P ); |
| 8862 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8863 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 8864 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8865 | |
| 8866 | return( 0 ); |
| 8867 | } |
Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 8868 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8869 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 8870 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 8871 | const unsigned char *dhm_P, size_t P_len, |
| 8872 | const unsigned char *dhm_G, size_t G_len ) |
| 8873 | { |
| 8874 | int ret; |
| 8875 | |
| 8876 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 8877 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 8878 | { |
| 8879 | mbedtls_mpi_free( &conf->dhm_P ); |
| 8880 | mbedtls_mpi_free( &conf->dhm_G ); |
| 8881 | return( ret ); |
| 8882 | } |
| 8883 | |
| 8884 | return( 0 ); |
| 8885 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8886 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8887 | 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] | 8888 | { |
| 8889 | int ret; |
| 8890 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 8891 | if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 || |
| 8892 | ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 ) |
| 8893 | { |
| 8894 | mbedtls_mpi_free( &conf->dhm_P ); |
| 8895 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 8896 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 8897 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 8898 | |
| 8899 | return( 0 ); |
| 8900 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 8901 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 8902 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 8903 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 8904 | /* |
| 8905 | * Set the minimum length for Diffie-Hellman parameters |
| 8906 | */ |
| 8907 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 8908 | unsigned int bitlen ) |
| 8909 | { |
| 8910 | conf->dhm_min_bitlen = bitlen; |
| 8911 | } |
| 8912 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 8913 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 8914 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 8915 | /* |
| 8916 | * Set allowed/preferred hashes for handshake signatures |
| 8917 | */ |
| 8918 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 8919 | const int *hashes ) |
| 8920 | { |
Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 8921 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 8922 | conf->sig_hashes = hashes; |
Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 8923 | #else |
| 8924 | ((void) conf); |
| 8925 | ((void) hashes); |
| 8926 | #endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 8927 | } |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8928 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 8929 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 8930 | #if defined(MBEDTLS_ECP_C) |
Hanno Becker | c1096e7 | 2019-06-19 12:30:41 +0100 | [diff] [blame] | 8931 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_EC) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 8932 | /* |
| 8933 | * Set the allowed elliptic curves |
| 8934 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8935 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8936 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 8937 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8938 | conf->curve_list = curve_list; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 8939 | } |
Hanno Becker | c1096e7 | 2019-06-19 12:30:41 +0100 | [diff] [blame] | 8940 | #endif /* MBEDTLS_SSL_CONF_SINGLE_EC */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8941 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 8942 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 8943 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8944 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8945 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8946 | /* Initialize to suppress unnecessary compiler warning */ |
| 8947 | size_t hostname_len = 0; |
| 8948 | |
| 8949 | /* Check if new hostname is valid before |
| 8950 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8951 | if( hostname != NULL ) |
| 8952 | { |
| 8953 | hostname_len = strlen( hostname ); |
| 8954 | |
| 8955 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 8956 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8957 | } |
| 8958 | |
| 8959 | /* Now it's clear that we will overwrite the old hostname, |
| 8960 | * so we can free it safely */ |
| 8961 | |
| 8962 | if( ssl->hostname != NULL ) |
| 8963 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 8964 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8965 | mbedtls_free( ssl->hostname ); |
| 8966 | } |
| 8967 | |
| 8968 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 8969 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8970 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8971 | { |
| 8972 | ssl->hostname = NULL; |
| 8973 | } |
| 8974 | else |
| 8975 | { |
| 8976 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8977 | if( ssl->hostname == NULL ) |
| 8978 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 8979 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8980 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 8981 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8982 | ssl->hostname[hostname_len] = '\0'; |
| 8983 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8984 | |
| 8985 | return( 0 ); |
| 8986 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 8987 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8988 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 8989 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8990 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8991 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 8992 | const unsigned char *, size_t), |
| 8993 | void *p_sni ) |
| 8994 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8995 | conf->f_sni = f_sni; |
| 8996 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 8997 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8998 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 8999 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9000 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9001 | 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] | 9002 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9003 | size_t cur_len, tot_len; |
| 9004 | const char **p; |
| 9005 | |
| 9006 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 9007 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 9008 | * MUST NOT be truncated." |
| 9009 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9010 | */ |
| 9011 | tot_len = 0; |
| 9012 | for( p = protos; *p != NULL; p++ ) |
| 9013 | { |
| 9014 | cur_len = strlen( *p ); |
| 9015 | tot_len += cur_len; |
| 9016 | |
| 9017 | if( cur_len == 0 || cur_len > 255 || tot_len > 65535 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9018 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9019 | } |
| 9020 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9021 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9022 | |
| 9023 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9024 | } |
| 9025 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9026 | 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] | 9027 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9028 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9029 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9030 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9031 | |
Hanno Becker | 33b9b25 | 2019-07-05 11:23:25 +0100 | [diff] [blame] | 9032 | #if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \ |
| 9033 | !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER) |
| 9034 | void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, |
| 9035 | int major, int minor ) |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 9036 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9037 | conf->max_major_ver = major; |
| 9038 | conf->max_minor_ver = minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 9039 | } |
Hanno Becker | 33b9b25 | 2019-07-05 11:23:25 +0100 | [diff] [blame] | 9040 | #endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER || |
| 9041 | MBEDTLS_SSL_CONF_MAX_MAJOR_VER */ |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 9042 | |
Hanno Becker | 33b9b25 | 2019-07-05 11:23:25 +0100 | [diff] [blame] | 9043 | #if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \ |
| 9044 | !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER) |
| 9045 | void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, |
| 9046 | int major, int minor ) |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 9047 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9048 | conf->min_major_ver = major; |
| 9049 | conf->min_minor_ver = minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 9050 | } |
Hanno Becker | 33b9b25 | 2019-07-05 11:23:25 +0100 | [diff] [blame] | 9051 | #endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER || |
| 9052 | MBEDTLS_SSL_CONF_MIN_MAJOR_VER */ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 9053 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9054 | #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] | 9055 | 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] | 9056 | { |
Manuel Pégourié-Gonnard | 684b059 | 2015-05-06 09:27:31 +0100 | [diff] [blame] | 9057 | conf->fallback = fallback; |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 9058 | } |
| 9059 | #endif |
| 9060 | |
Hanno Becker | c2cfdaa | 2019-06-13 12:33:03 +0100 | [diff] [blame] | 9061 | #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] | 9062 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 9063 | char cert_req_ca_list ) |
| 9064 | { |
| 9065 | conf->cert_req_ca_list = cert_req_ca_list; |
| 9066 | } |
| 9067 | #endif |
| 9068 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9069 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9070 | 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] | 9071 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9072 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 9073 | } |
| 9074 | #endif |
| 9075 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9076 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Hanno Becker | f765ce6 | 2019-06-21 13:17:14 +0100 | [diff] [blame] | 9077 | #if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9078 | 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] | 9079 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9080 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 9081 | } |
Hanno Becker | f765ce6 | 2019-06-21 13:17:14 +0100 | [diff] [blame] | 9082 | #endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */ |
| 9083 | #if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET) |
Jarno Lamsa | 7a5e2be | 2019-06-10 10:13:03 +0300 | [diff] [blame] | 9084 | void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf, |
Jarno Lamsa | 842be16 | 2019-06-10 15:05:33 +0300 | [diff] [blame] | 9085 | char ems_enf ) |
Jarno Lamsa | 7a5e2be | 2019-06-10 10:13:03 +0300 | [diff] [blame] | 9086 | { |
| 9087 | conf->enforce_extended_master_secret = ems_enf; |
| 9088 | } |
Hanno Becker | f765ce6 | 2019-06-21 13:17:14 +0100 | [diff] [blame] | 9089 | #endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */ |
Hanno Becker | aabbb58 | 2019-06-11 13:43:27 +0100 | [diff] [blame] | 9090 | #endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 9091 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 9092 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9093 | 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] | 9094 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9095 | conf->arc4_disabled = arc4; |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 9096 | } |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 9097 | #endif |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 9098 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9099 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9100 | 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] | 9101 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9102 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9103 | 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] | 9104 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9105 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9106 | } |
| 9107 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 9108 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9109 | |
| 9110 | return( 0 ); |
| 9111 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9112 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9113 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9114 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 9115 | 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] | 9116 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9117 | conf->trunc_hmac = truncate; |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 9118 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9119 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 9120 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9121 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9122 | 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] | 9123 | { |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 9124 | conf->cbc_record_splitting = split; |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 9125 | } |
| 9126 | #endif |
| 9127 | |
Hanno Becker | b0b2b67 | 2019-06-12 16:58:10 +0100 | [diff] [blame] | 9128 | #if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9129 | 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] | 9130 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9131 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9132 | } |
Hanno Becker | b0b2b67 | 2019-06-12 16:58:10 +0100 | [diff] [blame] | 9133 | #endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9134 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9135 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9136 | 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] | 9137 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9138 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 9139 | } |
| 9140 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9141 | 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] | 9142 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9143 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 9144 | } |
| 9145 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9146 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 9147 | const unsigned char period[8] ) |
| 9148 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9149 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 9150 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9151 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9152 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9153 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9154 | #if defined(MBEDTLS_SSL_CLI_C) |
| 9155 | 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] | 9156 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 9157 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 9158 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9159 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 9160 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9161 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 9162 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 9163 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 9164 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 9165 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 9166 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 9167 | conf->f_ticket_write = f_ticket_write; |
| 9168 | conf->f_ticket_parse = f_ticket_parse; |
| 9169 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 9170 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9171 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9172 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 9173 | |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 9174 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 9175 | void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf, |
| 9176 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 9177 | void *p_export_keys ) |
| 9178 | { |
| 9179 | conf->f_export_keys = f_export_keys; |
| 9180 | conf->p_export_keys = p_export_keys; |
| 9181 | } |
| 9182 | #endif |
| 9183 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 9184 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9185 | void mbedtls_ssl_conf_async_private_cb( |
| 9186 | mbedtls_ssl_config *conf, |
| 9187 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 9188 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 9189 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 9190 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9191 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9192 | { |
| 9193 | conf->f_async_sign_start = f_async_sign; |
| 9194 | conf->f_async_decrypt_start = f_async_decrypt; |
| 9195 | conf->f_async_resume = f_async_resume; |
| 9196 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9197 | conf->p_async_config_data = async_config_data; |
| 9198 | } |
| 9199 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 9200 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 9201 | { |
| 9202 | return( conf->p_async_config_data ); |
| 9203 | } |
| 9204 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 9205 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9206 | { |
| 9207 | if( ssl->handshake == NULL ) |
| 9208 | return( NULL ); |
| 9209 | else |
| 9210 | return( ssl->handshake->user_async_ctx ); |
| 9211 | } |
| 9212 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 9213 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9214 | void *ctx ) |
| 9215 | { |
| 9216 | if( ssl->handshake != NULL ) |
| 9217 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9218 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 9219 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9220 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9221 | /* |
| 9222 | * SSL get accessors |
| 9223 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9224 | size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9225 | { |
| 9226 | return( ssl->in_offt == NULL ? 0 : ssl->in_msglen ); |
| 9227 | } |
| 9228 | |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9229 | int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl ) |
| 9230 | { |
| 9231 | /* |
| 9232 | * Case A: We're currently holding back |
| 9233 | * a message for further processing. |
| 9234 | */ |
| 9235 | |
| 9236 | if( ssl->keep_current_message == 1 ) |
| 9237 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9238 | 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] | 9239 | return( 1 ); |
| 9240 | } |
| 9241 | |
| 9242 | /* |
| 9243 | * Case B: Further records are pending in the current datagram. |
| 9244 | */ |
| 9245 | |
| 9246 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 9247 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9248 | ssl->in_left > ssl->next_record_offset ) |
| 9249 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9250 | 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] | 9251 | return( 1 ); |
| 9252 | } |
| 9253 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 9254 | |
| 9255 | /* |
| 9256 | * Case C: A handshake message is being processed. |
| 9257 | */ |
| 9258 | |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9259 | if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen ) |
| 9260 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9261 | 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] | 9262 | return( 1 ); |
| 9263 | } |
| 9264 | |
| 9265 | /* |
| 9266 | * Case D: An application data message is being processed |
| 9267 | */ |
| 9268 | if( ssl->in_offt != NULL ) |
| 9269 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9270 | 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] | 9271 | return( 1 ); |
| 9272 | } |
| 9273 | |
| 9274 | /* |
| 9275 | * In all other cases, the rest of the message can be dropped. |
Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 9276 | * As in ssl_get_next_record, this needs to be adapted if |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9277 | * we implement support for multiple alerts in single records. |
| 9278 | */ |
| 9279 | |
| 9280 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) ); |
| 9281 | return( 0 ); |
| 9282 | } |
| 9283 | |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 9284 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9285 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 9286 | if( ssl->session != NULL ) |
| 9287 | return( ssl->session->verify_result ); |
| 9288 | |
| 9289 | if( ssl->session_negotiate != NULL ) |
| 9290 | return( ssl->session_negotiate->verify_result ); |
| 9291 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 9292 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9293 | } |
| 9294 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9295 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 9296 | { |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 9297 | int suite; |
| 9298 | |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 9299 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9300 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 9301 | |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 9302 | suite = mbedtls_ssl_session_get_ciphersuite( ssl->session ); |
| 9303 | return( mbedtls_ssl_get_ciphersuite_name( suite ) ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 9304 | } |
| 9305 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9306 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9307 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9308 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 9309 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9310 | { |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 9311 | switch( mbedtls_ssl_get_minor_ver( ssl ) ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9312 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9313 | case MBEDTLS_SSL_MINOR_VERSION_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9314 | return( "DTLSv1.0" ); |
| 9315 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9316 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9317 | return( "DTLSv1.2" ); |
| 9318 | |
| 9319 | default: |
| 9320 | return( "unknown (DTLS)" ); |
| 9321 | } |
| 9322 | } |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9323 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 9324 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 9325 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9326 | { |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 9327 | switch( mbedtls_ssl_get_minor_ver( ssl ) ) |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9328 | { |
| 9329 | case MBEDTLS_SSL_MINOR_VERSION_0: |
| 9330 | return( "SSLv3.0" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9331 | |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9332 | case MBEDTLS_SSL_MINOR_VERSION_1: |
| 9333 | return( "TLSv1.0" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9334 | |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9335 | case MBEDTLS_SSL_MINOR_VERSION_2: |
| 9336 | return( "TLSv1.1" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9337 | |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9338 | case MBEDTLS_SSL_MINOR_VERSION_3: |
| 9339 | return( "TLSv1.2" ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 9340 | |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9341 | default: |
| 9342 | return( "unknown" ); |
| 9343 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9344 | } |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9345 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9346 | } |
| 9347 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9348 | 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] | 9349 | { |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9350 | size_t transform_expansion = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9351 | const mbedtls_ssl_transform *transform = ssl->transform_out; |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9352 | unsigned block_size; |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9353 | |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 9354 | size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl ); |
| 9355 | |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 9356 | if( transform == NULL ) |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 9357 | return( (int) out_hdr_len ); |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 9358 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9359 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 9360 | if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL ) |
| 9361 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9362 | #endif |
| 9363 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9364 | switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) ) |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9365 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9366 | case MBEDTLS_MODE_GCM: |
| 9367 | case MBEDTLS_MODE_CCM: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9368 | case MBEDTLS_MODE_CHACHAPOLY: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9369 | case MBEDTLS_MODE_STREAM: |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9370 | transform_expansion = transform->minlen; |
| 9371 | break; |
| 9372 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9373 | case MBEDTLS_MODE_CBC: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9374 | |
| 9375 | block_size = mbedtls_cipher_get_block_size( |
| 9376 | &transform->cipher_ctx_enc ); |
| 9377 | |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9378 | /* Expansion due to the addition of the MAC. */ |
| 9379 | transform_expansion += transform->maclen; |
| 9380 | |
| 9381 | /* Expansion due to the addition of CBC padding; |
| 9382 | * Theoretically up to 256 bytes, but we never use |
| 9383 | * more than the block size of the underlying cipher. */ |
| 9384 | transform_expansion += block_size; |
| 9385 | |
| 9386 | /* For TLS 1.1 or higher, an explicit IV is added |
| 9387 | * after the record header. */ |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9388 | #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] | 9389 | if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9390 | transform_expansion += block_size; |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9391 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9392 | |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9393 | break; |
| 9394 | |
| 9395 | default: |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 9396 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9397 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9398 | } |
| 9399 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 9400 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | add0190 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 9401 | if( transform->out_cid_len != 0 ) |
| 9402 | transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 9403 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | add0190 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 9404 | |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 9405 | return( (int)( out_hdr_len + transform_expansion ) ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9406 | } |
| 9407 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9408 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 9409 | size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 9410 | { |
| 9411 | size_t max_len; |
| 9412 | |
| 9413 | /* |
| 9414 | * Assume mfl_code is correct since it was checked when set |
| 9415 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9416 | 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] | 9417 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 9418 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9419 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9420 | 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] | 9421 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9422 | 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] | 9423 | } |
| 9424 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 9425 | /* During a handshake, use the value being negotiated */ |
| 9426 | if( ssl->session_negotiate != NULL && |
| 9427 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 9428 | { |
| 9429 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 9430 | } |
| 9431 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9432 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9433 | } |
| 9434 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 9435 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9436 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9437 | static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl ) |
| 9438 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 9439 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 9440 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT && |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 9441 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 9442 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 9443 | return ( 0 ); |
| 9444 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9445 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 9446 | return( ssl->mtu ); |
| 9447 | |
| 9448 | if( ssl->mtu == 0 ) |
| 9449 | return( ssl->handshake->mtu ); |
| 9450 | |
| 9451 | return( ssl->mtu < ssl->handshake->mtu ? |
| 9452 | ssl->mtu : ssl->handshake->mtu ); |
| 9453 | } |
| 9454 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 9455 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9456 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 9457 | { |
| 9458 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 9459 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 9460 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 9461 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9462 | (void) ssl; |
| 9463 | #endif |
| 9464 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9465 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 9466 | const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl ); |
| 9467 | |
| 9468 | if( max_len > mfl ) |
| 9469 | max_len = mfl; |
| 9470 | #endif |
| 9471 | |
| 9472 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9473 | if( ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9474 | { |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9475 | const size_t mtu = ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9476 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 9477 | const size_t overhead = (size_t) ret; |
| 9478 | |
| 9479 | if( ret < 0 ) |
| 9480 | return( ret ); |
| 9481 | |
| 9482 | if( mtu <= overhead ) |
| 9483 | { |
| 9484 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 9485 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 9486 | } |
| 9487 | |
| 9488 | if( max_len > mtu - overhead ) |
| 9489 | max_len = mtu - overhead; |
| 9490 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9491 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9492 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 9493 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 9494 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9495 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9496 | #endif |
| 9497 | |
| 9498 | return( (int) max_len ); |
| 9499 | } |
| 9500 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9501 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 9502 | 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] | 9503 | { |
| 9504 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9505 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9506 | |
Hanno Becker | bfab9df | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 9507 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9508 | return( ssl->session->peer_cert ); |
Hanno Becker | bfab9df | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 9509 | #else |
| 9510 | return( NULL ); |
| 9511 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9512 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9513 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9514 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9515 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 933b9fc | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 9516 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 9517 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9518 | { |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9519 | if( ssl == NULL || |
| 9520 | dst == NULL || |
| 9521 | ssl->session == NULL || |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 9522 | 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] | 9523 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9524 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9525 | } |
| 9526 | |
Hanno Becker | 58fccf2 | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 9527 | return( mbedtls_ssl_session_copy( dst, ssl->session ) ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9528 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9529 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9530 | |
Manuel Pégourié-Gonnard | 37a5324 | 2019-05-20 11:12:28 +0200 | [diff] [blame] | 9531 | const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl ) |
| 9532 | { |
| 9533 | if( ssl == NULL ) |
| 9534 | return( NULL ); |
| 9535 | |
| 9536 | return( ssl->session ); |
| 9537 | } |
| 9538 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9539 | /* |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9540 | * Define ticket header determining Mbed TLS version |
| 9541 | * and structure of the ticket. |
| 9542 | */ |
| 9543 | |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9544 | /* |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9545 | * Define bitflag determining compile-time settings influencing |
| 9546 | * structure of serialized SSL sessions. |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9547 | */ |
| 9548 | |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9549 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9550 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9551 | #else |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9552 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9553 | #endif /* MBEDTLS_HAVE_TIME */ |
| 9554 | |
| 9555 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9556 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9557 | #else |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9558 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9559 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 9560 | |
| 9561 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9562 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9563 | #else |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9564 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9565 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 9566 | |
| 9567 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9568 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9569 | #else |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9570 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9571 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 9572 | |
| 9573 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9574 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9575 | #else |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9576 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9577 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
| 9578 | |
| 9579 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9580 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9581 | #else |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9582 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9583 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 9584 | |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9585 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 9586 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 9587 | #else |
| 9588 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 9589 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 9590 | |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 9591 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 9592 | #define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1 |
| 9593 | #else |
| 9594 | #define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0 |
| 9595 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 9596 | |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9597 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 9598 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 9599 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 9600 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
| 9601 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4 |
| 9602 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5 |
| 9603 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6 |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 9604 | #define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7 |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9605 | |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9606 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9607 | ( (uint16_t) ( \ |
| 9608 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 9609 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 9610 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 9611 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
| 9612 | ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \ |
| 9613 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 9614 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \ |
| 9615 | ( 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] | 9616 | |
Hanno Becker | 557fe9f | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 9617 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9618 | MBEDTLS_VERSION_MAJOR, |
| 9619 | MBEDTLS_VERSION_MINOR, |
| 9620 | MBEDTLS_VERSION_PATCH, |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9621 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF, |
| 9622 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF, |
Hanno Becker | 557fe9f | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 9623 | }; |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9624 | |
| 9625 | /* |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9626 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9627 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 9628 | * |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9629 | * opaque mbedtls_version[3]; // major, minor, patch |
| 9630 | * opaque session_format[2]; // version-specific 16-bit field determining |
| 9631 | * // the format of the remaining |
| 9632 | * // serialized data. |
Hanno Becker | b36db4f | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 9633 | * |
| 9634 | * Note: When updating the format, remember to keep |
| 9635 | * these version+format bytes. |
| 9636 | * |
Hanno Becker | 7bf7710 | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 9637 | * // In this version, `session_format` determines |
| 9638 | * // the setting of those compile-time |
| 9639 | * // configuration options which influence |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9640 | * // the structure of mbedtls_ssl_session. |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9641 | * uint64 start_time; |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9642 | * uint8 ciphersuite[2]; // defined by the standard |
| 9643 | * uint8 compression; // 0 or 1 |
| 9644 | * uint8 session_id_len; // at most 32 |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9645 | * opaque session_id[32]; |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9646 | * opaque master[48]; // fixed length in the standard |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9647 | * uint32 verify_result; |
Hanno Becker | 0528f82 | 2019-06-18 12:45:31 +0100 | [diff] [blame] | 9648 | * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) { |
| 9649 | * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert |
| 9650 | * case disabled: uint8_t peer_cert_digest_type; |
| 9651 | * opaque peer_cert_digest<0..2^8-1>; |
| 9652 | * } |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9653 | * 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] | 9654 | * uint32 ticket_lifetime; |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9655 | * uint8 mfl_code; // up to 255 according to standard |
| 9656 | * uint8 trunc_hmac; // 0 or 1 |
| 9657 | * uint8 encrypt_then_mac; // 0 or 1 |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9658 | * |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9659 | * The order is the same as in the definition of the structure, except |
| 9660 | * verify_result is put before peer_cert so that all mandatory fields come |
| 9661 | * together in one block. |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9662 | */ |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 9663 | static int ssl_session_save( const mbedtls_ssl_session *session, |
| 9664 | unsigned char omit_header, |
| 9665 | unsigned char *buf, |
| 9666 | size_t buf_len, |
| 9667 | size_t *olen ) |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9668 | { |
| 9669 | unsigned char *p = buf; |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9670 | size_t used = 0; |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9671 | #if defined(MBEDTLS_HAVE_TIME) |
| 9672 | uint64_t start; |
| 9673 | #endif |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9674 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 9675 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9676 | size_t cert_len; |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9677 | #endif |
Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 9678 | #endif |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9679 | |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 9680 | if( !omit_header ) |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9681 | { |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 9682 | /* |
| 9683 | * Add version identifier |
| 9684 | */ |
| 9685 | |
| 9686 | used += sizeof( ssl_serialized_session_header ); |
| 9687 | |
| 9688 | if( used <= buf_len ) |
| 9689 | { |
| 9690 | memcpy( p, ssl_serialized_session_header, |
| 9691 | sizeof( ssl_serialized_session_header ) ); |
| 9692 | p += sizeof( ssl_serialized_session_header ); |
| 9693 | } |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9694 | } |
| 9695 | |
| 9696 | /* |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9697 | * Time |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9698 | */ |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9699 | #if defined(MBEDTLS_HAVE_TIME) |
| 9700 | used += 8; |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9701 | |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9702 | if( used <= buf_len ) |
| 9703 | { |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9704 | start = (uint64_t) session->start; |
| 9705 | |
| 9706 | *p++ = (unsigned char)( ( start >> 56 ) & 0xFF ); |
| 9707 | *p++ = (unsigned char)( ( start >> 48 ) & 0xFF ); |
| 9708 | *p++ = (unsigned char)( ( start >> 40 ) & 0xFF ); |
| 9709 | *p++ = (unsigned char)( ( start >> 32 ) & 0xFF ); |
| 9710 | *p++ = (unsigned char)( ( start >> 24 ) & 0xFF ); |
| 9711 | *p++ = (unsigned char)( ( start >> 16 ) & 0xFF ); |
| 9712 | *p++ = (unsigned char)( ( start >> 8 ) & 0xFF ); |
| 9713 | *p++ = (unsigned char)( ( start ) & 0xFF ); |
| 9714 | } |
| 9715 | #endif /* MBEDTLS_HAVE_TIME */ |
| 9716 | |
| 9717 | /* |
| 9718 | * Basic mandatory fields |
| 9719 | */ |
| 9720 | used += 2 /* ciphersuite */ |
| 9721 | + 1 /* compression */ |
| 9722 | + 1 /* id_len */ |
| 9723 | + sizeof( session->id ) |
| 9724 | + sizeof( session->master ) |
| 9725 | + 4; /* verify_result */ |
| 9726 | |
| 9727 | if( used <= buf_len ) |
| 9728 | { |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 9729 | const int ciphersuite = |
| 9730 | mbedtls_ssl_session_get_ciphersuite( session ); |
| 9731 | *p++ = (unsigned char)( ( ciphersuite >> 8 ) & 0xFF ); |
| 9732 | *p++ = (unsigned char)( ( ciphersuite ) & 0xFF ); |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9733 | |
| 9734 | *p++ = (unsigned char)( session->compression & 0xFF ); |
| 9735 | |
| 9736 | *p++ = (unsigned char)( session->id_len & 0xFF ); |
| 9737 | memcpy( p, session->id, 32 ); |
| 9738 | p += 32; |
| 9739 | |
| 9740 | memcpy( p, session->master, 48 ); |
| 9741 | p += 48; |
| 9742 | |
| 9743 | *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF ); |
| 9744 | *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF ); |
| 9745 | *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF ); |
| 9746 | *p++ = (unsigned char)( ( session->verify_result ) & 0xFF ); |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9747 | } |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9748 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9749 | /* |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9750 | * Peer's end-entity certificate |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9751 | */ |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9752 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 9753 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9754 | if( session->peer_cert == NULL ) |
| 9755 | cert_len = 0; |
| 9756 | else |
| 9757 | cert_len = session->peer_cert->raw.len; |
| 9758 | |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9759 | used += 3 + cert_len; |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9760 | |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9761 | if( used <= buf_len ) |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9762 | { |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9763 | *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF ); |
| 9764 | *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF ); |
| 9765 | *p++ = (unsigned char)( ( cert_len ) & 0xFF ); |
| 9766 | |
| 9767 | if( session->peer_cert != NULL ) |
| 9768 | { |
| 9769 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 9770 | p += cert_len; |
| 9771 | } |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9772 | } |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 9773 | |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 9774 | #elif defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 9775 | /* Digest of peer certificate */ |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 9776 | if( session->peer_cert_digest != NULL ) |
| 9777 | { |
| 9778 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 9779 | if( used <= buf_len ) |
| 9780 | { |
| 9781 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 9782 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 9783 | memcpy( p, session->peer_cert_digest, |
| 9784 | session->peer_cert_digest_len ); |
| 9785 | p += session->peer_cert_digest_len; |
| 9786 | } |
| 9787 | } |
| 9788 | else |
| 9789 | { |
| 9790 | used += 2; |
| 9791 | if( used <= buf_len ) |
| 9792 | { |
| 9793 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 9794 | *p++ = 0; |
| 9795 | } |
| 9796 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 9797 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9798 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 9799 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9800 | /* |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9801 | * Session ticket if any, plus associated data |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9802 | */ |
| 9803 | #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] | 9804 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9805 | |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9806 | if( used <= buf_len ) |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9807 | { |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9808 | *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF ); |
| 9809 | *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF ); |
| 9810 | *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF ); |
| 9811 | |
| 9812 | if( session->ticket != NULL ) |
| 9813 | { |
| 9814 | memcpy( p, session->ticket, session->ticket_len ); |
| 9815 | p += session->ticket_len; |
| 9816 | } |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9817 | |
| 9818 | *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF ); |
| 9819 | *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF ); |
| 9820 | *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF ); |
| 9821 | *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF ); |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9822 | } |
| 9823 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 9824 | |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9825 | /* |
| 9826 | * Misc extension-related info |
| 9827 | */ |
| 9828 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 9829 | used += 1; |
| 9830 | |
| 9831 | if( used <= buf_len ) |
| 9832 | *p++ = session->mfl_code; |
| 9833 | #endif |
| 9834 | |
| 9835 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 9836 | used += 1; |
| 9837 | |
| 9838 | if( used <= buf_len ) |
| 9839 | *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF ); |
| 9840 | #endif |
| 9841 | |
| 9842 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 9843 | used += 1; |
| 9844 | |
| 9845 | if( used <= buf_len ) |
| 9846 | *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF ); |
| 9847 | #endif |
| 9848 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9849 | /* Done */ |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9850 | *olen = used; |
| 9851 | |
| 9852 | if( used > buf_len ) |
| 9853 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9854 | |
| 9855 | return( 0 ); |
| 9856 | } |
| 9857 | |
| 9858 | /* |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 9859 | * Public wrapper for ssl_session_save() |
| 9860 | */ |
| 9861 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 9862 | unsigned char *buf, |
| 9863 | size_t buf_len, |
| 9864 | size_t *olen ) |
| 9865 | { |
| 9866 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); |
| 9867 | } |
| 9868 | |
| 9869 | /* |
Manuel Pégourié-Gonnard | 8175816 | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 9870 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 9871 | * |
| 9872 | * 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] | 9873 | * case of error, and has an extra option omit_header. |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9874 | */ |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 9875 | static int ssl_session_load( mbedtls_ssl_session *session, |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 9876 | unsigned char omit_header, |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 9877 | const unsigned char *buf, |
| 9878 | size_t len ) |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9879 | { |
| 9880 | const unsigned char *p = buf; |
| 9881 | const unsigned char * const end = buf + len; |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 9882 | int ciphersuite; |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9883 | #if defined(MBEDTLS_HAVE_TIME) |
| 9884 | uint64_t start; |
| 9885 | #endif |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9886 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 9887 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9888 | size_t cert_len; |
Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 9889 | #endif |
| 9890 | #endif |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9891 | |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 9892 | if( !omit_header ) |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9893 | { |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 9894 | /* |
| 9895 | * Check version identifier |
| 9896 | */ |
| 9897 | |
| 9898 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
| 9899 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9900 | |
| 9901 | if( memcmp( p, ssl_serialized_session_header, |
| 9902 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 9903 | { |
| 9904 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 9905 | } |
| 9906 | p += sizeof( ssl_serialized_session_header ); |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9907 | } |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9908 | |
| 9909 | /* |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9910 | * Time |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9911 | */ |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9912 | #if defined(MBEDTLS_HAVE_TIME) |
| 9913 | if( 8 > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9914 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9915 | |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9916 | start = ( (uint64_t) p[0] << 56 ) | |
| 9917 | ( (uint64_t) p[1] << 48 ) | |
| 9918 | ( (uint64_t) p[2] << 40 ) | |
| 9919 | ( (uint64_t) p[3] << 32 ) | |
| 9920 | ( (uint64_t) p[4] << 24 ) | |
| 9921 | ( (uint64_t) p[5] << 16 ) | |
| 9922 | ( (uint64_t) p[6] << 8 ) | |
| 9923 | ( (uint64_t) p[7] ); |
| 9924 | p += 8; |
| 9925 | |
| 9926 | session->start = (time_t) start; |
| 9927 | #endif /* MBEDTLS_HAVE_TIME */ |
| 9928 | |
| 9929 | /* |
| 9930 | * Basic mandatory fields |
| 9931 | */ |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 9932 | |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9933 | if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
| 9934 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9935 | |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 9936 | ciphersuite = ( p[0] << 8 ) | p[1]; |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9937 | p += 2; |
| 9938 | |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 9939 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 9940 | session->ciphersuite = ciphersuite; |
| 9941 | #else |
| 9942 | if( ciphersuite != |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 9943 | MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) ) |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 9944 | { |
| 9945 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 9946 | } |
| 9947 | #endif |
| 9948 | |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9949 | session->compression = *p++; |
| 9950 | |
| 9951 | session->id_len = *p++; |
| 9952 | memcpy( session->id, p, 32 ); |
| 9953 | p += 32; |
| 9954 | |
| 9955 | memcpy( session->master, p, 48 ); |
| 9956 | p += 48; |
| 9957 | |
| 9958 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 9959 | ( (uint32_t) p[1] << 16 ) | |
| 9960 | ( (uint32_t) p[2] << 8 ) | |
| 9961 | ( (uint32_t) p[3] ); |
| 9962 | p += 4; |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9963 | |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 9964 | /* Immediately clear invalid pointer values that have been read, in case |
| 9965 | * we exit early before we replaced them with valid ones. */ |
| 9966 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 9967 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 9968 | session->peer_cert = NULL; |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 9969 | #elif defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 9970 | session->peer_cert_digest = NULL; |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 9971 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 9972 | #endif |
| 9973 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 9974 | session->ticket = NULL; |
| 9975 | #endif |
| 9976 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9977 | /* |
| 9978 | * Peer certificate |
| 9979 | */ |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9980 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 9981 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9982 | if( 3 > (size_t)( end - p ) ) |
| 9983 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9984 | |
| 9985 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 9986 | p += 3; |
| 9987 | |
| 9988 | if( cert_len == 0 ) |
| 9989 | { |
| 9990 | session->peer_cert = NULL; |
| 9991 | } |
| 9992 | else |
| 9993 | { |
| 9994 | int ret; |
| 9995 | |
| 9996 | if( cert_len > (size_t)( end - p ) ) |
| 9997 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9998 | |
| 9999 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 10000 | |
| 10001 | if( session->peer_cert == NULL ) |
| 10002 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 10003 | |
| 10004 | mbedtls_x509_crt_init( session->peer_cert ); |
| 10005 | |
| 10006 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 10007 | p, cert_len ) ) != 0 ) |
| 10008 | { |
| 10009 | mbedtls_x509_crt_free( session->peer_cert ); |
| 10010 | mbedtls_free( session->peer_cert ); |
| 10011 | session->peer_cert = NULL; |
| 10012 | return( ret ); |
| 10013 | } |
| 10014 | |
| 10015 | p += cert_len; |
| 10016 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 10017 | #elif defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 10018 | /* Deserialize CRT digest from the end of the ticket. */ |
| 10019 | if( 2 > (size_t)( end - p ) ) |
| 10020 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10021 | |
| 10022 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 10023 | session->peer_cert_digest_len = (size_t) *p++; |
| 10024 | |
| 10025 | if( session->peer_cert_digest_len != 0 ) |
| 10026 | { |
Hanno Becker | 2326d20 | 2019-06-06 14:54:55 +0100 | [diff] [blame] | 10027 | const mbedtls_md_info_t *md_info = |
| 10028 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); |
| 10029 | if( md_info == NULL ) |
| 10030 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10031 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) |
| 10032 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10033 | |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 10034 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) |
| 10035 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10036 | |
| 10037 | session->peer_cert_digest = |
| 10038 | mbedtls_calloc( 1, session->peer_cert_digest_len ); |
| 10039 | if( session->peer_cert_digest == NULL ) |
| 10040 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 10041 | |
| 10042 | memcpy( session->peer_cert_digest, p, |
| 10043 | session->peer_cert_digest_len ); |
| 10044 | p += session->peer_cert_digest_len; |
| 10045 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 10046 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10047 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 10048 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10049 | /* |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10050 | * Session ticket and associated data |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10051 | */ |
| 10052 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 10053 | if( 3 > (size_t)( end - p ) ) |
| 10054 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10055 | |
| 10056 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 10057 | p += 3; |
| 10058 | |
| 10059 | if( session->ticket_len != 0 ) |
| 10060 | { |
| 10061 | if( session->ticket_len > (size_t)( end - p ) ) |
| 10062 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10063 | |
| 10064 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 10065 | if( session->ticket == NULL ) |
| 10066 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 10067 | |
| 10068 | memcpy( session->ticket, p, session->ticket_len ); |
| 10069 | p += session->ticket_len; |
| 10070 | } |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10071 | |
| 10072 | if( 4 > (size_t)( end - p ) ) |
| 10073 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10074 | |
| 10075 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 10076 | ( (uint32_t) p[1] << 16 ) | |
| 10077 | ( (uint32_t) p[2] << 8 ) | |
| 10078 | ( (uint32_t) p[3] ); |
| 10079 | p += 4; |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10080 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 10081 | |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10082 | /* |
| 10083 | * Misc extension-related info |
| 10084 | */ |
| 10085 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 10086 | if( 1 > (size_t)( end - p ) ) |
| 10087 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10088 | |
| 10089 | session->mfl_code = *p++; |
| 10090 | #endif |
| 10091 | |
| 10092 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 10093 | if( 1 > (size_t)( end - p ) ) |
| 10094 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10095 | |
| 10096 | session->trunc_hmac = *p++; |
| 10097 | #endif |
| 10098 | |
| 10099 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 10100 | if( 1 > (size_t)( end - p ) ) |
| 10101 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10102 | |
| 10103 | session->encrypt_then_mac = *p++; |
| 10104 | #endif |
| 10105 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10106 | /* Done, should have consumed entire buffer */ |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10107 | if( p != end ) |
| 10108 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10109 | |
| 10110 | return( 0 ); |
| 10111 | } |
| 10112 | |
| 10113 | /* |
Manuel Pégourié-Gonnard | 8175816 | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 10114 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 10115 | */ |
| 10116 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 10117 | const unsigned char *buf, |
| 10118 | size_t len ) |
| 10119 | { |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 10120 | int ret = ssl_session_load( session, 0, buf, len ); |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 10121 | |
| 10122 | if( ret != 0 ) |
| 10123 | mbedtls_ssl_session_free( session ); |
| 10124 | |
| 10125 | return( ret ); |
| 10126 | } |
| 10127 | |
| 10128 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10129 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10130 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10131 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10132 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10133 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10134 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10135 | if( ssl == NULL || ssl->conf == NULL ) |
| 10136 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10137 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10138 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10139 | 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] | 10140 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10141 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10142 | #if defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10143 | 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] | 10144 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10145 | #endif |
| 10146 | |
Hanno Becker | b82350b | 2019-07-26 07:24:05 +0100 | [diff] [blame] | 10147 | ssl_send_pending_fatal_alert( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10148 | return( ret ); |
| 10149 | } |
| 10150 | |
| 10151 | /* |
| 10152 | * Perform the SSL handshake |
| 10153 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10154 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10155 | { |
| 10156 | int ret = 0; |
| 10157 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10158 | if( ssl == NULL || ssl->conf == NULL ) |
| 10159 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10160 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10161 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10162 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10163 | while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10164 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10165 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10166 | |
| 10167 | if( ret != 0 ) |
| 10168 | break; |
| 10169 | } |
| 10170 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10171 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10172 | |
| 10173 | return( ret ); |
| 10174 | } |
| 10175 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10176 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 10177 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10178 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10179 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10180 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10181 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10182 | { |
| 10183 | int ret; |
| 10184 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10185 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10186 | |
| 10187 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10188 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 10189 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10190 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 10191 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10192 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 10193 | 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] | 10194 | return( ret ); |
| 10195 | } |
| 10196 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10197 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10198 | |
| 10199 | return( 0 ); |
| 10200 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10201 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10202 | |
| 10203 | /* |
| 10204 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10205 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 10206 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 10207 | * - 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] | 10208 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10209 | * 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] | 10210 | * 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] | 10211 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10212 | static int ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10213 | { |
| 10214 | int ret; |
| 10215 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10216 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10217 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10218 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 10219 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10220 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 10221 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 10222 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10223 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 10224 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10225 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 10226 | { |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10227 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 10228 | MBEDTLS_SSL_IS_SERVER ) |
| 10229 | { |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 10230 | ssl->handshake->out_msg_seq = 1; |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10231 | } |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 10232 | else |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10233 | { |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 10234 | ssl->handshake->in_msg_seq = 1; |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10235 | } |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 10236 | } |
| 10237 | #endif |
| 10238 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10239 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 10240 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10241 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10242 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10243 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10244 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10245 | return( ret ); |
| 10246 | } |
| 10247 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10248 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10249 | |
| 10250 | return( 0 ); |
| 10251 | } |
| 10252 | |
| 10253 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10254 | * Renegotiate current connection on client, |
| 10255 | * or request renegotiation on server |
| 10256 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10257 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10258 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10259 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10260 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10261 | if( ssl == NULL || ssl->conf == NULL ) |
| 10262 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10263 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10264 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10265 | /* On server, just send the request */ |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10266 | 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] | 10267 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10268 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 10269 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10270 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10271 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 10272 | |
| 10273 | /* Did we already try/start sending HelloRequest? */ |
| 10274 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10275 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 10276 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10277 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10278 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10279 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10280 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10281 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10282 | /* |
| 10283 | * On client, either start the renegotiation process or, |
| 10284 | * if already in progress, continue the handshake |
| 10285 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10286 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10287 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10288 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 10289 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10290 | |
| 10291 | if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 ) |
| 10292 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10293 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10294 | return( ret ); |
| 10295 | } |
| 10296 | } |
| 10297 | else |
| 10298 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10299 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10300 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10301 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10302 | return( ret ); |
| 10303 | } |
| 10304 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10305 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10306 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 10307 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10308 | } |
| 10309 | |
| 10310 | /* |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10311 | * Check record counters and renegotiate if they're above the limit. |
| 10312 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10313 | static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10314 | { |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 10315 | size_t ep_len = ssl_ep_len( ssl ); |
| 10316 | int in_ctr_cmp; |
| 10317 | int out_ctr_cmp; |
| 10318 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10319 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER || |
| 10320 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING || |
Manuel Pégourié-Gonnard | 18332c5 | 2019-07-29 12:17:52 +0200 | [diff] [blame] | 10321 | ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10322 | { |
| 10323 | return( 0 ); |
| 10324 | } |
| 10325 | |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 10326 | in_ctr_cmp = memcmp( ssl->in_ctr + ep_len, |
| 10327 | ssl->conf->renego_period + ep_len, 8 - ep_len ); |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 10328 | out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len, |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 10329 | ssl->conf->renego_period + ep_len, 8 - ep_len ); |
| 10330 | |
| 10331 | if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10332 | { |
| 10333 | return( 0 ); |
| 10334 | } |
| 10335 | |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 10336 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10337 | return( mbedtls_ssl_renegotiate( ssl ) ); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10338 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10339 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10340 | |
| 10341 | /* |
| 10342 | * Receive application data decrypted from the SSL layer |
| 10343 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10344 | 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] | 10345 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10346 | int ret; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 10347 | size_t n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10348 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10349 | if( ssl == NULL || ssl->conf == NULL ) |
| 10350 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10351 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10352 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10353 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10354 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 10355 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 10356 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10357 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 10358 | return( ret ); |
| 10359 | |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10360 | if( ssl->handshake != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10361 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10362 | { |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 10363 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10364 | return( ret ); |
| 10365 | } |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 10366 | } |
| 10367 | #endif |
| 10368 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10369 | /* |
| 10370 | * Check if renegotiation is necessary and/or handshake is |
| 10371 | * in process. If yes, perform/continue, and fall through |
| 10372 | * if an unexpected packet is received while the client |
| 10373 | * is waiting for the ServerHello. |
| 10374 | * |
| 10375 | * (There is no equivalent to the last condition on |
| 10376 | * the server-side as it is not treated as within |
| 10377 | * a handshake while waiting for the ClientHello |
| 10378 | * after a renegotiation request.) |
| 10379 | */ |
| 10380 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10381 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10382 | ret = ssl_check_ctr_renegotiate( ssl ); |
| 10383 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 10384 | ret != 0 ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10385 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10386 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10387 | return( ret ); |
| 10388 | } |
| 10389 | #endif |
| 10390 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10391 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10392 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10393 | ret = mbedtls_ssl_handshake( ssl ); |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10394 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 10395 | ret != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10396 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10397 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10398 | return( ret ); |
| 10399 | } |
| 10400 | } |
| 10401 | |
Hanno Becker | e41158b | 2017-10-23 13:30:32 +0100 | [diff] [blame] | 10402 | /* Loop as long as no application data record is available */ |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10403 | while( ssl->in_offt == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10404 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 10405 | /* Start timer if not already running */ |
Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 10406 | if( mbedtls_ssl_get_get_timer( ssl ) != NULL && |
| 10407 | mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 ) |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 10408 | { |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 10409 | ssl_set_timer( ssl, |
| 10410 | mbedtls_ssl_conf_get_read_timeout( ssl->conf ) ); |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 10411 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 10412 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 10413 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10414 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10415 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) |
| 10416 | return( 0 ); |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 10417 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10418 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 10419 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10420 | } |
| 10421 | |
| 10422 | if( ssl->in_msglen == 0 && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10423 | ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10424 | { |
| 10425 | /* |
| 10426 | * OpenSSL sends empty messages to randomize the IV |
| 10427 | */ |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 10428 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10429 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10430 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 10431 | return( 0 ); |
| 10432 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10433 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10434 | return( ret ); |
| 10435 | } |
| 10436 | } |
| 10437 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10438 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10439 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10440 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10441 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10442 | /* |
| 10443 | * - For client-side, expect SERVER_HELLO_REQUEST. |
| 10444 | * - For server-side, expect CLIENT_HELLO. |
| 10445 | * - Fail (TLS) or silently drop record (DTLS) in other cases. |
| 10446 | */ |
| 10447 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10448 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10449 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 10450 | MBEDTLS_SSL_IS_CLIENT && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10451 | ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST || |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10452 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10453 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10454 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10455 | |
| 10456 | /* With DTLS, drop the packet (probably from last handshake) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10457 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 10458 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10459 | { |
| 10460 | continue; |
| 10461 | } |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 10462 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10463 | #endif |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 10464 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 10465 | { |
| 10466 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 10467 | } |
| 10468 | #endif |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10469 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10470 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10471 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10472 | #if defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10473 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 10474 | MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10475 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10476 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10477 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10478 | |
| 10479 | /* With DTLS, drop the packet (probably from last handshake) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10480 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 10481 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10482 | { |
| 10483 | continue; |
| 10484 | } |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 10485 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10486 | #endif |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 10487 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 10488 | { |
| 10489 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 10490 | } |
| 10491 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10492 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10493 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 10494 | |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 10495 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10496 | /* Determine whether renegotiation attempt should be accepted */ |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 10497 | if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
| 10498 | ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
Hanno Becker | b0b2b67 | 2019-06-12 16:58:10 +0100 | [diff] [blame] | 10499 | mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) == |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 10500 | MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) ) |
| 10501 | { |
| 10502 | /* |
| 10503 | * Accept renegotiation request |
| 10504 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10505 | |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 10506 | /* DTLS clients need to know renego is server-initiated */ |
| 10507 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 10508 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10509 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 10510 | MBEDTLS_SSL_IS_CLIENT ) |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 10511 | { |
| 10512 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
| 10513 | } |
| 10514 | #endif |
| 10515 | ret = ssl_start_renegotiation( ssl ); |
| 10516 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 10517 | ret != 0 ) |
| 10518 | { |
| 10519 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret ); |
| 10520 | return( ret ); |
| 10521 | } |
| 10522 | } |
| 10523 | else |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 10524 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10525 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10526 | /* |
| 10527 | * Refuse renegotiation |
| 10528 | */ |
| 10529 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10530 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10531 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10532 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 10533 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10534 | { |
Gilles Peskine | 92e4426 | 2017-05-10 17:27:49 +0200 | [diff] [blame] | 10535 | /* SSLv3 does not have a "no_renegotiation" warning, so |
| 10536 | we send a fatal alert and abort the connection. */ |
| 10537 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 10538 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 10539 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 10540 | } |
| 10541 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10542 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 10543 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 10544 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 10545 | if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 10546 | { |
Hanno Becker | 2e8d133 | 2019-07-25 10:27:36 +0100 | [diff] [blame] | 10547 | ret = mbedtls_ssl_send_alert_message( ssl, |
| 10548 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 10549 | MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ); |
| 10550 | if( ret != 0 ) |
| 10551 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10552 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 10553 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10554 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || |
| 10555 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 10556 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10557 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 10558 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 10559 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10560 | } |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 10561 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10562 | /* At this point, we don't know whether the renegotiation has been |
| 10563 | * completed or not. The cases to consider are the following: |
| 10564 | * 1) The renegotiation is complete. In this case, no new record |
| 10565 | * has been read yet. |
| 10566 | * 2) The renegotiation is incomplete because the client received |
| 10567 | * an application data record while awaiting the ServerHello. |
| 10568 | * 3) The renegotiation is incomplete because the client received |
| 10569 | * a non-handshake, non-application data message while awaiting |
| 10570 | * the ServerHello. |
| 10571 | * In each of these case, looping will be the proper action: |
| 10572 | * - For 1), the next iteration will read a new record and check |
| 10573 | * if it's application data. |
| 10574 | * - For 2), the loop condition isn't satisfied as application data |
| 10575 | * is present, hence continue is the same as break |
| 10576 | * - For 3), the loop condition is satisfied and read_record |
| 10577 | * will re-deliver the message that was held back by the client |
| 10578 | * when expecting the ServerHello. |
| 10579 | */ |
| 10580 | continue; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10581 | } |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 10582 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10583 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 10584 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10585 | if( ssl->conf->renego_max_records >= 0 ) |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 10586 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10587 | if( ++ssl->renego_records_seen > ssl->conf->renego_max_records ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10588 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10589 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, " |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10590 | "but not honored by client" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10591 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10592 | } |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 10593 | } |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 10594 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10595 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 10596 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10597 | /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */ |
| 10598 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 10599 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10600 | 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] | 10601 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 10602 | } |
| 10603 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10604 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10605 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10606 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) ); |
| 10607 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10608 | } |
| 10609 | |
| 10610 | ssl->in_offt = ssl->in_msg; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 10611 | |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 10612 | /* We're going to return something now, cancel timer, |
| 10613 | * except if handshake (renegotiation) is in progress */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10614 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 10615 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10616 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 10617 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10618 | /* If we requested renego but received AppData, resend HelloRequest. |
| 10619 | * Do it now, after setting in_offt, to avoid taking this branch |
| 10620 | * again if ssl_write_hello_request() returns WANT_WRITE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10621 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10622 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 10623 | MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10624 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10625 | { |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10626 | if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10627 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10628 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10629 | return( ret ); |
| 10630 | } |
| 10631 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10632 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10633 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10634 | } |
| 10635 | |
| 10636 | n = ( len < ssl->in_msglen ) |
| 10637 | ? len : ssl->in_msglen; |
| 10638 | |
| 10639 | memcpy( buf, ssl->in_offt, n ); |
| 10640 | ssl->in_msglen -= n; |
| 10641 | |
| 10642 | if( ssl->in_msglen == 0 ) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10643 | { |
| 10644 | /* all bytes consumed */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10645 | ssl->in_offt = NULL; |
Hanno Becker | bdf3905 | 2017-06-09 10:42:03 +0100 | [diff] [blame] | 10646 | ssl->keep_current_message = 0; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10647 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10648 | else |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10649 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10650 | /* more data available */ |
| 10651 | ssl->in_offt += n; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10652 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10653 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10654 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10655 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 10656 | return( (int) n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10657 | } |
| 10658 | |
| 10659 | /* |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 10660 | * Send application data to be encrypted by the SSL layer, taking care of max |
| 10661 | * fragment length and buffer size. |
| 10662 | * |
| 10663 | * According to RFC 5246 Section 6.2.1: |
| 10664 | * |
| 10665 | * Zero-length fragments of Application data MAY be sent as they are |
| 10666 | * potentially useful as a traffic analysis countermeasure. |
| 10667 | * |
| 10668 | * Therefore, it is possible that the input message length is 0 and the |
| 10669 | * corresponding return code is 0 on success. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10670 | */ |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10671 | static int ssl_write_real( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10672 | const unsigned char *buf, size_t len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10673 | { |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 10674 | int ret = mbedtls_ssl_get_max_out_record_payload( ssl ); |
| 10675 | const size_t max_len = (size_t) ret; |
| 10676 | |
| 10677 | if( ret < 0 ) |
| 10678 | { |
| 10679 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret ); |
| 10680 | return( ret ); |
| 10681 | } |
| 10682 | |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10683 | if( len > max_len ) |
| 10684 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10685 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 10686 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10687 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10688 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) " |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10689 | "maximum fragment length: %d > %d", |
| 10690 | len, max_len ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10691 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10692 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 10693 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10694 | #endif |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 10695 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 10696 | { |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10697 | len = max_len; |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 10698 | } |
| 10699 | #endif |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10700 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10701 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10702 | if( ssl->out_left != 0 ) |
| 10703 | { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 10704 | /* |
| 10705 | * The user has previously tried to send the data and |
| 10706 | * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially |
| 10707 | * written. In this case, we expect the high-level write function |
| 10708 | * (e.g. mbedtls_ssl_write()) to be called with the same parameters |
| 10709 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10710 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10711 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10712 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10713 | return( ret ); |
| 10714 | } |
| 10715 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10716 | else |
Paul Bakker | 1fd00bf | 2011-03-14 20:50:15 +0000 | [diff] [blame] | 10717 | { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 10718 | /* |
| 10719 | * The user is trying to send a message the first time, so we need to |
| 10720 | * copy the data into the internal buffers and setup the data structure |
| 10721 | * to keep track of partial writes |
| 10722 | */ |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10723 | ssl->out_msglen = len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10724 | ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10725 | memcpy( ssl->out_msg, buf, len ); |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10726 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 10727 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10728 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10729 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10730 | return( ret ); |
| 10731 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10732 | } |
| 10733 | |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10734 | return( (int) len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10735 | } |
| 10736 | |
| 10737 | /* |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10738 | * Write application data, doing 1/n-1 splitting if necessary. |
| 10739 | * |
| 10740 | * 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] | 10741 | * then the caller will call us again with the same arguments, so |
Hanno Becker | 2b187c4 | 2017-09-18 14:58:11 +0100 | [diff] [blame] | 10742 | * remember whether we already did the split or not. |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10743 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10744 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10745 | static int ssl_write_split( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10746 | const unsigned char *buf, size_t len ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10747 | { |
| 10748 | int ret; |
| 10749 | |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 10750 | if( ssl->conf->cbc_record_splitting == |
| 10751 | MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED || |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 10752 | len <= 1 || |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 10753 | 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] | 10754 | mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc ) |
| 10755 | != MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10756 | { |
| 10757 | return( ssl_write_real( ssl, buf, len ) ); |
| 10758 | } |
| 10759 | |
| 10760 | if( ssl->split_done == 0 ) |
| 10761 | { |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 10762 | if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10763 | return( ret ); |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 10764 | ssl->split_done = 1; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10765 | } |
| 10766 | |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 10767 | if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 ) |
| 10768 | return( ret ); |
| 10769 | ssl->split_done = 0; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10770 | |
| 10771 | return( ret + 1 ); |
| 10772 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10773 | #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */ |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10774 | |
| 10775 | /* |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10776 | * Write application data (public-facing wrapper) |
| 10777 | */ |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10778 | 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] | 10779 | { |
| 10780 | int ret; |
| 10781 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10782 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10783 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10784 | if( ssl == NULL || ssl->conf == NULL ) |
| 10785 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10786 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10787 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10788 | if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 ) |
| 10789 | { |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10790 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10791 | return( ret ); |
| 10792 | } |
| 10793 | #endif |
| 10794 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10795 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10796 | { |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10797 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10798 | { |
Manuel Pégourié-Gonnard | 151dc77 | 2015-05-14 13:55:51 +0200 | [diff] [blame] | 10799 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10800 | return( ret ); |
| 10801 | } |
| 10802 | } |
| 10803 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10804 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10805 | ret = ssl_write_split( ssl, buf, len ); |
| 10806 | #else |
| 10807 | ret = ssl_write_real( ssl, buf, len ); |
| 10808 | #endif |
| 10809 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10810 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10811 | |
| 10812 | return( ret ); |
| 10813 | } |
| 10814 | |
| 10815 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10816 | * Notify the peer that the connection is being closed |
| 10817 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10818 | int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10819 | { |
| 10820 | int ret; |
| 10821 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10822 | if( ssl == NULL || ssl->conf == NULL ) |
| 10823 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10824 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10825 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10826 | |
Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 10827 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10828 | return( mbedtls_ssl_flush_output( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10829 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10830 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10831 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10832 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 10833 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 10834 | MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10835 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10836 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10837 | return( ret ); |
| 10838 | } |
| 10839 | } |
| 10840 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10841 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10842 | |
Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 10843 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10844 | } |
| 10845 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10846 | void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10847 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 10848 | if( transform == NULL ) |
| 10849 | return; |
| 10850 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10851 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10852 | deflateEnd( &transform->ctx_deflate ); |
| 10853 | inflateEnd( &transform->ctx_inflate ); |
| 10854 | #endif |
| 10855 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10856 | mbedtls_cipher_free( &transform->cipher_ctx_enc ); |
| 10857 | mbedtls_cipher_free( &transform->cipher_ctx_dec ); |
Manuel Pégourié-Gonnard | f71e587 | 2013-09-23 17:12:43 +0200 | [diff] [blame] | 10858 | |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 10859 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10860 | mbedtls_md_free( &transform->md_ctx_enc ); |
| 10861 | mbedtls_md_free( &transform->md_ctx_dec ); |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 10862 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 10863 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10864 | mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10865 | } |
| 10866 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10867 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10868 | 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] | 10869 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10870 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 10871 | |
| 10872 | while( cur != NULL ) |
| 10873 | { |
| 10874 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10875 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 10876 | cur = next; |
| 10877 | } |
| 10878 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10879 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 10880 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10881 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 10882 | |
| 10883 | static void ssl_buffering_free( mbedtls_ssl_context *ssl ) |
| 10884 | { |
| 10885 | unsigned offset; |
| 10886 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 10887 | |
| 10888 | if( hs == NULL ) |
| 10889 | return; |
| 10890 | |
Hanno Becker | 283f5ef | 2018-08-24 09:34:47 +0100 | [diff] [blame] | 10891 | ssl_free_buffered_record( ssl ); |
| 10892 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10893 | for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ ) |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 10894 | ssl_buffering_free_slot( ssl, offset ); |
| 10895 | } |
| 10896 | |
| 10897 | static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl, |
| 10898 | uint8_t slot ) |
| 10899 | { |
| 10900 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 10901 | mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot]; |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 10902 | |
| 10903 | if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS ) |
| 10904 | return; |
| 10905 | |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 10906 | if( hs_buf->is_valid == 1 ) |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10907 | { |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 10908 | hs->buffering.total_bytes_buffered -= hs_buf->data_len; |
Hanno Becker | 805f2e1 | 2018-10-12 16:31:41 +0100 | [diff] [blame] | 10909 | mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len ); |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 10910 | mbedtls_free( hs_buf->data ); |
| 10911 | memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10912 | } |
| 10913 | } |
| 10914 | |
| 10915 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 10916 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 10917 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10918 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 10919 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 10920 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 10921 | if( handshake == NULL ) |
| 10922 | return; |
| 10923 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 10924 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 10925 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 10926 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 10927 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 10928 | handshake->async_in_progress = 0; |
| 10929 | } |
| 10930 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 10931 | |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 10932 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 10933 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 10934 | mbedtls_md5_free( &handshake->fin_md5 ); |
| 10935 | mbedtls_sha1_free( &handshake->fin_sha1 ); |
| 10936 | #endif |
| 10937 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 10938 | #if defined(MBEDTLS_SHA256_C) |
| 10939 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 10940 | #endif |
| 10941 | #if defined(MBEDTLS_SHA512_C) |
| 10942 | mbedtls_sha512_free( &handshake->fin_sha512 ); |
| 10943 | #endif |
| 10944 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 10945 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10946 | #if defined(MBEDTLS_DHM_C) |
| 10947 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10948 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10949 | #if defined(MBEDTLS_ECDH_C) |
| 10950 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 10951 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 10952 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 10953 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 10954 | #if defined(MBEDTLS_SSL_CLI_C) |
| 10955 | mbedtls_free( handshake->ecjpake_cache ); |
| 10956 | handshake->ecjpake_cache = NULL; |
| 10957 | handshake->ecjpake_cache_len = 0; |
| 10958 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 10959 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 10960 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 10961 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 10962 | if( handshake->psk != NULL ) |
| 10963 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10964 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 10965 | mbedtls_free( handshake->psk ); |
| 10966 | } |
| 10967 | #endif |
| 10968 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10969 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 10970 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 10971 | /* |
| 10972 | * Free only the linked list wrapper, not the keys themselves |
| 10973 | * since the belong to the SNI callback |
| 10974 | */ |
| 10975 | if( handshake->sni_key_cert != NULL ) |
| 10976 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10977 | mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next; |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 10978 | |
| 10979 | while( cur != NULL ) |
| 10980 | { |
| 10981 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10982 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 10983 | cur = next; |
| 10984 | } |
| 10985 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10986 | #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] | 10987 | |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 10988 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 10989 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 10990 | if( handshake->ecrs_peer_cert != NULL ) |
| 10991 | { |
| 10992 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 10993 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 10994 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 10995 | #endif |
| 10996 | |
Hanno Becker | 3bf8cdf | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 10997 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 10998 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 10999 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 11000 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 11001 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11002 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11003 | mbedtls_free( handshake->verify_cookie ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 11004 | ssl_flight_free( handshake->flight ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 11005 | ssl_buffering_free( ssl ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 11006 | #endif |
| 11007 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11008 | mbedtls_platform_zeroize( handshake, |
| 11009 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11010 | } |
| 11011 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11012 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11013 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 11014 | if( session == NULL ) |
| 11015 | return; |
| 11016 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11017 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 2214159 | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 11018 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 11019 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 11020 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 11021 | #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] | 11022 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 11023 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 11024 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11025 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11026 | } |
| 11027 | |
Manuel Pégourié-Gonnard | 4c1d06e | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 11028 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | a7cd483 | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 11029 | |
| 11030 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 11031 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 11032 | #else |
| 11033 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 11034 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 11035 | |
| 11036 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
| 11037 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
| 11038 | #else |
| 11039 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u |
| 11040 | #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */ |
| 11041 | |
| 11042 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 11043 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 11044 | #else |
| 11045 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 11046 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 11047 | |
| 11048 | #if defined(MBEDTLS_SSL_ALPN) |
| 11049 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 11050 | #else |
| 11051 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 11052 | #endif /* MBEDTLS_SSL_ALPN */ |
| 11053 | |
| 11054 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 11055 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 11056 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 11057 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 11058 | |
| 11059 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
| 11060 | ( (uint32_t) ( \ |
| 11061 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \ |
| 11062 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \ |
| 11063 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \ |
| 11064 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \ |
| 11065 | 0u ) ) |
| 11066 | |
Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11067 | static unsigned char ssl_serialized_context_header[] = { |
| 11068 | MBEDTLS_VERSION_MAJOR, |
| 11069 | MBEDTLS_VERSION_MINOR, |
| 11070 | MBEDTLS_VERSION_PATCH, |
| 11071 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF, |
| 11072 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF, |
Manuel Pégourié-Gonnard | a7cd483 | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 11073 | ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF, |
| 11074 | ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF, |
| 11075 | ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF, |
Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11076 | }; |
| 11077 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11078 | /* |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11079 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | b6163ef | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 11080 | * |
| 11081 | * The format of the serialized data is: |
| 11082 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 11083 | * |
| 11084 | * // header |
| 11085 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11086 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | b6163ef | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 11087 | * // the format of the remaining |
Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11088 | * // serialized data. |
Manuel Pégourié-Gonnard | a7cd483 | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 11089 | * Note: When updating the format, remember to keep these |
| 11090 | * 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] | 11091 | * |
| 11092 | * // session sub-structure |
| 11093 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 11094 | * // transform sub-structure |
| 11095 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 11096 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 11097 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 11098 | * // fields from ssl_context |
| 11099 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 11100 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 11101 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 11102 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 11103 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 11104 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 11105 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 11106 | * |
| 11107 | * Note that many fields of the ssl_context or sub-structures are not |
| 11108 | * serialized, as they fall in one of the following categories: |
| 11109 | * |
| 11110 | * 1. forced value (eg in_left must be 0) |
| 11111 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 11112 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 11113 | * 4. value was temporary (eg content of input buffer) |
| 11114 | * 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] | 11115 | */ |
| 11116 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 11117 | unsigned char *buf, |
| 11118 | size_t buf_len, |
| 11119 | size_t *olen ) |
| 11120 | { |
Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11121 | unsigned char *p = buf; |
| 11122 | size_t used = 0; |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11123 | size_t session_len; |
| 11124 | int ret = 0; |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11125 | |
Manuel Pégourié-Gonnard | 569ed6b | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 11126 | /* |
Manuel Pégourié-Gonnard | 69a3e41 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11127 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 11128 | * this function's documentation. |
| 11129 | * |
| 11130 | * These are due to assumptions/limitations in the implementation. Some of |
| 11131 | * them are likely to stay (no handshake in progress) some might go away |
| 11132 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 569ed6b | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 11133 | */ |
Manuel Pégourié-Gonnard | 69a3e41 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11134 | /* The initial handshake must be over */ |
| 11135 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | 569ed6b | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 11136 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 69a3e41 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11137 | if( ssl->handshake != NULL ) |
| 11138 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11139 | /* Double-check that sub-structures are indeed ready */ |
| 11140 | if( ssl->transform == NULL || ssl->session == NULL ) |
| 11141 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11142 | /* There must be no pending incoming or outgoing data */ |
| 11143 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) |
| 11144 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11145 | if( ssl->out_left != 0 ) |
| 11146 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11147 | /* Protocol must be DLTS, not TLS */ |
| 11148 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) |
| 11149 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11150 | /* Version must be 1.2 */ |
| 11151 | if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
| 11152 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11153 | if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 11154 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11155 | /* We must be using an AEAD ciphersuite */ |
| 11156 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) |
| 11157 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11158 | /* Renegotiation must not be enabled */ |
| 11159 | if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) ) |
| 11160 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11161 | |
Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11162 | /* |
| 11163 | * Version and format identifier |
| 11164 | */ |
| 11165 | used += sizeof( ssl_serialized_context_header ); |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11166 | |
Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11167 | if( used <= buf_len ) |
| 11168 | { |
| 11169 | memcpy( p, ssl_serialized_context_header, |
| 11170 | sizeof( ssl_serialized_context_header ) ); |
| 11171 | p += sizeof( ssl_serialized_context_header ); |
| 11172 | } |
| 11173 | |
| 11174 | /* |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11175 | * Session (length + data) |
| 11176 | */ |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 11177 | 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] | 11178 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
| 11179 | return( ret ); |
| 11180 | |
| 11181 | used += 4 + session_len; |
| 11182 | if( used <= buf_len ) |
| 11183 | { |
| 11184 | *p++ = (unsigned char)( ( session_len >> 24 ) & 0xFF ); |
| 11185 | *p++ = (unsigned char)( ( session_len >> 16 ) & 0xFF ); |
| 11186 | *p++ = (unsigned char)( ( session_len >> 8 ) & 0xFF ); |
| 11187 | *p++ = (unsigned char)( ( session_len ) & 0xFF ); |
| 11188 | |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 11189 | ret = ssl_session_save( ssl->session, 1, |
| 11190 | p, session_len, &session_len ); |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11191 | if( ret != 0 ) |
| 11192 | return( ret ); |
| 11193 | |
| 11194 | p += session_len; |
| 11195 | } |
| 11196 | |
| 11197 | /* |
Manuel Pégourié-Gonnard | 322f3c7 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11198 | * Transform |
| 11199 | */ |
| 11200 | used += sizeof( ssl->transform->randbytes ); |
| 11201 | if( used <= buf_len ) |
| 11202 | { |
| 11203 | memcpy( p, ssl->transform->randbytes, |
| 11204 | sizeof( ssl->transform->randbytes ) ); |
| 11205 | p += sizeof( ssl->transform->randbytes ); |
| 11206 | } |
| 11207 | |
| 11208 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 11209 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
| 11210 | if( used <= buf_len ) |
| 11211 | { |
| 11212 | *p++ = ssl->transform->in_cid_len; |
| 11213 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); |
| 11214 | p += ssl->transform->in_cid_len; |
| 11215 | |
| 11216 | *p++ = ssl->transform->out_cid_len; |
| 11217 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); |
| 11218 | p += ssl->transform->out_cid_len; |
| 11219 | } |
| 11220 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 11221 | |
| 11222 | /* |
Manuel Pégourié-Gonnard | 16d1485 | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 11223 | * Saved fields from top-level ssl_context structure |
| 11224 | */ |
| 11225 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
| 11226 | used += 4; |
| 11227 | if( used <= buf_len ) |
| 11228 | { |
| 11229 | *p++ = (unsigned char)( ( ssl->badmac_seen >> 24 ) & 0xFF ); |
| 11230 | *p++ = (unsigned char)( ( ssl->badmac_seen >> 16 ) & 0xFF ); |
| 11231 | *p++ = (unsigned char)( ( ssl->badmac_seen >> 8 ) & 0xFF ); |
| 11232 | *p++ = (unsigned char)( ( ssl->badmac_seen ) & 0xFF ); |
| 11233 | } |
| 11234 | #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */ |
| 11235 | |
| 11236 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 11237 | used += 16; |
| 11238 | if( used <= buf_len ) |
| 11239 | { |
| 11240 | *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF ); |
| 11241 | *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF ); |
| 11242 | *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF ); |
| 11243 | *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF ); |
| 11244 | *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF ); |
| 11245 | *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF ); |
| 11246 | *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF ); |
| 11247 | *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF ); |
| 11248 | |
| 11249 | *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF ); |
| 11250 | *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF ); |
| 11251 | *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF ); |
| 11252 | *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF ); |
| 11253 | *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF ); |
| 11254 | *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF ); |
| 11255 | *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF ); |
| 11256 | *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF ); |
| 11257 | } |
| 11258 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 11259 | |
| 11260 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11261 | used += 1; |
| 11262 | if( used <= buf_len ) |
| 11263 | { |
| 11264 | *p++ = ssl->disable_datagram_packing; |
| 11265 | } |
| 11266 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 11267 | |
| 11268 | used += 8; |
| 11269 | if( used <= buf_len ) |
| 11270 | { |
| 11271 | memcpy( p, ssl->cur_out_ctr, 8 ); |
| 11272 | p += 8; |
| 11273 | } |
| 11274 | |
| 11275 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11276 | used += 2; |
| 11277 | if( used <= buf_len ) |
| 11278 | { |
| 11279 | *p++ = (unsigned char)( ( ssl->mtu >> 8 ) & 0xFF ); |
| 11280 | *p++ = (unsigned char)( ( ssl->mtu ) & 0xFF ); |
| 11281 | } |
| 11282 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 11283 | |
| 11284 | #if defined(MBEDTLS_SSL_ALPN) |
| 11285 | { |
| 11286 | const uint8_t alpn_len = ssl->alpn_chosen |
Manuel Pégourié-Gonnard | 7af7375 | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 11287 | ? (uint8_t) strlen( ssl->alpn_chosen ) |
Manuel Pégourié-Gonnard | 16d1485 | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 11288 | : 0; |
| 11289 | |
| 11290 | used += 1 + alpn_len; |
| 11291 | if( used <= buf_len ) |
| 11292 | { |
| 11293 | *p++ = alpn_len; |
| 11294 | |
| 11295 | if( ssl->alpn_chosen != NULL ) |
| 11296 | { |
| 11297 | memcpy( p, ssl->alpn_chosen, alpn_len ); |
| 11298 | p += alpn_len; |
| 11299 | } |
| 11300 | } |
| 11301 | } |
| 11302 | #endif /* MBEDTLS_SSL_ALPN */ |
| 11303 | |
| 11304 | /* |
Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11305 | * Done |
| 11306 | */ |
| 11307 | *olen = used; |
| 11308 | |
| 11309 | if( used > buf_len ) |
| 11310 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11311 | |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11312 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); |
| 11313 | |
Manuel Pégourié-Gonnard | bc847ca | 2019-07-23 14:51:09 +0200 | [diff] [blame] | 11314 | return( ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11315 | } |
| 11316 | |
| 11317 | /* |
Manuel Pégourié-Gonnard | 322f3c7 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11318 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 11319 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 11320 | */ |
| 11321 | typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen, |
| 11322 | const char *label, |
| 11323 | const unsigned char *random, size_t rlen, |
| 11324 | unsigned char *dstbuf, size_t dlen ); |
| 11325 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) |
| 11326 | { |
| 11327 | mbedtls_ssl_ciphersuite_handle_t const info = |
| 11328 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); |
| 11329 | const mbedtls_md_type_t hash = mbedtls_ssl_suite_get_mac( info ); |
| 11330 | |
Manuel Pégourié-Gonnard | 2cc9223 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 11331 | #if defined(MBEDTLS_SHA512_C) |
| 11332 | if( hash == MBEDTLS_MD_SHA384 ) |
| 11333 | return( tls_prf_sha384 ); |
| 11334 | #else |
| 11335 | (void) hash; |
| 11336 | #endif |
| 11337 | return( tls_prf_sha256 ); |
Manuel Pégourié-Gonnard | 322f3c7 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11338 | } |
| 11339 | |
| 11340 | /* |
Manuel Pégourié-Gonnard | 8175816 | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 11341 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11342 | * |
| 11343 | * This internal version is wrapped by a public function that cleans up in |
| 11344 | * case of error. |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11345 | */ |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11346 | static int ssl_context_load( mbedtls_ssl_context *ssl, |
| 11347 | const unsigned char *buf, |
| 11348 | size_t len ) |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11349 | { |
Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11350 | const unsigned char *p = buf; |
| 11351 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11352 | size_t session_len; |
| 11353 | int ret; |
Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11354 | |
Manuel Pégourié-Gonnard | 5e534ba | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 11355 | /* |
| 11356 | * The context should have been freshly setup or reset. |
| 11357 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 14e2a8a | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 11358 | * (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] | 11359 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 11360 | */ |
| 11361 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 11362 | ssl->session != NULL ) |
| 11363 | { |
| 11364 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11365 | } |
| 11366 | |
| 11367 | /* |
| 11368 | * We can't check that the config matches the initial one, but we can at |
| 11369 | * least check it matches the requirements for serializing. |
| 11370 | */ |
| 11371 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) || |
Manuel Pégourié-Gonnard | 73a4636 | 2019-07-23 15:16:19 +0200 | [diff] [blame] | 11372 | mbedtls_ssl_conf_get_max_major_ver( ssl->conf ) < |
| 11373 | MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 11374 | mbedtls_ssl_conf_get_min_major_ver( ssl->conf ) > |
| 11375 | MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 11376 | mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) < |
| 11377 | MBEDTLS_SSL_MINOR_VERSION_3 || |
| 11378 | mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) > |
| 11379 | MBEDTLS_SSL_MINOR_VERSION_3 || |
Manuel Pégourié-Gonnard | 18332c5 | 2019-07-29 12:17:52 +0200 | [diff] [blame] | 11380 | mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) ) |
Manuel Pégourié-Gonnard | 5e534ba | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 11381 | { |
| 11382 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11383 | } |
| 11384 | |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11385 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); |
| 11386 | |
Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11387 | /* |
| 11388 | * Check version identifier |
| 11389 | */ |
| 11390 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) |
| 11391 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11392 | |
| 11393 | if( memcmp( p, ssl_serialized_context_header, |
| 11394 | sizeof( ssl_serialized_context_header ) ) != 0 ) |
| 11395 | { |
| 11396 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 11397 | } |
| 11398 | p += sizeof( ssl_serialized_context_header ); |
| 11399 | |
| 11400 | /* |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11401 | * Session |
| 11402 | */ |
| 11403 | if( (size_t)( end - p ) < 4 ) |
| 11404 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11405 | |
| 11406 | session_len = ( (size_t) p[0] << 24 ) | |
| 11407 | ( (size_t) p[1] << 16 ) | |
| 11408 | ( (size_t) p[2] << 8 ) | |
| 11409 | ( (size_t) p[3] ); |
| 11410 | p += 4; |
| 11411 | |
Manuel Pégourié-Gonnard | ff22200 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 11412 | /* This has been allocated by ssl_handshake_init(), called by |
| 11413 | * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */ |
| 11414 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11415 | ssl->session_in = ssl->session; |
| 11416 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | ff22200 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 11417 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11418 | |
| 11419 | if( (size_t)( end - p ) < session_len ) |
| 11420 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11421 | |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 11422 | ret = ssl_session_load( ssl->session, 1, p, session_len ); |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11423 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 11424 | { |
| 11425 | mbedtls_ssl_session_free( ssl->session ); |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11426 | return( ret ); |
Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 11427 | } |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11428 | |
| 11429 | p += session_len; |
| 11430 | |
| 11431 | /* |
Manuel Pégourié-Gonnard | 322f3c7 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11432 | * Transform |
| 11433 | */ |
| 11434 | |
Manuel Pégourié-Gonnard | ff22200 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 11435 | /* This has been allocated by ssl_handshake_init(), called by |
| 11436 | * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */ |
| 11437 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | 322f3c7 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11438 | ssl->transform_in = ssl->transform; |
| 11439 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | ff22200 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 11440 | ssl->transform_negotiate = NULL; |
Manuel Pégourié-Gonnard | 322f3c7 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11441 | |
| 11442 | /* Read random bytes and populate structure */ |
| 11443 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) |
| 11444 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11445 | |
| 11446 | ret = ssl_populate_transform( ssl->transform, |
| 11447 | mbedtls_ssl_session_get_ciphersuite( ssl->session ), |
| 11448 | ssl->session->master, |
| 11449 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
| 11450 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 11451 | ssl->session->encrypt_then_mac, |
| 11452 | #endif |
| 11453 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 11454 | ssl->session->trunc_hmac, |
| 11455 | #endif |
| 11456 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
| 11457 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 11458 | ssl->session->compression, |
| 11459 | #endif |
| 11460 | ssl_tls12prf_from_cs( |
| 11461 | mbedtls_ssl_session_get_ciphersuite( ssl->session) ), |
| 11462 | p, /* currently pointing to randbytes */ |
| 11463 | MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */ |
| 11464 | mbedtls_ssl_conf_get_endpoint( ssl->conf ), |
| 11465 | ssl ); |
| 11466 | if( ret != 0 ) |
| 11467 | return( ret ); |
| 11468 | |
| 11469 | p += sizeof( ssl->transform->randbytes ); |
| 11470 | |
| 11471 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 11472 | /* Read connection IDs and store them */ |
| 11473 | if( (size_t)( end - p ) < 1 ) |
| 11474 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11475 | |
| 11476 | ssl->transform->in_cid_len = *p++; |
| 11477 | |
Manuel Pégourié-Gonnard | 2f3fa62 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 11478 | 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] | 11479 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11480 | |
| 11481 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); |
| 11482 | p += ssl->transform->in_cid_len; |
| 11483 | |
| 11484 | ssl->transform->out_cid_len = *p++; |
| 11485 | |
| 11486 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) |
| 11487 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11488 | |
| 11489 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); |
| 11490 | p += ssl->transform->out_cid_len; |
| 11491 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 11492 | |
| 11493 | /* |
Manuel Pégourié-Gonnard | 16d1485 | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 11494 | * Saved fields from top-level ssl_context structure |
| 11495 | */ |
| 11496 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
| 11497 | if( (size_t)( end - p ) < 4 ) |
| 11498 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11499 | |
| 11500 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | |
| 11501 | ( (uint32_t) p[1] << 16 ) | |
| 11502 | ( (uint32_t) p[2] << 8 ) | |
| 11503 | ( (uint32_t) p[3] ); |
| 11504 | p += 4; |
| 11505 | #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */ |
| 11506 | |
| 11507 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 11508 | if( (size_t)( end - p ) < 16 ) |
| 11509 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11510 | |
| 11511 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | |
| 11512 | ( (uint64_t) p[1] << 48 ) | |
| 11513 | ( (uint64_t) p[2] << 40 ) | |
| 11514 | ( (uint64_t) p[3] << 32 ) | |
| 11515 | ( (uint64_t) p[4] << 24 ) | |
| 11516 | ( (uint64_t) p[5] << 16 ) | |
| 11517 | ( (uint64_t) p[6] << 8 ) | |
| 11518 | ( (uint64_t) p[7] ); |
| 11519 | p += 8; |
| 11520 | |
| 11521 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | |
| 11522 | ( (uint64_t) p[1] << 48 ) | |
| 11523 | ( (uint64_t) p[2] << 40 ) | |
| 11524 | ( (uint64_t) p[3] << 32 ) | |
| 11525 | ( (uint64_t) p[4] << 24 ) | |
| 11526 | ( (uint64_t) p[5] << 16 ) | |
| 11527 | ( (uint64_t) p[6] << 8 ) | |
| 11528 | ( (uint64_t) p[7] ); |
| 11529 | p += 8; |
| 11530 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 11531 | |
| 11532 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11533 | if( (size_t)( end - p ) < 1 ) |
| 11534 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11535 | |
| 11536 | ssl->disable_datagram_packing = *p++; |
| 11537 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 11538 | |
| 11539 | if( (size_t)( end - p ) < 8 ) |
| 11540 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11541 | |
| 11542 | memcpy( ssl->cur_out_ctr, p, 8 ); |
| 11543 | p += 8; |
| 11544 | |
| 11545 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11546 | if( (size_t)( end - p ) < 2 ) |
| 11547 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11548 | |
| 11549 | ssl->mtu = ( p[0] << 8 ) | p[1]; |
| 11550 | p += 2; |
| 11551 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 11552 | |
| 11553 | #if defined(MBEDTLS_SSL_ALPN) |
| 11554 | { |
| 11555 | uint8_t alpn_len; |
| 11556 | const char **cur; |
| 11557 | |
| 11558 | if( (size_t)( end - p ) < 1 ) |
| 11559 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11560 | |
| 11561 | alpn_len = *p++; |
| 11562 | |
| 11563 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) |
| 11564 | { |
| 11565 | /* alpn_chosen should point to an item in the configured list */ |
| 11566 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 11567 | { |
| 11568 | if( strlen( *cur ) == alpn_len && |
| 11569 | memcmp( p, cur, alpn_len ) == 0 ) |
| 11570 | { |
| 11571 | ssl->alpn_chosen = *cur; |
| 11572 | break; |
| 11573 | } |
| 11574 | } |
| 11575 | } |
| 11576 | |
| 11577 | /* can only happen on conf mismatch */ |
| 11578 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) |
| 11579 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11580 | |
| 11581 | p += alpn_len; |
| 11582 | } |
| 11583 | #endif /* MBEDTLS_SSL_ALPN */ |
| 11584 | |
| 11585 | /* |
Manuel Pégourié-Gonnard | 138079d | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 11586 | * Forced fields from top-level ssl_context structure |
| 11587 | * |
| 11588 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 11589 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 11590 | */ |
| 11591 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
| 11592 | |
Manuel Pégourié-Gonnard | 2cc9223 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 11593 | #if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER) |
Manuel Pégourié-Gonnard | 138079d | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 11594 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
Manuel Pégourié-Gonnard | 2cc9223 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 11595 | #endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */ |
| 11596 | #if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER) |
Manuel Pégourié-Gonnard | 138079d | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 11597 | ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; |
Manuel Pégourié-Gonnard | 2cc9223 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 11598 | #endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */ |
Manuel Pégourié-Gonnard | 138079d | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 11599 | |
| 11600 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11601 | ssl->in_epoch = 1; |
| 11602 | #endif |
| 11603 | |
| 11604 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 11605 | * which we don't want - otherwise we'd end up freeing the wrong transform |
| 11606 | * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */ |
| 11607 | if( ssl->handshake != NULL ) |
| 11608 | { |
| 11609 | mbedtls_ssl_handshake_free( ssl ); |
| 11610 | mbedtls_free( ssl->handshake ); |
| 11611 | ssl->handshake = NULL; |
| 11612 | } |
| 11613 | |
| 11614 | /* |
Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11615 | * Done - should have consumed entire buffer |
| 11616 | */ |
| 11617 | if( p != end ) |
| 11618 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11619 | |
| 11620 | return( 0 ); |
| 11621 | } |
| 11622 | |
| 11623 | /* |
Manuel Pégourié-Gonnard | 8175816 | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 11624 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11625 | */ |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11626 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11627 | const unsigned char *buf, |
| 11628 | size_t len ) |
| 11629 | { |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11630 | int ret = ssl_context_load( context, buf, len ); |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11631 | |
Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11632 | if( ret != 0 ) |
| 11633 | mbedtls_ssl_free( context ); |
| 11634 | |
| 11635 | return( ret ); |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11636 | } |
Manuel Pégourié-Gonnard | 4c1d06e | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 11637 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11638 | |
| 11639 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11640 | * Free an SSL context |
| 11641 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11642 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11643 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 11644 | if( ssl == NULL ) |
| 11645 | return; |
| 11646 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11647 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11648 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 11649 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11650 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 11651 | 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] | 11652 | mbedtls_free( ssl->out_buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11653 | } |
| 11654 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 11655 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11656 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 11657 | 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] | 11658 | mbedtls_free( ssl->in_buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11659 | } |
| 11660 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11661 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 11662 | if( ssl->compress_buf != NULL ) |
| 11663 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 11664 | 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] | 11665 | mbedtls_free( ssl->compress_buf ); |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 11666 | } |
| 11667 | #endif |
| 11668 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11669 | if( ssl->transform ) |
| 11670 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11671 | mbedtls_ssl_transform_free( ssl->transform ); |
| 11672 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11673 | } |
| 11674 | |
| 11675 | if( ssl->handshake ) |
| 11676 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 11677 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11678 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 11679 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11680 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11681 | mbedtls_free( ssl->handshake ); |
| 11682 | mbedtls_free( ssl->transform_negotiate ); |
| 11683 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11684 | } |
| 11685 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 11686 | if( ssl->session ) |
| 11687 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11688 | mbedtls_ssl_session_free( ssl->session ); |
| 11689 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 11690 | } |
| 11691 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 11692 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 11693 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11694 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11695 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11696 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11697 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 11698 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11699 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11700 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 11701 | if( mbedtls_ssl_hw_record_finish != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 11702 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11703 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) ); |
| 11704 | mbedtls_ssl_hw_record_finish( ssl ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 11705 | } |
| 11706 | #endif |
| 11707 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 11708 | #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] | 11709 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 11710 | #endif |
| 11711 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11712 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 11713 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 11714 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11715 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11716 | } |
| 11717 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11718 | /* |
| 11719 | * Initialze mbedtls_ssl_config |
| 11720 | */ |
| 11721 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 11722 | { |
| 11723 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | e744eab | 2019-03-18 10:51:18 +0100 | [diff] [blame] | 11724 | |
| 11725 | #if !defined(MBEDTLS_SSL_PROTO_TLS) |
| 11726 | conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM; |
| 11727 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11728 | } |
| 11729 | |
Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 11730 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 11731 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 11732 | static int ssl_preset_default_hashes[] = { |
| 11733 | #if defined(MBEDTLS_SHA512_C) |
| 11734 | MBEDTLS_MD_SHA512, |
| 11735 | MBEDTLS_MD_SHA384, |
| 11736 | #endif |
| 11737 | #if defined(MBEDTLS_SHA256_C) |
| 11738 | MBEDTLS_MD_SHA256, |
| 11739 | MBEDTLS_MD_SHA224, |
| 11740 | #endif |
Gilles Peskine | 5d2511c | 2017-05-12 13:16:40 +0200 | [diff] [blame] | 11741 | #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] | 11742 | MBEDTLS_MD_SHA1, |
| 11743 | #endif |
| 11744 | MBEDTLS_MD_NONE |
| 11745 | }; |
Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 11746 | #endif |
Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 11747 | #endif |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 11748 | |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 11749 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11750 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 11751 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 11752 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 11753 | 0 |
| 11754 | }; |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 11755 | #endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11756 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 11757 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 11758 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11759 | static int ssl_preset_suiteb_hashes[] = { |
| 11760 | MBEDTLS_MD_SHA256, |
| 11761 | MBEDTLS_MD_SHA384, |
| 11762 | MBEDTLS_MD_NONE |
| 11763 | }; |
| 11764 | #endif |
Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 11765 | #endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11766 | |
Hanno Becker | c1096e7 | 2019-06-19 12:30:41 +0100 | [diff] [blame] | 11767 | #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] | 11768 | static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = { |
Jaeden Amero | 16529b2 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 11769 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11770 | MBEDTLS_ECP_DP_SECP256R1, |
Jaeden Amero | 16529b2 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 11771 | #endif |
| 11772 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11773 | MBEDTLS_ECP_DP_SECP384R1, |
Jaeden Amero | 16529b2 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 11774 | #endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11775 | MBEDTLS_ECP_DP_NONE |
| 11776 | }; |
| 11777 | #endif |
| 11778 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11779 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 11780 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11781 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 11782 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11783 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11784 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 11785 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11786 | int ret; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 11787 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11788 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 11789 | /* Use the functions here so that they are covered in tests, |
| 11790 | * but otherwise access member directly for efficiency */ |
| 11791 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 11792 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11793 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11794 | /* |
| 11795 | * Things that are common to all presets |
| 11796 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 11797 | #if defined(MBEDTLS_SSL_CLI_C) |
| 11798 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 11799 | { |
Hanno Becker | acd4fc0 | 2019-06-12 16:40:50 +0100 | [diff] [blame] | 11800 | #if !defined(MBEDTLS_SSL_CONF_AUTHMODE) |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 11801 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
Hanno Becker | acd4fc0 | 2019-06-12 16:40:50 +0100 | [diff] [blame] | 11802 | #endif /* !MBEDTLS_SSL_CONF_AUTHMODE */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 11803 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 11804 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 11805 | #endif |
| 11806 | } |
| 11807 | #endif |
| 11808 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 11809 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11810 | conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED; |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 11811 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11812 | |
| 11813 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 11814 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 11815 | #endif |
| 11816 | |
| 11817 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Hanno Becker | aabbb58 | 2019-06-11 13:43:27 +0100 | [diff] [blame] | 11818 | #if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11819 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
Hanno Becker | aabbb58 | 2019-06-11 13:43:27 +0100 | [diff] [blame] | 11820 | #endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */ |
| 11821 | #if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET) |
Jarno Lamsa | d9382f8 | 2019-06-10 10:27:14 +0300 | [diff] [blame] | 11822 | conf->enforce_extended_master_secret = |
Jarno Lamsa | 18b9a49 | 2019-06-10 15:23:29 +0300 | [diff] [blame] | 11823 | MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED; |
Hanno Becker | aabbb58 | 2019-06-11 13:43:27 +0100 | [diff] [blame] | 11824 | #endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11825 | #endif |
| 11826 | |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 11827 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
| 11828 | conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED; |
| 11829 | #endif |
| 11830 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 11831 | #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] | 11832 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 11833 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 11834 | #endif |
| 11835 | |
Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 11836 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \ |
| 11837 | !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11838 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 11839 | #endif |
| 11840 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 11841 | #if defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | c2cfdaa | 2019-06-13 12:33:03 +0100 | [diff] [blame] | 11842 | #if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST) |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 11843 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
Hanno Becker | c2cfdaa | 2019-06-13 12:33:03 +0100 | [diff] [blame] | 11844 | #endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */ |
| 11845 | #endif /* MBEDTLS_SSL_SRV_C */ |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 11846 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11847 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 11848 | #if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11849 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 11850 | #endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */ |
| 11851 | #if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11852 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 11853 | #endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */ |
| 11854 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11855 | |
| 11856 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 11857 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 11858 | memset( conf->renego_period, 0x00, 2 ); |
| 11859 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11860 | #endif |
| 11861 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11862 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
| 11863 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 11864 | { |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 11865 | const unsigned char dhm_p[] = |
| 11866 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 11867 | const unsigned char dhm_g[] = |
| 11868 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
| 11869 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 11870 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 11871 | dhm_p, sizeof( dhm_p ), |
| 11872 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11873 | { |
| 11874 | return( ret ); |
| 11875 | } |
| 11876 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 11877 | #endif |
| 11878 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11879 | /* |
| 11880 | * Preset-specific defaults |
| 11881 | */ |
| 11882 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11883 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11884 | /* |
| 11885 | * NSA Suite B |
| 11886 | */ |
| 11887 | case MBEDTLS_SSL_PRESET_SUITEB: |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 11888 | #if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11889 | conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 11890 | #endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */ |
| 11891 | #if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11892 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 11893 | #endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */ |
| 11894 | #if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11895 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 11896 | #endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */ |
| 11897 | #if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11898 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 11899 | #endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11900 | |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 11901 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11902 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = |
| 11903 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = |
| 11904 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = |
| 11905 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = |
| 11906 | ssl_preset_suiteb_ciphersuites; |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 11907 | #endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11908 | |
| 11909 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 11910 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11911 | #endif |
| 11912 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 11913 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 11914 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11915 | conf->sig_hashes = ssl_preset_suiteb_hashes; |
| 11916 | #endif |
Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 11917 | #endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11918 | |
| 11919 | #if defined(MBEDTLS_ECP_C) |
Hanno Becker | c1096e7 | 2019-06-19 12:30:41 +0100 | [diff] [blame] | 11920 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_EC) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11921 | conf->curve_list = ssl_preset_suiteb_curves; |
| 11922 | #endif |
Hanno Becker | c1096e7 | 2019-06-19 12:30:41 +0100 | [diff] [blame] | 11923 | #endif |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 11924 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11925 | |
| 11926 | /* |
| 11927 | * Default |
| 11928 | */ |
| 11929 | default: |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 11930 | #if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER) |
Ron Eldor | 5e9f14d | 2017-05-28 10:46:38 +0300 | [diff] [blame] | 11931 | conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION > |
| 11932 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ? |
| 11933 | MBEDTLS_SSL_MIN_MAJOR_VERSION : |
| 11934 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION; |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 11935 | #endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */ |
| 11936 | #if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) |
Ron Eldor | 5e9f14d | 2017-05-28 10:46:38 +0300 | [diff] [blame] | 11937 | conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION > |
| 11938 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ? |
| 11939 | MBEDTLS_SSL_MIN_MINOR_VERSION : |
| 11940 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11941 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 11942 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) ) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11943 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2; |
| 11944 | #endif |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 11945 | #endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */ |
| 11946 | #if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER) |
| 11947 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 11948 | #endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */ |
| 11949 | #if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) |
| 11950 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 11951 | #endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11952 | |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 11953 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11954 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = |
| 11955 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = |
| 11956 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = |
| 11957 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = |
| 11958 | mbedtls_ssl_list_ciphersuites(); |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 11959 | #endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11960 | |
| 11961 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 11962 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 11963 | #endif |
| 11964 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 11965 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 11966 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 11967 | conf->sig_hashes = ssl_preset_default_hashes; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11968 | #endif |
Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 11969 | #endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11970 | |
| 11971 | #if defined(MBEDTLS_ECP_C) |
Hanno Becker | c1096e7 | 2019-06-19 12:30:41 +0100 | [diff] [blame] | 11972 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_EC) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11973 | conf->curve_list = mbedtls_ecp_grp_id_list(); |
| 11974 | #endif |
Hanno Becker | c1096e7 | 2019-06-19 12:30:41 +0100 | [diff] [blame] | 11975 | #endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11976 | |
| 11977 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 11978 | conf->dhm_min_bitlen = 1024; |
| 11979 | #endif |
| 11980 | } |
| 11981 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11982 | return( 0 ); |
| 11983 | } |
| 11984 | |
| 11985 | /* |
| 11986 | * Free mbedtls_ssl_config |
| 11987 | */ |
| 11988 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 11989 | { |
| 11990 | #if defined(MBEDTLS_DHM_C) |
| 11991 | mbedtls_mpi_free( &conf->dhm_P ); |
| 11992 | mbedtls_mpi_free( &conf->dhm_G ); |
| 11993 | #endif |
| 11994 | |
| 11995 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 11996 | if( conf->psk != NULL ) |
| 11997 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11998 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11999 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 12000 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12001 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 12002 | } |
| 12003 | |
| 12004 | if( conf->psk_identity != NULL ) |
| 12005 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 12006 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 12007 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 12008 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12009 | conf->psk_identity_len = 0; |
| 12010 | } |
| 12011 | #endif |
| 12012 | |
| 12013 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 12014 | ssl_key_cert_free( conf->key_cert ); |
| 12015 | #endif |
| 12016 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 12017 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12018 | } |
| 12019 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 12020 | #if defined(MBEDTLS_PK_C) && \ |
| 12021 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 12022 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12023 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 12024 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12025 | 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] | 12026 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12027 | #if defined(MBEDTLS_RSA_C) |
| 12028 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 12029 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 12030 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12031 | #if defined(MBEDTLS_ECDSA_C) |
| 12032 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 12033 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 12034 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12035 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 12036 | } |
| 12037 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 12038 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 12039 | { |
| 12040 | switch( type ) { |
| 12041 | case MBEDTLS_PK_RSA: |
| 12042 | return( MBEDTLS_SSL_SIG_RSA ); |
| 12043 | case MBEDTLS_PK_ECDSA: |
| 12044 | case MBEDTLS_PK_ECKEY: |
| 12045 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 12046 | default: |
| 12047 | return( MBEDTLS_SSL_SIG_ANON ); |
| 12048 | } |
| 12049 | } |
| 12050 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12051 | 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] | 12052 | { |
| 12053 | switch( sig ) |
| 12054 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12055 | #if defined(MBEDTLS_RSA_C) |
| 12056 | case MBEDTLS_SSL_SIG_RSA: |
| 12057 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12058 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12059 | #if defined(MBEDTLS_ECDSA_C) |
| 12060 | case MBEDTLS_SSL_SIG_ECDSA: |
| 12061 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12062 | #endif |
| 12063 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12064 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12065 | } |
| 12066 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 12067 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12068 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 12069 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 12070 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 12071 | |
| 12072 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ |
| 12073 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set, |
| 12074 | mbedtls_pk_type_t sig_alg ) |
| 12075 | { |
| 12076 | switch( sig_alg ) |
| 12077 | { |
| 12078 | case MBEDTLS_PK_RSA: |
| 12079 | return( set->rsa ); |
| 12080 | case MBEDTLS_PK_ECDSA: |
| 12081 | return( set->ecdsa ); |
| 12082 | default: |
| 12083 | return( MBEDTLS_MD_NONE ); |
| 12084 | } |
| 12085 | } |
| 12086 | |
| 12087 | /* Add a signature-hash-pair to a signature-hash set */ |
| 12088 | void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set, |
| 12089 | mbedtls_pk_type_t sig_alg, |
| 12090 | mbedtls_md_type_t md_alg ) |
| 12091 | { |
| 12092 | switch( sig_alg ) |
| 12093 | { |
| 12094 | case MBEDTLS_PK_RSA: |
| 12095 | if( set->rsa == MBEDTLS_MD_NONE ) |
| 12096 | set->rsa = md_alg; |
| 12097 | break; |
| 12098 | |
| 12099 | case MBEDTLS_PK_ECDSA: |
| 12100 | if( set->ecdsa == MBEDTLS_MD_NONE ) |
| 12101 | set->ecdsa = md_alg; |
| 12102 | break; |
| 12103 | |
| 12104 | default: |
| 12105 | break; |
| 12106 | } |
| 12107 | } |
| 12108 | |
| 12109 | /* Allow exactly one hash algorithm for each signature. */ |
| 12110 | void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set, |
| 12111 | mbedtls_md_type_t md_alg ) |
| 12112 | { |
| 12113 | set->rsa = md_alg; |
| 12114 | set->ecdsa = md_alg; |
| 12115 | } |
| 12116 | |
| 12117 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2) && |
| 12118 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
| 12119 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 12120 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 12121 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 12122 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12123 | 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] | 12124 | { |
| 12125 | switch( hash ) |
| 12126 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12127 | #if defined(MBEDTLS_MD5_C) |
| 12128 | case MBEDTLS_SSL_HASH_MD5: |
| 12129 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12130 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12131 | #if defined(MBEDTLS_SHA1_C) |
| 12132 | case MBEDTLS_SSL_HASH_SHA1: |
| 12133 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12134 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12135 | #if defined(MBEDTLS_SHA256_C) |
| 12136 | case MBEDTLS_SSL_HASH_SHA224: |
| 12137 | return( MBEDTLS_MD_SHA224 ); |
| 12138 | case MBEDTLS_SSL_HASH_SHA256: |
| 12139 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12140 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12141 | #if defined(MBEDTLS_SHA512_C) |
| 12142 | case MBEDTLS_SSL_HASH_SHA384: |
| 12143 | return( MBEDTLS_MD_SHA384 ); |
| 12144 | case MBEDTLS_SSL_HASH_SHA512: |
| 12145 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12146 | #endif |
| 12147 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12148 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12149 | } |
| 12150 | } |
| 12151 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 12152 | /* |
| 12153 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 12154 | */ |
| 12155 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 12156 | { |
| 12157 | switch( md ) |
| 12158 | { |
| 12159 | #if defined(MBEDTLS_MD5_C) |
| 12160 | case MBEDTLS_MD_MD5: |
| 12161 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 12162 | #endif |
| 12163 | #if defined(MBEDTLS_SHA1_C) |
| 12164 | case MBEDTLS_MD_SHA1: |
| 12165 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 12166 | #endif |
| 12167 | #if defined(MBEDTLS_SHA256_C) |
| 12168 | case MBEDTLS_MD_SHA224: |
| 12169 | return( MBEDTLS_SSL_HASH_SHA224 ); |
| 12170 | case MBEDTLS_MD_SHA256: |
| 12171 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 12172 | #endif |
| 12173 | #if defined(MBEDTLS_SHA512_C) |
| 12174 | case MBEDTLS_MD_SHA384: |
| 12175 | return( MBEDTLS_SSL_HASH_SHA384 ); |
| 12176 | case MBEDTLS_MD_SHA512: |
| 12177 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 12178 | #endif |
| 12179 | default: |
| 12180 | return( MBEDTLS_SSL_HASH_NONE ); |
| 12181 | } |
| 12182 | } |
| 12183 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 12184 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 12185 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 12186 | * 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] | 12187 | * 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] | 12188 | */ |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 12189 | 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] | 12190 | { |
Hanno Becker | a4a9c69 | 2019-06-18 16:55:47 +0100 | [diff] [blame] | 12191 | MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id ) |
| 12192 | if( own_ec_id == grp_id ) |
| 12193 | return( 0 ); |
| 12194 | MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 12195 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 12196 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 12197 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 12198 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12199 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 12200 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 12201 | /* |
| 12202 | * Check if a hash proposed by the peer is in our list. |
| 12203 | * Return 0 if we're willing to use it, -1 otherwise. |
| 12204 | */ |
| 12205 | int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl, |
| 12206 | mbedtls_md_type_t md ) |
| 12207 | { |
Hanno Becker | f1bc9e1 | 2019-06-19 16:23:21 +0100 | [diff] [blame] | 12208 | MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg ) |
| 12209 | if( md_alg == md ) |
| 12210 | return( 0 ); |
| 12211 | MBEDTLS_SSL_END_FOR_EACH_SIG_HASH |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 12212 | |
| 12213 | return( -1 ); |
| 12214 | } |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 12215 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 12216 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12217 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 12218 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 12219 | mbedtls_ssl_ciphersuite_handle_t ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12220 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 12221 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12222 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12223 | int ret = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12224 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12225 | int usage = 0; |
| 12226 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12227 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12228 | const char *ext_oid; |
| 12229 | size_t ext_len; |
| 12230 | #endif |
| 12231 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12232 | #if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \ |
| 12233 | !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12234 | ((void) cert); |
| 12235 | ((void) cert_endpoint); |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12236 | ((void) flags); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12237 | #endif |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12238 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12239 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
| 12240 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12241 | { |
| 12242 | /* Server part of the key exchange */ |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 12243 | switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12244 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12245 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 12246 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 12247 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12248 | break; |
| 12249 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12250 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 12251 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 12252 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 12253 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12254 | break; |
| 12255 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12256 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 12257 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 12258 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12259 | break; |
| 12260 | |
| 12261 | /* 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] | 12262 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 12263 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 12264 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 12265 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 12266 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12267 | usage = 0; |
| 12268 | } |
| 12269 | } |
| 12270 | else |
| 12271 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12272 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 12273 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12274 | } |
| 12275 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12276 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12277 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 12278 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12279 | ret = -1; |
| 12280 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12281 | #else |
| 12282 | ((void) ciphersuite); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12283 | #endif /* MBEDTLS_X509_CHECK_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12284 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12285 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
| 12286 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12287 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12288 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 12289 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12290 | } |
| 12291 | else |
| 12292 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12293 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 12294 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12295 | } |
| 12296 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12297 | 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] | 12298 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 12299 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12300 | ret = -1; |
| 12301 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12302 | #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12303 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12304 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12305 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12306 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 12307 | |
Hanno Becker | 0a64170 | 2019-06-24 11:19:58 +0100 | [diff] [blame] | 12308 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 12309 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 12310 | { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 12311 | switch( md ) |
| 12312 | { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 12313 | #if defined(MBEDTLS_SHA512_C) |
| 12314 | case MBEDTLS_SSL_HASH_SHA384: |
| 12315 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 12316 | break; |
| 12317 | #endif |
| 12318 | #if defined(MBEDTLS_SHA256_C) |
| 12319 | case MBEDTLS_SSL_HASH_SHA256: |
| 12320 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 12321 | break; |
| 12322 | #endif |
Hanno Becker | 627fbee | 2019-06-24 11:21:53 +0100 | [diff] [blame] | 12323 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 12324 | default: |
Hanno Becker | 627fbee | 2019-06-24 11:21:53 +0100 | [diff] [blame] | 12325 | return( MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 12326 | } |
| 12327 | |
Hanno Becker | 627fbee | 2019-06-24 11:21:53 +0100 | [diff] [blame] | 12328 | return( 0 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 12329 | } |
Hanno Becker | 0a64170 | 2019-06-24 11:19:58 +0100 | [diff] [blame] | 12330 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 12331 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12332 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 12333 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 12334 | int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl, |
| 12335 | unsigned char *output, |
| 12336 | unsigned char *data, size_t data_len ) |
| 12337 | { |
| 12338 | int ret = 0; |
| 12339 | mbedtls_md5_context mbedtls_md5; |
| 12340 | mbedtls_sha1_context mbedtls_sha1; |
| 12341 | |
| 12342 | mbedtls_md5_init( &mbedtls_md5 ); |
| 12343 | mbedtls_sha1_init( &mbedtls_sha1 ); |
| 12344 | |
| 12345 | /* |
| 12346 | * digitally-signed struct { |
| 12347 | * opaque md5_hash[16]; |
| 12348 | * opaque sha_hash[20]; |
| 12349 | * }; |
| 12350 | * |
| 12351 | * md5_hash |
| 12352 | * MD5(ClientHello.random + ServerHello.random |
| 12353 | * + ServerParams); |
| 12354 | * sha_hash |
| 12355 | * SHA(ClientHello.random + ServerHello.random |
| 12356 | * + ServerParams); |
| 12357 | */ |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12358 | if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12359 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12360 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12361 | goto exit; |
| 12362 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12363 | if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12364 | ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 12365 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12366 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12367 | goto exit; |
| 12368 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12369 | 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] | 12370 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12371 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12372 | goto exit; |
| 12373 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12374 | if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12375 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12376 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12377 | goto exit; |
| 12378 | } |
| 12379 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12380 | if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12381 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12382 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12383 | goto exit; |
| 12384 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12385 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12386 | ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 12387 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12388 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12389 | goto exit; |
| 12390 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12391 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12392 | data_len ) ) != 0 ) |
| 12393 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12394 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12395 | goto exit; |
| 12396 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12397 | if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12398 | output + 16 ) ) != 0 ) |
| 12399 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12400 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12401 | goto exit; |
| 12402 | } |
| 12403 | |
| 12404 | exit: |
| 12405 | mbedtls_md5_free( &mbedtls_md5 ); |
| 12406 | mbedtls_sha1_free( &mbedtls_sha1 ); |
| 12407 | |
| 12408 | if( ret != 0 ) |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 12409 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 12410 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12411 | |
| 12412 | return( ret ); |
| 12413 | |
| 12414 | } |
| 12415 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ |
| 12416 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 12417 | |
| 12418 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 12419 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 12420 | 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] | 12421 | unsigned char *hash, size_t *hashlen, |
| 12422 | unsigned char *data, size_t data_len, |
| 12423 | mbedtls_md_type_t md_alg ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12424 | { |
| 12425 | int ret = 0; |
| 12426 | mbedtls_md_context_t ctx; |
| 12427 | 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] | 12428 | *hashlen = mbedtls_md_get_size( md_info ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12429 | |
| 12430 | mbedtls_md_init( &ctx ); |
| 12431 | |
| 12432 | /* |
| 12433 | * digitally-signed struct { |
| 12434 | * opaque client_random[32]; |
| 12435 | * opaque server_random[32]; |
| 12436 | * ServerDHParams params; |
| 12437 | * }; |
| 12438 | */ |
| 12439 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 12440 | { |
| 12441 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 12442 | goto exit; |
| 12443 | } |
| 12444 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 12445 | { |
| 12446 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 12447 | goto exit; |
| 12448 | } |
| 12449 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 12450 | { |
| 12451 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 12452 | goto exit; |
| 12453 | } |
| 12454 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 12455 | { |
| 12456 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 12457 | goto exit; |
| 12458 | } |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 12459 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12460 | { |
| 12461 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 12462 | goto exit; |
| 12463 | } |
| 12464 | |
| 12465 | exit: |
| 12466 | mbedtls_md_free( &ctx ); |
| 12467 | |
| 12468 | if( ret != 0 ) |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 12469 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 12470 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12471 | |
| 12472 | return( ret ); |
| 12473 | } |
| 12474 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 12475 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 12476 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12477 | #endif /* MBEDTLS_SSL_TLS_C */ |